o
    {,h+                     @   s   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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d Zdd ZG dd deZG dd deZG dd dZG dd deZdS )    )partial)urlsplit)iscoroutinefunctionmarkcoroutinefunction)settings)auth)REDIRECT_FIELD_NAMEload_backend)RemoteUserBackend)redirect_to_login)ImproperlyConfigured)resolve_url)MiddlewareMixin)SimpleLazyObjectc                 C   s   t | dst| | _| jS )N_cached_user)hasattrr   get_userr   request r   T/var/www/html/rh/venv/lib/python3.10/site-packages/django/contrib/auth/middleware.pyr      s   
r   c                    s$   t | dst| I d H | _| jS )N_acached_user)r   r   	aget_userr   r   r   r   r   auser   s   
r   c                   @   s   e Zd Zdd ZdS )AuthenticationMiddlewarec                    s4   t  ds	tdt fd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                      s   t  S )N)r   r   r   r   r   <lambda>'   s    z:AuthenticationMiddleware.process_request.<locals>.<lambda>)r   r   r   userr   r   )selfr   r   r   r   process_request   s   
z(AuthenticationMiddleware.process_requestN)__name__
__module____qualname__r   r   r   r   r   r      s    r   c                   @   s4   e Zd ZdZeZdd Zdd Zdd Zd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                 C   s(   |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_kwargsr   r   r   process_view4   s
   z$LoginRequiredMiddleware.process_viewc                 C   s&   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(   r,   r   r   r   get_login_url=   s   z%LoginRequiredMiddleware.get_login_urlc                 C   s   t |d| jS )Nredirect_field_name)r&   r0   )r   r(   r   r   r   get_redirect_field_nameG   s   z/LoginRequiredMiddleware.get_redirect_field_namec           	      C   sp   |  }t| |}t|d d \}}t|d d \}}|r%||kr/|r+||kr/| }t||| |S )N   )build_absolute_urir   r/   r   get_full_pathr   r1   )	r   r   r(   pathresolved_login_urllogin_schemelogin_netloccurrent_schemecurrent_netlocr   r   r   r'   J   s   z,LoginRequiredMiddleware.handle_no_permissionN)
r    r!   r"   __doc__r   r0   r+   r/   r1   r'   r   r   r   r   r#   +   s    	
r#   c                       sX   e Zd ZdZdZdZ fddZdZdZdd Z	dd	 Z
d
d Zdd Zdd Z  ZS )RemoteUserMiddlewarea  
    Middleware for utilizing web-server-provided authentication.

    If request.user is not authenticated, then this middleware attempts to
    authenticate the username from the ``REMOTE_USER`` key in ``request.META``,
    an environment variable commonly set by the webserver.

    If authentication is successful, the user is automatically logged in to
    persist the user in the session.

    The ``request.META`` key is configurable and defaults to ``REMOTE_USER``.
    Subclass this class and change the ``header`` attribute if you need to
    use a different key from ``request.META``, for example a HTTP request
    header.
    Tc                    s<   |d u rt d|| _t|| _| jrt|  t   d S )Nzget_response must be provided.)
ValueErrorget_responser   is_asyncr   super__init__)r   r>   	__class__r   r   rA   q   s   
zRemoteUserMiddleware.__init__REMOTE_USERc                 C   s   | j r| |S t|dstdz|j| j }W n ty3   | jr,|jj	r,| 
| | | Y S w |jj	rM|j | ||krH| |S | 
| tj||d}|r_||_t|| | |S )Nr   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?   	__acall__r   r   METAheaderKeyErrorforce_logout_if_no_headerr   r%   _remove_invalid_userr>   get_usernameclean_usernamer   authenticateloginr   r   usernamer   r   r   r   __call__   s,   






zRemoteUserMiddleware.__call__c                    s   t |ds
tdz
|jd| j  }W n& ty:   | jr0| I d H }|jr0| |I d H  | 	|I d H  Y S w | I d H }|jr_|
 | ||krW| 	|I d H S | |I d H  tj||dI d H }|rw||_t||I d H  | 	|I d H S )Nr   rE   HTTP_rF   )r   r   rI   rJ   rK   rL   r   r%   _aremove_invalid_userr>   rN   rO   r   aauthenticater   aloginrR   r   r   r   rH      s0   
	zRemoteUserMiddleware.__acall__c                 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	   rO   AttributeError)r   rS   r   backend_strbackendr   r   r   rO      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   rY   ImportErrorlogout
isinstancer
   r   r   stored_backendr   r   r   rM      s   
z)RemoteUserMiddleware._remove_invalid_userc                    sh   zt |jtjdI dH }W n ty"   t|I dH  Y dS w t|tr2t|I dH  dS dS r]   )	r	   r   agetr   rY   r`   alogoutrb   r
   rc   r   r   r   rV      s   
z*RemoteUserMiddleware._aremove_invalid_user)r    r!   r"   r;   sync_capableasync_capablerA   rJ   rL   rT   rH   rO   rM   rV   __classcell__r   r   rB   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 ``request.META`` key is not found in the request. Useful for
    setups when the external authentication 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;   rL   r   r   r   r   rj     s    
rj   N) 	functoolsr   urllib.parser   asgiref.syncr   r   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#   r<   rj   r   r   r   r   <module>   s&    2 (