@@ -27,29 +27,31 @@ class UserModelEmailBackend(ModelBackend):
27
27
"""
28
28
29
29
def authenticate (self , request , username = None , password = None ):
30
+ if not username or not password :
31
+ return
32
+
30
33
User = get_user_model ()
31
- if username and password :
32
- try :
33
- user = User .objects .get (
34
- email__iexact = username ,
35
- login_type = LoginTypeChoices .default ,
36
- )
37
- if check_password (
38
- password , user .password
39
- ) and self .user_can_authenticate (user ):
40
- return user
41
- except User .MultipleObjectsReturned :
42
- # Found multiple users with this email (shouldn't happen if we added checks)
43
- # Run the default password hasher once to reduce the timing
44
- # difference between an existing and a nonexistent user (#20760).
45
- User ().set_password (password )
46
- return None
47
- except User .DoesNotExist :
48
- # No user was found, return None - triggers default login failed
49
- # Run the default password hasher once to reduce the timing
50
- # difference between an existing and a nonexistent user (#20760).
51
- User ().set_password (password )
52
- return None
34
+ try :
35
+ user = User .objects .get (
36
+ email__iexact = username ,
37
+ login_type = LoginTypeChoices .default ,
38
+ )
39
+ if check_password (password , user .password ) and self .user_can_authenticate (
40
+ user
41
+ ):
42
+ return user
43
+ except User .MultipleObjectsReturned :
44
+ # Found multiple users with this email (shouldn't happen if we added checks)
45
+ # Run the default password hasher once to reduce the timing
46
+ # difference between an existing and a nonexistent user (#20760).
47
+ User ().set_password (password )
48
+ return None
49
+ except User .DoesNotExist :
50
+ # No user was found, return None - triggers default login failed
51
+ # Run the default password hasher once to reduce the timing
52
+ # difference between an existing and a nonexistent user (#20760).
53
+ User ().set_password (password )
54
+ return None
53
55
54
56
55
57
class Verify2FATokenBackend (BaseBackend ):
@@ -58,17 +60,18 @@ class Verify2FATokenBackend(BaseBackend):
58
60
"""
59
61
60
62
def authenticate (self , request , * , user = None , token = None ):
61
- # 2FA with sms verification
62
- if user and token :
63
- accepted , drift = accept_totp (
64
- key = user .seed ,
65
- response = token ,
66
- period = getattr (settings , "ACCOUNTS_USER_TOKEN_EXPIRE_TIME" , 300 ),
67
- )
68
- if not accepted :
69
- return None
63
+ if not user or not token :
64
+ return
70
65
71
- return user
66
+ accepted , drift = accept_totp (
67
+ key = user .seed ,
68
+ response = token ,
69
+ period = getattr (settings , "ACCOUNTS_USER_TOKEN_EXPIRE_TIME" , 300 ),
70
+ )
71
+ if not accepted :
72
+ return None
73
+
74
+ return user
72
75
73
76
74
77
class CustomAxesBackend (AxesBackend ):
0 commit comments