
    Nwg                         d Z ddlZddl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 Z G d d	e      Z G d
 d      Z G d de      Z G d de      Z G d de      Z G d de      Zy)z+
Provides various authentication policies.
    N)authenticateget_user_model)CsrfViewMiddleware)gettext_lazy)HTTP_HEADER_ENCODING
exceptionsc                     | j                   j                  dd      }t        |t              r|j	                  t
              }|S )z
    Return request's 'Authorization:' header, as a bytestring.

    Hide some test client ickyness where the header can be unicode.
    HTTP_AUTHORIZATION    )METAget
isinstancestrencoder   )requestauths     T/var/www/horilla/myenv/lib/python3.12/site-packages/rest_framework/authentication.pyget_authorization_headerr      s9     <<0#6D${{/0Kr   c                       e Zd Zd Zy)	CSRFCheckc                     |S N )selfr   reasons      r   _rejectzCSRFCheck._reject   s    r   N)__name__
__module____qualname__r   r   r   r   r   r      s    r   r   c                       e Zd ZdZd Zd Zy)BaseAuthenticationzF
    All authentication classes should extend BaseAuthentication.
    c                     t        d      )zS
        Authenticate the request and return a two-tuple of (user, token).
        z#.authenticate() must be overridden.)NotImplementedErrorr   r   s     r   r   zBaseAuthentication.authenticate&   s     ""GHHr   c                      y)z
        Return a string to be used as the value of the `WWW-Authenticate`
        header in a `401 Unauthenticated` response, or `None` if the
        authentication scheme should return `403 Permission Denied` responses.
        Nr   r$   s     r   authenticate_headerz&BaseAuthentication.authenticate_header,   s     	r   N)r   r   r   __doc__r   r&   r   r   r   r!   r!   !   s    Ir   r!   c                   (    e Zd ZdZdZd ZddZd Zy)BasicAuthenticationz>
    HTTP Basic authentication against username/password.
    apic                    t        |      j                         }|r|d   j                         dk7  ryt        |      dk(  r t	        d      }t        j                  |      t        |      dkD  r t	        d      }t        j                  |      	 	 t        j                  |d         j                  d      }|j                  d
d      \  }}| j                  |||      S # t        $ r* t        j                  |d         j                  d	      }Y Zw xY w# t        t        t        t        j                  f$ r! t	        d      }t        j                  |      w xY w)z
        Returns a `User` if a correct username and password have been supplied
        using HTTP Basic authentication.  Otherwise returns `None`.
        r   s   basicN   z.Invalid basic header. No credentials provided.   zCInvalid basic header. Credentials string should not contain spaces.zutf-8zlatin-1:z?Invalid basic header. Credentials not correctly base64 encoded.)r   splitlowerlen_r   AuthenticationFailedbase64	b64decodedecodeUnicodeDecodeError	TypeError
