a
    `mg                     @   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l	m
Z
 d dlmZmZ d dlmZmZ de
jfdd	Zd"ddZdd Zdd Zdd Zd#ddZdd Zdd Zdd Zdd Zdd Zd$d d!ZdS )%    N)IterableMapping)jwk)Key)
ALGORITHMS)JWSErrorJWSSignatureError)base64url_decodebase64url_encodec                 C   s<   |t jvrtd| t||d}t| }t||||}|S )aw  Signs a claims set and returns a JWS string.

    Args:
        payload (str or dict): A string to sign
        key (str or dict): The key to use for signing the claim set. Can be
            individual JWK or JWK set.
        headers (dict, optional): A set of headers that will be added to
            the default headers.  Any headers that are added as additional
            headers will override the default headers.
        algorithm (str, optional): The algorithm to use for signing the
            the claims.  Defaults to HS256.

    Returns:
        str: The string representation of the header, claims, and signature.

    Raises:
        JWSError: If there is an error signing the token.

    Examples:

        >>> jws.sign({'a': 'b'}, 'secret', algorithm='HS256')
        'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhIjoiYiJ9.jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8'

    zAlgorithm %s not supported.)additional_headers)r   Z	SUPPORTEDr   _encode_header_encode_payload_sign_header_and_claims)payloadkeyheaders	algorithmencoded_headerZencoded_payloadZsigned_output r   ?/var/www/html/idle/venv/lib/python3.9/site-packages/jose/jws.pysign   s    
r   Tc                 C   s(   t | \}}}}|r$t||||| |S )a  Verifies a JWS string's signature.

    Args:
        token (str): A signed JWS to be verified.
        key (str or dict): A key to attempt to verify the payload with. Can be
            individual JWK or JWK set.
        algorithms (str or list): Valid algorithms that should be used to verify the JWS.

    Returns:
        str: The str representation of the payload, assuming the signature is valid.

    Raises:
        JWSError: If there is an exception verifying a token.

    Examples:

        >>> token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhIjoiYiJ9.jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8'
        >>> jws.verify(token, 'secret', algorithms='HS256')

    )_load_verify_signature)tokenr   
algorithmsverifyheaderr   signing_input	signaturer   r   r   r   0   s    r   c                 C   s   t | \}}}}|S )a!  Returns the decoded headers without verification of any kind.

    Args:
        token (str): A signed JWS to decode the headers from.

    Returns:
        dict: The dict representation of the token headers.

    Raises:
        JWSError: If there is an exception decoding the token.
    r   r   r   Zclaimsr   r   r   r   r   get_unverified_headerN   s    r!   c                 C   s   t | S )a{  Returns the decoded headers without verification of any kind.

    This is simply a wrapper of get_unverified_header() for backwards
    compatibility.

    Args:
        token (str): A signed JWS to decode the headers from.

    Returns:
        dict: The dict representation of the token headers.

    Raises:
        JWSError: If there is an exception decoding the token.
    )r!   )r   r   r   r   get_unverified_headers^   s    r"   c                 C   s   t | \}}}}|S )a  Returns the decoded claims without verification of any kind.

    Args:
        token (str): A signed JWS to decode the headers from.

    Returns:
        str: The str representation of the token claims.

    Raises:
        JWSError: If there is an exception decoding the token.
    r   r    r   r   r   get_unverified_claimsp   s    r#   c                 C   s6   d| d}|r| | tj|dddd}t|S )NZJWT)typalg,:T)
separators	sort_keysutf-8)updatejsondumpsencoder
   )r   r   r   Zjson_headerr   r   r   r      s    

r   c                 C   s>   t | tr6ztj| ddd} W n ty4   Y n0 t| S )Nr&   )r)   r+   )
isinstancer   r-   r.   r/   
ValueErrorr
   )r   r   r   r   r      s    
r   c           	   
   C   s   d | |g}z$t|ts&t||}||}W n, ty^ } zt|W Y d }~n
d }~0 0 t|}d | ||g}|	dS )N   .r+   )
joinr0   r   r   	constructr   	Exceptionr   r
   decode)	r   Zencoded_claimsr   r   r   r   eZencoded_signatureencoded_stringr   r   r   r      s    
r   c           
   
   C   s<  t | tr| d} z,| dd\}}|dd\}}t|}W n8 tyZ   tdY n  tt	j
fyx   tdY n0 zt|d}W n0 ty } ztd| W Y d }~n
d }~0 0 t |tstdzt|}W n  tt	j
fy   tdY n0 zt|}	W n" tt	j
fy.   td	Y n0 ||||	fS )
Nr+   r2      zNot enough segmentszInvalid header paddingzInvalid header string: %sz,Invalid header string: must be a json objectzInvalid payload paddingzInvalid crypto padding)r0   strr/   rsplitsplitr	   r1   r   	TypeErrorbinasciiErrorr-   loadsr6   r   )
Zjwtr   Zcrypto_segmentZheader_segmentZclaims_segmentZheader_datar   r7   r   r   r   r   r   r      s2    

"
r   c              	   C   sP   | D ]F}t |tst||}z|||r4W  dS W q tyH   Y q0 qdS )NTF)r0   r   r   r4   r   r5   )keysr   r   r%   r   r   r   r   _sig_matches_keys   s    
rB   c                 C   s   t | tr| fS ztj| ttd} W n ty6   Y n0 t | trxd| v rR| d S d| v r`| fS |  }|rp|S | fS n(t | trt | tst | t	s| S | fS d S )N)	parse_intparse_floatrA   Zkty)
r0   r   r-   r@   r:   r5   r   valuesr   bytes)r   rE   r   r   r   	_get_keys   s$    

rG    c                 C   s   | d}|std|d ur.||vr.tdt|}zt|| ||sLt W n6 tyh   tdY n ty   td| Y n0 d S )Nr%   z-No algorithm was specified in the JWS header.z&The specified alg value is not allowedzSignature verification failed.z$Invalid or unsupported algorithm: %s)getr   rG   rB   r   )r   r   r   r   r   r%   rA   r   r   r   r      s    

r   )T)N)rH   N)r>   r-   collections.abcr   r   Zjoser   Zjose.backends.baser   Zjose.constantsr   Zjose.exceptionsr   r   Z
jose.utilsr	   r
   ZHS256r   r   r!   r"   r#   r   r   r   r   rB   rG   r   r   r   r   r   <module>   s&   $

!!