U
    ƽb                  	   @   s   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
 zd dl mZ dZW n ek
rl   d	ZY nX d
ddddddddh	ZG dd dZdS )    N)gettext_lazy)InvalidAlgorithmErrorInvalidTokenError
algorithms   )TokenBackendError)format_lazy)PyJWKClientTFZHS256ZHS384ZHS512ZRS256ZRS384ZRS512ZES256ZES384ZES512c                   @   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 )TokenBackendN r   )jwk_urlc                 C   sP   |  | || _|| _|| _|| _|| _tr@|r8t|nd | _nd | _|| _	d S )N)
_validate_algorithm	algorithmsigning_keyverifying_keyaudienceissuerJWK_CLIENT_AVAILABLEr	   jwks_clientleeway)selfr   r   r   r   r   r   r    r   E/tmp/pip-unpacked-wheel-cvqxdfy5/rest_framework_simplejwt/backends.py__init__   s    

zTokenBackend.__init__c                 C   s@   |t krtttd||tjk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   5   s     z TokenBackend._validate_algorithmc                 C   s,   | j dr| jS | jr&| j|jS | jS )NZHS)r   
startswithr   r   Zget_signing_key_from_jwtkeyr   )r   tokenr   r   r   get_verifying_keyF   s
    zTokenBackend.get_verifying_keyc                 C   s\   |  }| jdk	r| j|d< | jdk	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"   O   s    





zTokenBackend.encodeTc              
   C   s   z6t j|| || jg| j| j| j| jdk	|ddW S  tk
rf } ztt	d|W 5 d}~X Y n  t
k
r   tt	dY nX 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%   `   s     zTokenBackend.decode)Nr   NNNr   )T)	__name__
__module____qualname__strr   r   r   r"   r%   r   r   r   r   r
      s         	r
   )r!   Zdjango.utils.translationr   r   r   r   r   
exceptionsr   utilsr   r	   r   ImportErrorr   r
   r   r   r   r   <module>   s(   