ValueErrorbinasciiErrorauthenticate_credentials)r   r   r   msgauth_decodeduseridpasswords          r   r   z BasicAuthentication.authenticate;   sB   
 (0668tAw}}(2t9>DEC11#66Y]YZC11#66		7K%//Q8??H  ,11#q9FH
 ,,VXwGG & K%//Q8??	JK :'98>>J 	7UVC11#66	7s+   'C  8D  0DD DD AENc                     t               j                  |d|i}t        dd|i|}|t        j                  t        d            |j                  st        j                  t        d            |dfS )z
        Authenticate the userid and password against username and password
        with optional request for context.
        r@   r   NzInvalid username/password.User inactive or deleted.r   )r   USERNAME_FIELDr   r   r3   r2   	is_active)r   r?   r@   r   credentialsusers         r   r<   z,BasicAuthentication.authenticate_credentialsY   sv     ++V
 ;G;{;<11!4P2QRR~~11!4O2PQQd|r   c                      d| j                   z  S )NzBasic realm="%s")www_authenticate_realmr$   s     r   r&   z'BasicAuthentication.authenticate_headerl   s    !D$?$???r   r   )r   r   r   r'   rH   r   r<   r&   r   r   r   r)   r)   5   s      #H<&@r   r)   c                       e Zd ZdZd Zd Zy)SessionAuthenticationz<
    Use Django's session framework for authentication.
    c                 x    t        |j                  dd      }|r|j                  sy| j                  |       |dfS )z{
        Returns a `User` if the request session currently has a logged in user.
        Otherwise returns `None`.
        rF   N)getattr_requestrD   enforce_csrfr   r   rF   s      r   r   z"SessionAuthentication.authenticateu   s@     w''6 4>>'" d|r   c                     d }t        |      }|j                  |       |j                  |ddi       }|rt        j                  d|z        y)zK
        Enforce CSRF validation for session based authentication.
        c                      y r   r   )r   s    r   dummy_get_responsez>SessionAuthentication.enforce_csrf.<locals>.dummy_get_response   s    r   Nr   zCSRF Failed: %s)r   process_requestprocess_viewr   PermissionDenied)r   r   rR   checkr   s        r   rN   z"SessionAuthentication.enforce_csrf   sW    	 ,-g&##GT2r:--.?&.HII r   N)r   r   r   r'   r   rN   r   r   r   rJ   rJ   p   s    $Jr   rJ   c                   2    e Zd ZdZdZdZd Z	 d Zd Zd Z	y)TokenAuthenticationa  
    Simple token based authentication.

    Clients should authenticate by passing the token key in the "Authorization"
    HTTP header, prepended with the string "Token ".  For example:

        Authorization: Token 401f7ac837da42b97f613d789819ff93537bee6a
    TokenNc                 B    | j                   | j                   S ddlm} |S )Nr   )rY   )modelrest_framework.authtoken.modelsrY   )r   rY   s     r   	get_modelzTokenAuthentication.get_model   s    ::!::9r   c                    t        |      j                         }|r<|d   j                         | j                  j                         j	                         k7  ry t        |      dk(  r t        d      }t        j                  |      t        |      dkD  r t        d      }t        j                  |      	 |d   j                         }| j                  |      S # t        $ r! t        d      }t        j                  |      w xY w)Nr   r,   z.Invalid token header. No credentials provided.r-   z=Invalid token header. Token string should not contain spaces.zIInvalid token header. Token string should not contain invalid characters.)r   r/   r0   keywordr   r1   r2   r   r3   r6   UnicodeErrorr<   )r   r   r   r=   tokens        r   r   z TokenAuthentication.authenticate   s    '0668tAw}}$,,*<*<*>*E*E*GGt9>DEC11#66Y]STC11#66	7GNN$E
 ,,U33	  	7_`C11#66	7s   6C *Dc                 `   | j                         }	 |j                  j                  d      j                  |      }|j                  j                  st        j                  t        d            |j                  |fS # |j                  $ r t        j                  t        d            w xY w)NrF   )keyzInvalid token.rB   )
r]   objectsselect_relatedr   DoesNotExistr   r3   r2   rF   rD   )r   rc   r[   ra   s       r   r<   z,TokenAuthentication.authenticate_credentials   s     	GMM008<<<EE zz##11!4O2PQQ

E"" !! 	G11!4D2EFF	Gs   +A? ?.B-c                     | j                   S r   )r_   r$   s     r   r&   z'TokenAuthentication.authenticate_header   s    ||r   )
r   r   r   r'   r_   r[   r]   r   r<   r&   r   r   r   rX   rX      s,     GE4*
#r   rX   c                       e Zd ZdZdZd Zy)RemoteUserAuthenticationa  
    REMOTE_USER authentication.

    To use this, set up your web server to perform authentication, which will
    set the REMOTE_USER environment variable. You will need to have
    'django.contrib.auth.backends.RemoteUserBackend in your
    AUTHENTICATION_BACKENDS setting
    REMOTE_USERc                     t        ||j                  j                  | j                              }|r|j                  r|d fS y y )N)r   remote_user)r   r   r   headerrD   rO   s      r   r   z%RemoteUserAuthentication.authenticate   s;    G9I9I$++9VWDNN$< #4r   N)r   r   r   r'   rm   r   r   r   r   ri   ri      s     F r   ri   )r'   r4   r:   django.contrib.authr   r   django.middleware.csrfr   django.utils.translationr   r2   rest_frameworkr   r   r   r   r!   r)   rJ   rX   ri   r   r   r   <module>rr      su      < 5 6 ;
"  (8@, 8@v$J. $JN<, <~ 1  r   