o
    Lf                     @   sf   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   )app_settings)AuthenticationMethod)filter_users_by_emailfilter_users_by_usernamec                   @   sD   e Zd Zdd Zdd Zdd Zdd Zed	d
 Zedd Z	dS )AuthenticationBackendc                 K   sl   d }t jtjkr| jdi |}|S t jtjkr,| jdi |}|s*| jdi |}|S | jdi |}|S )N )r   AUTHENTICATION_METHODr   EMAIL_authenticate_by_emailUSERNAME_EMAIL_authenticate_by_username)selfrequestcredentialsretr   r   ^/var/www/html/humari/django-venv/lib/python3.10/site-packages/allauth/account/auth_backends.pyauthenticate   s   z"AuthenticationBackend.authenticatec                 K   s   t j}|d}|d}t }|r|d u s|d u rd S zt| }W n |jy6   t  | Y d S w | ||r?|S d S )Nusernamepassword)r   USER_MODEL_USERNAME_FIELDgetr   r	   DoesNotExistset_password_check_password)r   r   username_fieldr   r   Useruserr   r   r   r      s   

z/AuthenticationBackend._authenticate_by_usernamec                 K   sD   | d| d}|r t|ddD ]}| ||d r|  S qd S )Nemailr   T)prefer_verifiedr   )r   r   r   )r   r   r!   r    r   r   r   r   0   s   z,AuthenticationBackend._authenticate_by_emailc                 C   s*   | |}|r| |}|s| | |S N)check_passworduser_can_authenticate_stash_user)r   r    r   r   r   r   r   r   =   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    r   r   r   r   r&   E   s   z!AuthenticationBackend._stash_userc                 C   s
   |  d S r#   )r&   )r)   r   r   r   unstash_authenticated_usera   s   
z0AuthenticationBackend.unstash_authenticated_userN)
__name__
__module____qualname__r   r   r   r   classmethodr&   r*   r   r   r   r   r
      s    
r
   N)	threadingr   django.contrib.authr   django.contrib.auth.backendsr    r   r   utilsr   r	   r(   r
   r   r   r   r   <module>   s    