| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from django.contrib import messages |
| 4 |
from django.contrib.auth.base_user import AbstractBaseUser |
| 5 |
from django.http import HttpRequest |
| 6 |
|
| 7 |
from allauth.account.adapter import get_adapter as get_account_adapter |
| 8 |
from allauth.account.internal.flows.reauthentication import ( |
| 9 |
raise_if_reauthentication_required, |
| 10 |
) |
| 11 |
from allauth.mfa import app_settings, signals |
| 12 |
from allauth.mfa.models import Authenticator |
| 13 |
from allauth.mfa.recovery_codes.internal.auth import RecoveryCodes |
| 14 |
|
| 15 |
|
| 16 |
def can_generate_recovery_codes(user: AbstractBaseUser) -> bool: |
| 17 |
return ( |
| 18 |
Authenticator.objects.filter(user_id=user.pk) |
| 19 |
.exclude(type=Authenticator.Type.RECOVERY_CODES) |
| 20 |
.exists() |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
def generate_recovery_codes(request: HttpRequest) -> Authenticator: |
| 25 |
raise_if_reauthentication_required(request) |
| 26 |
assert request.user.is_authenticated # nosec |
| 27 |
Authenticator.objects.filter( |
| 28 |
user=request.user, type=Authenticator.Type.RECOVERY_CODES |
| 29 |
).delete() |
| 30 |
rc_auth = RecoveryCodes.activate(request.user) |
| 31 |
authenticator = rc_auth.instance |
| 32 |
add_codes_generated_message(request) |
| 33 |
signals.authenticator_reset.send( |
| 34 |
sender=Authenticator, |
| 35 |
request=request, |
| 36 |
user=request.user, |
| 37 |
authenticator=authenticator, |
| 38 |
) |
| 39 |
adapter = get_account_adapter(request) |
| 40 |
adapter.send_notification_mail("mfa/email/recovery_codes_generated", request.user) |
| 41 |
return authenticator |
| 42 |
|
| 43 |
|
| 44 |
def view_recovery_codes(request: HttpRequest) -> tuple[RecoveryCodes | None, bool]: |
| 45 |
if not request.user.is_authenticated: |
| 46 |
return None, False |
| 47 |
authenticator = Authenticator.objects.filter( |
| 48 |
user=request.user, |
| 49 |
type=Authenticator.Type.RECOVERY_CODES, |
| 50 |
).first() |
| 51 |
if not authenticator: |
| 52 |
return None, False |
| 53 |
raise_if_reauthentication_required(request) |
| 54 |
wrapper = authenticator.wrap() |
| 55 |
did_view = wrapper.did_view |
| 56 |
if not did_view: |
| 57 |
wrapper.mark_as_viewed() |
| 58 |
can_view = (not app_settings.RECOVERY_CODES_SHOW_ONCE) or not did_view |
| 59 |
return wrapper, can_view |
| 60 |
|
| 61 |
|
| 62 |
def auto_generate_recovery_codes(request: HttpRequest) -> Authenticator | None: |
| 63 |
"""Automatically (implicitly) setup recovery codes when another |
| 64 |
authenticator is setup for. As this is part of setting up another (primary) |
| 65 |
authenticator, we do not send a notification email in this case. |
| 66 |
""" |
| 67 |
if not request.user.is_authenticated: |
| 68 |
return None |
| 69 |
if Authenticator.Type.RECOVERY_CODES not in app_settings.SUPPORTED_TYPES: |
| 70 |
return None |
| 71 |
has_rc = Authenticator.objects.filter( |
| 72 |
user=request.user, type=Authenticator.Type.RECOVERY_CODES |
| 73 |
).exists() |
| 74 |
if has_rc: |
| 75 |
return None |
| 76 |
rc = RecoveryCodes.activate(request.user) |
| 77 |
signals.authenticator_added.send( |
| 78 |
sender=Authenticator, |
| 79 |
request=request, |
| 80 |
user=request.user, |
| 81 |
authenticator=rc.instance, |
| 82 |
) |
| 83 |
add_codes_generated_message(request) |
| 84 |
return rc.instance |
| 85 |
|
| 86 |
|
| 87 |
def add_codes_generated_message(request: HttpRequest) -> None: |
| 88 |
adapter = get_account_adapter(request) |
| 89 |
adapter.add_message( |
| 90 |
request, |
| 91 |
messages.WARNING if app_settings.RECOVERY_CODES_SHOW_ONCE else messages.SUCCESS, |
| 92 |
"mfa/messages/recovery_codes_generated.txt", |
| 93 |
message_context={ |
| 94 |
"MFA_RECOVERY_CODES_SHOW_ONCE": app_settings.RECOVERY_CODES_SHOW_ONCE |
| 95 |
}, |
| 96 |
) |