Debian Codegraph @

allauth/headless/mfa/response.py

raw
1 from __future__ import annotations
2
3 from http import HTTPStatus
4
5 from django.http import HttpRequest
6
7 from allauth.headless.base.response import APIResponse
8 from allauth.mfa import app_settings as mfa_settings
9
10
11 def get_config_data(request: HttpRequest) -> dict:
12 data = {
13 "mfa": {
14 "supported_types": mfa_settings.SUPPORTED_TYPES,
15 "passkey_login_enabled": mfa_settings.PASSKEY_LOGIN_ENABLED,
16 }
17 }
18 return data
19
20
21 def _authenticator_data(authenticator, sensitive: bool = False) -> dict:
22 data = {
23 "type": authenticator.type,
24 "created_at": authenticator.created_at.timestamp(),
25 "last_used_at": (
26 authenticator.last_used_at.timestamp()
27 if authenticator.last_used_at
28 else None
29 ),
30 }
31 if authenticator.type == authenticator.Type.TOTP:
32 pass
33 elif authenticator.type == authenticator.Type.RECOVERY_CODES:
34 wrapped = authenticator.wrap()
35 unused_codes = wrapped.get_unused_codes()
36 data.update(
37 {
38 "total_code_count": len(wrapped.generate_codes()),
39 "unused_code_count": len(unused_codes),
40 }
41 )
42 if sensitive:
43 data["unused_codes"] = unused_codes
44 elif authenticator.type == authenticator.Type.WEBAUTHN:
45 wrapped = authenticator.wrap()
46 data["id"] = authenticator.pk
47 data["name"] = wrapped.name
48 passwordless = wrapped.is_passwordless
49 if passwordless is not None:
50 data["is_passwordless"] = passwordless
51 return data
52
53
54 class AuthenticatorDeletedResponse(APIResponse):
55 pass
56
57
58 class AuthenticatorsDeletedResponse(APIResponse):
59 pass
60
61
62 class TOTPNotFoundResponse(APIResponse):
63 def __init__(self, request: HttpRequest, secret, totp_url) -> None:
64 super().__init__(
65 request,
66 meta={
67 "secret": secret,
68 "totp_url": totp_url,
69 },
70 status=HTTPStatus.NOT_FOUND,
71 )
72
73
74 class TOTPResponse(APIResponse):
75 def __init__(self, request: HttpRequest, authenticator) -> None:
76 data = _authenticator_data(authenticator)
77 super().__init__(request, data=data)
78
79
80 class AuthenticatorsResponse(APIResponse):
81 def __init__(self, request: HttpRequest, authenticators) -> None:
82 data = [_authenticator_data(authenticator) for authenticator in authenticators]
83 super().__init__(request, data=data)
84
85
86 class AuthenticatorResponse(APIResponse):
87 def __init__(self, request: HttpRequest, authenticator, meta=None) -> None:
88 data = _authenticator_data(authenticator)
89 super().__init__(request, data=data, meta=meta)
90
91
92 class RecoveryCodesNotFoundResponse(APIResponse):
93 def __init__(self, request: HttpRequest) -> None:
94 super().__init__(request, status=HTTPStatus.NOT_FOUND)
95
96
97 class RecoveryCodesResponse(APIResponse):
98 def __init__(self, request: HttpRequest, authenticator, can_view: bool) -> None:
99 data = _authenticator_data(authenticator, sensitive=can_view)
100 super().__init__(request, data=data)
101
102
103 class AddWebAuthnResponse(APIResponse):
104 def __init__(self, request: HttpRequest, registration_data) -> None:
105 super().__init__(request, data={"creation_options": registration_data})
106
107
108 class WebAuthnRequestOptionsResponse(APIResponse):
109 def __init__(self, request: HttpRequest, request_options) -> None:
110 super().__init__(request, data={"request_options": request_options})