o
    ÿÑÐb±  ã                   @   sd   d dl mZmZmZ d dlmZmZ d dlmZm	Z	m
Z
mZmZmZmZ G dd„ deƒZdd„ ZdS )	é    )Úto_bytesÚurlsafe_b64encodeÚjson_b64encode)Úextract_headerÚextract_segment)ÚDecodeErrorÚMissingAlgorithmErrorÚUnsupportedAlgorithmErrorÚMissingEncryptionAlgorithmErrorÚ#UnsupportedEncryptionAlgorithmErrorÚ$UnsupportedCompressionAlgorithmErrorÚInvalidHeaderParameterNamec                   @   sl   e Zd Zeg d¢ƒZi Zi Zi Zddd„Ze	dd„ ƒZ
dd„ Zdd	d
„Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚJsonWebEncryption)ÚalgÚencÚzipZjkuÚjwkÚkidZx5uZx5cZx5tzx5t#S256ÚtypZctyÚcritNc                 C   s   || _ || _d S ©N)Ú_algorithmsÚ_private_headers)ÚselfZ
algorithmsZprivate_headers© r   úW/var/www/secure340b-portal/env/lib/python3.10/site-packages/authlib/jose/rfc7516/jwe.pyÚ__init__    s   
zJsonWebEncryption.__init__c                 C   sn   |r|j dkrtd |¡ƒ‚|jdkr|| j|j< dS |jdkr(|| j|j< dS |jdkr5|| j|j< dS dS )z?Register an algorithm for ``alg`` or ``enc`` or ``zip`` of JWE.ZJWEzInvalid algorithm for JWE, {!r}r   r   r   N)Zalgorithm_typeÚ
ValueErrorÚformatZalgorithm_locationÚALG_REGISTRYÚnameÚENC_REGISTRYÚZIP_REGISTRY)ÚclsÚ	algorithmr   r   r   Úregister_algorithm$   s   ÿ


ÿz$JsonWebEncryption.register_algorithmc                 C   sØ   |   |¡}|  |¡}|  |¡}|  ||¡ t|||ƒ}| |||¡}|d }|d }	d|v r5| |d ¡ | ¡ }
t|ƒ}t	|dƒ}|rL| 
t	|ƒ¡}nt	|ƒ}| |||
|¡\}}d |t|	ƒt|
ƒt|ƒt|ƒg¡S )aC  Generate a JWE Compact Serialization. The JWE Compact Serialization
        represents encrypted content as a compact, URL-safe string.  This
        string is:

            BASE64URL(UTF8(JWE Protected Header)) || '.' ||
            BASE64URL(JWE Encrypted Key) || '.' ||
            BASE64URL(JWE Initialization Vector) || '.' ||
            BASE64URL(JWE Ciphertext) || '.' ||
            BASE64URL(JWE Authentication Tag)

        Only one recipient is supported by the JWE Compact Serialization and
        it provides no syntax to represent JWE Shared Unprotected Header, JWE
        Per-Recipient Unprotected Header, or JWE AAD values.

        :param protected: A dict of protected header
        :param payload: A string/dict of payload
        :param key: Private key used to generate signature
        :return: byte
        ÚcekÚekÚheaderÚasciió   .)Úget_header_algÚget_header_encÚget_header_zipÚ_validate_private_headersÚprepare_keyÚwrapÚupdateZgenerate_ivr   r   ÚcompressZencryptÚjoinr   )r   Ú	protectedÚpayloadÚkeyr   r   Úzip_algÚwrappedr&   r'   ÚivZprotected_segmentÚaadÚmsgÚ
ciphertextÚtagr   r   r   Úserialize_compact2   s0   



ûz#JsonWebEncryption.serialize_compactc                 C   s  zt |ƒ}| d¡\}}}}}W n ty   tdƒ‚w t|tƒ}	t|tdƒ}
t|tdƒ}t|tdƒ}t|tdƒ}|  |	¡}|  |	¡}|  |	¡}|  	|	|¡ t
||	|ƒ}| ||
|	|¡}t |dƒ}| |||||¡}|rt| t |ƒ¡}n|}|r|||ƒ}|	|dœS )	a  Exact JWS Compact Serialization, and validate with the given key.

        :param s: text of JWS Compact Serialization
        :param key: key used to verify the signature
        :param decode: a function to decode plaintext data
        :return: dict
        r*   zNot enough segmentszencryption keyzinitialization vectorr<   zauthentication tagr)   )r(   r5   )r   Úrsplitr   r   r   r   r+   r,   r-   r.   r/   ÚunwrapZdecryptÚ
decompress)r   Úsr6   ÚdecodeZprotected_sZek_sZiv_sZciphertext_sZtag_sr4   r'   r9   r<   r=   r   r   r7   r&   r:   r;   r5   r   r   r   Údeserialize_compacts   s2   ÿ





z%JsonWebEncryption.deserialize_compactc                 C   óF   d|vrt ƒ ‚|d }| jr|| jvrtƒ ‚|| jvrtƒ ‚| j| S )Nr   )r   r   r	   r   )r   r(   r   r   r   r   r+   ›   s   

z JsonWebEncryption.get_header_algc                 C   rE   )Nr   )r
   r   r   r!   )r   r(   r   r   r   r   r,   ¦   s   

z JsonWebEncryption.get_header_encc                 C   sD   d|v r |d }| j r|| j vrtƒ ‚|| jvrtƒ ‚| j| S d S )Nr   )r   r   r"   )r   r(   Úzr   r   r   r-   °   s   

úz JsonWebEncryption.get_header_zipc                 C   sT   | j d u rd S | j ¡ }| | j ¡}|jr| |j¡}|D ]
}||vr't|ƒ‚qd S r   )r   Ú!REGISTERED_HEADER_PARAMETER_NAMESÚcopyÚunionZEXTRA_HEADERSr   )r   r(   r   ÚnamesÚkr   r   r   r.   ¹   s   

ÿÿz+JsonWebEncryption._validate_private_headers)NNr   )Ú__name__Ú
__module__Ú__qualname__Ú	frozensetrG   r   r!   r"   r   Úclassmethodr%   r>   rD   r+   r,   r-   r.   r   r   r   r   r      s    


A(
	r   c                 C   s.   t |ƒr
||d ƒ}nd|v r|d }|  |¡S )Nr   )Úcallabler/   )r   r(   r6   r   r   r   r/   Ê   s
   
r/   N)Zauthlib.common.encodingr   r   r   Zauthlib.jose.utilr   r   Zauthlib.jose.errorsr   r   r	   r
   r   r   r   Úobjectr   r/   r   r   r   r   Ú<module>   s    $ 8