
    Dvg.                     
   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dS )    )partial)urlparse)settings)auth)REDIRECT_FIELD_NAMEload_backend)RemoteUserBackend)redirect_to_login)ImproperlyConfigured)resolve_url)MiddlewareMixin)SimpleLazyObjectc                 b    t          | d          st          j        |           | _        | j        S )N_cached_user)hasattrr   get_userr   requests    [/var/www/pixelcanvas.ch/venv/lib/python3.11/site-packages/django/contrib/auth/middleware.pyr   r      s/    7N++ 6#}W55    c                 r   K   t          | d          st          j        |            d {V | _        | j        S )N_acached_user)r   r   	aget_userr   r   s    r   auserr      sE      7O,, >&*nW&=&= = = = = = =  r   c                       e Zd Zd ZdS )AuthenticationMiddlewarec                     t          d          st          d          t          fd          _        t	          t
                    _        d S )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    0A0A r   )r   r   r   userr   r   )selfr   s    `r   process_requestz(AuthenticationMiddleware.process_request   s]    w	** 	&M   ((A(A(A(ABBw//r   N)__name__
__module____qualname__r#    r   r   r   r      s#        
0 
0 
0 
0 
0r   r   c                   .    e Zd ZdZeZd Zd Zd Zd Z	dS )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d S t          |dd          sd S |                     ||          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   sD    <( 	4y"2D99 	4(()<<<r   c                 |    t          |dd           p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;   sH    I{D99OX=O	 	&-  
 9~~r   c                 .    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9QRRRr   c                 f   |                                 }t          |                     |                    }t          |          d d         \  }}t          |          d d         \  }}|r||k    r|r||k    r|                                }t          |||                     |                    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)FGG &..@%A%A"1"%E"l)1$);& 	+ > > !? , > >((**D ((33
 
 	
r   N)
r$   r%   r&   __doc__r   r9   r2   r7   r:   r.   r'   r   r   r)   r)   )   se          .= = =  S S S
 
 
 
 
r   r)   c                   ,    e Zd ZdZdZdZd Zd Zd ZdS )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                 }n9# t          $ r, | j        r!|j        j        r|                     |           Y d S w xY w|j        j        rH|j        	                                | 
                    ||          k    rd S |                     |           t          j        ||          }|r||_        t          j        ||           d S d S )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'' 	&:  	|DK0HH 	 	 	 - 3',2O 3))'222FF	 <( 	3|((**d.A.A(G.T.TTT ))'222  h??? 	&  GLJw%%%%%		& 	&s   4 2A*)A*c                     |j         t          j                 }t          j        |          }	 |                    |          }n# t
          $ r Y nw xY w|S )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   sb    
 od&>?#K00	--h77HH 	 	 	D	s   A 
AAc                    	 t          |j                            t          j        d                    }t          |t                    rt          j        |           dS dS # t          $ r t          j        |           Y dS 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   s    
		%)##D$<bAA N .*;<< %G$$$$$% %	  	! 	! 	!K      	!s   2A! !BBN)	r$   r%   r&   rE   rL   rN   r#   rQ   rO   r'   r   r   rG   rG   [   sY           F $%& %& %&N  % % % % %r   rG   c                       e Zd ZdZdZdS )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      s         ! ! ! ! ! !                   A A A A A A A A : : : : : : 7 7 7 7 7 7 7 7 7 7 7 7 ( ( ( ( ( ( 4 4 4 4 4 4 4 4 4 4 4 4     ! ! !0 0 0 0 0 0 0 0/
 /
 /
 /
 /
o /
 /
 /
dV% V% V% V% V%? V% V% V%r& & & & &%9 & & & & &r   