| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import TypeVar |
| 4 |
|
| 5 |
|
| 6 |
_T = TypeVar("_T") |
| 7 |
|
| 8 |
|
| 9 |
class AppSettings: |
| 10 |
def __init__(self, prefix: str) -> None: |
| 11 |
self.prefix = prefix |
| 12 |
|
| 13 |
def _setting(self, name: str, dflt: _T) -> _T: |
| 14 |
from allauth.utils import get_setting |
| 15 |
|
| 16 |
return get_setting(self.prefix + name, dflt) |
| 17 |
|
| 18 |
@property |
| 19 |
def QUERY_EMAIL(self) -> bool: |
| 20 |
""" |
| 21 |
Request email address from 3rd party account provider? |
| 22 |
E.g. using OpenID AX |
| 23 |
""" |
| 24 |
return self._setting("QUERY_EMAIL", self.EMAIL_REQUIRED) |
| 25 |
|
| 26 |
@property |
| 27 |
def AUTO_SIGNUP(self) -> bool: |
| 28 |
""" |
| 29 |
Attempt to bypass the signup form by using fields (e.g. username, |
| 30 |
email) retrieved from the social account provider. If a conflict |
| 31 |
arises due to a duplicate email signup form will still kick in. |
| 32 |
""" |
| 33 |
return self._setting("AUTO_SIGNUP", True) |
| 34 |
|
| 35 |
@property |
| 36 |
def PROVIDERS(self) -> dict: |
| 37 |
""" |
| 38 |
Provider specific settings |
| 39 |
""" |
| 40 |
ret: dict = self._setting("PROVIDERS", {}) |
| 41 |
oidc = ret.get("openid_connect") |
| 42 |
if oidc: |
| 43 |
ret["openid_connect"] = self._migrate_oidc(oidc) |
| 44 |
return ret |
| 45 |
|
| 46 |
def _migrate_oidc(self, oidc: dict) -> dict: |
| 47 |
servers = oidc.get("SERVERS") |
| 48 |
if servers is None: |
| 49 |
return oidc |
| 50 |
ret = {} |
| 51 |
apps = [] |
| 52 |
for server in servers: |
| 53 |
app = dict(**server["APP"]) |
| 54 |
app_settings = {} |
| 55 |
if "token_auth_method" in server: |
| 56 |
app_settings["token_auth_method"] = server["token_auth_method"] |
| 57 |
app_settings["server_url"] = server["server_url"] |
| 58 |
app.update( |
| 59 |
{ |
| 60 |
"name": server.get("name", ""), |
| 61 |
"provider_id": server["id"], |
| 62 |
"settings": app_settings, |
| 63 |
} |
| 64 |
) |
| 65 |
assert app["provider_id"] # nosec |
| 66 |
apps.append(app) |
| 67 |
ret["APPS"] = apps |
| 68 |
return ret |
| 69 |
|
| 70 |
@property |
| 71 |
def EMAIL_REQUIRED(self) -> bool: |
| 72 |
""" |
| 73 |
The user is required to hand over an email address when signing up |
| 74 |
""" |
| 75 |
from allauth.account import app_settings as account_settings |
| 76 |
|
| 77 |
fields = account_settings.SIGNUP_FIELDS |
| 78 |
email_required = "email" in fields and fields["email"].get("required") |
| 79 |
return self._setting("EMAIL_REQUIRED", email_required) |
| 80 |
|
| 81 |
@property |
| 82 |
def EMAIL_VERIFICATION(self): |
| 83 |
""" |
| 84 |
See email verification method. When `None`, the default |
| 85 |
`allauth.account` logic kicks in. |
| 86 |
""" |
| 87 |
from allauth import app_settings as allauth_settings |
| 88 |
from allauth.account import app_settings as account_settings |
| 89 |
|
| 90 |
dflt = ( |
| 91 |
account_settings.EmailVerificationMethod.NONE |
| 92 |
if allauth_settings.SOCIALACCOUNT_ONLY |
| 93 |
else None |
| 94 |
) |
| 95 |
return self._setting("EMAIL_VERIFICATION", dflt) |
| 96 |
|
| 97 |
@property |
| 98 |
def EMAIL_AUTHENTICATION(self) -> bool: |
| 99 |
"""Consider a scenario where a social login occurs, and the social |
| 100 |
account comes with a verified email address (verified by the account |
| 101 |
provider), but that email address is already taken by a local user |
| 102 |
account. Additionally, assume that the local user account does not have |
| 103 |
any social account connected. Now, if the provider can be fully trusted, |
| 104 |
you can argue that we should treat this scenario as a login to the |
| 105 |
existing local user account even if the local account does not already |
| 106 |
have the social account connected, because -- according to the provider |
| 107 |
-- the user logging in has ownership of the email address. This is how |
| 108 |
this scenario is handled when `EMAIL_AUTHENTICATION` is set to |
| 109 |
`True`. As this implies that an untrustworthy provider can login to any |
| 110 |
local account by fabricating social account data, this setting defaults |
| 111 |
to `False`. Only set it to `True` if you are using providers that can be |
| 112 |
fully trusted. |
| 113 |
""" |
| 114 |
return self._setting("EMAIL_AUTHENTICATION", False) |
| 115 |
|
| 116 |
@property |
| 117 |
def EMAIL_AUTHENTICATION_AUTO_CONNECT(self) -> bool: |
| 118 |
"""In case email authentication is applied, this setting controls |
| 119 |
whether or not the social account is automatically connected to the |
| 120 |
local account. In case of ``False`` (the default) the local account |
| 121 |
remains unchanged during the login. In case of ``True``, the social |
| 122 |
account for which the email matched, is automatically added to the list |
| 123 |
of social accounts connected to the local account. As a result, even if |
| 124 |
the user were to change the email address afterwards, social login |
| 125 |
would still be possible when using ``True``, but not in case of |
| 126 |
``False``. |
| 127 |
""" |
| 128 |
return self._setting("EMAIL_AUTHENTICATION_AUTO_CONNECT", False) |
| 129 |
|
| 130 |
@property |
| 131 |
def ADAPTER(self) -> str: |
| 132 |
return self._setting( |
| 133 |
"ADAPTER", |
| 134 |
"allauth.socialaccount.adapter.DefaultSocialAccountAdapter", |
| 135 |
) |
| 136 |
|
| 137 |
@property |
| 138 |
def FORMS(self) -> dict: |
| 139 |
return self._setting("FORMS", {}) |
| 140 |
|
| 141 |
@property |
| 142 |
def LOGIN_ON_GET(self) -> bool: |
| 143 |
return self._setting("LOGIN_ON_GET", False) |
| 144 |
|
| 145 |
@property |
| 146 |
def STORE_TOKENS(self) -> bool: |
| 147 |
return self._setting("STORE_TOKENS", False) |
| 148 |
|
| 149 |
@property |
| 150 |
def UID_MAX_LENGTH(self) -> int: |
| 151 |
return 191 |
| 152 |
|
| 153 |
@property |
| 154 |
def SOCIALACCOUNT_STR(self): |
| 155 |
return self._setting("SOCIALACCOUNT_STR", None) |
| 156 |
|
| 157 |
@property |
| 158 |
def REQUESTS_TIMEOUT(self) -> int: |
| 159 |
return self._setting("REQUESTS_TIMEOUT", 5) |
| 160 |
|
| 161 |
@property |
| 162 |
def OPENID_CONNECT_URL_PREFIX(self) -> str: |
| 163 |
return self._setting("OPENID_CONNECT_URL_PREFIX", "oidc") |
| 164 |
|
| 165 |
|
| 166 |
_app_settings = AppSettings("SOCIALACCOUNT_") |
| 167 |
|
| 168 |
|
| 169 |
def __getattr__(name): |
| 170 |
# See https://peps.python.org/pep-0562/ |
| 171 |
return getattr(_app_settings, name) |