o
    "4g"                     @   s   d dl 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mZ e	dd ZdefddZdefddZdefddZdS )    N)SimpleNamespace)HttpResponseRedirect)NoReverseMatchreverse)sync_and_async_middleware)iscoroutinefunction)get_adapter)flows)context)ImmediateHttpResponseReauthenticationRequiredc                    s4   t  r fdd}n fdd}dd }||_|S )Nc                    sb   t  | _t|   | I d H }t| |rt| I d H }|W  d    S 1 s*w   Y  d S N)r   allauthr
   request_context_should_redirect_accounts_aredirect_accountsrequestresponseget_response c/var/www/html/authentication-server/venv/lib/python3.10/site-packages/allauth/account/middleware.py
middleware   s   
$z%AccountMiddleware.<locals>.middlewarec                    sT   t  | _t|   | }t| |rt| }|W  d    S 1 s#w   Y  d S r   )r   r   r
   r   r   _redirect_accountsr   r   r   r   r   !   s   
$c                 S   sR   t |tr|jS t |tr'td}t | j}|r |d d }tj	
| |S d S )Naccount_loginr   url)
isinstancer   r   r   r   r   get_reauthentication_methodsuserr	   reauthenticationsuspend_request)r   	exceptionredirect_urlmethodsr   r   r   process_exception)   s   

z,AccountMiddleware.<locals>.process_exception)r   r%   )r   r   r%   r   r   r   AccountMiddleware   s   

r&   returnc                 C   sf   |j dkrdS z
td}td}W n
 ty   Y dS w tj||g}t|dks/|| jkr1dS dS )a  
    URLs should be hackable. Yet, assuming allauth is included like this...

        path("accounts/", include("allauth.urls")),

    ... and a user would attempt to navigate to /accounts/, a 404 would be
    presented. This code catches that 404, and redirects to either the email
    management overview or the login page, depending on whether or not the user
    is authenticated.
    i  Fr   account_email   T)status_coder   r   ospathcommonprefixlen)r   r   
login_path
email_pathprefixr   r   r   r   7   s   
r   c                    s4   t d}t d}|  I d H }t|jr|S |S Nr(   r   )r   auserr   is_authenticatedr   r0   r/   r   r   r   r   r   R   s
   r   c                 C   s*   t d}t d}| j}t|jr|S |S r2   )r   r   r   r4   r5   r   r   r   r   Y   s   r   )r+   typesr   django.httpr   django.urlsr   r   django.utils.decoratorsr   asgiref.syncr   allauth.account.adapterr   allauth.account.internalr	   allauth.corer
   allauth.core.exceptionsr   r   r&   boolr   r   r   r   r   r   r   <module>   s    
#