o
    ¦ó·iþ  ã                   @   s  U d dl 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ƒsYefZdd„ eD ƒZe e! e"d< edeeƒZ#G dd„ dej$ƒZ%G dd„ de%ƒZ&e&Z'de#de(fdd„Z)dS )é    )ÚOptionalÚTypeVar)Úget_user_model)ÚAbstractBaseUser)Úgettext_lazy)ÚHTTP_HEADER_ENCODINGÚauthentication)ÚRequesté   )ÚAuthenticationFailedÚInvalidTokenÚ
TokenError)Ú	TokenUser)Úapi_settings)ÚToken)Úget_md5_hash_passwordc                 C   s   h | ]}|  t¡’qS © )Úencoder   )Ú.0Úhr   r   úmC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\rest_framework_simplejwt/authentication.pyÚ	<setcomp>   s    
ÿr   ÚAUTH_HEADER_TYPE_BYTESÚAuthUserc                       s¦   e Zd ZdZdZdZd‡ fdd„Zdedee	e
ef  fd	d
„Zdedefdd„Zdedefdd„Zdedee fdd„Zdedefdd„Zdede
fdd„Z‡  ZS )ÚJWTAuthenticationzy
    An authentication plugin that authenticates requests through a JSON web
    token provided in a request header.
    Úapizapplication/jsonÚreturnNc                    s   t ƒ j|i |¤Ž tƒ | _d S ©N)ÚsuperÚ__init__r   Ú
user_model)ÚselfÚargsÚkwargs©Ú	__class__r   r   r   $   s   zJWTAuthentication.__init__Úrequestc                 C   sD   |   |¡}|d u rd S |  |¡}|d u rd S |  |¡}|  |¡|fS r   )Ú
get_headerÚget_raw_tokenÚget_validated_tokenÚget_user)r!   r&   ÚheaderÚ	raw_tokenÚvalidated_tokenr   r   r   Úauthenticate(   s   


zJWTAuthentication.authenticatec                 C   s   d  td | j¡S )Nz{} realm="{}"r   )ÚformatÚAUTH_HEADER_TYPESÚwww_authenticate_realm)r!   r&   r   r   r   Úauthenticate_header5   s   þz%JWTAuthentication.authenticate_headerc                 C   s&   |j  tj¡}t|tƒr| t¡}|S )zc
        Extracts the header containing the JSON web token from the given
        request.
        N)ZMETAÚgetr   ZAUTH_HEADER_NAMEÚ
isinstanceÚstrr   r   )r!   r&   r+   r   r   r   r'   ;   s   

zJWTAuthentication.get_headerr+   c                 C   sL   |  ¡ }t|ƒdkrdS |d tvrdS t|ƒdkr"ttdƒ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 valuesZbad_authorization_header©Úcoder
   )ÚsplitÚlenr   r   Ú_)r!   r+   Úpartsr   r   r   r(   H   s   þzJWTAuthentication.get_raw_tokenr,   c                 C   sp   g }t jD ])}z||ƒW   S  ty. } z| |j|j|jd dœ¡ W Y d}~qd}~ww ttdƒ|dœƒ‚)zk
        Validates an encoded JSON web token and returns a validated token
        wrapper object.
        r   )Ztoken_classÚ
token_typeÚmessageNz(Given token not valid for any token type)ZdetailÚmessages)	r   ZAUTH_TOKEN_CLASSESr   ÚappendÚ__name__r=   r"   r   r;   )r!   r,   r?   Z	AuthTokenÚer   r   r   r)   _   s$   
ýÿ€ÿ	þÿz%JWTAuthentication.get_validated_tokenr-   c              
   C   sÐ   z|t j }W n ty } zttdƒƒ|‚d}~ww z| jjjd
i t j|i¤Ž}W n | jj	yA } z	t
tdƒdd�|‚d}~ww t jrP|jsPt
tdƒdd�‚t jrf| t j¡t|jƒkrft
tdƒd	d�‚|S )zU
        Attempts to find and return a user using the given validated token.
        ú3Token contained no recognizable user identificationNzUser not foundZuser_not_foundr7   zUser is inactiveZuser_inactivez%The user's password has been changed.Zpassword_changedr   )r   ÚUSER_ID_CLAIMÚKeyErrorr   r;   r    Zobjectsr3   ZUSER_ID_FIELDZDoesNotExistr   ÚCHECK_USER_IS_ACTIVEÚ	is_activeZCHECK_REVOKE_TOKENZREVOKE_TOKEN_CLAIMr   Úpassword)r!   r-   Zuser_idrB   Úuserr   r   r   r*   x   s@   ÿþ€ÿÿþ€ÿÿþÿzJWTAuthentication.get_user)r   N)rA   Ú
__module__Ú__qualname__Ú__doc__r1   Ú
media_typer   r	   r   Útupler   r   r.   r5   r2   Úbytesr'   r(   r)   r*   Ú__classcell__r   r   r$   r   r      s    r   c                   @   s"   e Zd ZdZdedefdd„ZdS )Ú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                 C   s    t j|vrttdƒƒ‚t  |¡S )zg
        Returns a stateless user object which is backed by the given validated
        token.
        rC   N)r   rD   r   r;   ZTOKEN_USER_CLASS)r!   r-   r   r   r   r*   ž   s   

z'JWTStatelessUserAuthentication.get_userN)rA   rJ   rK   rL   r   r   r*   r   r   r   r   rQ   ˜   s    rQ   rI   r   c                 C   s   | d uo
t j p
| jS r   )r   rF   rG   )rI   r   r   r   Ú default_user_authentication_rule®   s   ÿrR   N)*Útypingr   r   Zdjango.contrib.authr   Zdjango.contrib.auth.modelsr   Zdjango.utils.translationr   r;   Zrest_frameworkr   r   Zrest_framework.requestr	   Ú
exceptionsr   r   r   Úmodelsr   Úsettingsr   Útokensr   Úutilsr   r0   r4   ÚlistrN   r   ÚsetrO   Ú__annotations__r   ZBaseAuthenticationr   rQ   ZJWTTokenUserAuthenticationÚboolrR   r   r   r   r   Ú<module>   s,    ÿ}