
    Vwgt0                        d dl m Z mZ d dlmZmZmZmZmZmZm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ d dlmZ dd	lmZmZ dd
lmZ ddlmZ ddlmZmZ ddlmZm Z m!Z!m"Z"m#Z# erddl$m%Z%  e	dd      Z& e	dee      Z' G d d      Z( G d dee&         Z) G d de)d   e(      Z* G d de(      Z+ G d de)d   e(      Z, G d de(      Z-y)    )datetime	timedelta)TYPE_CHECKINGAnyDictGenericOptionalTypeTypeVar)uuid4)settings)AbstractBaseUser)import_string)gettext_lazy   )TokenBackendError
TokenError)	TokenUser)api_settings)BlacklistedTokenOutstandingToken)aware_utcnowdatetime_from_epochdatetime_to_epochformat_lazyget_md5_hash_password)TokenBackendTToken)boundAuthUserc            	          e Zd ZU dZdZee   ed<   dZee	   ed<   d$ded    de
ddfdZdefd	Zd
efdZd
ededdfdZd
eddfdZd
edefdZd%d
edee   defdZdefdZd&dZd&dZd&dZ	 	 	 d'dedee   dee	   ddfdZd(dedee   ddfdZ	 d)dedee   ddfdZedee   dedefd       ZdZ ed    ed!<   e!d*d"       Z"d*d#Z#y)+r   zb
    A class which validates and wraps an existing JWT or can be used to build a
    new JWT.
    N
token_typelifetimetokenverifyreturnc                 0   | j                   | j                  t        t        d            || _        t               | _        |=| j                         }	 |j                  ||      | _	        |r| j                          yyt        j                  | j                   i| _	        | j                  | j                  | j                         | j                  | j                         | j!                          y# t        $ r t        t        d            w xY w)z
        !!!! IMPORTANT !!!! MUST raise a TokenError with a user-facing error
        message if the given token is invalid, expired, or otherwise not safe
        to use.
        Nz,Cannot create token with no type or lifetime)r&   zToken is invalid or expired	from_timer$   )at_time)r#   r$   r   _r%   r   current_timeget_token_backenddecodepayloadr   r&   r   TOKEN_TYPE_CLAIMset_expset_iatset_jti)selfr%   r&   token_backends       V/var/www/horilla/myenv/lib/python3.12/site-packages/rest_framework_simplejwt/tokens.py__init__zToken.__init__'   s     ??"dmm&;QMNOO
(N  224MC,33E&3I   )994??KDL LL4#4#4t}}LMLL!2!2L3 LLN % C #@!ABBCs   C7 7Dc                 ,    t        | j                        S N)reprr0   r5   s    r7   __repr__zToken.__repr__K   s    DLL!!    keyc                      | j                   |   S r:   r0   r5   r?   s     r7   __getitem__zToken.__getitem__N   s    ||C  r>   valuec                 "    || j                   |<   y r:   rA   )r5   r?   rD   s      r7   __setitem__zToken.__setitem__Q   s    !Sr>   c                     | j                   |= y r:   rA   rB   s     r7   __delitem__zToken.__delitem__T   s    LLr>   c                     || j                   v S r:   rA   rB   s     r7   __contains__zToken.__contains__W   s    dll""r>   defaultc                 :    | j                   j                  ||      S r:   )r0   get)r5   r?   rK   s      r7   rM   z	Token.getZ   s    ||W--r>   c                 T    | j                         j                  | j                        S )zG
        Signs and returns a token as a base64 encoded string.
        )r.   encoder0   r<   s    r7   __str__zToken.__str__]   s"     %%'..t||<<r>   c                     | j                          t        j                  0t        j                  | j                  vrt	        t        d            t        j                  | j                          yy)z
        Performs additional validation steps which were not performed when this
        token was decoded.  This method is part of the "public" API to indicate
        the intention that it may be overridden in subclasses.
        NzToken has no id)	check_expr   	JTI_CLAIMr0   r   r,   r1   verify_token_typer<   s    r7   r&   zToken.verifyc   s\     	 "".&&dll:Q0122((4""$ 5r>   c                     	 | j                   t        j                     }| j                  |k7  rt	        t        d            y# t        $ r t	        t        d            w xY w)zY
        Ensures that the token type claim is present and has the correct value.
        zToken has no typezToken has wrong typeN)r0   r   r1   KeyErrorr   r,   r#   )r5   r#   s     r7   rT   zToken.verify_token_type{   sa    	5l&C&CDJ ??j(Q5677 )  	5Q2344	5s   A A!c                 b    t               j                  | j                  t        j                  <   y)a  
        Populates the configured jti claim of a token with a string where there
        is a negligible probability that the same string will be chosen at a
        later time.

        See here:
        https://tools.ietf.org/html/rfc7519#section-4.1.7
        N)r   hexr0   r   rS   r<   s    r7   r4   zToken.set_jti   s     05w{{\++,r>   claimr*   c                 r    || j                   }|| j                  }t        ||z         | j                  |<   y)z
        Updates the expiration time of a token.

        See here:
        https://tools.ietf.org/html/rfc7519#section-4.1.4
        N)r-   r$   r   r0   )r5   rY   r*   r$   s       r7   r2   zToken.set_exp   s<     ))I}}H/	H0DEUr>   r+   c                 P    || j                   }t        |      | j                  |<   y)z
        Updates the time at which the token was issued.

        See here:
        https://tools.ietf.org/html/rfc7519#section-4.1.6
        N)r-   r   r0   )r5   rY   r+   s      r7   r3   zToken.set_iat   s'     ?''G/8Ur>   r-   c                 4   || j                   }	 | j                  |   }t        |      }| j                         j                         }|||z
  k  rt        t	        t        d      |            y# t        $ r t        t	        t        d      |            w xY w)z
        Checks whether a timestamp value in the given claim has passed (since
        the given datetime value in `current_time`).  Raises a TokenError with
        a user-facing error message if so.
        NzToken has no '{}' claimzToken '{}' claim has expired)	r-   r0   rV   r   r   r,   r   r.   
