Debian Codegraph @

allauth/app_settings.py

raw
1 from __future__ import annotations
2
3 from typing import TypeVar
4
5 from django.apps import apps
6
7 from allauth.core.internal.cryptokit import UserCodeFormat
8
9
10 _T = TypeVar("_T")
11
12
13 class AppSettings:
14 def __init__(self, prefix: str) -> None:
15 self.prefix = prefix
16
17 def _setting(self, name: str, dflt: _T) -> _T:
18 from allauth.utils import get_setting
19
20 return get_setting(self.prefix + name, dflt)
21
22 @property
23 def SITES_ENABLED(self) -> bool:
24 return apps.is_installed("django.contrib.sites")
25
26 @property
27 def SOCIALACCOUNT_ENABLED(self) -> bool:
28 return apps.is_installed("allauth.socialaccount")
29
30 @property
31 def SOCIALACCOUNT_ONLY(self) -> bool:
32 from allauth.utils import get_setting
33
34 return get_setting("SOCIALACCOUNT_ONLY", False)
35
36 @property
37 def MFA_ENABLED(self) -> bool:
38 return apps.is_installed("allauth.mfa")
39
40 @property
41 def USERSESSIONS_ENABLED(self) -> bool:
42 return apps.is_installed("allauth.usersessions")
43
44 @property
45 def HEADLESS_ENABLED(self) -> bool:
46 return apps.is_installed("allauth.headless")
47
48 @property
49 def HEADLESS_ONLY(self) -> bool:
50 from allauth.utils import get_setting
51
52 return get_setting("HEADLESS_ONLY", False)
53
54 @property
55 def DEFAULT_AUTO_FIELD(self):
56 return self._setting("DEFAULT_AUTO_FIELD", None)
57
58 @property
59 def TRUSTED_PROXY_COUNT(self) -> int:
60 """
61 As the ``X-Forwarded-For`` header can be spoofed, you need to
62 configure the number of proxies that are under your control and hence,
63 can be trusted. The default is 0, meaning, no proxies are trusted. As a
64 result, the ``X-Forwarded-For`` header will be disregarded by default.
65 """
66 return self._setting("TRUSTED_PROXY_COUNT", 0)
67
68 @property
69 def TRUSTED_CLIENT_IP_HEADER(self) -> str | None:
70 """
71 If your service is running behind a trusted proxy that sets a custom header
72 containing the client IP address, specify that header name here. The client
73 IP will be extracted from this header instead of ``X-Forwarded-For``.
74 Examples: ``"CF-Connecting-IP"`` (Cloudflare), ``"X-Real-IP"`` (nginx).
75 """
76 return self._setting("TRUSTED_CLIENT_IP_HEADER", None)
77
78 @property
79 def RATE_LIMIT_IPV6_PREFIX(self) -> int:
80 """
81 For rate limiting purposes, IPv6 addresses are truncated to their network
82 prefix to prevent attackers from bypassing rate limits by rotating through
83 addresses within their allocated prefix. This setting controls the prefix
84 length. The default is 64, which corresponds to a standard /64 subnet.
85 """
86 return self._setting("RATE_LIMIT_IPV6_PREFIX", 64)
87
88 @property
89 def USER_CODE_FORMAT(self) -> UserCodeFormat:
90 """
91 Controls the format of user-facing verification codes (e.g. email
92 verification, phone verification, login codes).
93 """
94 return self._setting("USER_CODE_FORMAT", UserCodeFormat())
95
96
97 _app_settings = AppSettings("ALLAUTH_")
98
99
100 def __getattr__(name):
101 # See https://peps.python.org/pep-0562/
102 return getattr(_app_settings, name)