o
    6d                     @   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   _C:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\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      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   r"   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   sJ   t | tr!ztj| ddd} W t| S  ty    Y t| S w t| S )Nr'   )r*   r,   )
isinstancer   r.   r/   r0   
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   .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   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 )
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   )
Zjwtr   Zcrypto_segmentZheader_segmentZclaims_segmentZheader_datar   r8   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)r1   r   r   r5   r   r6   )keysr   r   r&   r   r   r   r   _sig_matches_keys   s   
rC   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_floatrB   Zkty)
r1   r   r.   rA   r;   r6   r   valuesr   bytes)r   rF   r   r   r   	_get_keys   s&   

rH    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   rH   rC   r   )r   r   r   r   r   r&   rB   r   r   r   r      s   
r   )T)N)rI   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   rC   rH   r   r   r   r   r   <module>   s(    
$
!!