o
    &zh`                     @   sr   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lmZmZ e aG d	d
 d
eZdS )    )local)get_user_model)ModelBackend)get_adapter)LoginMethod   )app_settings)filter_users_by_emailfilter_users_by_usernamec                   @   sz   e Zd Zdd Zdd Z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d Z	dd Z
edd Zedd ZdS )AuthenticationBackendc                 K   s>   | d}|s	d S d| _| j|fi |}| js| | |S )NpasswordF)get_did_check_password_authenticate_mitigate_timing_attack)selfrequestcredentialsr   user r   X/var/www/html/kangema/venv/lib/python3.10/site-packages/allauth/account/auth_backends.pyauthenticate   s   

z"AuthenticationBackend.authenticatec                 K   s   | d}| d}|r&tjtjv r| ||}|r|S | ||}|r&|S | d}|r7| ||}|r7|S | d}|rH| ||}|rH|S d S )Nr   usernameemailphone)r   r   EMAILr   LOGIN_METHODS_authenticate_by_email_authenticate_by_username_authenticate_by_phone)r   r   r   r   r   r   r   r   r   r   r   r      s*   



z#AuthenticationBackend._authenticater   r   c                 C   s0   |rt jtjvr
d S t }||}| ||S N)r   PHONEr   r   r   get_user_by_phone_check_password)r   r   r   adapterr   r   r   r   r   9   s
   
z,AuthenticationBackend._authenticate_by_phoner   c                 C   s2   t jtjvstjr|sd S t| }| ||S r    )r   USERNAMEr   r   USER_MODEL_USERNAME_FIELDr
   firstr#   )r   r   r   r   r   r   r   r   @   s   z/AuthenticationBackend._authenticate_by_usernamer   c                 C   sB   |rt jtjvr
d S t|dd}|D ]}| ||r|  S qd S )NT)prefer_verified)r   r   r   r   r	   r#   )r   r   r   usersr   r   r   r   r   J   s   z,AuthenticationBackend._authenticate_by_emailc                 C   s   t   | d S r    )r   set_password)r   r   r   r   r   r   W   s   z-AuthenticationBackend._mitigate_timing_attackc                 C   s@   |sd S d| _ ||}|r| |}|s| | |r|S d S )NT)r   check_passworduser_can_authenticate_stash_user)r   r   r   okr   r   r   r#   Z   s   


z%AuthenticationBackend._check_passwordc                 C   s   t tdd}|t_|S )a  Now, be aware, the following is quite ugly, let me explain:

        Even if the user credentials match, the authentication can fail because
        Django's default ModelBackend calls user_can_authenticate(), which
        checks `is_active`. Now, earlier versions of allauth did not do this
        and simply returned the user as authenticated, even in case of
        `is_active=False`. For allauth scope, this does not pose a problem, as
        these users are properly redirected to an account inactive page.

        This does pose a problem when the allauth backend is used in a
        different context where allauth is not responsible for the login. Then,
        by not checking on `user_can_authenticate()` users will allow to become
        authenticated whereas according to Django logic this should not be
        allowed.

        In order to preserve the allauth behavior while respecting Django's
        logic, we stash a user for which the password check succeeded but
        `user_can_authenticate()` failed. In the allauth authentication logic,
        we can then unstash this user and proceed pointing the user to the
        account inactive page.
        r   N)getattr_stashr   )clsr   retr   r   r   r-   e   s   z!AuthenticationBackend._stash_userc                 C   s
   |  d S r    )r-   )r1   r   r   r   unstash_authenticated_user   s   
z0AuthenticationBackend.unstash_authenticated_userN)__name__
__module____qualname__r   r   strr   r   r   r   r#   classmethodr-   r3   r   r   r   r   r      s     



r   N)	threadingr   django.contrib.authr   django.contrib.auth.backendsr   allauth.account.adapterr   allauth.account.app_settingsr    r   utilsr	   r
   r0   r   r   r   r   r   <module>   s    