o
    6h                     @   s   d dl Z d dlZz
d dlmZmZ W n ey#   d dlmZmZ Y nw 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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   	SUPPORTEDr   _encode_header_encode_payload_sign_header_and_claims)payloadkeyheaders	algorithmencoded_headerencoded_payloadsigned_output r   \/var/www/html/construction_image-detection-poc/venv/lib/python3.10/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      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   r&   r(   c                 C   s6   d| d}|r| | tj|dddd}t|S )NJWT)typalg,:T)
separators	sort_keysutf-8)updatejsondumpsencoder
   )r   r   r   json_headerr   r   r   r      s   

r   c                 C   sJ   t | tr!ztj| ddd} W t| S  ty    Y t| S w t| S )Nr,   )r/   r1   )
isinstancer   r3   r4   r5   
ValueErrorr
   )r   r   r   r   r      s   
r   c           	   
   C   sv   d | |g}zt|tst||}||}W n ty) } zt|d }~ww t|}d | ||g}|	dS )N   .r1   )
joinr7   r   r   	constructr   	Exceptionr   r
   decode)	r   encoded_claimsr   r   r    r!   eencoded_signatureencoded_stringr   r   r   r      s   

r   c           
   
   C   s  t | tr
| d} z| dd\}}|dd\}}t|}W n ty+   td tt	j
fy8   tdw z
t|d}W n tyU } ztd| d }~ww t |ts_tdzt|}W n tt	j
fys   tdw zt|}	W n tt	j
fy   td	w ||||	fS )
Nr1   r9      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)r7   strr5   rsplitsplitr	   r8   r   	TypeErrorbinasciiErrorr3   loadsr=   r   )
jwtr    crypto_segmentheader_segmentclaims_segmentheader_datar   r?   r   r!   r   r   r   r      s<   


r   c              	   C   sN   | D ]"}t |tst||}z|||rW  dS W q ty$   Y qw dS )NTF)r7   r   r   r;   r   r<   )keysr    r!   r+   r   r   r   r   _sig_matches_keys   s   
rP   c                 C   s   t | tr| fS z
tj| ttd} W n	 ty   Y nw t | tr;d| v r)| d S d| v r0| fS |  }|r8|S | fS t | trLt | tsLt | t	sL| S | fS )N)	parse_intparse_floatrO   kty)
r7   r   r3   rI   rC   r<   r   valuesr   bytes)r   rT   r   r   r   	_get_keys   s&   

rV    c                 C   s   | d}|std|d ur||vrtdt|}zt|| ||s&t W d S  ty3   td ty?   td| w )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   rV   rP   r   )r    r   r!   r   r   r+   rO   r   r   r   r      s   
r   )T)N)rW   N) rG   r3   collections.abcr   r   ImportErrorcollectionsjoser   jose.backends.baser   jose.constantsr   jose.exceptionsr   r   
jose.utilsr	   r
   HS256r   r   r%   r'   r(   r   r   r   r   rP   rV   r   r   r   r   r   <module>   s0    
$
! 