o
    —¨Êh7  ã                   @   s„   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d„ ZG d	d
„ d
e	ƒZG dd„ de	ƒZG dd„ deƒZdS )é    )Úauth)Úload_backend)ÚRemoteUserBackend)ÚImproperlyConfigured)ÚMiddlewareMixin)ÚSimpleLazyObjectc                 C   s   t | dƒst | ¡| _| jS )NÚ_cached_user)Úhasattrr   Úget_userr   ©Úrequest© r   úP/var/www/html/env/lib/python3.10/site-packages/django/contrib/auth/middleware.pyr
   	   s   
r
   c                   @   s   e Zd Zdd„ ZdS )ÚAuthenticationMiddlewarec                    s(   t ˆ dƒs	tdƒ‚t‡ fdd„ƒˆ _d S )NÚsessionzñThe 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                      s   t ˆ ƒS )N)r
   r   r   r   r   Ú<lambda>   s    z:AuthenticationMiddleware.process_request.<locals>.<lambda>)r	   r   r   Úuser)Úselfr   r   r   r   Úprocess_request   s
   
ÿz(AuthenticationMiddleware.process_requestN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r      s    r   c                   @   s0   e Zd ZdZdZdZdd„ Zdd„ Zd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                 C   s¨   t |dƒs	tdƒ‚z|j| j }W n ty'   | jr$|jjr$|  |¡ Y d S w |jjr>|j 	¡ |  
||¡kr9d S |  |¡ tj||d�}|rR||_t ||¡ d S d S )Nr   zçThe 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   r   r   r   r   0   s(   
ÿ
ú

üz$RemoteUserMiddleware.process_requestc                 C   s<   |j tj }t |¡}z| |¡}W |S  ty   Y |S 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Úbackendr   r   r   r"   W   s   
þþz#RemoteUserMiddleware.clean_usernamec                 C   sT   zt |j tjd¡ƒ}W n ty   t |¡ Y dS w t|tƒr(t |¡ dS dS )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&   ÚImportErrorÚlogoutÚ
isinstancer   )r   r   Ústored_backendr   r   r   r    d   s   ÿþ
ÿz)RemoteUserMiddleware._remove_invalid_userN)	r   r   r   Ú__doc__r   r   r   r"   r    r   r   r   r   r      s    'r   c                   @   s   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   r0   r   r   r   r   r   r1   u   s    
r1   N)Údjango.contribr   Údjango.contrib.authr   Údjango.contrib.auth.backendsr   Údjango.core.exceptionsr   Údjango.utils.deprecationr   Údjango.utils.functionalr   r
   r   r   r1   r   r   r   r   Ú<module>   s    Y