| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import Any |
| 4 |
|
| 5 |
from django.contrib.auth.decorators import login_required |
| 6 |
from django.core.exceptions import PermissionDenied |
| 7 |
from django.http import Http404, HttpRequest, HttpResponse, HttpResponseBase |
| 8 |
from django.urls import reverse_lazy |
| 9 |
from django.utils.decorators import method_decorator |
| 10 |
from django.views.decorators.cache import never_cache |
| 11 |
from django.views.generic import TemplateView |
| 12 |
from django.views.generic.edit import FormView |
| 13 |
|
| 14 |
from allauth.account import app_settings as account_settings |
| 15 |
from allauth.account.decorators import reauthentication_required |
| 16 |
from allauth.core.internal.httpkit import authenticated_user |
| 17 |
from allauth.mfa import app_settings |
| 18 |
from allauth.mfa.models import Authenticator |
| 19 |
from allauth.mfa.recovery_codes.forms import GenerateRecoveryCodesForm |
| 20 |
from allauth.mfa.recovery_codes.internal import flows |
| 21 |
from allauth.utils import get_form_class |
| 22 |
|
| 23 |
|
| 24 |
@method_decorator(reauthentication_required, name="dispatch") |
| 25 |
class GenerateRecoveryCodesView(FormView): |
| 26 |
form_class = GenerateRecoveryCodesForm |
| 27 |
template_name = f"mfa/recovery_codes/generate.{account_settings.TEMPLATE_EXTENSION}" |
| 28 |
success_url = reverse_lazy("mfa_view_recovery_codes") |
| 29 |
|
| 30 |
def form_valid(self, form) -> HttpResponse: |
| 31 |
flows.generate_recovery_codes(self.request) |
| 32 |
return super().form_valid(form) |
| 33 |
|
| 34 |
def get_context_data(self, **kwargs) -> dict: |
| 35 |
ret = super().get_context_data(**kwargs) |
| 36 |
unused_codes = [] |
| 37 |
authenticator = Authenticator.objects.filter( |
| 38 |
user_id=authenticated_user(self.request).pk, |
| 39 |
type=Authenticator.Type.RECOVERY_CODES, |
| 40 |
).first() |
| 41 |
if authenticator: |
| 42 |
unused_codes = authenticator.wrap().get_unused_codes() |
| 43 |
ret["unused_code_count"] = len(unused_codes) |
| 44 |
return ret |
| 45 |
|
| 46 |
def get_form_kwargs(self) -> dict: |
| 47 |
ret = super().get_form_kwargs() |
| 48 |
ret["user"] = self.request.user |
| 49 |
return ret |
| 50 |
|
| 51 |
def get_form_class(self): |
| 52 |
return get_form_class( |
| 53 |
app_settings.FORMS, "generate_recovery_codes", self.form_class |
| 54 |
) |
| 55 |
|
| 56 |
|
| 57 |
generate_recovery_codes = GenerateRecoveryCodesView.as_view() |
| 58 |
|
| 59 |
|
| 60 |
@method_decorator(login_required, name="dispatch") |
| 61 |
@method_decorator(never_cache, name="dispatch") |
| 62 |
class DownloadRecoveryCodesView(TemplateView): |
| 63 |
template_name = "mfa/recovery_codes/download.txt" |
| 64 |
content_type = "text/plain" |
| 65 |
|
| 66 |
def dispatch( |
| 67 |
self, request: HttpRequest, *args: Any, **kwargs: Any |
| 68 |
) -> HttpResponseBase: |
| 69 |
recovery_codes, can_view = flows.view_recovery_codes(self.request) |
| 70 |
if not recovery_codes: |
| 71 |
raise Http404() |
| 72 |
if not can_view: |
| 73 |
raise PermissionDenied |
| 74 |
self.authenticator = recovery_codes.instance |
| 75 |
self.unused_codes = recovery_codes.get_unused_codes() |
| 76 |
if not self.unused_codes: |
| 77 |
raise Http404() |
| 78 |
return super().dispatch(request, *args, **kwargs) |
| 79 |
|
| 80 |
def get_context_data(self, **kwargs) -> dict: |
| 81 |
ret = super().get_context_data(**kwargs) |
| 82 |
ret["unused_codes"] = self.unused_codes |
| 83 |
return ret |
| 84 |
|
| 85 |
def render_to_response(self, context, **response_kwargs): |
| 86 |
response = super().render_to_response(context, **response_kwargs) |
| 87 |
response["Content-Disposition"] = 'attachment; filename="recovery-codes.txt"' |
| 88 |
return response |
| 89 |
|
| 90 |
|
| 91 |
download_recovery_codes = DownloadRecoveryCodesView.as_view() |
| 92 |
|
| 93 |
|
| 94 |
@method_decorator(login_required, name="dispatch") |
| 95 |
class ViewRecoveryCodesView(TemplateView): |
| 96 |
template_name = f"mfa/recovery_codes/index.{account_settings.TEMPLATE_EXTENSION}" |
| 97 |
|
| 98 |
def get_context_data(self, **kwargs) -> dict: |
| 99 |
ret = super().get_context_data(**kwargs) |
| 100 |
recovery_codes, can_view = flows.view_recovery_codes(self.request) |
| 101 |
if not recovery_codes: |
| 102 |
raise Http404() |
| 103 |
unused_codes = recovery_codes.get_unused_codes() |
| 104 |
if not can_view: |
| 105 |
# Prevent accidental leakage into templates at all costs. |
| 106 |
unused_codes = ["****" for _ in unused_codes] |
| 107 |
ret.update( |
| 108 |
{ |
| 109 |
"unused_codes": unused_codes, |
| 110 |
"can_view_codes": can_view, |
| 111 |
"can_download_codes": not app_settings.RECOVERY_CODES_SHOW_ONCE |
| 112 |
and len(unused_codes) > 0, |
| 113 |
"total_count": app_settings.RECOVERY_CODE_COUNT, |
| 114 |
"MFA_RECOVERY_CODES_SHOW_ONCE": app_settings.RECOVERY_CODES_SHOW_ONCE, |
| 115 |
"can_generate_codes": not app_settings.RECOVERY_CODES_SHOW_ONCE |
| 116 |
or not can_view, |
| 117 |
} |
| 118 |
) |
| 119 |
return ret |
| 120 |
|
| 121 |
|
| 122 |
view_recovery_codes = ViewRecoveryCodesView.as_view() |