o
    Mf                     @   s  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mZmZ dZdZdZdZdd Zd)ddZdd Zeddd Zdd Zedd*ddZd*ddZ dd  Z!d!d" Z"d#d$ Z#d%d& Z$d'd( Z%dS )+    N)apps)settings)ImproperlyConfiguredPermissionDenied)rotate_token)constant_time_compareimport_string)sensitive_variables   )user_logged_inuser_logged_outuser_login_failed_auth_user_id_auth_user_backend_auth_user_hashnextc                 C   s
   t |  S Nr   )path r   ^/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/django/contrib/auth/__init__.pyload_backend      
r   Fc                 C   s>   g }t jD ]}t|}|| r||fn| q|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backendr   r   r   _get_backends   s   
r   c                   C   s
   t ddS )NFr   )r   r   r   r   r   get_backends%   r   r!   credentialsc                 C   s2   t dt j}d}| D ]}||r|| |< q| 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keyr   r   r   _clean_credentials)   s   
r*   c                 C   s   t  jj| jt S r   )get_user_model_metapk	to_pythonsessionSESSION_KEY)requestr   r   r   _get_user_session_key9   s   r2   c              	   K   s   t ddD ]@\}}t|j}z|j| fi | W n	 ty#   Y qw z|j| fi |}W n
 ty9   Y  nw |du r?q||_|  S tj	t
t|| d dS )zC
    If the given credentials are valid, return a User object.
    Tr    N)senderr"   r1   )r   inspect	signatureauthenticatebind	TypeErrorr   r   r   send__name__r*   )r1   r"   r   r   backend_signatureuserr   r   r   r6   ?   s"   r6   c                 C   s$  d}|du r	| j }t|dr| }t| jv r0t| |jks*|r/t| jt	d|s/| j
  n| j  z|p:|j}W n ty[   tdd}t|dkrU|d \}}ntdY nw t|tsgtd	| |jj|| jt< || jt< || jt	< t| d
r|| _ t|  tj|j| |d dS )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get_session_auth_hashTr    r   r   zYou have multiple authentication backends configured and therefore must provide the `backend` argument or set the `backend` attribute on the user.z5backend must be a dotted import path string (got %r).r<   r3   r1   r<   )r<   hasattrr>   r0   r/   r2   r-   r   getHASH_SESSION_KEYflush	cycle_keyr   AttributeErrorr   len
ValueError
isinstancestrr8   r,   value_to_stringBACKEND_SESSION_KEYr   r   r9   	__class__)r1   r<   r   session_auth_hashr   _r   r   r   loginZ   sD   








rO   c                 C   s^   t | dd}t |ddsd}tj|j| |d | j  t| dr-ddlm} | | _	dS dS )z_
    Remove the authenticated user's ID from the request and flush their session
    data.
    r<   Nis_authenticatedTr?   r   AnonymousUser)
getattrr   r9   rL   r/   rC   r@   django.contrib.auth.modelsrR   r<   )r1   r<   rR   r   r   r   logout   s   

rU   c                   C   sD   z	t jtjddW S  ty   td ty!   tdtj 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_MODELrG   r   LookupErrorr   r   r   r   r+      s   r+   c                 C   s   ddl m} d}zt| }| jt }W n	 ty   Y n:w |tjv rVt|}|	|}t
|drV| jt}|o>t|| }|sV|rOt
|drOt|| sV| j  d}|pZ| S )z
    Return the user model instance associated with the given request session.
    If no user is retrieved, return an instance of `AnonymousUser`.
    r   rQ   Nr>   _legacy_get_session_auth_hash)modelsrR   r2   r/   rK   KeyErrorr   r   r   get_userr@   rA   rB   r   r>   r[   rC   )r1   rR   r<   user_idr   r   session_hashsession_hash_verifiedr   r   r   r^      s6   




r^   c                 C   s   d| |j f S )zI
    Return the codename of the permission for the specified action.
    z%s_%s)
model_name)actionoptsr   r   r   get_permission_codename   s   re   c                 C   s8   | j   t|dr| j|kr| | j t< dS dS dS )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)r/   rD   r@   r<   r>   rB   )r1   r<   r   r   r   update_session_auth_hash   s   
	rf   )Fr   )&r4   r#   django.appsr   rW   django.confr   django.core.exceptionsr   r   django.middleware.csrfr   django.utils.cryptor   django.utils.module_loadingr	   django.views.decorators.debugr
   signalsr   r   r   r0   rK   rB   REDIRECT_FIELD_NAMEr   r   r!   r*   r2   r6   rO   rU   r+   r^   re   rf   r   r   r   r   <module>   s8    


0#