3
g                 @   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| | _| jS )N_cached_user)hasattrr   get_userr   )request r   \/var/www/tester-filtro-web/env/lib/python3.6/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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                  s   t  S )N)r
   r   )r   r   r   <lambda>   s    z:AuthenticationMiddleware.process_request.<locals>.<lambda>)r	   AssertionErrorr   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.
    ZREMOTE_USERTc             C   s   t |dstdy|j| j }W n, tk
rN   | jrJ|jjrJ| j| d S X |jjr||jj	 | j
||krrd S | j| tj||d}|r||_tj|| 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.)Zremote_user)r	   r   ZMETAheaderKeyErrorforce_logout_if_no_headerr   is_authenticated_remove_invalid_userZget_usernameclean_usernamer   Zauthenticatelogin)r   r   usernamer   r   r   r   r   .   s"    


z$RemoteUserMiddleware.process_requestc             C   s>   |j tj }tj|}y|j|}W n tk
r8   Y nX |S )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   Zbackend_strbackendr   r   r   r   T   s    
z#RemoteUserMiddleware.clean_usernamec             C   sP   yt |jjtjd}W n tk
r6   tj| Y nX t|trLtj| 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!   ImportErrorZlogout
isinstancer   )r   r   Zstored_backendr   r   r   r   a   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   r(   r   r   r   r   r   r)   p   s   	r)   N)Zdjango.contribr   Zdjango.contrib.authr   Zdjango.contrib.auth.backendsr   Zdjango.core.exceptionsr   Zdjango.utils.deprecationr   Zdjango.utils.functionalr   r
   r   r   r)   r   r   r   r   <module>   s   V