
    hY:                     j   d dl Z d dlZd dl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 d dlmZ d d	lmZ d
dlmZmZmZ dZdZdZdZd Zd%dZd Zd Z d&dZ! ed      d        Z"d Z#d Z$ ed      d&d       Z% ed      d&d       Z&d&dZ'd&dZ(d Z)d Z*d Z+d  Z,d! Z-d" Z.d# Z/d$ Z0y)'    N)apps)settings)ImproperlyConfiguredPermissionDenied)rotate_tokenconstant_time_compare)RemovedInDjango61Warningimport_string)sensitive_variables   )user_logged_inuser_logged_outuser_login_failed_auth_user_id_auth_user_backend_auth_user_hashnextc                 "     t        |              S Nr   )paths    W/var/www/html/backend/venv/lib/python3.12/site-packages/django/contrib/auth/__init__.pyload_backendr      s    =      c                     g }t         j                  D ]$  }t        |      }|j                  | r||fn|       & |st	        d      |S )Nz\No authentication backends have been defined. Does AUTHENTICATION_BACKENDS contain anything?)r   AUTHENTICATION_BACKENDSr   appendr   )return_tuplesbackendsbackend_pathbackends       r   _get_backendsr#      s[    H 88 O|,=,/gNO "8
 	
 Or   c                      t        d      S )NFr   )r#    r   r   get_backendsr'   '   s    u--r   c              +      K   t        d      D ]>  \  }}t        j                  |j                        }	  |j                  | fi | ||f @ y # t
        $ r Y Mw xY ww)NTr%   )r#   inspect	signatureauthenticatebind	TypeError)requestcredentialsr"   r!   backend_signatures        r   _get_compatible_backendsr1   +   ss     !.T!B $#--g.B.BC	"""7:k:
 |##$  	 	s(   2A A	A 	AA AA c                     	 |xs | j                   }t        |t              st        d|z        |S # t        $ r1 t        d      }t        |      dk(  r|d   \  }}Y |S t        d      w xY w)Nz5backend must be a dotted import path string (got %r).Tr%   r   r   zYou have multiple authentication backends configured and therefore must provide the `backend` argument or set the `backend` attribute on the user.)r"   
isinstancestrr-   AttributeErrorr#   len
ValueError)userr"   r    _s       r   _get_backend_from_userr:   7   s    )T\\ '3'G'Q  N  	 t4x=A!!JAw N 3 	s   2 +A, A,r/   c                     t        j                  dt         j                        }d}| D ]  }|j                  |      s|| |<    | S )z
    Clean a dictionary of credentials of potentially sensitive info before
    sending to less secure functions.

    Not comprehensive - intended for user_login_failed signal
    z'api|token|key|secret|password|signaturez********************)recompileIsearch)r/   SENSITIVE_CREDENTIALSCLEANSED_SUBSTITUTEkeys       r   _clean_credentialsrC   L   sR     JJ'PRTRVRVW0 3 '',2K3 r   c                 ~    t               j                  j                  j                  | j                  t
                 S r   )get_user_model_metapk	to_pythonsessionSESSION_KEY)r.   s    r   _get_user_session_keyrK   \   s-     !!$$..w{/KLLr   c                    K   | j                   j                  t               d {   }|
t               t	               j
                  j                  j                  |      S 7 =wr   )rI   agetrJ   KeyErrorrE   rF   rG   rH   )r.   session_keys     r   _aget_user_session_keyrP   b   sR       ,,[99Kj!!$$..{;; :s   #A%A#>A%c                     t        | fi |D ]&  \  }}	  |j                  | fi |}|||_        |c S  t	        j
                  t        t        |      |        y# t        $ r Y  2w xY w)zC
    If the given credentials are valid, return a User object.
    Nsenderr/   r.   )r1   r+   r   r"   r   send__name__rC   r.   r/   r"   r!   r8   s        r   r+   r+   k   s    
 ":'!Q[!Q 	'7''?;?D
 <# %7%Dg   	 	s   A	A)(A)c                   K   t        | fi |D ].  \  }}	  |j                  | fi | d{   }|%||_        |c S  t	        j
                  t        t        |      |        d{    y7 A# t        $ r Y  <w xY w7 w)zSee authenticate().NrR   )r1   aauthenticater   r"   r   asendrU   rC   rV   s        r   rX   rX      s      ":'!Q[!Q 	...wF+FFD
 <# 
!
!%7%Dg   G 	 	sC   BA0A.A07B(B )B.A00	A=9B<A==Bc                    d}|(| j                   }t        j                  dt        d       t	        |d      r|j                         }t        | j                  v r_t        |       |j                  k7  s,|r_t        | j                  j                  t        d      |      s5| j                  j                          n| j                  j                          t        ||      }|j                   j                  j#                  |      | j                  t        <   || j                  t$        <   || j                  t        <   t	        | d      r|| _         t'        |        t)        j*                  |j,                  | |	       y)
z
    Persist a user id and a backend in the request. This way a user doesn't
    have to reauthenticate on every request. Note that data set during
    the anonymous session is retained when the user logs in.
     N;Fallback to request.user when user is None will be removed.   
