o
    ¤ó·iºU  ã                   @  sŽ  U d dl mZ d dlZd dlZd dlZd dlmZ d dlmZm	Z	m
Z
 d dlmZmZmZ d dlmZmZmZmZ ddlmZmZmZ dd	lmZmZmZmZmZmZmZmZm Z  dd
lm!Z! esie"e #dd¡ƒr¯d dl$Z$e$j%dkryd dlm&Z& nd dl'm&Z& ddl(m)Z)m*Z* ddl+m,Z, ddl-m.Z.m/Z/m0Z0 ee)e,e1e2f Z3de4d< ee*e,e1e2f Z5de4d< G dd„ dƒZ6e6ƒ Z7ee7_8e7j9Z9e7j:Z:e7j;Z;dS )é    )ÚannotationsN)Útimegm)Ú	ContainerÚIterableÚSequence)ÚdatetimeÚ	timedeltaÚtimezone)ÚTYPE_CHECKINGÚAnyÚUnionÚcasté   )ÚPyJWSÚ_ALGORITHM_UNSETÚ_jws_global_obj)	ÚDecodeErrorÚExpiredSignatureErrorÚImmatureSignatureErrorÚInvalidAudienceErrorÚInvalidIssuedAtErrorÚInvalidIssuerErrorÚInvalidJTIErrorÚInvalidSubjectErrorÚMissingRequiredClaimError)ÚRemovedInPyjwt3WarningZSPHINX_BUILDÚ )é   é
   )Ú	TypeAlias)ÚAllowedPrivateKeysÚAllowedPublicKeys)ÚPyJWK)ÚFullOptionsÚOptionsÚ
