| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from http import HTTPStatus |
| 4 |
from typing import Any |
| 5 |
|
| 6 |
from django.core.exceptions import ValidationError |
| 7 |
from django.http import HttpRequest, HttpResponse, HttpResponseBase |
| 8 |
|
| 9 |
from allauth.account.internal.stagekit import get_pending_stage |
| 10 |
from allauth.account.models import Login |
| 11 |
from allauth.core.internal.httpkit import authenticated_user |
| 12 |
from allauth.headless.account.views import SignupView |
| 13 |
from allauth.headless.base.response import AuthenticationResponse, ConflictResponse |
| 14 |
from allauth.headless.base.views import ( |
| 15 |
APIView, |
| 16 |
AuthenticatedAPIView, |
| 17 |
AuthenticationStageAPIView, |
| 18 |
) |
| 19 |
from allauth.headless.internal.restkit.response import ErrorResponse |
| 20 |
from allauth.headless.mfa import response |
| 21 |
from allauth.headless.mfa.inputs import ( |
| 22 |
ActivateTOTPInput, |
| 23 |
AddWebAuthnInput, |
| 24 |
AuthenticateInput, |
| 25 |
AuthenticateWebAuthnInput, |
| 26 |
CreateWebAuthnInput, |
| 27 |
DeleteWebAuthnInput, |
| 28 |
GenerateRecoveryCodesInput, |
| 29 |
LoginWebAuthnInput, |
| 30 |
ReauthenticateWebAuthnInput, |
| 31 |
SignupWebAuthnInput, |
| 32 |
TrustInput, |
| 33 |
UpdateWebAuthnInput, |
| 34 |
) |
| 35 |
from allauth.mfa.adapter import DefaultMFAAdapter, get_adapter |
| 36 |
from allauth.mfa.internal.flows import add |
| 37 |
from allauth.mfa.internal.flows.trust import trust_browser |
| 38 |
from allauth.mfa.models import Authenticator |
| 39 |
from allauth.mfa.recovery_codes.internal import flows as recovery_codes_flows |
| 40 |
from allauth.mfa.stages import AuthenticateStage, TrustStage |
| 41 |
from allauth.mfa.totp.internal import auth as totp_auth, flows as totp_flows |
| 42 |
from allauth.mfa.webauthn.internal import auth as webauthn_auth, flows as webauthn_flows |
| 43 |
from allauth.mfa.webauthn.stages import PasskeySignupStage |
| 44 |
|
| 45 |
|
| 46 |
def _validate_can_add_authenticator(request: HttpRequest) -> ErrorResponse | None: |
| 47 |
assert request.user.is_authenticated # nosec |
| 48 |
try: |
| 49 |
add.validate_can_add_authenticator(request.user) |
| 50 |
except ValidationError as e: |
| 51 |
return ErrorResponse(request, status=HTTPStatus.CONFLICT, exception=e) |
| 52 |
return None |
| 53 |
|
| 54 |
|
| 55 |
class AuthenticateView(AuthenticationStageAPIView): |
| 56 |
input_class = AuthenticateInput |
| 57 |
stage_class = AuthenticateStage |
| 58 |
input: AuthenticateInput |
| 59 |
|
| 60 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 61 |
self.input.save() |
| 62 |
return self.respond_next_stage() |
| 63 |
|
| 64 |
def get_input_kwargs(self) -> dict: |
| 65 |
return {"user": self.stage.login.user} |
| 66 |
|
| 67 |
|
| 68 |
class ReauthenticateView(AuthenticatedAPIView): |
| 69 |
input_class = AuthenticateInput |
| 70 |
input: AuthenticateInput |
| 71 |
|
| 72 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 73 |
self.input.save() |
| 74 |
return AuthenticationResponse(self.request) |
| 75 |
|
| 76 |
def get_input_kwargs(self) -> dict: |
| 77 |
return {"user": self.request.user} |
| 78 |
|
| 79 |
|
| 80 |
class AuthenticatorsView(AuthenticatedAPIView): |
| 81 |
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 82 |
assert request.user.is_authenticated # nosec |
| 83 |
authenticators = Authenticator.objects.filter(user=request.user) |
| 84 |
return response.AuthenticatorsResponse(request, authenticators) |
| 85 |
|
| 86 |
|
| 87 |
class ManageTOTPView(AuthenticatedAPIView): |
| 88 |
input_class = {"POST": ActivateTOTPInput} |
| 89 |
|
| 90 |
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 91 |
authenticator = self._get_authenticator() |
| 92 |
if not authenticator: |
| 93 |
err = _validate_can_add_authenticator(request) |
| 94 |
if err: |
| 95 |
return err |
| 96 |
adapter: DefaultMFAAdapter = get_adapter() |
| 97 |
secret = totp_auth.get_totp_secret(regenerate=True) |
| 98 |
totp_url: str = adapter.build_totp_url(authenticated_user(request), secret) |
| 99 |
return response.TOTPNotFoundResponse(request, secret, totp_url) |
| 100 |
return response.TOTPResponse(request, authenticator) |
| 101 |
|
| 102 |
def _get_authenticator(self) -> Authenticator | None: |
| 103 |
user = self.request.user |
| 104 |
if not user.is_authenticated: |
| 105 |
return None |
| 106 |
return Authenticator.objects.filter( |
| 107 |
type=Authenticator.Type.TOTP, user=user |
| 108 |
).first() |
| 109 |
|
| 110 |
def get_input_kwargs(self) -> dict: |
| 111 |
return {"user": self.request.user} |
| 112 |
|
| 113 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 114 |
authenticator = totp_flows.activate_totp(request, self.input)[0] |
| 115 |
return response.TOTPResponse(request, authenticator) |
| 116 |
|
| 117 |
def delete(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 118 |
authenticator = self._get_authenticator() |
| 119 |
if authenticator: |
| 120 |
totp_flows.deactivate_totp(request, authenticator) |
| 121 |
return response.AuthenticatorDeletedResponse(request) |
| 122 |
|
| 123 |
|
| 124 |
class ManageRecoveryCodesView(AuthenticatedAPIView): |
| 125 |
input_class = GenerateRecoveryCodesInput |
| 126 |
|
| 127 |
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 128 |
recovery_codes, can_view = recovery_codes_flows.view_recovery_codes(request) |
| 129 |
if not recovery_codes: |
| 130 |
return response.RecoveryCodesNotFoundResponse(request) |
| 131 |
return response.RecoveryCodesResponse( |
| 132 |
request, recovery_codes.instance, can_view=can_view |
| 133 |
) |
| 134 |
|
| 135 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 136 |
authenticator = recovery_codes_flows.generate_recovery_codes(request) |
| 137 |
authenticator.wrap().mark_as_viewed() |
| 138 |
return response.RecoveryCodesResponse(request, authenticator, can_view=True) |
| 139 |
|
| 140 |
def get_input_kwargs(self) -> dict: |
| 141 |
return {"user": self.request.user} |
| 142 |
|
| 143 |
|
| 144 |
class ManageWebAuthnView(AuthenticatedAPIView): |
| 145 |
input_class = { |
| 146 |
"POST": AddWebAuthnInput, |
| 147 |
"PUT": UpdateWebAuthnInput, |
| 148 |
"DELETE": DeleteWebAuthnInput, |
| 149 |
} |
| 150 |
|
| 151 |
def handle( |
| 152 |
self, request: HttpRequest, *args: Any, **kwargs: Any |
| 153 |
) -> HttpResponseBase: |
| 154 |
if request.method in ["GET", "POST"]: |
| 155 |
err = _validate_can_add_authenticator(request) |
| 156 |
if err: |
| 157 |
return err |
| 158 |
return super().handle(request, *args, **kwargs) |
| 159 |
|
| 160 |
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 161 |
passwordless = "passwordless" in request.GET |
| 162 |
creation_options = webauthn_flows.begin_registration( |
| 163 |
request, request.user, passwordless |
| 164 |
) |
| 165 |
return response.AddWebAuthnResponse(request, creation_options) |
| 166 |
|
| 167 |
def get_input_kwargs(self) -> dict: |
| 168 |
return {"user": self.request.user} |
| 169 |
|
| 170 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 171 |
auth, rc_auth = webauthn_flows.add_authenticator( |
| 172 |
request, |
| 173 |
name=self.input.cleaned_data["name"], |
| 174 |
credential=self.input.cleaned_data["credential"], |
| 175 |
) |
| 176 |
did_generate_recovery_codes = bool(rc_auth) |
| 177 |
return response.AuthenticatorResponse( |
| 178 |
request, |
| 179 |
auth, |
| 180 |
meta={"recovery_codes_generated": did_generate_recovery_codes}, |
| 181 |
) |
| 182 |
|
| 183 |
def put(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 184 |
authenticator = self.input.cleaned_data["id"] |
| 185 |
webauthn_flows.rename_authenticator( |
| 186 |
request, authenticator, self.input.cleaned_data["name"] |
| 187 |
) |
| 188 |
return response.AuthenticatorResponse(request, authenticator) |
| 189 |
|
| 190 |
def delete(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 191 |
authenticators = self.input.cleaned_data["authenticators"] |
| 192 |
webauthn_flows.remove_authenticators(request, authenticators) |
| 193 |
return response.AuthenticatorsDeletedResponse(request) |
| 194 |
|
| 195 |
|
| 196 |
class ReauthenticateWebAuthnView(AuthenticatedAPIView): |
| 197 |
input_class = { |
| 198 |
"POST": ReauthenticateWebAuthnInput, |
| 199 |
} |
| 200 |
|
| 201 |
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 202 |
request_options = webauthn_auth.begin_authentication( |
| 203 |
authenticated_user(request) |
| 204 |
) |
| 205 |
return response.WebAuthnRequestOptionsResponse(request, request_options) |
| 206 |
|
| 207 |
def get_input_kwargs(self) -> dict: |
| 208 |
return {"user": self.request.user} |
| 209 |
|
| 210 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 211 |
authenticator = self.input.cleaned_data["credential"] |
| 212 |
webauthn_flows.reauthenticate(request, authenticator) |
| 213 |
return AuthenticationResponse(self.request) |
| 214 |
|
| 215 |
|
| 216 |
class AuthenticateWebAuthnView(AuthenticationStageAPIView): |
| 217 |
input_class = { |
| 218 |
"POST": AuthenticateWebAuthnInput, |
| 219 |
} |
| 220 |
stage_class = AuthenticateStage |
| 221 |
input: AuthenticateWebAuthnInput |
| 222 |
|
| 223 |
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 224 |
request_options = webauthn_auth.begin_authentication(self.stage.login.user) |
| 225 |
return response.WebAuthnRequestOptionsResponse(request, request_options) |
| 226 |
|
| 227 |
def get_input_kwargs(self) -> dict: |
| 228 |
return {"user": self.stage.login.user} |
| 229 |
|
| 230 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 231 |
self.input.save() |
| 232 |
return self.respond_next_stage() |
| 233 |
|
| 234 |
|
| 235 |
class LoginWebAuthnView(APIView): |
| 236 |
input_class = { |
| 237 |
"POST": LoginWebAuthnInput, |
| 238 |
} |
| 239 |
|
| 240 |
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 241 |
request_options = webauthn_auth.begin_authentication() |
| 242 |
return response.WebAuthnRequestOptionsResponse(request, request_options) |
| 243 |
|
| 244 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 245 |
authenticator = self.input.cleaned_data["credential"] |
| 246 |
redirect_url = None |
| 247 |
login = Login(user=authenticator.user, redirect_url=redirect_url) |
| 248 |
webauthn_flows.perform_passwordless_login(request, authenticator, login) |
| 249 |
return AuthenticationResponse(request) |
| 250 |
|
| 251 |
|
| 252 |
class SignupWebAuthnView(SignupView): |
| 253 |
input_class = { |
| 254 |
"POST": SignupWebAuthnInput, |
| 255 |
"PUT": CreateWebAuthnInput, |
| 256 |
} |
| 257 |
by_passkey = True |
| 258 |
|
| 259 |
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 260 |
resp = self._require_stage() |
| 261 |
if resp: |
| 262 |
return resp |
| 263 |
creation_options = webauthn_flows.begin_registration( |
| 264 |
request, self.stage.login.user, passwordless=True, signup=True |
| 265 |
) |
| 266 |
return response.AddWebAuthnResponse(request, creation_options) |
| 267 |
|
| 268 |
def _prep_stage(self): |
| 269 |
if hasattr(self, "stage"): |
| 270 |
return self.stage |
| 271 |
self.stage = get_pending_stage(self.request) |
| 272 |
return self.stage |
| 273 |
|
| 274 |
def _require_stage(self) -> ConflictResponse | None: |
| 275 |
self._prep_stage() |
| 276 |
if not self.stage or self.stage.key != PasskeySignupStage.key: |
| 277 |
return ConflictResponse(self.request) |
| 278 |
return None |
| 279 |
|
| 280 |
def get_input_kwargs(self) -> dict: |
| 281 |
ret = super().get_input_kwargs() |
| 282 |
self._prep_stage() |
| 283 |
if self.stage and self.request.method == "PUT": |
| 284 |
ret["user"] = self.stage.login.user |
| 285 |
return ret |
| 286 |
|
| 287 |
def put(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 288 |
resp = self._require_stage() |
| 289 |
if resp: |
| 290 |
return resp |
| 291 |
webauthn_flows.signup_authenticator( |
| 292 |
request, |
| 293 |
user=self.stage.login.user, |
| 294 |
name=self.input.cleaned_data["name"], |
| 295 |
credential=self.input.cleaned_data["credential"], |
| 296 |
) |
| 297 |
self.stage.exit() |
| 298 |
return AuthenticationResponse(request) |
| 299 |
|
| 300 |
|
| 301 |
class TrustView(AuthenticationStageAPIView): |
| 302 |
input_class = TrustInput |
| 303 |
stage_class = TrustStage |
| 304 |
|
| 305 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 306 |
trust = self.input.cleaned_data["trust"] |
| 307 |
response = self.respond_next_stage() |
| 308 |
if trust: |
| 309 |
trust_browser(request, self.stage.login.user, response) |
| 310 |
return response |