Debian Codegraph @

allauth/mfa/webauthn/forms.py

raw
1 from __future__ import annotations
2
3 from django import forms
4 from django.utils.translation import gettext_lazy as _
5
6 from allauth.core import context
7 from allauth.mfa import app_settings
8 from allauth.mfa.adapter import get_adapter
9 from allauth.mfa.base.internal.flows import check_rate_limit, post_authentication
10 from allauth.mfa.models import Authenticator
11 from allauth.mfa.webauthn.internal import auth, flows
12
13
14 class _BaseAddWebAuthnForm(forms.Form):
15 name = forms.CharField(required=False)
16 credential = forms.JSONField(required=True, widget=forms.HiddenInput)
17
18 def __init__(self, *args, **kwargs) -> None:
19 self.user = kwargs.pop("user")
20 initial = kwargs.setdefault("initial", {})
21 initial.setdefault(
22 "name",
23 get_adapter().generate_authenticator_name(
24 self.user, Authenticator.Type.WEBAUTHN
25 ),
26 )
27 super().__init__(*args, **kwargs)
28
29 def clean_name(self) -> str:
30 """
31 We don't want to make `name` a required field, as the WebAuthn
32 ceremony happens before posting the resulting credential, and we don't
33 want to reject a valid credential because of a missing name -- it might
34 be resident already. So, gracefully plug in a name.
35 """
36 name = self.cleaned_data["name"]
37 if not name:
38 name = get_adapter().generate_authenticator_name(
39 self.user, Authenticator.Type.WEBAUTHN
40 )
41 return name
42
43 def clean(self) -> dict:
44 cleaned_data = super().clean() or {}
45 credential = cleaned_data.get("credential")
46 if credential:
47 # Explicitly parse JSON payload -- otherwise, register_complete()
48 # crashes with some random TypeError and we don't want to do
49 # Pokemon-style exception handling.
50 auth.parse_registration_response(credential)
51 auth.complete_registration(credential)
52 return cleaned_data
53
54
55 class AddWebAuthnForm(_BaseAddWebAuthnForm):
56 if app_settings.PASSKEY_LOGIN_ENABLED:
57 passwordless = forms.BooleanField(
58 label=_("Passwordless"),
59 required=False,
60 help_text=_(
61 "Enabling passwordless operation allows you to sign in using just this key, but imposes additional requirements such as biometrics or PIN protection."
62 ),
63 )
64
65
66 class SignupWebAuthnForm(_BaseAddWebAuthnForm):
67 pass
68
69
70 class AuthenticateWebAuthnForm(forms.Form):
71 credential = forms.JSONField(required=True, widget=forms.HiddenInput)
72 reauthenticated = False
73 passwordless = False
74
75 def __init__(self, *args, **kwargs) -> None:
76 self.user = kwargs.pop("user")
77 super().__init__(*args, **kwargs)
78
79 def clean_credential(self) -> Authenticator:
80 credential = self.cleaned_data["credential"]
81 # Explicitly parse JSON payload -- otherwise, authenticate_complete()
82 # crashes with some random TypeError and we don't want to do
83 # Pokemon-style exception handling.
84 auth.parse_authentication_response(credential)
85 user = self.user
86 if user is None:
87 user = auth.extract_user_from_response(credential)
88 clear_rl = check_rate_limit(user)
89 authenticator = auth.complete_authentication(user, credential)
90 clear_rl()
91 return authenticator
92
93 def save(self) -> None:
94 authenticator = self.cleaned_data["credential"]
95 post_authentication(
96 context.request,
97 authenticator,
98 reauthenticated=self.reauthenticated,
99 passwordless=self.passwordless,
100 )
101
102
103 class LoginWebAuthnForm(AuthenticateWebAuthnForm):
104 reauthenticated = False
105 passwordless = True
106
107 def __init__(self, *args, **kwargs) -> None:
108 super().__init__(*args, user=None, **kwargs)
109
110
111 class ReauthenticateWebAuthnForm(AuthenticateWebAuthnForm):
112 reauthenticated = True
113 passwordless = False
114
115
116 class EditWebAuthnForm(forms.Form):
117 name = forms.CharField(required=True)
118
119 def __init__(self, *args, **kwargs) -> None:
120 self.instance = kwargs.pop("instance")
121 initial = kwargs.setdefault("initial", {})
122 initial.setdefault("name", self.instance.wrap().name)
123 super().__init__(*args, **kwargs)
124
125 def save(self) -> Authenticator:
126 flows.rename_authenticator(
127 context.request, self.instance, self.cleaned_data["name"]
128 )
129 return self.instance