| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import Any |
| 4 |
|
| 5 |
from django.contrib import messages |
| 6 |
from django.contrib.auth.decorators import login_required |
| 7 |
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect, JsonResponse |
| 8 |
from django.urls import reverse, reverse_lazy |
| 9 |
from django.utils.decorators import method_decorator |
| 10 |
from django.views.generic.edit import DeleteView, FormView, UpdateView |
| 11 |
from django.views.generic.list import ListView |
| 12 |
|
| 13 |
from allauth.account import app_settings as account_settings |
| 14 |
from allauth.account.adapter import get_adapter as get_account_adapter |
| 15 |
from allauth.account.decorators import reauthentication_required |
| 16 |
from allauth.account.internal.decorators import login_stage_required |
| 17 |
from allauth.account.internal.templatekit import get_entrance_context_data |
| 18 |
from allauth.account.mixins import NextRedirectMixin, RedirectAuthenticatedUserMixin |
| 19 |
from allauth.account.models import Login |
| 20 |
from allauth.account.views import BaseReauthenticateView |
| 21 |
from allauth.mfa import app_settings |
| 22 |
from allauth.mfa.internal.flows.add import redirect_if_add_not_allowed |
| 23 |
from allauth.mfa.models import Authenticator |
| 24 |
from allauth.mfa.webauthn.forms import ( |
| 25 |
AddWebAuthnForm, |
| 26 |
EditWebAuthnForm, |
| 27 |
LoginWebAuthnForm, |
| 28 |
ReauthenticateWebAuthnForm, |
| 29 |
SignupWebAuthnForm, |
| 30 |
) |
| 31 |
from allauth.mfa.webauthn.internal import auth, flows |
| 32 |
from allauth.mfa.webauthn.stages import PasskeySignupStage |
| 33 |
from allauth.utils import get_form_class |
| 34 |
|
| 35 |
|
| 36 |
@method_decorator(redirect_if_add_not_allowed, name="dispatch") |
| 37 |
@method_decorator(reauthentication_required, name="dispatch") |
| 38 |
class AddWebAuthnView(FormView): |
| 39 |
form_class = AddWebAuthnForm |
| 40 |
template_name = f"mfa/webauthn/add_form.{account_settings.TEMPLATE_EXTENSION}" |
| 41 |
|
| 42 |
def get_context_data(self, **kwargs) -> dict: |
| 43 |
ret = super().get_context_data() |
| 44 |
user = self.request.user |
| 45 |
assert user.is_authenticated # nosec |
| 46 |
creation_options = auth.begin_registration(user, False) |
| 47 |
ret["js_data"] = {"creation_options": creation_options} |
| 48 |
return ret |
| 49 |
|
| 50 |
def get_form_class(self): |
| 51 |
return get_form_class(app_settings.FORMS, "add_webauthn", self.form_class) |
| 52 |
|
| 53 |
def get_form_kwargs(self) -> dict: |
| 54 |
ret = super().get_form_kwargs() |
| 55 |
ret["user"] = self.request.user |
| 56 |
return ret |
| 57 |
|
| 58 |
def get_success_url(self) -> str: |
| 59 |
if self.did_generate_recovery_codes: |
| 60 |
return reverse("mfa_view_recovery_codes") |
| 61 |
return reverse("mfa_index") |
| 62 |
|
| 63 |
def form_valid(self, form) -> HttpResponse: |
| 64 |
auth, rc_auth = flows.add_authenticator( |
| 65 |
self.request, |
| 66 |
name=form.cleaned_data["name"], |
| 67 |
credential=form.cleaned_data["credential"], |
| 68 |
) |
| 69 |
self.did_generate_recovery_codes = bool(rc_auth) |
| 70 |
return super().form_valid(form) |
| 71 |
|
| 72 |
|
| 73 |
add_webauthn = AddWebAuthnView.as_view() |
| 74 |
|
| 75 |
|
| 76 |
@method_decorator(login_required, name="dispatch") |
| 77 |
class ListWebAuthnView(ListView): |
| 78 |
template_name = ( |
| 79 |
f"mfa/webauthn/authenticator_list.{account_settings.TEMPLATE_EXTENSION}" |
| 80 |
) |
| 81 |
context_object_name = "authenticators" |
| 82 |
|
| 83 |
def get_queryset(self): |
| 84 |
return Authenticator.objects.filter( |
| 85 |
user=self.request.user, type=Authenticator.Type.WEBAUTHN |
| 86 |
) |
| 87 |
|
| 88 |
|
| 89 |
list_webauthn = ListWebAuthnView.as_view() |
| 90 |
|
| 91 |
|
| 92 |
@method_decorator(reauthentication_required, name="dispatch") |
| 93 |
class RemoveWebAuthnView(NextRedirectMixin, DeleteView): |
| 94 |
object: Authenticator # https://github.com/typeddjango/django-stubs/issues/1227 |
| 95 |
template_name = ( |
| 96 |
"mfa/webauthn/authenticator_confirm_delete." |
| 97 |
+ account_settings.TEMPLATE_EXTENSION |
| 98 |
) |
| 99 |
success_url = reverse_lazy("mfa_list_webauthn") |
| 100 |
|
| 101 |
def get_queryset(self): |
| 102 |
return Authenticator.objects.filter( |
| 103 |
user=self.request.user, type=Authenticator.Type.WEBAUTHN |
| 104 |
) |
| 105 |
|
| 106 |
def form_valid(self, form) -> HttpResponse: |
| 107 |
authenticator = self.get_object() |
| 108 |
flows.remove_authenticator(self.request, authenticator) |
| 109 |
return HttpResponseRedirect(self.get_success_url()) |
| 110 |
|
| 111 |
|
| 112 |
remove_webauthn = RemoveWebAuthnView.as_view() |
| 113 |
|
| 114 |
|
| 115 |
class LoginWebAuthnView(RedirectAuthenticatedUserMixin, FormView): |
| 116 |
form_class = LoginWebAuthnForm |
| 117 |
|
| 118 |
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 119 |
if get_account_adapter().is_ajax(request): |
| 120 |
request_options = auth.begin_authentication(user=None) |
| 121 |
data = {"request_options": request_options} |
| 122 |
return JsonResponse(data) |
| 123 |
return HttpResponseRedirect(reverse("account_login")) |
| 124 |
|
| 125 |
def get_form_class(self): |
| 126 |
return get_form_class(app_settings.FORMS, "login_webauthn", self.form_class) |
| 127 |
|
| 128 |
def form_invalid(self, form) -> HttpResponse: |
| 129 |
for message in form.errors.get("credential", []): |
| 130 |
get_account_adapter().add_message( |
| 131 |
self.request, messages.ERROR, message=message |
| 132 |
) |
| 133 |
return HttpResponseRedirect(reverse("account_login")) |
| 134 |
|
| 135 |
def form_valid(self, form) -> HttpResponse: |
| 136 |
authenticator = form.cleaned_data["credential"] |
| 137 |
redirect_url = None |
| 138 |
login = Login(user=authenticator.user, redirect_url=redirect_url) |
| 139 |
return flows.perform_passwordless_login(self.request, authenticator, login) |
| 140 |
|
| 141 |
def get_context_data(self, **kwargs) -> dict: |
| 142 |
ret = super().get_context_data(**kwargs) |
| 143 |
ret.update(get_entrance_context_data(self.request)) |
| 144 |
return ret |
| 145 |
|
| 146 |
|
| 147 |
login_webauthn = LoginWebAuthnView.as_view() |
| 148 |
|
| 149 |
|
| 150 |
@method_decorator(login_required, name="dispatch") |
| 151 |
class ReauthenticateWebAuthnView(BaseReauthenticateView): |
| 152 |
form_class = ReauthenticateWebAuthnForm |
| 153 |
template_name = f"mfa/webauthn/reauthenticate.{account_settings.TEMPLATE_EXTENSION}" |
| 154 |
|
| 155 |
def get_form_class(self): |
| 156 |
return get_form_class( |
| 157 |
app_settings.FORMS, "reauthenticate_webauthn", self.form_class |
| 158 |
) |
| 159 |
|
| 160 |
def get_form_kwargs(self) -> dict: |
| 161 |
ret = super().get_form_kwargs() |
| 162 |
ret["user"] = self.request.user |
| 163 |
return ret |
| 164 |
|
| 165 |
def form_invalid(self, form) -> HttpResponse: |
| 166 |
for message in form.errors.get("credential", []): |
| 167 |
get_account_adapter().add_message( |
| 168 |
self.request, messages.ERROR, message=message |
| 169 |
) |
| 170 |
return HttpResponseRedirect(reverse("account_login")) |
| 171 |
|
| 172 |
def form_valid(self, form) -> HttpResponse: |
| 173 |
authenticator = form.cleaned_data["credential"] |
| 174 |
flows.reauthenticate(self.request, authenticator) |
| 175 |
return super().form_valid(form) |
| 176 |
|
| 177 |
def get_context_data(self, **kwargs) -> dict: |
| 178 |
ret = super().get_context_data() |
| 179 |
user = self.request.user |
| 180 |
assert user.is_authenticated # nosec |
| 181 |
request_options = auth.begin_authentication(user) |
| 182 |
ret["js_data"] = {"request_options": request_options} |
| 183 |
return ret |
| 184 |
|
| 185 |
|
| 186 |
reauthenticate_webauthn = ReauthenticateWebAuthnView.as_view() |
| 187 |
|
| 188 |
|
| 189 |
@method_decorator(reauthentication_required, name="dispatch") |
| 190 |
class EditWebAuthnView(NextRedirectMixin, UpdateView): |
| 191 |
form_class = EditWebAuthnForm |
| 192 |
template_name = f"mfa/webauthn/edit_form.{account_settings.TEMPLATE_EXTENSION}" |
| 193 |
success_url = reverse_lazy("mfa_list_webauthn") |
| 194 |
|
| 195 |
def get_form_class(self): |
| 196 |
return get_form_class(app_settings.FORMS, "edit_webauthn", self.form_class) |
| 197 |
|
| 198 |
def get_queryset(self): |
| 199 |
return Authenticator.objects.filter( |
| 200 |
user=self.request.user, type=Authenticator.Type.WEBAUTHN |
| 201 |
) |
| 202 |
|
| 203 |
|
| 204 |
edit_webauthn = EditWebAuthnView.as_view() |
| 205 |
|
| 206 |
|
| 207 |
@method_decorator( |
| 208 |
login_stage_required( |
| 209 |
stage=PasskeySignupStage.key, redirect_urlname="account_signup" |
| 210 |
), |
| 211 |
name="dispatch", |
| 212 |
) |
| 213 |
class SignupWebAuthnView(FormView): |
| 214 |
form_class = SignupWebAuthnForm |
| 215 |
template_name = f"mfa/webauthn/signup_form.{account_settings.TEMPLATE_EXTENSION}" |
| 216 |
|
| 217 |
@property |
| 218 |
def _login_stage(self): |
| 219 |
return self.request._login_stage |
| 220 |
|
| 221 |
def get_context_data(self, **kwargs) -> dict: |
| 222 |
ret = super().get_context_data() |
| 223 |
ret.update(get_entrance_context_data(self.request)) |
| 224 |
stage = self._login_stage |
| 225 |
creation_options = auth.begin_registration(stage.login.user, True) |
| 226 |
ret["js_data"] = {"creation_options": creation_options} |
| 227 |
return ret |
| 228 |
|
| 229 |
def get_form_class(self): |
| 230 |
return get_form_class(app_settings.FORMS, "signup_webauthn", self.form_class) |
| 231 |
|
| 232 |
def get_form_kwargs(self) -> dict: |
| 233 |
ret = super().get_form_kwargs() |
| 234 |
stage = self._login_stage |
| 235 |
ret["user"] = stage.login.user |
| 236 |
return ret |
| 237 |
|
| 238 |
def form_valid(self, form) -> HttpResponse: |
| 239 |
stage = self._login_stage |
| 240 |
flows.signup_authenticator( |
| 241 |
self.request, |
| 242 |
user=stage.login.user, |
| 243 |
name=form.cleaned_data["name"], |
| 244 |
credential=form.cleaned_data["credential"], |
| 245 |
) |
| 246 |
return stage.exit() |
| 247 |
|
| 248 |
|
| 249 |
signup_webauthn = SignupWebAuthnView.as_view() |