
    Vwg                        U d dl 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m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 ddlmZ ej:                  Z eej:                  ee f      sefZeD  ch c]  } | jC                  e       c} Z"ee#   e$d<    edee      Z% G d dejL                        Z' G d de'      Z(e(Z)de%de*fdZ+yc c} w )    )OptionalSetTupleTypeVar)get_user_model)AbstractBaseUser)gettext_lazy)HTTP_HEADER_ENCODINGauthentication)Request   )AuthenticationFailedInvalidToken
TokenError)	TokenUser)api_settings)Token)get_md5_hash_passwordAUTH_HEADER_TYPE_BYTESAuthUserc                        e Zd ZdZdZdZd fdZdedee	e
ef      fdZdedefdZdedefd	Zd
edee   fdZdedefdZdede
fdZ xZS )JWTAuthenticationzy
    An authentication plugin that authenticates requests through a JSON web
    token provided in a request header.
    apizapplication/jsonreturnc                 B    t        |   |i | t               | _        y N)super__init__r   
user_model)selfargskwargs	__class__s      ^/var/www/horilla/myenv/lib/python3.12/site-packages/rest_framework_simplejwt/authentication.pyr   zJWTAuthentication.__init__$   s    $)&)(*    requestc                     | j                  |      }|y | j                  |      }|y | j                  |      }| j                  |      |fS r   )
get_headerget_raw_tokenget_validated_tokenget_user)r    r&   header	raw_tokenvalidated_tokens        r$   authenticatezJWTAuthentication.authenticate(   sU    )>&&v.	229=}}_->>r%   c                 H    dj                  t        d   | j                        S )Nz{} realm="{}"r   )formatAUTH_HEADER_TYPESwww_authenticate_realm)r    r&   s     r$   authenticate_headerz%JWTAuthentication.authenticate_header5   s&    %%a ''
 	
r%   c                     |j                   j                  t        j                        }t	        |t
              r|j                  t              }|S )zc
        Extracts the header containing the JSON web token from the given
        request.
        )METAgetr   AUTH_HEADER_NAME
isinstancestrencoder
   )r    r&   r,   s      r$   r(   zJWTAuthentication.get_header;   s<    
 !!,"?"?@fc"]]#78Fr%   r,   c                     |j                         }t        |      dk(  ry|d   t        vryt        |      dk7  rt        t	        d      d      |d   S )zm
        Extracts an unvalidated JSON web token from the given "Authorization"
        header value.
        r   N   z<Authorization header must contain two space-delimited valuesbad_authorization_headercoder   )splitlenr   r   _)r    r,   partss      r$   r)   zJWTAuthentication.get_raw_tokenH   s\    
 u:?811u:?&PQ/ 
 Qxr%   r-   c                 
   g }t         j                  D ]  }	  ||      c S  t        t        d      |d      # t        $ r@}|j                  |j                  |j
                  |j                  d   d       Y d}~jd}~ww xY w)zk
        Validates an encoded JSON web token and returns a validated token
        wrapper object.
        r   )token_class
token_typemessageNz(Given token not valid for any token type)detailmessages)	r   AUTH_TOKEN_CLASSESr   append__name__rG   r!   r   rC   )r    r-   rJ   	AuthTokenes        r$   r*   z%JWTAuthentication.get_validated_token_   s    
 %88 
	I	 ++
	 FG$
 	
  '0'9'9&/&:&:#$66!9 s   9	B6A==Br.   c                 ^   	 |t         j                     }	  | j
                  j                  j                  d	i t         j                  |i}t         j                  r"|j                  st        t	        d      d      t         j                  rK|j                  t         j                        t        |j                         k7  rt        t	        d      d      |S # t        $ r t        t	        d            w xY w# | j
                  j                  $ r t        t	        d      d      w xY w)
zU
        Attempts to find and return a user using the given validated token.
        3Token contained no recognizable user identificationzUser not founduser_not_foundr?   zUser is inactiveuser_inactivez%The user's password has been changed.password_changed )r   USER_ID_CLAIMKeyErrorr   rC   r   objectsr7   USER_ID_FIELDDoesNotExistr   CHECK_USER_IS_ACTIVE	is_activeCHECK_REVOKE_TOKENREVOKE_TOKEN_CLAIMr   password)r    r.   user_idusers       r$   r+   zJWTAuthentication.get_userx   s   	Y%l&@&@AG	S.4??**..W,2L2Lg1VWD ,,T^^&q);'<?SS**""//&t}}56 +=>EW  '  	Yq!VWXX	Y
 ++ 	S&q)9':AQRR	Ss   C 6C< C9<0D,)r   N)rM   
__module____qualname____doc__r3   
media_typer   r   r   r   r   r   r/   r:   r4   bytesr(   r)   r*   r+   __classcell__)r#   s   @r$   r   r      s    
 ##J+?G ?x9O0P ?
7 
s 
' e E huo .
U 
u 
2 ( r%   r   c                        e Zd ZdZdedefdZy)JWTStatelessUserAuthenticationz
    An authentication plugin that authenticates requests through a JSON web
    token provided in a request header without performing a database lookup to obtain a user instance.
    r.   r   c                 x    t         j                  |vrt        t        d            t        j                  |      S )zg
        Returns a stateless user object which is backed by the given validated
        token.
        rQ   )r   rV   r   rC   TOKEN_USER_CLASS)r    r.   s     r$   r+   z'JWTStatelessUserAuthentication.get_user   s6    
 %%_< q!VWXX,,_==r%   N)rM   rb   rc   rd   r   r   r+   rU   r%   r$   ri   ri      s    

> 
>( 
>r%   ri   ra   r   c                 L    | d uxr t         j                   xs | j                  S r   )r   r[   r\   )ra   s    r$    default_user_authentication_rulerm      s+     t ---?r%   N),typingr   r   r   r   django.contrib.authr   django.contrib.auth.modelsr   django.utils.translationr	   rC   rest_frameworkr
   r   rest_framework.requestr   
exceptionsr   r   r   modelsr   settingsr   tokensr   utilsr   r2   r9   listtupler;   r   rf   __annotations__r   BaseAuthenticationr   ri   JWTTokenUserAuthenticationboolrm   )hs   0r$   <module>r      s    0 0 . 7 6 ? * F F  "  ( 22 ,004-@*, ->&'(AHH!"& E
  :/;v99 vr>%6 >& < 
8 
 
m&s   8C