get_leeway)r5   rY   r-   claim_value
claim_timeleeways         r7   rR   zToken.check_exp   s     ,,L	O,,u-K )5
'')446..[+I)JERSS /  	O[+D)EuMNN	Os   A/ /(Bclsuserc                    t        |t        j                        }t        |t              st        |      } |        }||t        j                  <   t        j                  r&t        |j                        |t        j                  <   |S )z
        Returns an authorization token for the given user that will be provided
        after authenticating the user's credentials.
        )getattrr   USER_ID_FIELD
isinstanceintstrUSER_ID_CLAIMCHECK_REVOKE_TOKENr   passwordREVOKE_TOKEN_CLAIM)ra   rb   user_idr%   s       r7   for_userzToken.for_user   sl     $ : :;'3''lG,3l(()**5J6E,112 r>   r   _token_backendc                 R    | j                   t        d      | _         | j                   S )Nz,rest_framework_simplejwt.state.token_backend)ro   r   r<   s    r7   r6   zToken.token_backend   s,    &"/>#D """r>   c                     | j                   S r:   )r6   r<   s    r7   r.   zToken.get_token_backend   s    !!!r>   )NTr:   r'   N)expNN)iatN)rs   N)r'   r   )$__name__
__module____qualname____doc__r#   r	   rh   __annotations__r$   r   boolr8   r=   rC   r   rF   rH   rJ   rM   rP   r&   rT   r4   r   r2   r3   rR   classmethodr
   r   r!   rn   ro   propertyr6   r.    r>   r7   r   r      s   
 !%J$$(Hhy!("hw/ " "PT "H"# "!s !"s "3 "4 "s t # # #.s .Xc] .c .= =%0
8	; (,(,	FF H%F 9%	F
 
F(
9S 
98H3E 
9QU 
9 FJTT080BT	T* d1g X !  & 04NH^,3# #"r>   c                        e Zd ZU dZeeef   ed<   dej                  v r(d fdZ
ddZdefdZedee   d	edef fd
       Z xZS  xZS )BlacklistMixina  
    If the `rest_framework_simplejwt.token_blacklist` app was configured to be
    used, tokens created from `BlacklistMixin` subclasses will insert
    themselves into an outstanding token list and also check for their
    membership in a token blacklist.
    r0   z(rest_framework_simplejwt.token_blacklistr'   c                 D    | j                          t        |   |i | y r:   )check_blacklistsuperr&   r5   argskwargs	__class__s      r7   r&   zBlacklistMixin.verify   s       "GND+F+r>   c                     | j                   t        j                     }t        j                  j                  |      j                         rt        t        d            y)zy
            Checks if this token is present in the token blacklist.  Raises
            `TokenError` if so.
            )
token__jtizToken is blacklistedN)	r0   r   rS   r   objectsfilterexistsr   r,   )r5   jtis     r7   r   zBlacklistMixin.check_blacklist   sO    
 ,,|556C''..#.>EEG #9!:;; Hr>   c           	      v   | j                   t        j                     }| j                   d   }| j                   j                  t        j                        }t
        j                  j                  ||| j                  t        |       t        |      d      \  }}t        j                  j                  |      S )z
            Ensures this token is included in the outstanding token list and
            adds it to the blacklist.
            rs   )rm   
