
    Gwg7                         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 d Z G d d	e	      Z G d
 de	      Z G d de      Zy)    )auth)load_backend)RemoteUserBackend)ImproperlyConfigured)MiddlewareMixin)SimpleLazyObjectc                 f    t        | d      st        j                  |       | _        | j                  S )N_cached_user)hasattrr   get_userr
   requests    U/var/www/horilla/myenv/lib/python3.12/site-packages/django/contrib/auth/middleware.pyr   r   	   s)    7N+#}}W5    c                       e Zd Zd Zy)AuthenticationMiddlewarec                 Z    t        d      st        d      t        fd      _        y )NsessionzThe Django authentication middleware requires session middleware to be installed. Edit your MIDDLEWARE setting to insert 'django.contrib.sessions.middleware.SessionMiddleware' before 'django.contrib.auth.middleware.AuthenticationMiddleware'.c                      t               S )N)r   r   s   r   <lambda>z:AuthenticationMiddleware.process_request.<locals>.<lambda>   s    0A r   )r   r   r   user)selfr   s    `r   process_requestz(AuthenticationMiddleware.process_request   s/    w	*&M  ((ABr   N)__name__
__module____qualname__r    r   r   r   r      s    	Cr   r   c                   *    e Zd ZdZdZdZd Zd Zd Zy)RemoteUserMiddlewarea  
    Middleware for utilizing web-server-provided authentication.

    If request.user is not authenticated, then this middleware attempts to
    authenticate the username passed in the ``REMOTE_USER`` request header.
    If authentication is successful, the user is automatically logged in to
    persist the user in the session.

    The header used is configurable and defaults to ``REMOTE_USER``.  Subclass
    this class and change the ``header`` attribute if you need to use a
    different header.
    REMOTE_USERTc                    t        |d      st        d      	 |j                  | j                     }|j                  j                  r?|j                  j                         | j                  ||      k(  ry | j                  |       t        j                  ||      }|r||_        t        j                  ||       y y # t        $ r6 | j
                  r'|j                  j                  r| j                  |       Y y w xY w)Nr   zThe Django remote user auth middleware requires the authentication middleware to be installed.  Edit your MIDDLEWARE setting to insert 'django.contrib.auth.middleware.AuthenticationMiddleware' before the RemoteUserMiddleware class.)remote_user)r   r   METAheaderKeyErrorforce_logout_if_no_headerr   is_authenticated_remove_invalid_userget_usernameclean_usernamer   authenticatelogin)r   r   usernamer   s       r   r   z$RemoteUserMiddleware.process_request0   s    w'&: 	||DKK0H <<((||((*d.A.A(G.TT ))'2   h?  GLJJw%	 +  	 --',,2O2O))'2	s   B? ?<C>=C>c                     |j                   t        j                     }t        j                  |      }	 |j	                  |      }|S # t
        $ r Y |S w xY w)zr
        Allow the backend to clean the username, if the backend defines a
        clean_username method.
        )r   r   BACKEND_SESSION_KEYr   r*   AttributeError)r   r-   r   backend_strbackends        r   r*   z#RemoteUserMiddleware.clean_usernameW   s]    
 ood&>&>?##K0	--h7H   		s   A 	AAc                     	 t        |j                  j                  t        j                  d            }t        |t              rt        j                  |       yy# t        $ r t        j                  |       Y yw xY w)z
        Remove the current authenticated user in the request which is invalid
        but only if the user is authenticated via the RemoteUserBackend.
         N)	r   r   getr   r/   
isinstancer   logoutImportError)r   r   stored_backends      r   r(   z)RemoteUserMiddleware._remove_invalid_userd   sf    
		%)##D$<$<bAN .*;<G$ =	  	!KK 	!s   3A A=<A=N)	r   r   r   __doc__r$   r&   r   r*   r(   r   r   r   r   r      s$      F $%&N%r   r   c                       e Zd ZdZdZy)PersistentRemoteUserMiddlewarea  
    Middleware for web-server provided authentication on logon pages.

    Like RemoteUserMiddleware but keeps the user authenticated even if
    the header (``REMOTE_USER``) is not found in the request. Useful
    for setups when the external authentication via ``REMOTE_USER``
    is only expected to happen on some "logon" URL and the rest of
    the application wants to use Django's authentication mechanism.
    FN)r   r   r   r:   r&   r   r   r   r<   r<   u   s     !&r   r<   N)django.contribr   django.contrib.authr   django.contrib.auth.backendsr   django.core.exceptionsr   django.utils.deprecationr   django.utils.functionalr   r   r   r   r<   r   r   r   <module>rC      sF     , : 7 4 4 
C 
CV%? V%r&%9 &r   