a
    ‹Qx_×  ã                   @   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   sh   d }t jtjkr"| jf i |¤Ž}nBt jtjkrT| jf i |¤Ž}|sd| jf i |¤Ž}n| jf i |¤Ž}|S ©N)r   ZAUTHENTICATION_METHODr   ZEMAILÚ_authenticate_by_emailZUSERNAME_EMAILÚ_authenticate_by_username)ÚselfÚrequestÚcredentialsÚret© r   úU/var/www/html/Ranjet/env/lib/python3.9/site-packages/allauth/account/auth_backends.pyÚauthenticate   s    z"AuthenticationBackend.authenticatec                 K   sv   t j}| d¡}| d¡}tƒ }|r4|d u s4|d u r8d S z"t|ƒ ¡ }|  ||¡rX|W S W n |jyp   Y d S 0 d S )NÚusernameÚpassword)r   ZUSER_MODEL_USERNAME_FIELDÚgetr   r
   Ú_check_passwordZDoesNotExist)r   r   Zusername_fieldr   r   Z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 r   )Zcheck_passwordZ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   Zdjango.contrib.auth.backendsr   Úutilsr   Ú r   r   r	   r
   r   r   r   r   r   r   Ú<module>   s   