SigOptionsr   ÚAllowedPrivateKeyTypesÚAllowedPublicKeyTypesc                   @  s  e Zd ZdZd[dd„Zed\d	d
„ƒZd]dd„ZdZd^dd„Zedddfd_dd„Z			d`dad!d"„Z
	#								$dbdcd7d8„Zddd:d;„Z	#								$dbded=d>„Z				$dfdgdAdB„ZdhdEdF„Z	dZdidGdH„ZdjdIdJ„ZdkdMdN„ZdkdOdP„ZdkdQdR„ZdSdTœdldVdW„ZdmdXdY„ZdS )nÚPyJWTNÚoptionsúOptions | NoneÚreturnÚNonec                 C  s6   |  |   ¡ | _|d ur|  |¡| _t|  ¡ d�| _d S )N)r)   )Ú_get_default_optionsr)   Ú_merge_optionsr   Ú_get_sig_optionsÚ_jws©Úselfr)   © r3   úQC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\jwt/api_jwt.pyÚ__init__+   s
   
zPyJWT.__init__r#   c                   C  s   ddddddddg dddœS )NTF)Úverify_signatureÚ
verify_expÚ
verify_nbfÚ
verify_iatÚ
verify_audÚ
verify_issÚ
verify_subÚ
verify_jtiÚrequireÚ
strict_audÚenforce_minimum_key_lengthr3   r3   r3   r3   r4   r-   3   s   õzPyJWT._get_default_optionsr%   c                 C  s   | j d | j  dd¡dœS )Nr6   r@   F)r6   r@   ©r)   Úget)r2   r3   r3   r4   r/   C   s
   ÿþzPyJWT._get_sig_optionsc                 C  s˜   |d u r| j S | dd¡sE| dd¡|d< | dd¡|d< | dd¡|d< | dd¡|d< | dd¡|d< | d	d¡|d	< | d
d¡|d
< i | j ¥|¥S )Nr6   Tr7   Fr8   r9   r:   r;   r<   r=   rA   r1   r3   r3   r4   r.   K   s   zPyJWT._merge_optionsTÚpayloadúdict[str, Any]Úkeyr&   Ú	algorithmú
str | NoneÚheadersúdict[str, Any] | NoneÚjson_encoderútype[json.JSONEncoder] | NoneÚsort_headersÚboolÚstrc           	      C  sŽ   t |tƒs	tdƒ‚| ¡ }dD ]}t | |¡tƒr#t||  ¡ ƒ||< qd|v r3t |d tƒs3tdƒ‚| j	|||d�}| j
j||||||d�S )a¦  Encode the ``payload`` as JSON Web Token.

        :param payload: JWT claims, e.g. ``dict(iss=..., aud=..., sub=...)``
        :type payload: dict[str, typing.Any]
        :param key: a key suitable for the chosen algorithm:

            * for **asymmetric algorithms**: PEM-formatted private key, a multiline string
            * for **symmetric algorithms**: plain string, sufficiently long for security

        :type key: str or bytes or PyJWK or :py:class:`jwt.algorithms.AllowedPrivateKeys`
        :param algorithm: algorithm to sign the token with, e.g. ``"ES256"``.
            If ``headers`` includes ``alg``, it will be preferred to this parameter.
            If ``key`` is a :class:`PyJWK` object, by default the key algorithm will be used.
        :type algorithm: str or None
        :param headers: additional JWT header fields, e.g. ``dict(kid="my-key-id")``.
        :type headers: dict[str, typing.Any] or None
        :param json_encoder: custom JSON encoder for ``payload`` and ``headers``
        :type json_encoder: json.JSONEncoder or None

        :rtype: str
        :returns: a JSON Web Token

        :raises TypeError: if ``payload`` is not a ``dict``
        zGExpecting a dict object, as JWT only supports JSON objects as payloads.)ÚexpÚiatÚnbfÚisszIssuer (iss) must be a string.)rH   rJ   )rL   N)Ú
isinstanceÚdictÚ	TypeErrorÚcopyrB   r   r   ÚutctimetuplerN   Ú_encode_payloadr0   Úencode)	r2   rC   rE   rF   rH   rJ   rL   Z
time_claimZjson_payloadr3   r3   r4   rY   Z   s0   
"ÿ€ýúzPyJWT.encodeÚbytesc                 C  s   t j|d|d� d¡S )zÚ
        Encode a given payload to the bytes to be signed.

        This method is intended to be overridden by subclasses that need to
        encode the payload in a different way, e.g. compress the payload.
        )ú,ú:)Ú
separatorsÚclszutf-8N)ÚjsonÚdumpsrY   )r2   rC   rH   rJ   r3   r3   r4   rX   œ   s   ýüzPyJWT._encode_payloadr   r   Újwtústr | bytesr'   Ú
algorithmsúSequence[str] | NoneÚverifyúbool | NoneÚdetached_payloadúbytes | NoneÚaudienceústr | Iterable[str] | NoneÚissuerústr | Container[str] | NoneÚsubjectÚleewayúfloat | timedeltaÚkwargsr   c                 K  s°   |rt jdt| ¡ ƒ› �tdd� |du rd}n| dd¡}|dur.||kr.t jdtdd� |  |¡}d|i}| jj	|||||d	�}|  
|¡}| j|||||
|	d
� ||d< |S )u$  Identical to ``jwt.decode`` except for return value which is a dictionary containing the token header (JOSE Header),
        the token payload (JWT Payload), and token signature (JWT Signature) on the keys "header", "payload",
        and "signature" respectively.

        :param jwt: the token to be decoded
        :type jwt: str or bytes
        :param key: the key suitable for the allowed algorithm
        :type key: str or bytes or PyJWK or :py:class:`jwt.algorithms.AllowedPublicKeys`

        :param algorithms: allowed algorithms, e.g. ``["ES256"]``

            .. warning::

               Do **not** compute the ``algorithms`` parameter based on
               the ``alg`` from the token itself, or on any other data
               that an attacker may be able to influence, as that might
               expose you to various vulnerabilities (see `RFC 8725 Â§2.1
               <https://www.rfc-editor.org/rfc/rfc8725.html#section-2.1>`_). Instead,
               either hard-code a fixed value for ``algorithms``, or
               configure it in the same place you configure the
               ``key``. Make sure not to mix symmetric and asymmetric
               algorithms that interpret the ``key`` in different ways
               (e.g. HS\* and RS\*).
        :type algorithms: typing.Sequence[str] or None

        :param jwt.types.Options options: extended decoding and validation options
            Refer to :py:class:`jwt.types.Options` for more information.

        :param audience: optional, the value for ``verify_aud`` check
        :type audience: str or typing.Iterable[str] or None
        :param issuer: optional, the value for ``verify_iss`` check
        :type issuer: str or typing.Container[str] or None
        :param leeway: a time margin in seconds for the expiration check
        :type leeway: float or datetime.timedelta
        :rtype: dict[str, typing.Any]
        :returns: Decoded JWT with the JOSE Header on the key ``header``, the JWS
         Payload on the key ``payload``, and the JWS Signature on the key ``signature``.
        zypassing additional kwargs to decode_complete() is deprecated and will be removed in pyjwt version 3. Unsupported kwargs: é   ©Ú
