
    f"                         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 d dlmZmZ ed        Zd Zd Zd ZdefdZdefdZ defdZ!y)    N)SimpleNamespace)settings)HttpResponseRedirect)NoReverseMatchreverse)sync_and_async_middleware)iscoroutinefunctionsync_to_async)app_settings)get_adapter)flows)context)ImmediateHttpResponseReauthenticationRequiredc                 H     t               r fd}n fd}d }||_        |S )Nc                 ,  K   t               | _        t        j                  |       5   |        d {   }t	        | |      rt        |        d {    t        | |      rt        |        d {   }|cd d d        S 7 M7 07 # 1 sw Y   y xY wwN)r   allauthr   request_context_should_check_dangling_login_acheck_dangling_login_should_redirect_accounts_aredirect_accountsrequestresponseget_responses     U/var/www/cs2snipe.com/venv/lib/python3.12/site-packages/allauth/account/middleware.py
middlewarez%AccountMiddleware.<locals>.middleware   s     -/GO((1  !-g!66/B0999,Wh?%8%AAH   69A   sP   %BBBBBB2B3B8
BBBBBBc                     t               | _        t        j                  |       5   |       }t	        | |      rt        |        t        | |      rt        |       }|cd d d        S # 1 sw Y   y xY wr   )r   r   r   r   r   _check_dangling_loginr   _redirect_accountsr   s     r   r   z%AccountMiddleware.<locals>.middleware%   sc    -/GO((1  '0/B)'2,Wh?1':H     s   8A((A1c                    t        |t              r|j                  S t        |t              rXt	        d      }t               j                  | j                        }|r|d   d   }t        j                  j                  | |      S y )Naccount_loginr   url)
isinstancer   r   r   r   r   get_reauthentication_methodsuserr   reauthenticationsuspend_request)r   	exceptionredirect_urlmethodss       r   process_exceptionz,AccountMiddleware.<locals>.process_exception/   ss    i!67%%%	#;<"?3L!m@@NG&qz%0))99'<PP =    )r	   r.   )r   r   r.   s   `  r   AccountMiddlewarer0      s+    <(	 	 Q $5J r/   c                 r   | j                   j                  d      }|r|dk7  ry|j                   j                  d      }|r|j                  d      d   }|r|dk7  ryt        j                  r)| j
                  j                  t        j                        s| j
                  dv ry|j                  d	z  d
k7  ryy)Nzsec-fetch-destdocumentFzcontent-type;r   z	text/html)z/favicon.icoz/robots.txtz/humans.txtd      T)headersget	partitionr   
STATIC_URLpath
startswithstatus_code)r   r   sec_fetch_destcontent_types       r   r   r   =   s    __(()9:N.J6##''7L#--c2153 	 7 78K8K L	  

 s"a'r/   c                    ddl m} t        | dd      s| j                  j	                  t
        j                  j                        x}rt        |t              rQ|j	                  di       j	                  di       j	                  d      }||j                  k(  rt        j                  sy | j                  j                  t
        j                  j                         y y y )Nr   )EmailVerificationStage_account_login_accessedFstatestagescurrent)allauth.account.stagesr@   getattrsessionr7   r   loginLOGIN_SESSION_KEYr&   dictkeyr   "EMAIL_VERIFICATION_BY_CODE_ENABLEDpop)r   r@   rH   current_stages       r   r!   r!   T   s    =75u=OO''(E(EFF5F%& %		'2 6 : :8R H L LY W!%;%?%??(KK OO = => G >r/   c                 J   K    t        t              |        d {    y 7 wr   )r
   r!   )r   s    r   r   r   j   s     
.--
.w
777s   #!#returnc                     |j                   dk7  ry	 t        d      }t        d      }t        j                  j                  ||g      }t        |      dk  s|| j                  k7  ryy# t        $ r Y yw xY w)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)r<   r   r   osr:   commonprefixlen)r   r   
login_path
email_pathprefixs        r   r   r   n   s|     s"_-
_-
 WW!!:z":;F
6{a6W\\1   s   A( (	A43A4c                    K   t        d      }t        d      }| j                          d {   }t        |j                  r|      S |      S 7 !wNrR   r$   )r   auserr   is_authenticatedr   rX   rW   r(   s       r   r   r      sH     )J)J Dd.C.C
TTTT !s   *AA"Ac                     t        d      }t        d      }| j                  }t        |j                  r|      S |      S r[   )r   r(   r   r]   r^   s       r   r"   r"      s:    )J)J<<Dd.C.C
TTTTr/   )"rT   typesr   django.confr   django.httpr   django.urlsr   r   django.utils.decoratorsr   asgiref.syncr	   r
   allauth.accountr   allauth.account.adapterr   allauth.account.internalr   allauth.corer   allauth.core.exceptionsr   r   r0   r   r!   r   boolr   r   r"    r/   r   <module>rm      sy    	 !   , / = ; ( / *   $ $N.?,8D 6U*> UU#7 Ur/   