U
    ˹h                     @   s   d dl Z d dlZzd dlmZmZ W n$ ek
rH   d dlmZmZ Y nX 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)r   r   )jwk)Key)
ALGORITHMS)JWSErrorJWSSignatureError)base64url_decodebase64url_encodec                 C   s<   |t jk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   L/var/www/html/timesheet/backend/venv/lib/python3.8/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   4   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   claimsr   r   r   r   r   get_unverified_headerR   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_headersb   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_claimst   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r8ztj| ddd} W n tk
r6   Y nX t| S )Nr'   )r*   r,   )
isinstancer   r.   r/   r0   
ValueErrorr
   )r   r   r   r   r      s    
r   c           	   
   C   s   d | |g}z$t|ts&t||}||}W n* tk
r\ } zt|W 5 d }~X Y nX t|}d | ||g}|	dS )N   .r,   )
joinr1   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   sD  t | tr| d} z,| dd\}}|dd\}}t|}W n< tk
r\   tdY n" tt	j
fk
r|   tdY nX zt|d}W n. tk
r } ztd| W 5 d }~X Y nX t |tstdzt|}W n$ tt	j
fk
r   tdY nX zt|}	W n$ tt	j
fk
r6   td	Y nX ||||	fS )
Nr,   r3      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)r1   strr0   rsplitsplitr	   r2   r   	TypeErrorbinasciiErrorr.   loadsr7   r   )
jwtr   Zcrypto_segmentZheader_segmentZclaims_segmentZheader_datar   r8   r   r   r   r   r   r      s2    


r   c              	   C   sR   | D ]H}t |tst||}z|||r4W  dS W q tk
rJ   Y qX qdS )NTF)r1   r   r   r5   r   r6   )keysr   r   r&   r   r   r   r   _sig_matches_keys   s    
rD   c                 C   s   t | tr| fS ztj| ttd} W n tk
r8   Y nX t | trzd| krT| d S d| krb| fS |  }|rr|S | fS n(t | trt | tst | t	s| S | fS d S )N)	parse_intparse_floatrC   Zkty)
r1   r   r.   rA   r;   r6   r   valuesr   bytes)r   rG   r   r   r   	_get_keys   s$    

rI    c                 C   s   | d}|std|d k	r.||kr.tdt|}zt|| ||sLt W n: tk
rj   tdY n  tk
r   td| Y nX 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   rI   rD   r   )r   r   r   r   r   r&   rC   r   r   r   r      s    

r   )T)N)rJ   N) r?   r.   collections.abcr   r   ImportErrorcollectionsjoser   Zjose.backends.baser   Zjose.constantsr   Zjose.exceptionsr   r   Z
jose.utilsr	   r
   HS256r   r   r"   r#   r$   r   r   r   r   rD   rI   r   r   r   r   r   <module>   s,   $

! 