o
    <‘ði  ã                   @   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 )ÚInvalidCEKeyLengthz€Invalid 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 )ÚInvalidJWEOperationz†Invalid 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   )ÚInvalidJWEKeyTypez“Invalid 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   )ÚInvalidJWEKeyLengthz—Invalid 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<   )ÚInvalidJWSERegOperationz«Invalid 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<   )ÚJWKeyNotFoundzÜThe 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*   ÿ