
    ʏe                     ,   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 Z	d Z
d Zd Z G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z edd      Z G d de      Zy)    N)urlsafe_b64decodeurlsafe_b64encode)
namedtuple)MutableMappingc                     t        | t              s| j                  d      } t        |       }|j	                  d      j                  d      S )Nutf-8=)
isinstancebytesencoder   decoderstrip)payloadr   s     1/usr/lib/python3/dist-packages/jwcrypto/common.pybase64url_encoder      s>    gu%..)w'F==!((--    c                     t        |       dz  }|dk(  r| dz  } n|dk(  r| dz  } n|dk7  rt        d      t        | j                  d            S )	N      z==   r	   r   zInvalid base64 stringr   )len
ValueErrorr   r   )r   sizes     r   base64url_decoder      sW    w<!Dqy4	3	011W^^G455r   c                 t    t        | t              r| j                  d      } t        j                  | dd      S )Nr   ),:T)
separators	sort_keys)r
   r   r   jsondumpsstrings    r   json_encoder$   !   s-    &% w'::ftDDr   c                 n    t        | t              r| j                  d      } t        j                  |       S )Nr   )r
   r   r   r    loadsr"   s    r   json_decoder'   '   s)    &% w'::fr   c                       e Zd Zy)JWExceptionN)__name__
__module____qualname__ r   r   r)   r)   -   s    r   r)   c                         e Zd Zd fd	Z xZS )InvalidJWAAlgorithmc                 D    d}|r|d|z  z  }t         t        |   |       y )NzInvalid JWA Algorithm namez (%s))superr/   __init__selfmessagemsg	__class__s      r   r2   zInvalidJWAAlgorithm.__init__2   s*    *7W$$C!41#6r   N)r*   r+   r,   r2   __classcell__r7   s   @r   r/   r/   1   s    7 7r   r/   c                   "     e Zd ZdZ fdZ xZS )InvalidCEKeyLengthzInvalid CEK Key Length.

    This exception is raised when a Content Encryption Key does not match
    the required length.
    c                 :    d||fz  }t         t        |   |       y )Nz&Expected key of length %d bits, got %d)r1   r<   r2   r4   expectedobtainedr6   r7   s       r   r2   zInvalidCEKeyLength.__init__@   s"    6(H9MM $05r   r*   r+   r,   __doc__r2   r9   r:   s   @r   r<   r<   9   s    6 6r   r<   c                   $     e Zd ZdZd fd	Z xZS )InvalidJWEOperationzInvalid JWS Object.

    This exception is raised when a requested operation cannot
    be execute due to unsatisfied conditions.
    c                 d    d }|r|}nd}|r|dt        |      z  z  }t        t        |   |       y NzUnknown Operation Failurez {%s})reprr1   rD   r2   r4   r5   	exceptionr6   r7   s       r   r2   zInvalidJWEOperation.__init__L   s;    C-C7T)_,,C!41#6r   NNrA   r:   s   @r   rD   rD   E   s    7 7r   rD   c                   "     e Zd ZdZ fdZ x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                 <    d|d|}t         t        |   |       y )NzExpected key type z, got )r1   rL   r2   r>   s       r   r2   zInvalidJWEKeyType.__init__^   s    08(C/4r   rA   r:   s   @r   rL   rL   W   s    5 5r   rL   c                   "     e Zd ZdZ fdZ x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                 :    d||fz  }t         t        |   |       y )Nz!Expected key of length %d, got %d)r1   rO   r2   r>   s       r   r2   zInvalidJWEKeyLength.__init__j   s"    1Xx4HH!41#6r   rA   r:   s   @r   rO   rO   c   s    7 7r   rO   c                   $     e Zd ZdZd fd	Z x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.
    c                 d    d }|r|}nd}|r|dt        |      z  z  }t        t        |   |       y rF   )rG   r1   rR   r2   rH   s       r   r2   z InvalidJWSERegOperation.__init__v   s;    C-C7T)_,,C%t5c:r   rJ   rA   r:   s   @r   rR   rR   o   s    ; ;r   rR   c                   $     e Zd ZdZd fd	Z x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.
    c                 :    |r|}nd}t         t        |   |       y )NzKey Not Found)r1   rU   r2   r3   s      r   r2   zJWKeyNotFound.__init__   s    C!CmT+C0r   r8   rA   r:   s   @r   rU   rU      s    1 1r   rU   	Parameterz*description mustprotect supported check_fnc                   8    e Zd Zd	dZd Zd Zd Zd Zd Zd Z	y)
JWSEHeaderRegistryNc                     |r6t        |t              rt        j                  |      | _        nt        d      i | _        t        j                  |        y )NzUnknown input type)r
   dictcopydeepcopy	_registryrR   r   r2   )r4   init_registrys     r   r2   zJWSEHeaderRegistry.__init__   s@    -.!%}!=-.BCCDN%r   c                     || j                   vrt        d|z        | j                   |   }|j                  y|j                  |      S )Nz No header "%s" found in registryT)r^   rR   check_fn)r4   hvalueparams       r   check_headerzJWSEHeaderRegistry.check_header   sR    DNN")*L,-+. / / q!>>!>>%((r   c                 8    | j                   j                  |      S r8   )r^   __getitem__r4   keys     r   rg   zJWSEHeaderRegistry.__getitem__   s    ~~))#..r   c                 6    | j                   j                         S r8   )r^   __iter__r4   s    r   rk   zJWSEHeaderRegistry.__iter__   s    ~~&&((r   c                     | j                   |   j                  s| j                   |   j                  rt        d      | j                   j	                  |       y )Nz-Unable to delete protected or supported field)r^   mustprotect	supportedrR   __delitem__rh   s     r   rp   zJWSEHeaderRegistry.__delitem__   sL    >>#**>>#(() +< = = NN&&s+r   c                     || j                   v rV| j                   |   }|j                  rt        d      |j                  r|j                  st        d      | j                   |= || j                   |<   y )Nz+Supported header already exists in registryz,Header specified should bea protected header)r^   ro   rR   rn   )r4   rb   jwse_header_paramps       r   __setitem__zJWSEHeaderRegistry.__setitem__   sv    q!A{{- /= > >'8'D'D- /C D D NN1%-qr   c                 6    | j                   j                         S r8   )r^   __len__rl   s    r   rv   zJWSEHeaderRegistry.__len__   s    ~~%%''r   r8   )
r*   r+   r,   r2   re   rg   rk   rp   rt   rv   r-   r   r   rY   rY      s%    	&	)/),.(r   rY   )r\   r    base64r   r   collectionsr   collections.abcr   r   r   r$   r'   	Exceptionr)   r/   r<   rD   rL   rO   rR   rU   JWSEHeaderParameterrY   r-   r   r   <module>r|      s      7 " *.6E	) 	7+ 7	6 	67+ 7$	5 	5	7+ 	7;k ;$1K 1( !!MO 5( 5(r   