
    k"g.                         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
 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d Z G d de      Z G d de      Z G d de      Z G d de      Zy)    )partial)urlparse)settings)auth)REDIRECT_FIELD_NAMEload_backend)RemoteUserBackend)redirect_to_login)ImproperlyConfigured)resolve_url)MiddlewareMixin)SimpleLazyObjectc                 f    t        | d      st        j                  |       | _        | j                  S )N_cached_user)hasattrr   get_userr   requests    W/var/www/html/djangosite/lib/python3.12/site-packages/django/contrib/auth/middleware.pyr   r      s)    7N+#}}W5    c                    K   t        | d      s"t        j                  |        d {   | _        | j                  S 7 w)N_acached_user)r   r   	aget_userr   r   s    r   auserr      s5     7O,&*nnW&= =    !>s   %?=?c                       e Zd Zd Zy)AuthenticationMiddlewarec                     t        d      st        d      t        fd      _        t	        t
              _        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   userr   r   )selfr   s    `r   process_requestz(AuthenticationMiddleware.process_request   s=    w	*&M  ((ABw/r   N)__name__
__module____qualname__r#    r   r   r   r      s    
0r   r   c                   ,    e Zd ZdZeZd Zd Zd Zd Z	y)LoginRequiredMiddlewarez
    Middleware that redirects all unauthenticated requests to a login page.

    Views using the login_not_required decorator will not be redirected.
    c                 p    |j                   j                  ry t        |dd      sy | j                  ||      S )Nlogin_requiredT)r!   is_authenticatedgetattrhandle_no_permission)r"   r   	view_func	view_argsview_kwargss        r   process_viewz$LoginRequiredMiddleware.process_view2   s5    <<((y"2D9(()<<r   c                 p    t        |dd       xs t        j                  }|st        d      t	        |      S )N	login_urlzNo login URL to redirect to. Define settings.LOGIN_URL or provide a login_url via the 'django.contrib.auth.decorators.login_required' decorator.)r-   r   	LOGIN_URLr   str)r"   r/   r4   s      r   get_login_urlz%LoginRequiredMiddleware.get_login_url;   s<    I{D9OX=O=O	&- 
 9~r   c                 0    t        |d| j                        S )Nredirect_field_name)r-   r9   )r"   r/   s     r   get_redirect_field_namez/LoginRequiredMiddleware.get_redirect_field_nameE   s    y"79Q9QRRr   c                    |j                         }t        | j                  |            }t        |      d d \  }}t        |      d d \  }}|r||k(  r|r||k(  r|j	                         }t        ||| j                  |            S )N   )build_absolute_urir   r7   r   get_full_pathr
   r:   )	r"   r   r/   pathresolved_login_urllogin_schemelogin_netloccurrent_schemecurrent_netlocs	            r   r.   z,LoginRequiredMiddleware.handle_no_permissionH   s    ))+(););I)FG &..@%A"1%E"l)1$);& > >((*D ((3
 	
r   N)
r$   r%   r&   __doc__r   r9   r2   r7   r:   r.   r'   r   r   r)   r)   )   s$     .=S
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!   r,   _remove_invalid_userget_usernameclean_usernamer   authenticatelogin)r"   r   usernamer!   s       r   r#   z$RemoteUserMiddleware.process_requesto   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   rQ   AttributeError)r"   rT   r   backend_strbackends        r   rQ   z#RemoteUserMiddleware.clean_username   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   rV   
isinstancer	   logoutImportError)r"   r   stored_backends      r   rO   z)RemoteUserMiddleware._remove_invalid_user   sf    
		%)##D$<$<bAN .*;<G$ =	  	!KK 	!s   3A A=<A=N)	r$   r%   r&   rE   rL   rN   r#   rQ   rO   r'   r   r   rG   rG   [   s$      F $%&N%r   rG   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&   rE   rN   r'   r   r   rb   rb      s     !&r   rb   N)	functoolsr   urllib.parser   django.confr   django.contribr   django.contrib.authr   r   django.contrib.auth.backendsr	   django.contrib.auth.viewsr
   django.core.exceptionsr   django.shortcutsr   django.utils.deprecationr   django.utils.functionalr   r   r   r   r)   rG   rb   r'   r   r   <module>rn      se     !    A : 7 7 ( 4 4 !0 0/
o /
dV%? V%r&%9 &r   