o
    ¦ó·ie  ã                   @   sæ   d dl Z d dlmZ d dlmZ d dlmZ d dlmZm	Z	m
Z
 d dlZd dl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 ddlmZ zd dlmZmZ dZW n eya   dZY nw h d£ ej¡Z G dd„ dƒZ!dS )é    N)ÚIterable)Ú	timedelta)Úcached_property)ÚAnyÚOptionalÚUnion)Úgettext_lazy)ÚExpiredSignatureErrorÚInvalidAlgorithmErrorÚInvalidTokenErrorÚ
algorithmsé   )ÚTokenBackendErrorÚTokenBackendExpiredToken)ÚToken)Úformat_lazy)ÚPyJWKClientÚPyJWKClientErrorTF>	   ZHS256ZRS384ZHS512ZES256ZES512ZRS512ZHS384ZRS256ZES384c                   @   s  e Zd Z							d#dedee dedeeedf dee dee d	eeee	df d
ee
ej  ddfdd„Zedefdd„ƒZedefdd„ƒZdee defdd„Zdeddfdd„Zde	fdd„Zdedefdd„Zdeeef defdd„Zd$ded edeeef fd!d"„ZdS )%ÚTokenBackendNÚ Ú	algorithmÚsigning_keyÚverifying_keyÚaudienceÚissuerÚjwk_urlÚleewayÚjson_encoderÚreturnc	           	      C   sV   |   |¡ || _|| _|| _|| _|| _tr |rt|ƒnd | _nd | _|| _	|| _
d S ©N)Ú_validate_algorithmr   r   r   r   r   ÚJWK_CLIENT_AVAILABLEr   Újwks_clientr   r   )	Úselfr   r   r   r   r   r   r   r   © r$   úgC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\rest_framework_simplejwt/backends.pyÚ__init__)   s   

zTokenBackend.__init__c                 C   ó   |   | j¡S r   )Ú_prepare_keyr   ©r#   r$   r$   r%   Úprepared_signing_keyD   ó   z!TokenBackend.prepared_signing_keyc                 C   r'   r   )r(   r   r)   r$   r$   r%   Úprepared_verifying_keyH   r+   z#TokenBackend.prepared_verifying_keyÚkeyc                 C   s4   |d u st tjdd ƒs|S t ¡  | j¡}| |¡S )NÚget_algorithm_by_name)ÚgetattrÚjwtZPyJWSr.   r   Zprepare_key)r#   r-   Zjws_algr$   r$   r%   r(   L   s   
zTokenBackend._prepare_keyc                 C   sD   |t vrtttdƒ|ƒƒ‚|tjv rtjs tttdƒ|ƒƒ‚dS dS )z”
        Ensure that the nominated algorithm is recognized, and that cryptography is installed for those
        algorithms that require it
        z Unrecognized algorithm type '{}'z/You must have cryptography installed to use {}.N)ÚALLOWED_ALGORITHMSr   r   Ú_r   Úrequires_cryptographyZ
has_crypto)r#   r   r$   r$   r%   r    S   s   ÿÿÿÿz TokenBackend._validate_algorithmc                 C   sZ   | j d u r
tdd�S t| j ttfƒrt| j d�S t| j tƒr!| j S tttdƒt| j ƒƒƒ‚)Nr   )ÚsecondszIUnrecognized type '{}', 'leeway' must be of type int, float or timedelta.)	r   r   Ú
isinstanceÚintÚfloatr   r   r2   Útyper)   r$   r$   r%   Ú
get_leewayd   s   

ÿüÿzTokenBackend.get_leewayÚtokenc              
   C   sV   | j  d¡r	| jS | jr(z| j |¡jW S  ty' } zttdƒƒ|‚d }~ww | j	S )NZHSúToken is invalid)
r   Ú
startswithr*   r"   Zget_signing_key_from_jwtr-   r   r   r2   r,   )r#   r:   Úer$   r$   r%   Úget_verifying_keyu   s   €ÿzTokenBackend.get_verifying_keyÚpayloadc                 C   s`   |  ¡ }| jdur| j|d< | jdur| j|d< tj|| j| j| jd�}t|t	ƒr.| 
d¡S |S )zL
        Returns an encoded token for the given payload dictionary.
        NZaudZiss)r   r   zutf-8)Úcopyr   r   r0   Úencoder*   r   r   r5   ÚbytesÚdecode)r#   r?   Zjwt_payloadr:   r$   r$   r%   rA   �   s   



ü

zTokenBackend.encodeTÚverifyc              
   C   s¨   zt j||  |¡| jg| j| j|  ¡ | jdu|dœd�W S  ty/ } ztt	dƒƒ|‚d}~w t
yA } ztt	dƒƒ|‚d}~w tyS } ztt	dƒƒ|‚d}~ww )zý
        Performs a validation of the given token and returns its payload
        dictionary.

        Raises a `TokenBackendError` if the token is malformed, if its
        signature check fails, or if its 'exp' claim indicates it has expired.
        N)Z
verify_audZverify_signature)r   r   r   r   ÚoptionszInvalid algorithm specifiedzToken is expiredr;   )r0   rC   r>   r   r   r   r9   r
   r   r2   r	   r   r   )r#   r:   rD   r=   r$   r$   r%   rC   —   s,   þù€€€ÿzTokenBackend.decode)Nr   NNNNN)T)Ú__name__Ú
__module__Ú__qualname__Ústrr   r   r   r7   r6   r   r8   ÚjsonÚJSONEncoderr&   r   r   r*   r,   r(   r    r9   r   r>   ÚdictrA   ÚboolrC   r$   r$   r$   r%   r   (   sJ    ÷þýüûúùø	÷

ö$r   )"rJ   Úcollections.abcr   Údatetimer   Ú	functoolsr   Útypingr   r   r   r0   Zdjango.utils.translationr   r2   r	   r
   r   r   Ú
exceptionsr   r   Útokensr   Úutilsr   r   r   r!   ÚImportErrorÚunionr3   r1   r   r$   r$   r$   r%   Ú<module>   s*    ÿ
ö