| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import Any |
| 4 |
|
| 5 |
from django.contrib.auth.decorators import login_required |
| 6 |
from django.forms import Form |
| 7 |
from django.http import ( |
| 8 |
HttpRequest, |
| 9 |
HttpResponse, |
| 10 |
HttpResponseBase, |
| 11 |
HttpResponseRedirect, |
| 12 |
) |
| 13 |
from django.urls import reverse |
| 14 |
from django.utils import timezone |
| 15 |
from django.utils.decorators import method_decorator |
| 16 |
from django.views.generic import TemplateView |
| 17 |
from django.views.generic.edit import FormView |
| 18 |
|
| 19 |
from allauth.account import app_settings as account_settings |
| 20 |
from allauth.account.internal.decorators import login_stage_required |
| 21 |
from allauth.account.internal.templatekit import get_entrance_context_data |
| 22 |
from allauth.account.views import BaseReauthenticateView |
| 23 |
from allauth.core.internal.httpkit import authenticated_user |
| 24 |
from allauth.mfa import app_settings |
| 25 |
from allauth.mfa.base.forms import AuthenticateForm, ReauthenticateForm |
| 26 |
from allauth.mfa.internal.flows import trust as trust_ |
| 27 |
from allauth.mfa.models import Authenticator |
| 28 |
from allauth.mfa.stages import AuthenticateStage, TrustStage |
| 29 |
from allauth.mfa.utils import is_mfa_enabled |
| 30 |
from allauth.mfa.webauthn.forms import AuthenticateWebAuthnForm |
| 31 |
from allauth.mfa.webauthn.internal.flows import auth as webauthn_auth |
| 32 |
from allauth.utils import get_form_class |
| 33 |
|
| 34 |
|
| 35 |
@method_decorator( |
| 36 |
login_stage_required(stage=AuthenticateStage.key, redirect_urlname="account_login"), |
| 37 |
name="dispatch", |
| 38 |
) |
| 39 |
class AuthenticateView(TemplateView): |
| 40 |
form_class = AuthenticateForm |
| 41 |
webauthn_form_class = AuthenticateWebAuthnForm |
| 42 |
template_name = f"mfa/authenticate.{account_settings.TEMPLATE_EXTENSION}" |
| 43 |
|
| 44 |
def dispatch( |
| 45 |
self, request: HttpRequest, *args: Any, **kwargs: Any |
| 46 |
) -> HttpResponseBase: |
| 47 |
self.stage = request._login_stage # type:ignore[attr-defined] |
| 48 |
if not is_mfa_enabled( |
| 49 |
self.stage.login.user, |
| 50 |
[Authenticator.Type.TOTP, Authenticator.Type.WEBAUTHN], |
| 51 |
): |
| 52 |
return HttpResponseRedirect(reverse("account_login")) |
| 53 |
self.form = self._build_forms() |
| 54 |
return super().dispatch(request, *args, **kwargs) |
| 55 |
|
| 56 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 57 |
if self.form.is_valid(): |
| 58 |
return self.form_valid(self.form) |
| 59 |
else: |
| 60 |
return self.form_invalid(self.form) |
| 61 |
|
| 62 |
def _build_forms(self): |
| 63 |
posted_form = None |
| 64 |
AuthenticateFormClass = self.get_form_class() |
| 65 |
AuthenticateWebAuthnFormClass = self.get_webauthn_form_class() |
| 66 |
user = self.stage.login.user |
| 67 |
support_webauthn = "webauthn" in app_settings.SUPPORTED_TYPES |
| 68 |
if self.request.method == "POST": |
| 69 |
if "code" in self.request.POST: |
| 70 |
posted_form = self.auth_form = AuthenticateFormClass( |
| 71 |
user=user, data=self.request.POST |
| 72 |
) |
| 73 |
self.webauthn_form = ( |
| 74 |
AuthenticateWebAuthnFormClass(user=user) |
| 75 |
if support_webauthn |
| 76 |
else None |
| 77 |
) |
| 78 |
else: |
| 79 |
self.auth_form = ( |
| 80 |
AuthenticateFormClass(user=user) if support_webauthn else None |
| 81 |
) |
| 82 |
posted_form = self.webauthn_form = AuthenticateWebAuthnFormClass( |
| 83 |
user=user, data=self.request.POST |
| 84 |
) |
| 85 |
else: |
| 86 |
self.auth_form = AuthenticateFormClass(user=user) |
| 87 |
self.webauthn_form = ( |
| 88 |
AuthenticateWebAuthnFormClass(user=user) if support_webauthn else None |
| 89 |
) |
| 90 |
return posted_form |
| 91 |
|
| 92 |
def get_form_class(self): |
| 93 |
return get_form_class(app_settings.FORMS, "authenticate", self.form_class) |
| 94 |
|
| 95 |
def get_webauthn_form_class(self): |
| 96 |
return get_form_class( |
| 97 |
app_settings.FORMS, "authenticate_webauthn", self.webauthn_form_class |
| 98 |
) |
| 99 |
|
| 100 |
def form_valid(self, form) -> HttpResponse: |
| 101 |
form.save() |
| 102 |
return self.stage.exit() |
| 103 |
|
| 104 |
def form_invalid(self, form) -> HttpResponse: |
| 105 |
return super().get(self.request) |
| 106 |
|
| 107 |
def get_context_data(self, **kwargs) -> dict: |
| 108 |
ret = super().get_context_data() |
| 109 |
ret.update(get_entrance_context_data(self.request)) |
| 110 |
ret.update( |
| 111 |
{ |
| 112 |
"form": self.auth_form, |
| 113 |
"MFA_SUPPORTED_TYPES": app_settings.SUPPORTED_TYPES, |
| 114 |
} |
| 115 |
) |
| 116 |
if self.webauthn_form: |
| 117 |
request_options = webauthn_auth.begin_authentication(self.stage.login.user) |
| 118 |
ret.update( |
| 119 |
{ |
| 120 |
"webauthn_form": self.webauthn_form, |
| 121 |
"js_data": {"request_options": request_options}, |
| 122 |
} |
| 123 |
) |
| 124 |
return ret |
| 125 |
|
| 126 |
|
| 127 |
authenticate = AuthenticateView.as_view() |
| 128 |
|
| 129 |
|
| 130 |
@method_decorator(login_required, name="dispatch") |
| 131 |
class ReauthenticateView(BaseReauthenticateView): |
| 132 |
form_class = ReauthenticateForm |
| 133 |
template_name = f"mfa/reauthenticate.{account_settings.TEMPLATE_EXTENSION}" |
| 134 |
|
| 135 |
def get_form_kwargs(self) -> dict: |
| 136 |
ret = super().get_form_kwargs() |
| 137 |
ret["user"] = self.request.user |
| 138 |
return ret |
| 139 |
|
| 140 |
def get_form_class(self): |
| 141 |
return get_form_class(app_settings.FORMS, "reauthenticate", self.form_class) |
| 142 |
|
| 143 |
def form_valid(self, form) -> HttpResponse: |
| 144 |
form.save() |
| 145 |
return super().form_valid(form) |
| 146 |
|
| 147 |
|
| 148 |
reauthenticate = ReauthenticateView.as_view() |
| 149 |
|
| 150 |
|
| 151 |
@method_decorator(login_required, name="dispatch") |
| 152 |
class IndexView(TemplateView): |
| 153 |
template_name = f"mfa/index.{account_settings.TEMPLATE_EXTENSION}" |
| 154 |
|
| 155 |
def get_context_data(self, **kwargs) -> dict: |
| 156 |
ret = super().get_context_data(**kwargs) |
| 157 |
authenticators: dict = {} |
| 158 |
user = authenticated_user(self.request) |
| 159 |
for auth in Authenticator.objects.filter(user_id=user.pk): |
| 160 |
if auth.type == Authenticator.Type.WEBAUTHN: |
| 161 |
auths = authenticators.setdefault(auth.type, []) |
| 162 |
auths.append(auth.wrap()) |
| 163 |
else: |
| 164 |
authenticators[auth.type] = auth.wrap() |
| 165 |
ret["authenticators"] = authenticators |
| 166 |
ret["MFA_SUPPORTED_TYPES"] = app_settings.SUPPORTED_TYPES |
| 167 |
ret["MFA_RECOVERY_CODES_SHOW_ONCE"] = app_settings.RECOVERY_CODES_SHOW_ONCE |
| 168 |
ret["is_mfa_enabled"] = is_mfa_enabled(authenticated_user(self.request)) |
| 169 |
return ret |
| 170 |
|
| 171 |
|
| 172 |
index = IndexView.as_view() |
| 173 |
|
| 174 |
|
| 175 |
@method_decorator( |
| 176 |
login_stage_required(stage=TrustStage.key, redirect_urlname="account_login"), |
| 177 |
name="dispatch", |
| 178 |
) |
| 179 |
class TrustView(FormView): |
| 180 |
form_class = Form |
| 181 |
template_name = f"mfa/trust.{account_settings.TEMPLATE_EXTENSION}" |
| 182 |
|
| 183 |
def form_valid(self, form) -> HttpResponse: |
| 184 |
do_trust = self.request.POST.get("action") == "trust" |
| 185 |
stage = self.request._login_stage # type:ignore[attr-defined] |
| 186 |
response = stage.exit() |
| 187 |
if do_trust: |
| 188 |
trust_.trust_browser(self.request, stage.login.user, response) |
| 189 |
return response |
| 190 |
|
| 191 |
def get_context_data(self, **kwargs) -> dict: |
| 192 |
ret = super().get_context_data(**kwargs) |
| 193 |
ret.update(get_entrance_context_data(self.request)) |
| 194 |
now = timezone.now() |
| 195 |
ret["trust_from"] = now |
| 196 |
ret["trust_until"] = now + app_settings.TRUST_COOKIE_AGE |
| 197 |
return ret |
| 198 |
|
| 199 |
|
| 200 |
trust = TrustView.as_view() |