Debian Codegraph @

allauth/account/checks.py

raw
1 from __future__ import annotations
2
3 from django.core.checks import Critical, Warning, register
4
5
6 @register()
7 def adapter_check(app_configs, **kwargs):
8 from allauth.account.adapter import get_adapter
9
10 ret = []
11 adapter = get_adapter()
12 if hasattr(adapter, "get_email_confirmation_redirect_url"):
13 ret.append(
14 Warning(
15 msg="adapter.get_email_confirmation_redirect_url(request) is deprecated, use adapter.get_email_verification_redirect_url(email_address)"
16 )
17 )
18 return ret
19
20
21 @register()
22 def settings_check(app_configs, **kwargs):
23 from django.conf import settings
24
25 from allauth import app_settings as allauth_app_settings
26 from allauth.account import app_settings
27
28 ret = []
29 if allauth_app_settings.SOCIALACCOUNT_ONLY:
30 if app_settings.LOGIN_BY_CODE_ENABLED:
31 ret.append(
32 Critical(
33 msg="SOCIALACCOUNT_ONLY does not work with ACCOUNT_LOGIN_BY_CODE_ENABLED"
34 )
35 )
36 if allauth_app_settings.MFA_ENABLED:
37 ret.append(
38 Critical(msg="SOCIALACCOUNT_ONLY does not work with 'allauth.mfa'")
39 )
40 if app_settings.EMAIL_VERIFICATION != app_settings.EmailVerificationMethod.NONE:
41 ret.append(
42 Critical(
43 msg="SOCIALACCOUNT_ONLY requires ACCOUNT_EMAIL_VERIFICATION = 'none'"
44 )
45 )
46 if (
47 app_settings.EMAIL_VERIFICATION_BY_CODE_ENABLED
48 and app_settings.EMAIL_VERIFICATION
49 != app_settings.EmailVerificationMethod.MANDATORY
50 ):
51 ret.append(
52 Critical(
53 msg="ACCOUNT_EMAIL_VERFICATION_BY_CODE requires ACCOUNT_EMAIL_VERIFICATION = 'mandatory'"
54 )
55 )
56
57 # Often made mistake: ACCOUNT_SIGNUP_FIELDS = [..., "password", ...]
58 signup_fields = getattr(settings, "ACCOUNT_SIGNUP_FIELDS", None)
59 for wrong_field, right_field in [
60 ("password", "password1"),
61 ("password*", "password1*"),
62 ]:
63 if signup_fields and wrong_field in signup_fields:
64 ret.append(
65 Critical(
66 msg=f"'{wrong_field}' is not a valid field for ACCOUNT_SIGNUP_FIELDS, use '{right_field}'",
67 )
68 )
69
70 # Cross-check SIGNUP_FIELDS against LOGIN_METHODS. E.g. login is by email, email should be required
71 signup_fields = app_settings.SIGNUP_FIELDS
72 if not any(
73 lm in signup_fields and signup_fields[lm]["required"]
74 for lm in app_settings.LOGIN_METHODS
75 ):
76 ret.append(
77 Warning(
78 msg="ACCOUNT_LOGIN_METHODS conflicts with ACCOUNT_SIGNUP_FIELDS",
79 id="account.W001",
80 )
81 )
82
83 # If login includes email, email must be unique
84 if (
85 app_settings.LoginMethod.EMAIL in app_settings.LOGIN_METHODS
86 and not app_settings.UNIQUE_EMAIL
87 ):
88 ret.append(
89 Critical(msg="Using email as a login method requires ACCOUNT_UNIQUE_EMAIL")
90 )
91
92 # Mandatory email verification requires email
93 email_required = "email" in signup_fields and signup_fields["email"]["required"]
94 if (
95 app_settings.EMAIL_VERIFICATION
96 == app_settings.EmailVerificationMethod.MANDATORY
97 and not email_required
98 ):
99 ret.append(
100 Critical(
101 msg="ACCOUNT_EMAIL_VERIFICATION = 'mandatory' requires 'email*' in ACCOUNT_SIGNUP_FIELDS"
102 )
103 )
104
105 if not app_settings.USER_MODEL_USERNAME_FIELD:
106 if "username" in signup_fields:
107 ret.append(
108 Critical(
109 msg="No ACCOUNT_USER_MODEL_USERNAME_FIELD, yet, ACCOUNT_SIGNUP_FIELDS contains 'username'"
110 )
111 )
112
113 if app_settings.LoginMethod.USERNAME in app_settings.LOGIN_METHODS:
114 ret.append(
115 Critical(
116 msg="No ACCOUNT_USER_MODEL_USERNAME_FIELD, yet, ACCOUNT_LOGIN_METHODS requires it"
117 )
118 )
119
120 if (
121 app_settings.MAX_EMAIL_ADDRESSES is not None
122 and app_settings.MAX_EMAIL_ADDRESSES <= 0
123 ):
124 ret.append(Critical(msg="ACCOUNT_MAX_EMAIL_ADDRESSES must be None or > 0"))
125
126 if app_settings.CHANGE_EMAIL:
127 if (
128 app_settings.MAX_EMAIL_ADDRESSES is not None
129 and app_settings.MAX_EMAIL_ADDRESSES != 2
130 ):
131 ret.append(
132 Critical(
133 msg="Invalid combination of ACCOUNT_CHANGE_EMAIL and ACCOUNT_MAX_EMAIL_ADDRESSES"
134 )
135 )
136 if hasattr(settings, "ACCOUNT_LOGIN_ATTEMPTS_LIMIT") or hasattr(
137 settings, "ACCOUNT_LOGIN_ATTEMPTS_TIMEOUT"
138 ):
139 ret.append(
140 Warning(
141 msg="settings.ACCOUNT_LOGIN_ATTEMPTS_LIMIT/TIMEOUT is deprecated, use: settings.ACCOUNT_RATE_LIMITS['login_failed']"
142 )
143 )
144
145 if hasattr(settings, "ACCOUNT_EMAIL_CONFIRMATION_COOLDOWN"):
146 ret.append(
147 Warning(
148 msg="settings.ACCOUNT_EMAIL_CONFIRMATION_COOLDOWN is deprecated, use: settings.ACCOUNT_RATE_LIMITS['confirm_email']"
149 )
150 )
151
152 if hasattr(settings, "ACCOUNT_AUTHENTICATION_METHOD"):
153 converted = set(settings.ACCOUNT_AUTHENTICATION_METHOD.split("_"))
154 ret.append(
155 Warning(
156 f"settings.ACCOUNT_AUTHENTICATION_METHOD is deprecated, use: settings.ACCOUNT_LOGIN_METHODS = {repr(converted)}"
157 )
158 )
159
160 for field in [
161 "ACCOUNT_USERNAME_REQUIRED",
162 "ACCOUNT_EMAIL_REQUIRED",
163 "ACCOUNT_SIGNUP_EMAIL_ENTER_TWICE",
164 "ACCOUNT_SIGNUP_PASSWORD_ENTER_TWICE",
165 ]:
166 if hasattr(settings, field):
167 signup_fields_converted = [
168 k + ("*" if v["required"] else "")
169 for k, v in app_settings.SIGNUP_FIELDS.items()
170 ]
171 ret.append(
172 Warning(
173 f"settings.{field} is deprecated, use: settings.ACCOUNT_SIGNUP_FIELDS = {repr(signup_fields_converted)}"
174 )
175 )
176
177 if (
178 not allauth_app_settings.MFA_ENABLED
179 and app_settings.LOGIN_BY_CODE_TRUST_ENABLED
180 ):
181 ret.append(
182 Critical(
183 msg="ACCOUNT_LOGIN_BY_CODE_TRUST_ENABLED requires MFA to be enabled"
184 )
185 )
186
187 return ret