o
    ëË·e×  ã                   @   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)ÚModelBackendé   )Úget_user_modelé   )Ú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   úT/var/www/ideatree/venv/lib/python3.10/site-packages/allauth/account/auth_backends.pyÚauthenticate   s   úÿz"AuthenticationBackend.authenticatec                 K   st   t j}| d¡}| d¡}tƒ }|r|d u s|d u rd S zt|ƒ ¡ }|  ||¡r,|W S W d S  |jy9   Y d S w )NÚusernameÚpassword)r   ÚUSER_MODEL_USERNAME_FIELDÚgetr   r
   Ú_check_passwordÚDoesNotExist)r   r   Úusername_fieldr   r   ÚUserÚuserr   r   r   r      s   

ÿÿz/AuthenticationBackend._authenticate_by_usernamec                 K   s@   |  d|  d¡¡}|rt|ƒD ]}|  ||d ¡r|  S qd S )NÚemailr   r   )r   r	   r   )r   r   r!   r    r   r   r   r   ,   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   9   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%   A   s   z!AuthenticationBackend._stash_userc                 C   s
   |   d ¡S r"   )r%   )r(   r   r   r   Úunstash_authenticated_user]   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.auth.backendsr   Úutilsr   Ú r   r   r	   r
   r'   r   r   r   r   r   Ú<module>   s    