| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from django.contrib.auth.base_user import AbstractBaseUser |
| 4 |
from django.dispatch import Signal |
| 5 |
|
| 6 |
from allauth.account import app_settings as account_settings |
| 7 |
from allauth.mfa import app_settings |
| 8 |
from allauth.mfa.adapter import get_adapter |
| 9 |
from allauth.mfa.utils import is_mfa_enabled |
| 10 |
|
| 11 |
|
| 12 |
# Emitted when an authenticator is added. |
| 13 |
# Arguments: |
| 14 |
# - request: HttpRequest |
| 15 |
# - user: User |
| 16 |
# - authenticator: Authenticator |
| 17 |
authenticator_added = Signal() |
| 18 |
|
| 19 |
# Emitted when an authenticator is removed. |
| 20 |
# Arguments: |
| 21 |
# - request: HttpRequest |
| 22 |
# - user: User |
| 23 |
# - authenticator: Authenticator |
| 24 |
authenticator_removed = Signal() |
| 25 |
|
| 26 |
# Emitted when an authenticator is reset (e.g. recovery codes regenerated). |
| 27 |
# Arguments: |
| 28 |
# - request: HttpRequest |
| 29 |
# - user: User |
| 30 |
# - authenticator: Authenticator |
| 31 |
authenticator_reset = Signal() |
| 32 |
|
| 33 |
# Emitted when an authenticator is successfully used (e.g. for login or reauthentication purposes). |
| 34 |
# Arguments: |
| 35 |
# - request: HttpRequest |
| 36 |
# - user: User |
| 37 |
# - authenticator: Authenticator |
| 38 |
# - reauthenticated:bool |
| 39 |
# - passwordless: bool |
| 40 |
authenticator_used = Signal() |
| 41 |
|
| 42 |
# Emitted when authentication via MFA failed, e.g. when an incorrect code was entered. |
| 43 |
# Arguments: |
| 44 |
# - request: HttpRequest |
| 45 |
# - user: User |
| 46 |
# - authenticator: Authenticator |
| 47 |
# - reauthentication: bool (optional) |
| 48 |
authentication_failed = Signal() |
| 49 |
|
| 50 |
|
| 51 |
def on_add_email(sender, email, user: AbstractBaseUser, **kwargs) -> None: |
| 52 |
if app_settings.ALLOW_UNVERIFIED_EMAIL: |
| 53 |
return |
| 54 |
if account_settings.EMAIL_VERIFICATION_BY_CODE_ENABLED: |
| 55 |
return |
| 56 |
if is_mfa_enabled(user): |
| 57 |
adapter = get_adapter() |
| 58 |
raise adapter.validation_error("add_email_blocked") |