| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import base64 |
| 4 |
from typing import Any |
| 5 |
|
| 6 |
from django.contrib.auth.decorators import login_required |
| 7 |
from django.http import ( |
| 8 |
HttpRequest, |
| 9 |
HttpResponse, |
| 10 |
HttpResponseBase, |
| 11 |
HttpResponseRedirect, |
| 12 |
) |
| 13 |
from django.shortcuts import get_object_or_404 |
| 14 |
from django.urls import reverse, reverse_lazy |
| 15 |
from django.utils.decorators import method_decorator |
| 16 |
from django.views.generic.edit import FormView |
| 17 |
|
| 18 |
from allauth.account import app_settings as account_settings |
| 19 |
from allauth.account.decorators import reauthentication_required |
| 20 |
from allauth.core.internal.httpkit import authenticated_user |
| 21 |
from allauth.mfa import app_settings |
| 22 |
from allauth.mfa.adapter import get_adapter |
| 23 |
from allauth.mfa.internal.flows.add import redirect_if_add_not_allowed |
| 24 |
from allauth.mfa.models import Authenticator |
| 25 |
from allauth.mfa.totp.forms import ActivateTOTPForm, DeactivateTOTPForm |
| 26 |
from allauth.mfa.totp.internal import flows |
| 27 |
from allauth.mfa.utils import is_mfa_enabled |
| 28 |
from allauth.utils import get_form_class |
| 29 |
|
| 30 |
|
| 31 |
@method_decorator(redirect_if_add_not_allowed, name="dispatch") |
| 32 |
@method_decorator(reauthentication_required, name="dispatch") |
| 33 |
class ActivateTOTPView(FormView): |
| 34 |
form_class = ActivateTOTPForm |
| 35 |
template_name = f"mfa/totp/activate_form.{account_settings.TEMPLATE_EXTENSION}" |
| 36 |
|
| 37 |
def dispatch( |
| 38 |
self, request: HttpRequest, *args: Any, **kwargs: Any |
| 39 |
) -> HttpResponseBase: |
| 40 |
user = authenticated_user(request) |
| 41 |
if is_mfa_enabled(user, [Authenticator.Type.TOTP]): |
| 42 |
return HttpResponseRedirect(reverse("mfa_deactivate_totp")) |
| 43 |
return super().dispatch(request, *args, **kwargs) |
| 44 |
|
| 45 |
def get_context_data(self, **kwargs) -> dict: |
| 46 |
ret = super().get_context_data(**kwargs) |
| 47 |
adapter = get_adapter() |
| 48 |
totp_url = adapter.build_totp_url( |
| 49 |
authenticated_user(self.request), |
| 50 |
ret["form"].secret, |
| 51 |
) |
| 52 |
totp_svg = adapter.build_totp_svg(totp_url) |
| 53 |
base64_data = base64.b64encode(totp_svg.encode("utf8")).decode("utf-8") |
| 54 |
totp_data_uri = f"data:image/svg+xml;base64,{base64_data}" |
| 55 |
ret.update( |
| 56 |
{ |
| 57 |
"totp_svg": totp_svg, |
| 58 |
"totp_svg_data_uri": totp_data_uri, |
| 59 |
"totp_url": totp_url, |
| 60 |
} |
| 61 |
) |
| 62 |
return ret |
| 63 |
|
| 64 |
def get_form_kwargs(self) -> dict: |
| 65 |
ret = super().get_form_kwargs() |
| 66 |
ret["user"] = self.request.user |
| 67 |
return ret |
| 68 |
|
| 69 |
def get_form_class(self): |
| 70 |
return get_form_class(app_settings.FORMS, "activate_totp", self.form_class) |
| 71 |
|
| 72 |
def get_success_url(self) -> str: |
| 73 |
if self.did_generate_recovery_codes: |
| 74 |
return reverse("mfa_view_recovery_codes") |
| 75 |
return reverse("mfa_index") |
| 76 |
|
| 77 |
def form_valid(self, form) -> HttpResponse: |
| 78 |
totp_auth, rc_auth = flows.activate_totp(self.request, form) |
| 79 |
self.did_generate_recovery_codes = bool(rc_auth) |
| 80 |
return super().form_valid(form) |
| 81 |
|
| 82 |
|
| 83 |
activate_totp = ActivateTOTPView.as_view() |
| 84 |
|
| 85 |
|
| 86 |
@method_decorator(login_required, name="dispatch") |
| 87 |
class DeactivateTOTPView(FormView): |
| 88 |
form_class = DeactivateTOTPForm |
| 89 |
template_name = f"mfa/totp/deactivate_form.{account_settings.TEMPLATE_EXTENSION}" |
| 90 |
success_url = reverse_lazy("mfa_index") |
| 91 |
|
| 92 |
def dispatch( |
| 93 |
self, request: HttpRequest, *args: Any, **kwargs: Any |
| 94 |
) -> HttpResponseBase: |
| 95 |
user = self.request.user |
| 96 |
assert user.is_authenticated # nosec |
| 97 |
self.authenticator = get_object_or_404( |
| 98 |
Authenticator, |
| 99 |
user=user, |
| 100 |
type=Authenticator.Type.TOTP, |
| 101 |
) |
| 102 |
if not is_mfa_enabled(user, [Authenticator.Type.TOTP]): |
| 103 |
return HttpResponseRedirect(reverse("mfa_activate_totp")) |
| 104 |
return self._dispatch(request, *args, **kwargs) |
| 105 |
|
| 106 |
@method_decorator(reauthentication_required) |
| 107 |
def _dispatch(self, request: HttpRequest, *args: Any, **kwargs: Any): |
| 108 |
"""There's no point to reauthenticate when MFA is not enabled, so the |
| 109 |
`is_mfa_enabled` check needs to go first, which is why we cannot slap a |
| 110 |
`reauthentication_required` decorator on the `dispatch` directly. |
| 111 |
""" |
| 112 |
return super().dispatch(request, *args, **kwargs) |
| 113 |
|
| 114 |
def get_form_kwargs(self) -> dict: |
| 115 |
ret = super().get_form_kwargs() |
| 116 |
ret["authenticator"] = self.authenticator |
| 117 |
# The deactivation form does not require input, yet, can generate |
| 118 |
# validation errors in case deactivation is not allowed. We want to |
| 119 |
# immediately present such errors even before the user actually posts |
| 120 |
# the form, which is why we put an empty data payload in here. |
| 121 |
ret.setdefault("data", {}) |
| 122 |
return ret |
| 123 |
|
| 124 |
def get_form_class(self): |
| 125 |
return get_form_class(app_settings.FORMS, "deactivate_totp", self.form_class) |
| 126 |
|
| 127 |
def form_valid(self, form) -> HttpResponse: |
| 128 |
flows.deactivate_totp(self.request, self.authenticator) |
| 129 |
return super().form_valid(form) |
| 130 |
|
| 131 |
|
| 132 |
deactivate_totp = DeactivateTOTPView.as_view() |