o
    h Ži}V  ã                   @  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 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sge!e "dd¡ƒrÌd dl#Z#e#j$dkrwd dlm%Z% nd dl&m%Z% ddl'm(Z( ddl)m*Z* ddl+m,Z,m-Z-m.Z. e(r¶ddl'm/Z/m0Z0 ee/e*e1e2f Z3de4d< ee0e*e1e2f Z5de4d< nee*e1e2f Z3de4d< 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Ú_jws_global_obj)	ÚDecodeErrorÚExpiredSignatureErrorÚImmatureSignatureErrorÚInvalidAudienceErrorÚInvalidIssuedAtErrorÚInvalidIssuerErrorÚInvalidJTIErrorÚInvalidSubjectErrorÚMissingRequiredClaimError)ÚRemovedInPyjwt3WarningZSPHINX_BUILDÚ )é   é
   )Ú	TypeAlias)Ú
has_crypto)ÚPyJWK)ÚFullOptionsÚOptionsÚ
SigOptions)ÚAllowedPrivateKeysÚAllowedPublicKeysr   ÚAllowedPrivateKeyTypesÚAllowedPublicKeyTypesc                   @  s  e Zd Zd[d\dd„Zed]d	d
„ƒZd^dd„Zd[d_dd„Z				d`dadd „Z		dbdcd"d#„Z		$								%ddded8d9„Z
dfd;d<„Z	$								%dddgd>d?„Z				%dhdidBdC„ZdjdFdG„Z	d[dkdHdI„ZdldJdK„ZdmdNdO„ZdmdPdQ„ZdmdRdS„ZdTdUœdndWdX„ZdodYdZ„ZdS )pÚ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   úUC:\Users\psant\Downloads\Anu Raj Backend\school\venv\Lib\site-packages\jwt/api_jwt.pyÚ__init__1   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-   9   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/   I   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.   Q   s   zPyJWT._merge_optionsÚHS256TÚ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.)rI   rK   )rM   N)Ú
isinstanceÚdictÚ	TypeErrorÚcopyrB   r   r   ÚutctimetuplerO   Ú_encode_payloadr0   Úencode)	r2   rD   rF   rG   rI   rK   rM   Z
time_claimZjson_payloadr3   r3   r4   rZ   `   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ÚdumpsrZ   )r2   rD   rI   rK   r3   r3   r4   rY   ¢   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.)Úcategoryrt   )rF   rd   r)   rh   )rj   rl   ro   rn   rD   )ÚwarningsÚwarnÚtupleÚkeysr   rB   ÚDeprecationWarningr.   r0   Údecode_completeÚ_decode_payloadÚ_validate_claims)r2   rb   rF   rd   r)   rf   rh   rj   rl   rn   ro   rq   r6   Zmerged_optionsZsig_optionsÚdecodedrD   r3   r3   r4   r{   ´   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.
        rD   zInvalid payload string: Nz-Invalid payload string: must be a json object)r`   ÚloadsÚ
ValueErrorr   rT   rU   )r2   r~   rD   Ú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: rr   rs   )rf   rh   rj   rn   rl   ro   rD   N)
rv   rw   rx   ry   r   r{   r   rU   rO   r   )r2   rb   rF   rd   r)   rf   rh   rj   rn   rl   ro   rq   r~   r3   r3   r4   Údecode2  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>   )ÚtzrQ   r9   rR   r8   rP   r7   r;   r:   r?   F©Ústrictr<   r=   )rT   r   Útotal_secondsrO   r   rV   Ú_validate_required_claimsr   Únowr	   ÚutcÚ	timestampÚ_validate_iatÚ_validate_nbfÚ_validate_expÚ_validate_issÚ_validate_audrB   Ú_validate_subÚ_validate_jti)r2   rD   r)   rj   rl   rn   ro   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   rD   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)rT   rO   r   rB   )r2   rD   rn   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)rT   rB   rO   r   )r2   rD   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 )NrQ   z)Issued At claim (iat) must be an integer.z The token is not yet valid (iat))Úintr€   r   r   )r2   rD   r‹   ro   rQ   r3   r3   r4   rŽ   ×  s   ÿþÿÿzPyJWT._validate_iatc                 C  rš   )NrR   z*Not Before claim (nbf) must be an integer.z The token is not yet valid (nbf))r›   r€   r   r   )r2   rD   r‹   ro   rR   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 )NrP   z/Expiration Time claim (exp) must be an integer.zSignature has expired)r›   r€   r   r   )r2   rD   r‹   ro   rP   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—   )rT   rO   )Ú.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Ÿ   3  s   € zAudience doesn't match)r   r   rT   rO   ÚlistÚanyÚall)r2   rD   rj   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 )NrS   z%Payload Issuer (iss) must be a stringzInvalid issuerz.Issuer param must be "str" or "Container[str]")r   rT   rO   r   rV   )r2   rD   rl   rS   r3   r3   r4   r‘   6  s,   

ÿÿÿþÿzPyJWT._validate_issr—   )r)   r*   r+   r,   )r+   r!   )r+   r#   )r)   r*   r+   r!   )rC   NNT)rD   rE   rF   r&   rG   rH   rI   rJ   rK   rL   rM   rN   r+   rO   )NN)rD   rE   rI   rJ   rK   rL   r+   r[   )	r   NNNNNNNr   )rb   rc   rF   r'   rd   re   r)   r*   rf   rg   rh   ri   rj   rk   rl   rm   rn   rH   ro   rp   rq   r   r+   rE   )r~   rE   r+   rE   )rb   rc   rF   r‚   rd   re   r)   r*   rf   rg   rh   ri   rj   rk   rn   rH   rl   rm   ro   rp   rq   r   r+   rE   )NNNr   )rD   rE   r)   r!   rj   r„   rl   r…   rn   rH   ro   rp   r+   r,   )rD   rE   r•   r–   r+   r,   )rD   rE   rn   rH   r+   r,   )rD   rE   r+   r,   )rD   rE   r‹   r™   ro   r™   r+   r,   )rD   rE   rj   rk   rˆ   rN   r+   r,   )rD   rE   rl   r…   r+   r,   )Ú__name__Ú
__module__Ú__qualname__r5   Ústaticmethodr-   r/   r.   rZ   rY   r{   r|   rƒ   r}   rŠ   r“   r”   rŽ   r�   r�   r’   r‘   r3   r3   r3   r4   r(   0   sd    
ùEüñ
nñSù
*
ÿ



û2r(   )<Ú
__future__r   r`   Úosrv   Úcalendarr   Úcollections.abcr   r   r   r   r   r	   Útypingr
   r   r   r   Zapi_jwsr   r   Ú
exceptionsr   r   r   r   r   r   r   r   r   r   rN   ÚgetenvÚsysÚversion_infor   Ztyping_extensionsrd   r   Zapi_jwkr    Útypesr!   r"   r#   r$   r%   rO   r[   r&   Ú__annotations__r'   r(   Z_jwt_global_objr0   rZ   r{   rƒ   r3   r3   r3   r4   Ú<module>   sF    ,
    $
