U
    >i
                     @   s   d dl 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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edddZedd ZedddZedddZdS )    N)
HTTPStatus)SimpleNamespace)HttpResponseRedirect)NoReverseMatchreverse)sync_and_async_middleware)iscoroutinefunctionsync_to_async)get_adapter)flows)context)ImmediateHttpResponseReauthenticationRequiredc                    s4   t  r fdd}n fdd}dd }||_|S )Nc              
      sT   t  | _t| 8  | I d H }t| |r:t| I d H }|W  5 Q R  S Q R X d S N)r   allauthr   request_context_should_redirect_accounts_aredirect_accountsrequestresponseget_response >/tmp/pip-unpacked-wheel-upujnpc2/allauth/account/middleware.py
middleware   s    
z%AccountMiddleware.<locals>.middlewarec              
      sH   t  | _t| ,  | }t| |r.t| }|W  5 Q R  S Q R X 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rNtd}t | j}|r@|d d }tj	
| |S d S )Naccount_loginr   url)
isinstancer   r   r   r   r
   Zget_reauthentication_methodsuserr   ZreauthenticationZsuspend_request)r   	exceptionZ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   sj   |j tjkrdS ztd}td}W n tk
r:   Y dS X tj||g}t|dksb|| jkrfd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.
    Fr   account_email   T)	status_coder   	NOT_FOUNDr   r   ospathcommonprefixlen)r   r   
login_path
email_pathprefixr   r   r   r   5   s    r   c                 C   s   | j S r   )r    )r   r   r   r   _async_get_userP   s    r1   c                    sJ   t d}t d}t| dr*|  I d H }nt| I d H }t|jrD|n|S )Nr&   r   auser)r   hasattrr2   r1   r   is_authenticatedr   r/   r.   r    r   r   r   r   U   s    
r   c                 C   s(   t d}t d}| j}t|jr"|n|S )Nr&   r   )r   r    r   r4   r5   r   r   r   r   `   s    r   )r*   httpr   typesr   Zdjango.httpr   Zdjango.urlsr   r   Zdjango.utils.decoratorsr   Zasgiref.syncr   r	   Zallauth.account.adapterr
   Zallauth.account.internalr   Zallauth.corer   Zallauth.core.exceptionsr   r   r$   boolr   r1   r   r   r   r   r   r   <module>   s"   
#
