
    uZh                         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y)    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          d/var/www/html/wine-match-dev/backend/winematch-backend/venv/lib/python3.12/site-packages/jose/jws.pysignr      sP    4 
,,,4y@AA#I'JN%g.O+NOYX[\M    c                 H    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-C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|j                  |       t        j                  |dd      j                  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      sN    9-F()** fWo	  K((r   c                     t        | t              r2	 t        j                  | d      j	                  d      } t        |       S t        |       S # t
        $ r Y t        |       S w xY w)Nr3   )r6   r8   )
isinstancer   r:   r;   r<   
ValueErrorr   )r   s    r   r   r      sj    '7#	jj% fWo  G$$G$$  	G$$	s   &A 	A$#A$c                 0   dj                  | |g      }	 t        |t              st        j                  ||      }|j                  |      }t        |      }dj                  | ||g      }|j                  d      S # t        $ r}t        |      d }~ww xY w)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#--Y/CHH]+	 )3YY@QRSN  ))  qks   7A< <	BBBc                    t        | t              r| j                  d      } 	 | j                  dd      \  }}|j	                  dd      \  }}t        |      }	 t        j                  |j                  d            }t        |t              st        d      	 t        |      }	 t        |      }	||||	fS # t        $ r t        d      t        t        j                  f$ r t        d      w xY w# t        $ r}t        d|z        d }~ww xY w# t        t        j                  f$ r t        d      w xY w# t        t        j                  f$ r t        d	      w xY w)
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&&~68K..w78 fg&EFF2">21$^4	 G]I66/  .,--x~~& 1/001
  82Q6778 x~~& 20112
 x~~& 1/001s;   5B6 $C1 D %D8 68C.1	D:DD%D58%Ec                     | D ]=  }t        |t              st        j                  ||      }	 |j	                  ||      r y? y# t
        $ r Y Lw xY w)NTF)r?   r   r   rD   r!   rE   )keysr#   r$   r2   r   s        r   _sig_matches_keysr[      s_     #s#--S)C	zz-3 4	   		s   A	AAc                 `   t        | t              r| fS 	 t        j                  | t        t              } t        | t              r'd| v r| d   S d| v r| fS | j                         }|r|S | fS t        | t              r"t        | t              st        | t              s| S | fS # t
        $ r Y ww xY w)N)	parse_intparse_floatrZ   kty)
r?   r   r:   rS   rM   rE   r   valuesr   bytes)r   r`   s     r   	_get_keysrb      s    #svjj= #wS=v;c\6M ZZ\F6M 
C	"JsC,@JsTYDZ
 v/  s    B! !	B-,B-c                    |j                  d      }|st        d      |||vrt        d      t        |      }	 t        || ||      s
t	               y # 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FGG#Z"7?@@S>DE }iE#%% F 9788 E=CDDEs   A +B )T)N) 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      sj      -  " % 7 9  $z/?/? !H< ($ )
%* 7B	BEr   