o
    &g                     @   s   d dl Z d dlZd dlmZmZ d dlmZ d dlmZ dd Z	dd Z
d	d
 Zdd ZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZeddZG dd  d eZdS )!    N)urlsafe_b64decodeurlsafe_b64encode)
namedtuple)MutableMappingc                 C   s,   t | ts
| d} t| }|ddS )Nutf-8=)
isinstancebytesencoder   decoderstrip)payloadr
    r   N/root/parts/websockify/install/lib/python3.10/site-packages/jwcrypto/common.pybase64url_encode   s   

r   c                 C   sN   t | d }|dkr| d7 } n|dkr| d7 } n|dkr tdt| dS )	N      z==   r   r   zInvalid base64 stringr   )len
ValueErrorr   r
   )r   sizer   r   r   base64url_decode   s   

r   c                 C   s$   t | tr
| d} tj| dddS )Nr   ),:T)
separators	sort_keys)r   r	   r   jsondumpsstringr   r   r   json_encode!   s   

r    c                 C   s   t | tr
| d} t| S )Nr   )r   r	   r   r   loadsr   r   r   r   json_decode'   s   


r"   c                   @   s   e Zd ZdS )JWExceptionN)__name__
__module____qualname__r   r   r   r   r#   -   s    r#   c                       s   e Zd Zd fdd	Z  ZS )InvalidJWAAlgorithmNc                    s(   d}|r
|d| 7 }t t| | d S )NzInvalid JWA Algorithm namez (%s))superr'   __init__selfmessagemsg	__class__r   r   r)   2   s   zInvalidJWAAlgorithm.__init__N)r$   r%   r&   r)   __classcell__r   r   r.   r   r'   1   s    r'   c                           e Zd ZdZ fddZ  ZS )InvalidCEKeyLengthzInvalid CEK Key Length.

    This exception is raised when a Content Encryption Key does not match
    the required length.
    c                        d||f }t t| | d S )Nz&Expected key of length %d bits, got %d)r(   r3   r)   r+   expectedobtainedr-   r.   r   r   r)   @      zInvalidCEKeyLength.__init__r$   r%   r&   __doc__r)   r1   r   r   r.   r   r3   9       r3   c                       "   e Zd ZdZd fdd	Z  ZS )InvalidJWEOperationzInvalid JWS Object.

    This exception is raised when a requested operation cannot
    be execute due to unsatisfied conditions.
    Nc                    :   d }|r|}nd}|r|dt | 7 }tt| | d S NzUnknown Operation Failurez {%s})reprr(   r=   r)   r+   r,   	exceptionr-   r.   r   r   r)   L      zInvalidJWEOperation.__init__NNr9   r   r   r.   r   r=   E       r=   c                       r2   )InvalidJWEKeyTypezInvalid JWE Key Type.

    This exception is raised when the provided JWK Key does not match
    the type required by the specified algorithm.
    c                    r4   )NzExpected key type %s, got %s)r(   rF   r)   r5   r.   r   r   r)   ^   r8   zInvalidJWEKeyType.__init__r9   r   r   r.   r   rF   W   r;   rF   c                       r2   )InvalidJWEKeyLengthzInvalid JWE Key Length.

    This exception is raised when the provided JWK Key does not match
    the length required by the specified algorithm.
    c                    r4   )Nz!Expected key of length %d, got %d)r(   rG   r)   r5   r.   r   r   r)   j   r8   zInvalidJWEKeyLength.__init__r9   r   r   r.   r   rG   c   r;   rG   c                       r<   )InvalidJWSERegOperationzInvalid JWSE Header Registry Operation.

    This exception is raised when there is an error in trying to add a JW
    Signature or Encryption header to the Registry.
    Nc                    r>   r?   )r@   r(   rH   r)   rA   r.   r   r   r)   v   rC   z InvalidJWSERegOperation.__init__rD   r9   r   r   r.   r   rH   o   rE   rH   c                       r<   )JWKeyNotFoundzThe key needed to complete the operation was not found.

    This exception is raised when a JWKSet is used to perform
    some operation and the key required to successfully complete
    the operation is not found.
    Nc                    s"   |r|}nd}t t| | d S )NzKey Not Found)r(   rI   r)   r*   r.   r   r   r)      s   zJWKeyNotFound.__init__r0   r9   r   r   r.   r   rI      s    rI   	Parameterz*description mustprotect supported check_fnc                   @   sF   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dS )JWSEHeaderRegistryNc                 C   s8   |rt |trt|| _ntdi | _t|  d S )NzUnknown input type)r   dictcopydeepcopy	_registryrH   r   r)   )r+   init_registryr   r   r   r)      s   
zJWSEHeaderRegistry.__init__c                 C   s8   || j vrtd| | j | }|jd u rdS ||S )Nz No header "%s" found in registryT)rO   rH   check_fn)r+   hvalueparamr   r   r   check_header   s   



zJWSEHeaderRegistry.check_headerc                 C   s   | j |S r0   )rO   __getitem__r+   keyr   r   r   rV      s   zJWSEHeaderRegistry.__getitem__c                 C   
   | j  S r0   )rO   __iter__r+   r   r   r   rZ         
zJWSEHeaderRegistry.__iter__c                 C   s0   | j | js| j | jrtd| j | d S )Nz-Unable to delete protected or supported field)rO   mustprotect	supportedrH   __delitem__rW   r   r   r   r_      s
   
zJWSEHeaderRegistry.__delitem__c                 C   sL   || j v r| j | }|jrtd|jr|jstd| j |= || j |< d S )Nz+Supported header already exists in registryz,Header specified should bea protected header)rO   r^   rH   r]   )r+   rR   jwse_header_parampr   r   r   __setitem__   s   

zJWSEHeaderRegistry.__setitem__c                 C   rY   r0   )rO   __len__r[   r   r   r   rc      r\   zJWSEHeaderRegistry.__len__r0   )
r$   r%   r&   r)   rU   rV   rZ   r_   rb   rc   r   r   r   r   rK      s    
rK   )rM   r   base64r   r   collectionsr   collections.abcr   r   r   r    r"   	Exceptionr#   r'   r3   r=   rF   rG   rH   rI   JWSEHeaderParameterrK   r   r   r   r   <module>   s*   