o
     eE                     @   s   d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	m
Z
 d dlZd dlmZ d dlmZmZmZ ddl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y[   dZY nw h dZG dd dZdS )    N)Iterable)	timedelta)AnyDictOptionalTypeUnion)gettext_lazy)InvalidAlgorithmErrorInvalidTokenError
algorithms   )TokenBackendError)Token)format_lazy)PyJWKClientPyJWKClientErrorTF>	   ES256ES384ES512HS256HS384HS512RS256RS384RS512c                   @   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deddfddZde	fddZdede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+   T/var/www/html/venv/lib/python3.10/site-packages/rest_framework_simplejwt/backends.py__init__#   s   

zTokenBackend.__init__c                 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_cryptography
has_crypto)r*   r   r+   r+   r,   r'   >   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   r/   type)r*   r+   r+   r,   
get_leewayO   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 )NHSToken is invalid or expired)
r   
startswithr   r)   get_signing_key_from_jwtkeyr   r   r/   r    )r*   r8   exr+   r+   r,   get_verifying_key`   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.
        Naudiss)r   r%   zutf-8)copyr!   r"   jwtencoder   r   r%   r3   bytesdecode)r*   r@   jwt_payloadr8   r+   r+   r,   rE   l   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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)
verify_audverify_signature)r   r!   r"   r$   optionszInvalid algorithm specifiedr:   )rD   rG   r?   r   r!   r"   r7   r
   r   r/   r   )r*   r8   rI   r>   r+   r+   r,   rG      s&   zTokenBackend.decode)Nr   NNNNN)T)__name__
__module____qualname__strr   r   r   r5   r4   r   r   jsonJSONEncoderr-   r'   r7   r   r?   r   r   rE   boolrG   r+   r+   r+   r,   r   "   s@    	

$r   )rQ   collections.abcr   datetimer   typingr   r   r   r   r   rD   django.utils.translationr	   r/   r
   r   r   
exceptionsr   tokensr   utilsr   r   r   r(   ImportErrorr.   r   r+   r+   r+   r,   <module>   s$    