stacklevelNTr6   zàThe `verify` argument to `decode` does nothing in PyJWT 2.0 and newer. The equivalent is setting `verify_signature` to False in the `options` dictionary. This invocation has a mismatch between the kwarg and the option entry.)Úcategoryrs   )rE   rc   r)   rg   )ri   rk   rn   rm   rC   )ÚwarningsÚwarnÚtupleÚkeysr   rB   ÚDeprecationWarningr.   r0   Údecode_completeÚ_decode_payloadÚ_validate_claims)r2   ra   rE   rc   r)   re   rg   ri   rk   rm   rn   rp   r6   Zmerged_optionsZsig_optionsÚdecodedrC   r3   r3   r4   rz   ®   sL   9
þûû
ÿû
ú	zPyJWT.decode_completer}   c              
   C  sR   z	t  |d ¡}W n ty } ztd|› �ƒ|‚d}~ww t|tƒs'tdƒ‚|S )a  
        Decode the payload from a JWS dictionary (payload, signature, header).

        This method is intended to be overridden by subclasses that need to
        decode the payload in a different way, e.g. decompress compressed
        payloads.
        rC   zInvalid payload string: Nz-Invalid payload string: must be a json object)r_   ÚloadsÚ
ValueErrorr   rS   rT   )r2   r}   rC   Úer3   r3   r4   r{     s   €ÿ
zPyJWT._decode_payloadú'AllowedPublicKeys | PyJWK | str | bytesc                 K  sV   |rt jdt| ¡ ƒ› �tdd� | j|||||||||	|
d�
}tttt	f |d ƒS )u¢  Verify the ``jwt`` token signature and return the token claims.

        :param jwt: the token to be decoded
        :type jwt: str or bytes
        :param key: the key suitable for the allowed algorithm
        :type key: str or bytes or PyJWK or :py:class:`jwt.algorithms.AllowedPublicKeys`

        :param algorithms: allowed algorithms, e.g. ``["ES256"]``
            If ``key`` is a :class:`PyJWK` object, allowed algorithms will default to the key algorithm.

            .. warning::

               Do **not** compute the ``algorithms`` parameter based on
               the ``alg`` from the token itself, or on any other data
               that an attacker may be able to influence, as that might
               expose you to various vulnerabilities (see `RFC 8725 Â§2.1
               <https://www.rfc-editor.org/rfc/rfc8725.html#section-2.1>`_). Instead,
               either hard-code a fixed value for ``algorithms``, or
               configure it in the same place you configure the
               ``key``. Make sure not to mix symmetric and asymmetric
               algorithms that interpret the ``key`` in different ways
               (e.g. HS\* and RS\*).
        :type algorithms: typing.Sequence[str] or None

        :param jwt.types.Options options: extended decoding and validation options
            Refer to :py:class:`jwt.types.Options` for more information.

        :param audience: optional, the value for ``verify_aud`` check
        :type audience: str or typing.Iterable[str] or None
        :param subject: optional, the value for ``verify_sub`` check
        :type subject: str or None
        :param issuer: optional, the value for ``verify_iss`` check
        :type issuer: str or typing.Container[str] or None
        :param leeway: a time margin in seconds for the expiration check
        :type leeway: float or datetime.timedelta
        :rtype: dict[str, typing.Any]
        :returns: the JWT claims
        zppassing additional kwargs to decode() is deprecated and will be removed in pyjwt version 3. Unsupported kwargs: rq   rr   )re   rg   ri   rm   rk   rn   rC   N)
