
    h                         S r SSKrSSKrSSKJrJr  SSKJr  SSKJ	r
  SSKJrJr  S r " S S	\5      r " S
 S5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rg)z+
Provides various authentication policies.
    N)authenticateget_user_model)CsrfViewMiddleware)gettext_lazy)HTTP_HEADER_ENCODING
exceptionsc                     U R                   R                  SS5      n[        U[        5      (       a  UR	                  [
        5      nU$ )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     O/var/www/html/env/lib/python3.13/site-packages/rest_framework/authentication.pyget_authorization_headerr      s<     <<0#6D${{/0Kr   c                       \ rS rSrS rSrg)	CSRFCheck   c                     U$ N )selfr   reasons      r   _rejectCSRFCheck._reject   s    r   r   N)__name__
__module____qualname____firstlineno__r   __static_attributes__r   r   r   r   r      s    r   r   c                   $    \ rS rSrSrS rS rSrg)BaseAuthentication!   z>
All authentication classes should extend BaseAuthentication.
c                     [        S5      e)zC
Authenticate the request and return a two-tuple of (user, token).
z#.authenticate() must be overridden.)NotImplementedErrorr   r   s     r   r   BaseAuthentication.authenticate&   s     ""GHHr   c                     g)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_header&BaseAuthentication.authenticate_header,   s     	r   r   N)r   r    r!   r"   __doc__r   r,   r#   r   r   r   r%   r%   !   s    Ir   r%   c                   2    \ rS rSrSrSrS rS	S jrS rSr	g)
BasicAuthentication5   z6
HTTP Basic authentication against username/password.
apic                    [        U5      R                  5       nU(       a  US   R                  5       S:w  a  g[        U5      S:X  a!  [	        S5      n[
        R                  " U5      e[        U5      S:  a!  [	        S5      n[
        R                  " U5      e  [        R                  " US   5      R                  S5      nUR                  S
S5      u  pVU R                  XVU5      $ ! [         a+    [        R                  " US   5      R                  S	5      n NZf = f! [        [        [        [        R                  4 a"    [	        S5      n[
        R                  " U5      ef = f)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    BasicAuthentication.authenticate;   s>   
 (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F
 ,,VwGG & K%//Q8??	JK :'98>>J 	7UVC11#66	7s+   (C) D! )2DD! DD! !AE'Nc                     [        5       R                  USU0n[        SSU0UD6nUc  [        R                  " [        S5      5      eUR                  (       d  [        R                  " [        S5      5      eUS4$ )zg
Authenticate the userid and password against username and password
with optional request for context.
rH   r   NzInvalid username/password.User inactive or deleted.r   )r   USERNAME_FIELDr   r   r;   r:   	is_active)r   rG   rH   r   credentialsusers         r   rD   ,BasicAuthentication.authenticate_credentialsY   sv     ++V
 ;G;{;<11!4P2QRR~~11!4O2PQQd|r   c                      SU R                   -  $ )NzBasic realm="%s")www_authenticate_realmr)   s     r   r,   'BasicAuthentication.authenticate_headerl   s    !D$?$???r   r   r   )
r   r    r!   r"   r.   rR   r   rD   r,   r#   r   r   r   r0   r0   5   s      #H<&@r   r0   c                   $    \ rS rSrSrS rS rSrg)SessionAuthenticationp   z4
Use Django's session framework for authentication.
c                     [        UR                  SS5      nU(       a  UR                  (       d  gU R                  U5        US4$ )zc
Returns a `User` if the request session currently has a logged in user.
Otherwise returns `None`.
rO   N)getattr_requestrM   enforce_csrfr   r   rO   s      r   r   "SessionAuthentication.authenticateu   s@     w''6 4>>'" d|r   c                     S n[        U5      nUR                  U5        UR                  USS0 5      nU(       a  [        R                  " SU-  5      eg)z;
Enforce CSRF validation for session based authentication.
c                     g r   r   )r   s    r   dummy_get_response>SessionAuthentication.enforce_csrf.<locals>.dummy_get_response   s    r   Nr   zCSRF Failed: %s)r   process_requestprocess_viewr   PermissionDenied)r   r   r_   checkr   s        r   rZ   "SessionAuthentication.enforce_csrf   sW    	 ,-g&##GT2r:--.?&.HII r   r   N)r   r    r!   r"   r.   r   rZ   r#   r   r   r   rU   rU   p   s    $Jr   rU   c                   :    \ rS rSrSrSrSrS r S rS r	S r
S	rg)
TokenAuthentication   z
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                 D    U R                   b  U R                   $ SSKJn  U$ )Nr   )ri   )modelrest_framework.authtoken.modelsri   )r   ri   s     r   	get_modelTokenAuthentication.get_model   s    ::!::9r   c                 &   [        U5      R                  5       nU(       a=  US   R                  5       U R                  R                  5       R	                  5       :w  a  g [        U5      S:X  a!  [        S5      n[        R                  " U5      e[        U5      S:  a!  [        S5      n[        R                  " U5      e US   R                  5       nU R                  U5      $ ! [         a"    [        S5      n[        R                  " U5      ef = f)Nr   r4   z.Invalid token header. No credentials provided.r5   z=Invalid token header. Token string should not contain spaces.zIInvalid token header. Token string should not contain invalid characters.)r   r7   r8   keywordr   r9   r:   r   r;   r>   UnicodeErrorrD   )r   r   r   rE   tokens        r   r    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    C$ $,Dc                 j   U R                  5       n UR                  R                  S5      R                  US9nUR                  R                  (       d  [
        R                  " [        S5      5      eUR                  U4$ ! UR                   a     [
        R                  " [        S5      5      ef = f)NrO   )keyzInvalid token.rK   )
rm   objectsselect_relatedr   DoesNotExistr   r;   r:   rO   rM   )r   ru   rk   rr   s       r   rD   ,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   (B 0B2c                     U R                   $ r   )rp   r)   s     r   r,   'TokenAuthentication.authenticate_header   s    ||r   r   )r   r    r!   r"   r.   rp   rk   rm   r   rD   r,   r#   r   r   r   rg   rg      s,     GE4*
#r   rg   c                   "    \ rS rSrSrSrS rSrg)RemoteUserAuthentication   a   
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                     [        XR                  R                  U R                  5      S9nU(       a  UR                  (       a  US 4$ g g )N)r   remote_user)r   r   r   headerrM   r[   s      r   r   %RemoteUserAuthentication.authenticate   s9    G9I9I$++9VWDNN$< #4r   r   N)r   r    r!   r"   r.   r   r   r#   r   r   r   r}   r}      s     F r   r}   )r.   r<   rB   django.contrib.authr   r   django.middleware.csrfr   django.utils.translationr   r:   rest_frameworkr   r   r   r   r%   r0   rU   rg   r}   r   r   r   <module>r      su      < 5 6 ;
"  (8@, 8@v$J. $JN<, <~ 1  r   