created_atr%   
expires_at)r   defaults)r%   )r0   r   rS   rM   ri   r   r   get_or_creater-   rh   r   r   )r5   r   rs   rm   r%   r,   s         r7   	blacklistzBlacklistMixin.blacklist  s    
 ,,|556C,,u%Cll&&|'A'ABG (//==&"&"3"3 Y"5c":	 > HE1 $++999FFr>   ra   rb   c           	          t         |   |      }|t        j                     }|d   }t        j
                  j                  ||t        |      |j                  t        |             |S )zH
            Adds this token to the outstanding token list.
            rs   )rb   r   r%   r   r   )
r   rn   r   rS   r   r   createrh   r-   r   )ra   rb   r%   r   rs   r   s        r7   rn   zBlacklistMixin.for_user  sk    
 G$T*E../C,C$$++%j --.s3 ,  Lr>   rr   )ru   rv   rw   rx   r   rh   r   ry   r   INSTALLED_APPSr&   r   r   r   r{   r
   r   r!   rn   __classcell__r   s   @r7   r   r      ss     #s(^1X5L5LL	,
	<	G/ 	G, 
	$q' 	 	a 	 
	Q Mr>   r   c                   <     e Zd ZdZej
                  Zd fdZ xZS )SlidingTokenslidingc                     t        |   |i | | j                  ;| j                  t        j
                  | j                  t        j                         y y )Nr)   )r   r8   r%   r2   r   SLIDING_TOKEN_REFRESH_EXP_CLAIMr-   SLIDING_TOKEN_REFRESH_LIFETIMEr   s      r7   r8   zSlidingToken.__init__4  sP    $)&)::LL<<++%DD   r>   rr   )	ru   rv   rw   r#   r   SLIDING_TOKEN_LIFETIMEr$   r8   r   r   s   @r7   r   r   0  s    J22H	 	r>   r   c                   (    e Zd ZdZej
                  Zy)AccessTokenaccessN)ru   rv   rw   r#   r   ACCESS_TOKEN_LIFETIMEr$   r}   r>   r7   r   r   @  s    J11Hr>   r   c                   v    e Zd ZdZej
                  Zej                  dej                  dfZ	e
Zede
fd       Zy)RefreshTokenrefreshrs   r   r'   c                     | j                         }|j                  | j                         | j                  }| j                  j                         D ]  \  }}||v r|||<    |S )z
        Returns an access token created from this refresh token.  Copies all
        claims present in this refresh token to the new access token except
        those claims listed in the `no_copy_claims` attribute.
        )r*   )access_token_classr2   r-   no_copy_claimsr0   items)r5   r   no_copyrY   rD   s        r7   access_tokenzRefreshToken.access_tokenT  sn     ((* 	!2!23%% LL..0 	"LE5!F5M	"
 r>   N)ru   rv   rw   r#   r   REFRESH_TOKEN_LIFETIMEr$   r1   rS   r   r   r   r|   r   r}   r>   r7   r   r   E  sT    J22H%%
 		N %k  r>   r   c                   *    e Zd ZdZ ed      ZddZy)UntypedTokenuntypedr   )secondsNc                      y)z
        Untyped tokens do not verify the "token_type" claim.  This is useful
        when performing general validation of a token's signature and other
        properties which do not relate to the token's intended use.
        Nr}   r<   s    r7   rT   zUntypedToken.verify_token_typep  s     	r>   rr   )ru   rv   rw   r#   r   r$   rT   r}   r>   r7   r   r   l  s    J#Hr>   r   N).r   r   typingr   r   r   r   r	   r
   r   uuidr   django.confr   django.contrib.auth.modelsr   django.utils.module_loadingr   django.utils.translationr   r,   
exceptionsr   r   modelsr   r   token_blacklist.modelsr   r   utilsr   r   r   r   r   backendsr   r   r!   r   r   r   r   r   r   r}   r>   r7   <module>r      s    ( M M M    7 5 6 5  " F  &Cw:/;I" I"XCWQZ CL>.15  2% 2
$>.15 $N
5 
r>   