a
    xd                     @   sZ   d dl mZ d dlZd dlmZmZmZmZ ddlm	Z	 ddl
mZ dZG dd	 d	ZdS )
    )gettext_lazyN)InvalidAlgorithmErrorInvalidTokenErrorPyJWKClient
algorithms   )TokenBackendError)format_lazy)ZHS256ZHS384ZHS512ZRS256ZRS384ZRS512c                   @   s>   e Zd ZdedddZdd Zdd	 Zd
d ZdddZdS )TokenBackendNr   )jwk_urlc                 C   sV   |  | || _|| _|| _|| _|r.t|nd | _|| _|drL|| _	n|| _	d S NZHS)
_validate_algorithm	algorithmsigning_keyaudienceissuerr   jwks_clientleeway
startswithverifying_key)selfr   r   r   r   r   r   r    r   Y/var/www/html/Ranjet/env/lib/python3.9/site-packages/rest_framework_simplejwt/backends.py__init__   s    


zTokenBackend.__init__c                 C   s@   |t vrtttd||tjv r<tjs<tttd|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   Zrequires_cryptographyZ
has_crypto)r   r   r   r   r   r   ,   s    z TokenBackend._validate_algorithmc                 C   s,   | j dr| jS | jr&| j|jS | jS r   )r   r   r   r   Zget_signing_key_from_jwtkeyr   )r   tokenr   r   r   get_verifying_key7   s
    zTokenBackend.get_verifying_keyc                 C   s\   |  }| jdur| j|d< | jdur0| j|d< tj|| j| jd}t|trX|	dS |S )zL
        Returns an encoded token for the given payload dictionary.
        NZaudZiss)r   zutf-8)
copyr   r   jwtencoder   r   
isinstancebytesdecode)r   payloadZjwt_payloadr   r   r   r   r!   @   s    





zTokenBackend.encodeTc              
   C   s   z6t j|| || jg| j| j| j| jdu|ddW S  tyh } ztt	d|W Y d}~n&d}~0  t
y   tt	dY n0 dS )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 invalid or expired)r    r$   r   r   r   r   r   r   r   r   r   )r   r   verifyexr   r   r   r$   Q   s     "zTokenBackend.decode)NNNNNr   )T)	__name__
__module____qualname__strr   r   r   r!   r$   r   r   r   r   r
      s         	r
   )Zdjango.utils.translationr   r   r    r   r   r   r   
exceptionsr   utilsr	   r   r
   r   r   r   r   <module>   s   