ru   rv   rw   rx   r   rz   r   rT   rN   r   )r2   ra   rE   rc   r)   re   rg   ri   rm   rk   rn   rp   r}   r3   r3   r4   Údecode,  s*   9
þûözPyJWT.decodeúIterable[str] | str | NoneúContainer[str] | str | Nonec                 C  s  t |tƒr	| ¡ }|d urt |ttfƒstdƒ‚|  ||d ¡ tjt	j
d� ¡ }d|v r8|d r8|  |||¡ d|v rG|d rG|  |||¡ d|v rV|d	 rV|  |||¡ |d
 r`|  ||¡ |d rp| j||| dd¡d� |d rz|  ||¡ |d r…|  |¡ d S d S )Nz+audience must be a string, iterable or Noner>   )ÚtzrP   r9   rQ   r8   rO   r7   r;   r:   r?   F©Ústrictr<   r=   )rS   r   Útotal_secondsrN   r   rU   Ú_validate_required_claimsr   Únowr	   ÚutcÚ	timestampÚ_validate_iatÚ_validate_nbfÚ_validate_expÚ_validate_issÚ_validate_audrB   Ú_validate_subÚ_validate_jti)r2   rC   r)   ri   rk   rm   rn   rŠ   r3   r3   r4   r|   {  s.   
	ÿÿzPyJWT._validate_claimsÚclaimsúIterable[str]c                 C  s$   |D ]}|  |¡d u rt|ƒ‚qd S ©N)rB   r   )r2   rC   r”   Zclaimr3   r3   r4   r‰   ¥  s
   ÿÿzPyJWT._validate_required_claimsc                 C  sH   d|vrdS t |d tƒstdƒ‚|dur | d¡|kr"tdƒ‚dS dS )zè
        Checks whether "sub" if in the payload is valid or not.
        This is an Optional claim

        :param payload(dict): The payload which needs to be validated
        :param subject(str): The subject of the token
        ÚsubNzSubject must be a stringzInvalid subject)rS   rN   r   rB   )r2   rC   rm   r3   r3   r4   r’   ®  s   þzPyJWT._validate_subc                 C  s(   d|vrdS t | d¡tƒstdƒ‚dS )z±
        Checks whether "jti" if in the payload is valid or not
        This is an Optional claim

        :param payload(dict): The payload which needs to be validated
        ZjtiNzJWT ID must be a string)rS   rB   rN   r   )r2   rC   r3   r3   r4   r“   Ã  s
   ÿzPyJWT._validate_jtirŠ   Úfloatc                 C  óB   zt |d ƒ}W n ty   tdƒd ‚w ||| krtdƒ‚d S )NrP   z)Issued At claim (iat) must be an integer.z The token is not yet valid (iat))Úintr   r   r   )r2   rC   rŠ   rn   rP   r3   r3   r4   r�   Ñ  s   ÿþÿÿzPyJWT._validate_iatc                 C  r™   )NrQ   z*Not Before claim (nbf) must be an integer.z The token is not yet valid (nbf))rš   r   r   r   )r2   rC   rŠ   rn   rQ   r3   r3   r4   rŽ   à  s   
ÿÿzPyJWT._validate_nbfc                 C  sB   zt |d ƒ}W n ty   tdƒd ‚w ||| krtdƒ‚d S )NrO   z/Expiration Time claim (exp) must be an integer.zSignature has expired)rš   r   r   r   )r2   rC   rŠ   rn   rO   r3   r3   r4   r�   î  s   ÿþÿÿzPyJWT._validate_expFr†   r‡   c                  sî   |d u rd|vs|d sd S t dƒ‚d|vs|d stdƒ‚|d ‰ |r@t|tƒs-t dƒ‚tˆ tƒs6t dƒ‚|ˆ kr>t dƒ‚d S tˆ tƒrHˆ g‰ tˆ tƒsQt dƒ‚tdd„ ˆ D ƒƒr^t dƒ‚t|tƒrf|g}t‡ fd	d„|D ƒƒrut d
ƒ‚d S )NÚaudzInvalid audiencezInvalid audience (strict)z&Invalid claim format in token (strict)zAudience doesn't match (strict)zInvalid claim format in tokenc                 s  s   � | ]	}t |tƒ V  qd S r–   )rS   rN   )Ú.0Úcr3   r3   r4   Ú	<genexpr>'  s   € z&PyJWT._validate_aud.<locals>.<genexpr>c                 3  s   � | ]}|ˆ vV  qd S r–   r3   )rœ   r›   ©Zaudience_claimsr3   r4   rž   -  s   € zAudience doesn't match)r   r   rS   rN   ÚlistÚanyÚall)r2   rC   ri   r‡   r3   rŸ   r4   r‘   þ  s4   




ÿzPyJWT._validate_audc                 C  s„   |d u rd S d|vrt dƒ‚|d }t|tƒstdƒ‚t|tƒr*||kr(tdƒ‚d S z||vr3tdƒ‚W d S  tyA   tdƒd ‚w )NrR   z%Payload Issuer (iss) must be a stringzInvalid issuerz.Issuer param must be "str" or "Container[str]")r   rS   rN   r   rU   )r2   rC   rk   rR   r3   r3   r4   r�   0  s,   

ÿÿÿþÿzPyJWT._validate_issr–   )r)   r*   r+   r,   )r+   r#   )r+   r%   )r)   r*   r+   r#   )rC   rD   rE   r&   rF   rG   rH   rI   rJ   rK   rL   rM   r+   rN   )NN)rC   rD   rH   rI   rJ   rK   r+   rZ   )	r   NNNNNNNr   )ra   rb   rE   r'   rc   rd   r)   r*   re   rf   rg   rh   ri   rj   rk   rl   rm   rG   rn   ro   rp   r   r+   rD   )r}   rD   r+   rD   )ra   rb   rE   r�   rc   rd   r)   r*   re   rf   rg   rh   ri   rj   rm   rG   rk   rl   rn   ro   rp   r   r+   rD   )NNNr   )rC   rD   r)   r#   ri   rƒ   rk   r„   rm   rG   rn   ro   r+   r,   )rC   rD   r”   r•   r+   r,   )rC   rD   rm   rG   r+   r,   )rC   rD   r+   r,   )rC   rD   rŠ   r˜   rn   r˜   r+   r,   )rC   rD   ri   rj   r‡   rM   r+   r,   )rC   rD   rk   r„   r+   r,   )Ú__name__Ú
__module__Ú__qualname__r5   Ústaticmethodr-   r/   r.   r   rY   rX   rz   r{   r‚   r|   r‰   r’   r“   r�   rŽ   r�   r‘   r�   r3   r3   r3   r4   r(   *   sd    
ùEüñ
nñSù
*
ÿ



û2r(   )<Ú
__future__r   r_   Úosru   Úcalendarr   Úcollections.abcr   r   r   r   r   r	   Útypingr
   r   r   r   Zapi_jwsr   r   r   Ú
exceptionsr   r   r   r   r   r   r   r   r   r   rM   ÚgetenvÚsysÚversion_infor   Ztyping_extensionsrc   r    r!   Zapi_jwkr"   Útypesr#   r$   r%   rN   rZ   r&   Ú__annotations__r'   r(   Z_jwt_global_objr0   rY   rz   r‚   r3   r3   r3   r4   Ú<module>   s>    ,
    $