stacklevelget_session_auth_hashr8   r"   r8   rS   r.   r8   )r8   warningswarnr
   hasattrr`   rJ   rI   rK   rG   r	   getHASH_SESSION_KEYflush	cycle_keyr:   rF   value_to_stringBACKEND_SESSION_KEYr   r   rT   	__class__r.   r8   r"   session_auth_hashs       r   loginro      s%    |||I$	
 t,- 668goo% )TWW4)##$4b9;L OO!!#!!#$$@G#'::==#@#@#FGOOK +2GOO'((9GOO$%wt~~wTJr   c                 H  K   d}|4t        j                  dt        d       | j                          d{   }t	        |d      r|j                         }| j                  j                  t               d{   rwt        |        d{   |j                  k7  s4|rwt        | j                  j                  t        d       d{   |      sE| j                  j                          d{    n"| j                  j                          d{    t!        ||      }| j                  j#                  t        |j$                  j                  j'                  |             d{    | j                  j#                  t(        |       d{    | j                  j#                  t        |       d{    t	        | d      r|| _        t-        |        t/        j0                  |j2                  | |	       d{    y7 7 7 7 I7 %7 7 7 7 c7 w)
zSee login().r[   Nr\   r]   r^   r`   ra   r8   rb   )rc   rd   r
   auserre   r`   rI   ahas_keyrJ   rP   rG   r	   rM   rg   aflush
acycle_keyr:   asetrF   rj   rk   r8   r   r   rY   rl   rm   s       r   aloginrv      s     |I$	

 ]]_$t,- 668__%%k222'00DGG;)oo**+;R@@! //((***oo((***$$@G
//

{DJJMM,I,I$,O
PPP
//

2G
<<<
//

/1B
CCCw


dnngD
QQQ9 %
 30 A +* Q<C Rs   4H"HAH"9H:H"H;H"H	&H"/H0"H"HAH"*H+'H"H'H":H;AH"H H"H"H"H"H"H"H"H"H" H"c                     t        | dd      }t        |dd      sd}t        j                  |j                  | |       | j                  j                          t        | d      rddlm}  |       | _	        yy)z_
    Remove the authenticated user's ID from the request and flush their session
    data.
    r8   Nis_authenticatedTrb   r   AnonymousUser)
getattrr   rT   rl   rI   rh   re   django.contrib.auth.modelsrz   r8   r.   r8   rz   s      r   logoutr~      se     7FD)D4+T2dKOOw<$  r   c                 H  K   t        | dd      }| |        d{   }t        |dd      sd}t        j                  |j                  | |       d{    | j                  j                          d{    t        | d      rddlm}  |       | _	        yy7 7 H7 (w)	zSee logout().rq   Nrx   Trb   r8   r   ry   )
r{   r   rY   rl   rI   rs   re   r|   rz   r8   r}   s      r   alogoutr     s      7GT*DV|4+T2


t~~wT
RRR
//
 
 
"""w<$    S"s3   B"B8B"B!B"7B 8%B"B" B"c                      	 t        j                  t        j                  d      S # t        $ r t        d      t        $ r t        dt        j                  z        w xY w)z?
    Return the User model that is active in this project.
    F)require_readyz:AUTH_USER_MODEL must be of the form 'app_label.model_name'z@AUTH_USER_MODEL refers to model '%s' that has not been installed)django_apps	get_modelr   AUTH_USER_MODELr7   r   LookupErrorr&   r   r   rE   rE     sg    

$$X%=%=USS 
"H
 	
  
"N&&'
 	

s	   $' 9A c                 \   ddl m} d}	 t        |       }| j                  t           }|t
        j                  v rt        |      }|j                  |      }t        |d      r| j                  j                  t              sd}n|j                         }t        |      }|sorPt        fd|j                         D              r.| j                  j!                          | j                  t        <   n| j                  j#                          d}	 |xs  |       S # t$        $ r Y w xY w)z
    Return the user model instance associated with the given request session.
    If no user is retrieved, return an instance of `AnonymousUser`.
    r   ry   Nr`   Fc              3   6   K   | ]  }t        |        y wr   r   .0fallback_auth_hashsession_hashs     r   	<genexpr>zget_user.<locals>.<genexpr>F  !      ,. .l<NO,   )modelsrz   rK   rI   rk   r   r   r   get_userre   rf   rg   r`   r	   anyget_session_auth_fallback_hashri   rh   rN   	r.   rz   r8   user_idr!   r"   session_hash_verifiedrn   r   s	           @r   r   r   '  s   
 &D$'0':; 8;;;"<0G##G,Dt45&223CD#,1)(,(B(B(D%,A$&7-) - $ ,262U2U2W, )  113<M(89--/#"=?"=  s   D 	D+*D+c                 N  K   ddl m} d}	 t        |        d{   }| j                  j	                  t
               d{   }|t        j                  v r t        |      }|j                  |       d{   }t        |d      r| j                  j	                  t               d{   sd}n.|j                         }xr t        |j                               }|srmt        fd|j                         D              rK| j                  j!                          d{    | j                  j#                  t               d{    n%| j                  j%                          d{    d}	 |xs  |       S 7 j7 F7 7 7 h7 B7 !# t&        $ r Y 'w xY ww)zSee get_user().r   ry   Nr`   Fc              3   6   K   | ]  }t        |        y wr   r   r   s     r   r   zaget_user.<locals>.<genexpr>o  r   r   )r   rz   rP   rI   rM   rk   r   r   r   	aget_userre   rg   r`   r	   r   r   rt   ru   rs   rN   r   s	           @r   r   r   S  s}    %D$.w77$__112EFF 8;;;"<0G **733Dt45%,__%9%9:J%KK#,1)(,(B(B(D%,8 -=R$d&@&@&B>) - $ ,262U2U2W, ) &oo88:::%oo223CEVWWW%oo44666#"=?"A 8F 4  L  ;W67  s   	F%F F&F FF 2F%9F:2F%,F-A:F%'F('F%F"F%2F3F%F F F%F%F%F%F%	F"F%!F""F%c                 $    | d|j                   S )zI
    Return the codename of the permission for the specified action.
    r9   )
model_name)actionoptss     r   get_permission_codenamer   |  s     doo..r   c                     | j                   j                          t        |d      r2| j                  |k(  r"|j	                         | j                   t
        <   yyy)aG  
    Updating a user's password logs out all sessions for the user.

    Take the current request and the updated user object from which the new
    session hash will be derived and update the session hash appropriately to
    prevent a password change from logging out the session from which the
    password was changed.
    r`   N)rI   ri   re   r8   r`   rg   r.   r8   s     r   update_session_auth_hashr     sK     OOt,-',,$2F,0,F,F,H() 3G-r   c                    K   | j                   j                          d{    t        |d      rG| j                  |k(  r7| j                   j	                  t
        |j                                d{    yyy7 X7 	w)zSee update_session_auth_hash().Nr`   )rI   rt   re   r8   ru   rg   r`   r   s     r   aupdate_session_auth_hashr     sh     
//
$
$
&&&t,-',,$2Foo""#3T5O5O5QRRR 3G- 'Rs"   A=A9AA=1A;2A=;A=)Fr   )1r)   r<   rc   django.appsr   r   django.confr   django.core.exceptionsr   r   django.middleware.csrfr   django.utils.cryptor	   django.utils.deprecationr
   django.utils.module_loadingr   django.views.decorators.debugr   signalsr   r   r   rJ   rk   rg   REDIRECT_FIELD_NAMEr   r#   r'   r1   r:   rC   rK   rP   r+   rX   ro   rv   r~   r   rE   r   r   r   r   r   r&   r   r   <module>r      s     	  +   I / 5 = 5 = G G* $  !
.	$* ]# $M< ]# $. ]# $*,K^(RV'$'"
")#X&#R/ISr   