o
    h                     @   s  d dl Z d dlmZmZ d dlmZ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
Zdd Zdedeeef fddZdedeeef dede	fddZdedede	fddZdedede	fddZdedefddZdedefddZdS )    N)AnyDict)
exceptions
validators)HttpRequestHttpResponse)app_settings)get_adapter)LoginMethod)Login)ImmediateHttpResponseaccount_authentication_methodsc                 K   sV   | j tg }|t d}| D ]\}}|dur|||< q|| || j t< dS )ai  Here we keep a log of all authentication methods used within the current
    session.  Important to note is that having entries here does not imply that
    a user is fully signed in. For example, consider a case where a user
    authenticates using a password, but fails to complete the 2FA challenge.
    Or, a user successfully signs in into an inactive account or one that still
    needs verification. In such cases, ``request.user`` is still anonymous, yet,
    we do have an entry here.

    Example data::

        {'method': 'password',
         'at': 1701423602.7184925,
         'username': 'john.doe'}

        {'method': 'socialaccount',
         'at': 1701423567.6368647,
         'provider': 'amazon',
         'uid': 'amzn1.account.K2LI23KL2LK2'}

        {'method': 'mfa',
         'at': 1701423602.6392953,
         'id': 1,
         'type': 'totp'}

    )methodatN)sessionget"AUTHENTICATION_METHODS_SESSION_KEYtimeitemsappend)requestr   
extra_datamethodsdatakv r   _/var/www/html/env_mimamsha/lib/python3.10/site-packages/allauth/account/internal/flows/login.pyrecord_authentication   s   
r   loginreturnc                 C   s   t | j| j| j| j| jdS )zc
    TODO: Just break backwards compatibility and pass only `login` to
    `pre/post_login()`.
    )email_verificationredirect_urlsignal_kwargssignupemail)dictr!   r"   r#   r$   r%   r   r   r   r   _get_login_hook_kwargs8   s   r(   r   credentialsc                    s0    fdddD }t | fddi| t| |S )Nc                    s"   i | ]}  |r|  |qS r   )r   ).0fieldr)   r   r   
<dictcomp>I   s    
z*perform_password_login.<locals>.<dictcomp>)r%   usernamer   password)r   perform_login)r   r)   r   r   r   r,   r   perform_password_loginF   s
   

r1   c                 C   s6   t  }t|}|j| |jfi |}|r|S t| |S )N)r	   r(   	pre_loginuserresume_login)r   r   adapterhook_kwargsresponser   r   r   r0   R   s   
r0   c           	   
   C   s   ddl m} t }|| |}z'| }|r|W S || |j t|}|j| |jfi |}|r3|W S W |S  ty\ } zt	j
rNddlm} t|j|rN||j}W Y d }~|S d }~ww )Nr   )LoginStageController)APIResponse)allauth.account.stagesr8   r	   handler   r3   r(   
post_loginr   allauth_settingsHEADLESS_ENABLED*allauth.headless.internal.restkit.responser9   
isinstancer7   )	r   r   r8   r5   ctrlr7   r6   er9   r   r   r   r4   [   s.   
	r4   c                 C   s   ddl m} || |rdS dS )Nr   )is_verification_rate_limitedTF)1allauth.account.internal.flows.email_verificationrC   )r   r   rC   r   r   r   is_login_rate_limiteds   s   
rE   c                 C   s   t tjdkrtttjS tjtjv r(z	t|  tjW S  t	j
y'   Y nw tjtjv rIzt jdd|  tjW S  t	j
yH   Y tjS w tjS )N   T)required)lenr   LOGIN_METHODSnextiterr
   EMAILr   validate_emailr   ValidationErrorPHONEr	   phone_form_fieldcleanUSERNAMEr'   r   r   r   derive_login_method}   s$   
rS   ) r   typingr   r   django.corer   r   django.httpr   r   allauthr   r=   allauth.accountallauth.account.adapterr	   allauth.account.app_settingsr
   allauth.account.modelsr   allauth.core.exceptionsr   r   r   strr(   r1   r0   r4   boolrE   rS   r   r   r   r   <module>   s4    &

	
