
    ]f                         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Zdd
Zd Zd Zd ZddZd Zd Zd Zd Zd ZddZdS )    N)IterableMapping)jwk)Key)
ALGORITHMS)JWSErrorJWSSignatureError)base64url_decodebase64url_encodec                     |t           j        vrt          d|z            t          ||          }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_outputs          :/var/www/html/env/lib/python3.11/site-packages/jose/jws.pysignr      s_    4 
,,,4y@AAA#I'JJJN%g..O+NOYX[\\M    Tc                 X    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	signatures           r   r!   r!   0   s<    , 16e-FG]I M-CLLLNr   c                 .    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$   s        r   get_unverified_headerr)   N        05U||,FFM9Mr   c                      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   s    r   get_unverified_headersr,   ^   s     !'''r   c                 .    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'   s        r   get_unverified_claimsr.   p   r*   r   c                     d| d}|r|                     |           t          j        |dd                              d          }t	          |          S )NJWT)typalg,:T)
separators	sort_keysutf-8)updatejsondumpsencoder   )r   r   r"   json_headers       r   r   r      si    9--F *()))*   fWoo	  K(((r   c                     t          | t                    r;	 t          j        | d                              d          } n# t
          $ r Y nw xY wt          |           S )Nr3   )r6   r8   )
isinstancer   r:   r;   r<   
ValueErrorr   )r   s    r   r   r      sy    '7## 	j%   fWoo G  	 	 	D	 G$$$s   )A 
AAc                 l   d                     | |g          }	 t          |t                    st          j        ||          }|                    |          }n!# t          $ r}t          |          d }~ww xY wt          |          }d                     | ||g          }|	                    d          S )N   .r8   )
joinr?   r   r   	constructr   	Exceptionr   r   decode)	r   encoded_claimsr   r   r#   r$   eencoded_signatureencoded_strings	            r   r   r      s    II~~>??M#s## 	0-Y//CHH]++		   qkk )33YY@QRSSN  )))s   ?A 
A7#A22A7c                 4   t          | t                    r|                     d          } 	 |                     dd          \  }}|                    dd          \  }}t          |          }nA# t          $ r t          d          t          t          j
        f$ r t          d          w xY w	 t          j        |                    d                    }n$# t          $ r}t          d|z            d }~ww xY wt          |t                    st          d          	 t          |          }n)# t          t          j
        f$ r t          d          w xY w	 t          |          }	n)# t          t          j
        f$ r t          d	          w xY w||||	fS )
Nr8   rB      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)r?   strr<   rsplitsplitr
   r@   r   	TypeErrorbinasciiErrorr:   loadsrF   r   )
jwtr#   crypto_segmentheader_segmentclaims_segmentheader_datar"   rH   r   r$   s
             r   r   r      s   #s "jj!!1(+

4(;(;%~)6)<)<T1)E)E&&~66 . . .,---x~& 1 1 1/00018K..w7788 8 8 82Q67778 fg&& GEFFF2">22x~& 2 2 2011121$^44		x~& 1 1 1/0001 G]I66s<   AA. .>B,0'C 
C9"C44C9!D1 1&EE+ +&Fc                     | D ]V}t          |t                    st          j        ||          }	 |                    ||          r dS G# t
          $ r Y Sw xY wdS )NTF)r?   r   r   rD   r!   rE   )keysr#   r$   r2   r   s        r   _sig_matches_keysr[      s      #s## 	*-S))C	zz-33 tt 	 	 	D	5s   A


AAc                    t          | t                    r| fS 	 t          j        | t          t                    } n# t
          $ r Y nw xY wt          | t                    r.d| v r| d         S d| v r| fS |                                 }|r|S | fS t          | t                    r,t          | t                    st          | t                    s| S | fS )N)	parse_intparse_floatrZ   kty)
r?   r   r:   rS   rM   rE   r   valuesr   bytes)r   r`   s     r   	_get_keysrb      s    #s vj===    #w S==v;c\\6M ZZ\\F 6M 
C	"	" JsC,@,@ JsTYDZDZ 
 vs   !< 
A	A	 c                 L   |                     d          }|st          d          |||vrt          d          t          |          }	 t          || ||          st	                      d S # t          $ r t          d          t          $ r t          d|z            w xY w)Nr2   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   rb   r[   r	   )r#   r"   r$   r   r    r2   rZ   s          r   r   r      s    
**U

C HFGGG#Z"7"7?@@@S>>DE }iEE 	&#%%%	& 	& 9 9 97888 E E E=CDDDEs    A. .5B#)T)N)rc   N)rQ   r:   collections.abcr   r   joser   jose.backends.baser   jose.constantsr   jose.exceptionsr   r	   
jose.utilsr
   r   HS256r   r!   r)   r,   r.   r   r   r   r   r[   rb   r    r   r   <module>rn      sz     - - - - - - - -       " " " " " " % % % % % % 7 7 7 7 7 7 7 7 9 9 9 9 9 9 9 9  $z/? ! ! ! !H   <   ( ( ($   ) ) ) )
% 
% 
%* * * 7 7 7B	 	 	  BE E E E E Er   