a
    |f                     @   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   ID:\Projects\storyit_web\backend\venv\Lib\site-packages\jwcrypto/common.pybase64url_encode   s    

r   c                 C   sN   t | d }|dkr| d7 } n"|dkr0| 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                       s    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                    s    d||f }t t| | d S )Nz&Expected key of length %d bits, got %d)r(   r1   r)   r+   expectedZobtainedr-   r.   r   r   r)   @   s    zInvalidCEKeyLength.__init__r$   r%   r&   __doc__r)   r0   r   r   r.   r   r1   9   s   r1   c                       s"   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                    s:   d }|r|}nd}|r&|dt | 7 }tt| | d S NzUnknown Operation Failurez {%s})reprr(   r6   r)   r+   r,   	exceptionr-   r.   r   r   r)   L   s    zInvalidJWEOperation.__init__)NNr4   r   r   r.   r   r6   E   s   r6   c                       s    e Zd ZdZ fddZ  ZS )InvalidJWEKeyTypezInvalid JWE Key Type.

    This exception is raised when the provided JWK Key does not match
    the type required by the specified algorithm.
    c                    s    d||f }t t| | d S )NzExpected key type %s, got %s)r(   r;   r)   r2   r.   r   r   r)   ^   s    zInvalidJWEKeyType.__init__r4   r   r   r.   r   r;   W   s   r;   c                       s    e Zd ZdZ fddZ  ZS )InvalidJWEKeyLengthzInvalid JWE Key Length.

    This exception is raised when the provided JWK Key does not match
    the length required by the specified algorithm.
    c                    s    d||f }t t| | d S )Nz!Expected key of length %d, got %d)r(   r<   r)   r2   r.   r   r   r)   j   s    zInvalidJWEKeyLength.__init__r4   r   r   r.   r   r<   c   s   r<   c                       s"   e Zd ZdZd fdd	Z  ZS )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                    s:   d }|r|}nd}|r&|dt | 7 }tt| | d S r7   )r8   r(   r=   r)   r9   r.   r   r   r)   v   s    z InvalidJWSERegOperation.__init__)NNr4   r   r   r.   r   r=   o   s   r=   c                       s"   e Zd ZdZd fdd	Z  ZS )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(   r>   r)   r*   r.   r   r   r)      s    zJWKeyNotFound.__init__)Nr4   r   r   r.   r   r>      s   r>   	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   s:   |r&t |trt|| _q,tdni | _t|  d S )NzUnknown input type)r   dictcopydeepcopy	_registryr=   r   r)   )r+   Zinit_registryr   r   r   r)      s    

zJWSEHeaderRegistry.__init__c                 C   s<   || j vrtd| | j | }|jd u r.dS ||S d S )Nz No header "%s" found in registryT)rD   r=   Zcheck_fn)r+   hvalueparamr   r   r   check_header   s    


zJWSEHeaderRegistry.check_headerc                 C   s   | j |S N)rD   __getitem__r+   keyr   r   r   rJ      s    zJWSEHeaderRegistry.__getitem__c                 C   s
   | j  S rI   )rD   __iter__r+   r   r   r   rM      s    zJWSEHeaderRegistry.__iter__c                 C   s2   | j | js| j | jr"tdn| j | d S )Nz-Unable to delete protected or supported field)rD   mustprotect	supportedr=   __delitem__rK   r   r   r   rQ      s
    

zJWSEHeaderRegistry.__delitem__c                 C   sP   || j v rB| j | }|jr$tdn|jr:|js:tdn| j |= || j |< d S )Nz+Supported header already exists in registryz,Header specified should bea protected header)rD   rP   r=   rO   )r+   rE   Zjwse_header_parampr   r   r   __setitem__   s    



zJWSEHeaderRegistry.__setitem__c                 C   s
   | j  S rI   )rD   __len__rN   r   r   r   rT      s    zJWSEHeaderRegistry.__len__)N)
r$   r%   r&   r)   rH   rJ   rM   rQ   rS   rT   r   r   r   r   r@      s   
r@   )rB   r   base64r   r   collectionsr   collections.abcr   r   r   r    r"   	Exceptionr#   r'   r1   r6   r;   r<   r=   r>   ZJWSEHeaderParameterr@   r   r   r   r   <module>   s(   