Debian Codegraph @

allauth/mfa/webauthn/internal/flows.py

raw
1 from __future__ import annotations
2
3 from collections.abc import Iterable
4
5 from django.contrib import messages
6 from django.http import HttpRequest, HttpResponse
7
8 from allauth.account.adapter import get_adapter as get_account_adapter
9 from allauth.account.authentication import get_authentication_records
10 from allauth.account.internal import flows
11 from allauth.account.internal.flows.reauthentication import (
12 raise_if_reauthentication_required,
13 )
14 from allauth.account.models import Login
15 from allauth.core.internal.httpkit import authenticated_user
16 from allauth.mfa import signals
17 from allauth.mfa.base.internal.flows import delete_and_cleanup, post_authentication
18 from allauth.mfa.models import Authenticator
19 from allauth.mfa.recovery_codes.internal.flows import auto_generate_recovery_codes
20 from allauth.mfa.webauthn.internal import auth
21
22
23 def begin_registration(
24 request: HttpRequest, user, passwordless: bool, signup: bool = False
25 ) -> dict:
26 if not signup:
27 raise_if_reauthentication_required(request)
28 creation_options = auth.begin_registration(user, passwordless)
29 return creation_options
30
31
32 def signup_authenticator(
33 request: HttpRequest, user, name: str, credential: dict
34 ) -> Authenticator:
35 authenticator, rc_authenticator = _signup_or_add_authenticator(
36 request, user, name, credential, signup=True
37 )
38 return authenticator
39
40
41 def add_authenticator(
42 request: HttpRequest, name: str, credential: dict
43 ) -> tuple[Authenticator, Authenticator | None]:
44 raise_if_reauthentication_required(request)
45 return _signup_or_add_authenticator(
46 request,
47 user=request.user,
48 name=name,
49 credential=credential,
50 signup=False,
51 )
52
53
54 def _signup_or_add_authenticator(
55 request: HttpRequest,
56 user,
57 name: str,
58 credential: dict,
59 signup: bool = False,
60 ) -> tuple[Authenticator, Authenticator | None]:
61 authenticator = auth.WebAuthn.add(
62 user,
63 name,
64 credential,
65 ).instance
66 signals.authenticator_added.send(
67 sender=Authenticator,
68 request=request,
69 user=user,
70 authenticator=authenticator,
71 )
72 adapter = get_account_adapter(request)
73 adapter.add_message(request, messages.SUCCESS, "mfa/messages/webauthn_added.txt")
74 if not signup:
75 adapter.send_notification_mail("mfa/email/webauthn_added", user)
76 rc_authenticator = None
77 if not signup:
78 rc_authenticator = auto_generate_recovery_codes(request)
79 return authenticator, rc_authenticator
80
81
82 def remove_authenticators(
83 request: HttpRequest, authenticators: Iterable[Authenticator]
84 ) -> None:
85 raise_if_reauthentication_required(request)
86 for authenticator in authenticators:
87 remove_authenticator(request, authenticator)
88
89
90 def remove_authenticator(request: HttpRequest, authenticator: Authenticator) -> None:
91 raise_if_reauthentication_required(request)
92 delete_and_cleanup(request, authenticator)
93 adapter = get_account_adapter(request)
94 adapter.add_message(request, messages.SUCCESS, "mfa/messages/webauthn_removed.txt")
95 adapter.send_notification_mail(
96 "mfa/email/webauthn_removed", authenticated_user(request)
97 )
98
99
100 def perform_passwordless_login(
101 request: HttpRequest, authenticator: Authenticator, login: Login
102 ) -> HttpResponse:
103 post_authentication(request, authenticator, passwordless=True)
104 return flows.login.perform_login(request, login)
105
106
107 def did_use_passwordless_login(request: HttpRequest) -> bool:
108 records = get_authentication_records(request)
109 return any(
110 (record.get("method"), record.get("type"), record.get("passwordless"))
111 == ("mfa", "webauthn", True)
112 for record in records
113 )
114
115
116 def reauthenticate(request: HttpRequest, authenticator: Authenticator) -> None:
117 post_authentication(request, authenticator, reauthenticated=True)
118
119
120 def rename_authenticator(
121 request: HttpRequest, authenticator: Authenticator, name: str
122 ) -> None:
123 raise_if_reauthentication_required(request)
124 wrapper = authenticator.wrap()
125 wrapper.name = name
126 authenticator.save()