| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from http import HTTPStatus |
| 4 |
from typing import Any |
| 5 |
|
| 6 |
from django.http import HttpRequest, HttpResponse, HttpResponseBase |
| 7 |
from django.utils.decorators import method_decorator |
| 8 |
|
| 9 |
from allauth.account import app_settings as account_settings |
| 10 |
from allauth.account.adapter import get_adapter as get_account_adapter |
| 11 |
from allauth.account.internal import flows |
| 12 |
from allauth.account.internal.constants import LoginStageKey |
| 13 |
from allauth.account.internal.flows import ( |
| 14 |
email_verification, |
| 15 |
manage_email, |
| 16 |
password_change, |
| 17 |
password_reset, |
| 18 |
password_reset_by_code, |
| 19 |
) |
| 20 |
from allauth.account.internal.flows.email_verification import ( |
| 21 |
send_verification_email_to_address, |
| 22 |
) |
| 23 |
from allauth.account.internal.flows.email_verification_by_code import ( |
| 24 |
EmailVerificationProcess, |
| 25 |
) |
| 26 |
from allauth.account.internal.flows.phone_verification import ( |
| 27 |
PhoneVerificationProcess, |
| 28 |
PhoneVerificationStageProcess, |
| 29 |
) |
| 30 |
from allauth.account.stages import ( |
| 31 |
EmailVerificationStage, |
| 32 |
LoginStageController, |
| 33 |
PhoneVerificationStage, |
| 34 |
) |
| 35 |
from allauth.core import ratelimit |
| 36 |
from allauth.core.exceptions import ImmediateHttpResponse, RateLimited |
| 37 |
from allauth.core.internal.httpkit import authenticated_user |
| 38 |
from allauth.decorators import rate_limit |
| 39 |
from allauth.headless.account import response |
| 40 |
from allauth.headless.account.inputs import ( |
| 41 |
AddEmailInput, |
| 42 |
ChangeEmailInput, |
| 43 |
ChangePasswordInput, |
| 44 |
ChangePhoneInput, |
| 45 |
ConfirmLoginCodeInput, |
| 46 |
DeleteEmailInput, |
| 47 |
LoginInput, |
| 48 |
MarkAsPrimaryEmailInput, |
| 49 |
ReauthenticateInput, |
| 50 |
RequestLoginCodeInput, |
| 51 |
RequestPasswordResetInput, |
| 52 |
ResendEmailVerificationInput, |
| 53 |
ResetPasswordInput, |
| 54 |
ResetPasswordKeyInput, |
| 55 |
SignupInput, |
| 56 |
VerifyEmailInput, |
| 57 |
VerifyPhoneInput, |
| 58 |
) |
| 59 |
from allauth.headless.base.response import ( |
| 60 |
AuthenticationResponse, |
| 61 |
ConflictResponse, |
| 62 |
ForbiddenResponse, |
| 63 |
RateLimitResponse, |
| 64 |
) |
| 65 |
from allauth.headless.base.views import APIView, AuthenticatedAPIView |
| 66 |
from allauth.headless.internal import authkit |
| 67 |
from allauth.headless.internal.restkit.inputs import Input |
| 68 |
from allauth.headless.internal.restkit.response import APIResponse, ErrorResponse |
| 69 |
|
| 70 |
|
| 71 |
class RequestLoginCodeView(APIView): |
| 72 |
input_class = RequestLoginCodeInput |
| 73 |
input: RequestLoginCodeInput |
| 74 |
|
| 75 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 76 |
flows.login_by_code.LoginCodeVerificationProcess.initiate( |
| 77 |
request=self.request, |
| 78 |
user=self.input._user, |
| 79 |
email=self.input.cleaned_data.get("email"), |
| 80 |
phone=self.input.cleaned_data.get("phone"), |
| 81 |
) |
| 82 |
return AuthenticationResponse(self.request) |
| 83 |
|
| 84 |
|
| 85 |
class ResendLoginCodeView(APIView): |
| 86 |
handle_json_input = False |
| 87 |
|
| 88 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 89 |
process = None |
| 90 |
stage = LoginStageController.enter(request, LoginStageKey.LOGIN_BY_CODE) |
| 91 |
if stage: |
| 92 |
process = flows.login_by_code.LoginCodeVerificationProcess.resume(stage) |
| 93 |
if not process or not process.can_resend: |
| 94 |
return ConflictResponse(request) |
| 95 |
try: |
| 96 |
process.resend() |
| 97 |
except RateLimited: |
| 98 |
return RateLimitResponse(request) |
| 99 |
return APIResponse(request) |
| 100 |
|
| 101 |
|
| 102 |
class ConfirmLoginCodeView(APIView): |
| 103 |
input_class = ConfirmLoginCodeInput |
| 104 |
|
| 105 |
def dispatch( |
| 106 |
self, request: HttpRequest, *args: Any, **kwargs: Any |
| 107 |
) -> HttpResponseBase: |
| 108 |
auth_status = authkit.AuthenticationStatus(request) |
| 109 |
self.stage = auth_status.get_pending_stage() |
| 110 |
if not self.stage or self.stage.key != LoginStageKey.LOGIN_BY_CODE: |
| 111 |
return ConflictResponse(request) |
| 112 |
self.process = flows.login_by_code.LoginCodeVerificationProcess.resume( |
| 113 |
self.stage |
| 114 |
) |
| 115 |
if not self.process: |
| 116 |
return ConflictResponse(request) |
| 117 |
return super().dispatch(request, *args, **kwargs) |
| 118 |
|
| 119 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 120 |
response = self.process.finish(None) |
| 121 |
return AuthenticationResponse.from_response(request, response) |
| 122 |
|
| 123 |
def get_input_kwargs(self) -> dict: |
| 124 |
kwargs = super().get_input_kwargs() |
| 125 |
kwargs["code"] = self.process.code |
| 126 |
return kwargs |
| 127 |
|
| 128 |
def handle_invalid_input(self, input) -> ErrorResponse: |
| 129 |
self.process.record_invalid_attempt() |
| 130 |
return super().handle_invalid_input(input) |
| 131 |
|
| 132 |
|
| 133 |
@method_decorator(rate_limit(action="login"), name="handle") |
| 134 |
class LoginView(APIView): |
| 135 |
input_class = LoginInput |
| 136 |
input: LoginInput |
| 137 |
|
| 138 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 139 |
if request.user.is_authenticated: |
| 140 |
return ConflictResponse(request) |
| 141 |
credentials = self.input.cleaned_data |
| 142 |
response = flows.login.perform_password_login( |
| 143 |
request, credentials, self.input.login |
| 144 |
) |
| 145 |
return AuthenticationResponse.from_response(request, response) |
| 146 |
|
| 147 |
|
| 148 |
@method_decorator(rate_limit(action="signup"), name="handle") |
| 149 |
class SignupView(APIView): |
| 150 |
input_class = {"POST": SignupInput} |
| 151 |
input: SignupInput |
| 152 |
by_passkey = False |
| 153 |
|
| 154 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 155 |
if request.user.is_authenticated: |
| 156 |
return ConflictResponse(request) |
| 157 |
if not get_account_adapter().is_open_for_signup(request): |
| 158 |
return ForbiddenResponse(request) |
| 159 |
user, resp = self.input.try_save(request) |
| 160 |
if not resp: |
| 161 |
try: |
| 162 |
resp = flows.signup.complete_signup( |
| 163 |
request, user=user, by_passkey=self.by_passkey |
| 164 |
) |
| 165 |
except ImmediateHttpResponse: |
| 166 |
pass |
| 167 |
return AuthenticationResponse.from_response(request, resp) |
| 168 |
|
| 169 |
|
| 170 |
class SessionView(APIView): |
| 171 |
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 172 |
return AuthenticationResponse(request) |
| 173 |
|
| 174 |
def delete(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 175 |
adapter = get_account_adapter() |
| 176 |
adapter.logout(request) |
| 177 |
return AuthenticationResponse(request) |
| 178 |
|
| 179 |
|
| 180 |
class VerifyEmailView(APIView): |
| 181 |
input_class = VerifyEmailInput |
| 182 |
input: VerifyEmailInput |
| 183 |
|
| 184 |
def handle( |
| 185 |
self, request: HttpRequest, *args: Any, **kwargs: Any |
| 186 |
) -> HttpResponseBase: |
| 187 |
self.stage = LoginStageController.enter(request, EmailVerificationStage.key) |
| 188 |
if ( |
| 189 |
not self.stage |
| 190 |
and account_settings.EMAIL_VERIFICATION_BY_CODE_ENABLED |
| 191 |
and not request.user.is_authenticated |
| 192 |
): |
| 193 |
return ConflictResponse(request) |
| 194 |
self.process = None |
| 195 |
if account_settings.EMAIL_VERIFICATION_BY_CODE_ENABLED: |
| 196 |
self.process = ( |
| 197 |
flows.email_verification_by_code.EmailVerificationProcess.resume( |
| 198 |
request |
| 199 |
) |
| 200 |
) |
| 201 |
if not self.process: |
| 202 |
return ConflictResponse(request) |
| 203 |
return super().handle(request, *args, **kwargs) |
| 204 |
|
| 205 |
def get_input_kwargs(self) -> dict: |
| 206 |
return {"process": self.process} |
| 207 |
|
| 208 |
def handle_invalid_input(self, input: VerifyEmailInput) -> ErrorResponse: |
| 209 |
if self.process: |
| 210 |
self.process.record_invalid_attempt() |
| 211 |
return super().handle_invalid_input(input) |
| 212 |
|
| 213 |
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 214 |
key = request.headers.get("x-email-verification-key", "") |
| 215 |
input = self.input_class({"key": key}, process=self.process) |
| 216 |
if not input.is_valid(): |
| 217 |
if self.process: |
| 218 |
self.process.record_invalid_attempt() |
| 219 |
return ErrorResponse(request, input=input) |
| 220 |
if self.process: |
| 221 |
email_address = self.process.email_address |
| 222 |
else: |
| 223 |
email_address = input.verification.email_address |
| 224 |
return response.VerifyEmailResponse(request, email_address, stage=self.stage) |
| 225 |
|
| 226 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 227 |
if self.process: |
| 228 |
email_address = self.process.finish() |
| 229 |
else: |
| 230 |
email_address = self.input.verification.confirm(request) |
| 231 |
if not email_address: |
| 232 |
# Should not happen, VerifyInputInput should have verified all |
| 233 |
# preconditions. |
| 234 |
return APIResponse(request, status=HTTPStatus.INTERNAL_SERVER_ERROR) |
| 235 |
response = None |
| 236 |
if self.stage: |
| 237 |
# Verifying email as part of login/signup flow may imply the user is |
| 238 |
# to be logged in... |
| 239 |
response = email_verification.login_on_verification(request, email_address) |
| 240 |
return AuthenticationResponse.from_response(request, response) |
| 241 |
|
| 242 |
|
| 243 |
class VerifyPhoneView(APIView): |
| 244 |
input_class = VerifyPhoneInput |
| 245 |
|
| 246 |
def handle( |
| 247 |
self, request: HttpRequest, *args: Any, **kwargs: Any |
| 248 |
) -> HttpResponseBase: |
| 249 |
self.stage = LoginStageController.enter(request, PhoneVerificationStage.key) |
| 250 |
self.process: PhoneVerificationProcess | None = None |
| 251 |
if self.stage: |
| 252 |
self.process = ( |
| 253 |
flows.phone_verification.PhoneVerificationStageProcess.resume( |
| 254 |
self.stage |
| 255 |
) |
| 256 |
) |
| 257 |
else: |
| 258 |
if not request.user.is_authenticated: |
| 259 |
return ConflictResponse(request) |
| 260 |
self.process = ( |
| 261 |
flows.phone_verification.ChangePhoneVerificationProcess.resume(request) |
| 262 |
) |
| 263 |
if not self.process: |
| 264 |
return ConflictResponse(request) |
| 265 |
return super().handle(request, *args, **kwargs) |
| 266 |
|
| 267 |
def get_input_kwargs(self) -> dict: |
| 268 |
assert self.process is not None # nosec |
| 269 |
return { |
| 270 |
"code": self.process.code, |
| 271 |
"user": self.process.user, |
| 272 |
"phone": self.process.phone, |
| 273 |
} |
| 274 |
|
| 275 |
def handle_invalid_input(self, input: VerifyPhoneInput) -> ErrorResponse: |
| 276 |
assert self.process is not None # nosec |
| 277 |
self.process.record_invalid_attempt() |
| 278 |
return super().handle_invalid_input(input) |
| 279 |
|
| 280 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 281 |
assert self.process is not None # nosec |
| 282 |
self.process.finish() |
| 283 |
response = None |
| 284 |
if self.stage: |
| 285 |
response = self.stage.exit() |
| 286 |
return AuthenticationResponse.from_response(request, response) |
| 287 |
|
| 288 |
|
| 289 |
class ResendPhoneVerificationCodeView(APIView): |
| 290 |
handle_json_input = False |
| 291 |
|
| 292 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 293 |
process = None |
| 294 |
stage = LoginStageController.enter(request, PhoneVerificationStage.key) |
| 295 |
if stage: |
| 296 |
process = flows.phone_verification.PhoneVerificationStageProcess.resume( |
| 297 |
stage |
| 298 |
) |
| 299 |
if not process or not process.can_resend: |
| 300 |
return ConflictResponse(request) |
| 301 |
try: |
| 302 |
process.resend() |
| 303 |
except RateLimited: |
| 304 |
return RateLimitResponse(request) |
| 305 |
return APIResponse(request) |
| 306 |
|
| 307 |
|
| 308 |
class ResendEmailVerificationCodeView(APIView): |
| 309 |
handle_json_input = False |
| 310 |
|
| 311 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 312 |
if not account_settings.EMAIL_VERIFICATION_BY_CODE_ENABLED: |
| 313 |
return ConflictResponse(request) |
| 314 |
process = flows.email_verification_by_code.EmailVerificationProcess.resume( |
| 315 |
request |
| 316 |
) |
| 317 |
if not process or not process.can_resend: |
| 318 |
return ConflictResponse(request) |
| 319 |
try: |
| 320 |
process.resend() |
| 321 |
except RateLimited: |
| 322 |
return RateLimitResponse(request) |
| 323 |
return APIResponse(request) |
| 324 |
|
| 325 |
|
| 326 |
class RequestPasswordResetView(APIView): |
| 327 |
input_class = RequestPasswordResetInput |
| 328 |
input: RequestPasswordResetInput |
| 329 |
|
| 330 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 331 |
r429 = ratelimit.consume_or_429( |
| 332 |
self.request, |
| 333 |
action="reset_password", |
| 334 |
key=self.input.cleaned_data["email"].lower(), |
| 335 |
) |
| 336 |
if r429: |
| 337 |
return r429 |
| 338 |
self.input.save(request) |
| 339 |
if account_settings.PASSWORD_RESET_BY_CODE_ENABLED: |
| 340 |
return AuthenticationResponse(request) |
| 341 |
return response.RequestPasswordResponse(request) |
| 342 |
|
| 343 |
|
| 344 |
@method_decorator(rate_limit(action="reset_password_from_key"), name="handle") |
| 345 |
class ResetPasswordView(APIView): |
| 346 |
input_class = ResetPasswordInput |
| 347 |
input: ResetPasswordInput |
| 348 |
|
| 349 |
def handle_invalid_input(self, input: ResetPasswordInput) -> ErrorResponse: |
| 350 |
if self.process and "key" in input.errors: |
| 351 |
self.process.record_invalid_attempt() |
| 352 |
return super().handle_invalid_input(input) |
| 353 |
|
| 354 |
def handle( |
| 355 |
self, request: HttpRequest, *args: Any, **kwargs: Any |
| 356 |
) -> HttpResponseBase: |
| 357 |
self.process = None |
| 358 |
if account_settings.PASSWORD_RESET_BY_CODE_ENABLED: |
| 359 |
self.process = ( |
| 360 |
password_reset_by_code.PasswordResetVerificationProcess.resume( |
| 361 |
self.request |
| 362 |
) |
| 363 |
) |
| 364 |
if not self.process: |
| 365 |
return ConflictResponse(request) |
| 366 |
return super().handle(request, *args, **kwargs) |
| 367 |
|
| 368 |
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 369 |
key = request.headers.get("X-Password-Reset-Key", "") |
| 370 |
if self.process and self.process.user: |
| 371 |
input = ResetPasswordKeyInput({"key": key}, code=self.process.code) |
| 372 |
if not input.is_valid(): |
| 373 |
self.process.record_invalid_attempt() |
| 374 |
return ErrorResponse(request, input=input) |
| 375 |
self.process.confirm_code() |
| 376 |
return response.PasswordResetKeyResponse(request, self.process.user) |
| 377 |
else: |
| 378 |
input = ResetPasswordKeyInput({"key": key}) |
| 379 |
if not input.is_valid(): |
| 380 |
return ErrorResponse(request, input=input) |
| 381 |
return response.PasswordResetKeyResponse(request, input.user) |
| 382 |
|
| 383 |
def get_input_kwargs(self) -> dict: |
| 384 |
ret = {} |
| 385 |
if self.process: |
| 386 |
ret.update({"code": self.process.code, "user": self.process.user}) |
| 387 |
return ret |
| 388 |
|
| 389 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 390 |
user = self.input.user |
| 391 |
flows.password_reset.reset_password(user, self.input.cleaned_data["password"]) |
| 392 |
if self.process: |
| 393 |
self.process.confirm_code() |
| 394 |
self.process.finish() |
| 395 |
else: |
| 396 |
password_reset.finalize_password_reset(request, user) |
| 397 |
return AuthenticationResponse(self.request) |
| 398 |
|
| 399 |
|
| 400 |
@method_decorator(rate_limit(action="change_password"), name="handle") |
| 401 |
class ChangePasswordView(AuthenticatedAPIView): |
| 402 |
input_class = ChangePasswordInput |
| 403 |
|
| 404 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 405 |
assert request.user.is_authenticated # nosec |
| 406 |
password_change.change_password( |
| 407 |
request.user, self.input.cleaned_data["new_password"] |
| 408 |
) |
| 409 |
is_set = not self.input.cleaned_data.get("current_password") |
| 410 |
if is_set: |
| 411 |
password_change.finalize_password_set(request, request.user) |
| 412 |
else: |
| 413 |
password_change.finalize_password_change(request, request.user) |
| 414 |
return AuthenticationResponse(request) |
| 415 |
|
| 416 |
def get_input_kwargs(self) -> dict: |
| 417 |
return {"user": self.request.user} |
| 418 |
|
| 419 |
|
| 420 |
class ManageEmailView(APIView): |
| 421 |
input_class = { |
| 422 |
"POST": AddEmailInput, |
| 423 |
"PUT": ResendEmailVerificationInput, |
| 424 |
"DELETE": DeleteEmailInput, |
| 425 |
"PATCH": MarkAsPrimaryEmailInput, |
| 426 |
} |
| 427 |
|
| 428 |
def dispatch( |
| 429 |
self, request: HttpRequest, *args: Any, **kwargs: Any |
| 430 |
) -> HttpResponseBase: |
| 431 |
self.verification_stage_process = None |
| 432 |
if request.user.is_authenticated: |
| 433 |
self.user = request.user |
| 434 |
elif request.method != "POST": |
| 435 |
return AuthenticationResponse(request) |
| 436 |
else: |
| 437 |
self.verification_stage_process = EmailVerificationProcess.resume(request) |
| 438 |
if ( |
| 439 |
not self.verification_stage_process |
| 440 |
or not self.verification_stage_process.can_change |
| 441 |
): |
| 442 |
return ConflictResponse(request) |
| 443 |
self.user = self.verification_stage_process.user # type:ignore[assignment] |
| 444 |
resp = ratelimit.consume_or_429(request, action="manage_email", user=self.user) |
| 445 |
if resp: |
| 446 |
return resp |
| 447 |
return super().dispatch(request, *args, **kwargs) |
| 448 |
|
| 449 |
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 450 |
return self._respond_email_list() |
| 451 |
|
| 452 |
def _respond_email_list(self) -> response.EmailAddressesResponse: |
| 453 |
addrs = manage_email.list_email_addresses(self.request, self.user) |
| 454 |
return response.EmailAddressesResponse(self.request, addrs) |
| 455 |
|
| 456 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 457 |
if self.verification_stage_process: |
| 458 |
input: ChangeEmailInput = self.input # type:ignore[assignment] |
| 459 |
self.verification_stage_process.change_to( |
| 460 |
email=input.cleaned_data["email"], |
| 461 |
account_already_exists=input.account_already_exists, |
| 462 |
) |
| 463 |
else: |
| 464 |
flows.manage_email.add_email(request, self.input) |
| 465 |
return self._respond_email_list() |
| 466 |
|
| 467 |
def delete(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 468 |
addr = self.input.cleaned_data["email"] |
| 469 |
if addr.pk: |
| 470 |
flows.manage_email.delete_email(request, addr) |
| 471 |
else: |
| 472 |
self.input.process.abort() # type:ignore[attr-defined] |
| 473 |
return self._respond_email_list() |
| 474 |
|
| 475 |
def patch(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 476 |
addr = self.input.cleaned_data["email"] |
| 477 |
flows.manage_email.mark_as_primary(request, addr) |
| 478 |
return self._respond_email_list() |
| 479 |
|
| 480 |
def put(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 481 |
addr = self.input.cleaned_data["email"] |
| 482 |
if process := self.input.process: # type:ignore[attr-defined] |
| 483 |
sent = False |
| 484 |
if process.can_resend: |
| 485 |
try: |
| 486 |
process.resend() |
| 487 |
sent = True |
| 488 |
except RateLimited: |
| 489 |
pass |
| 490 |
else: |
| 491 |
sent = send_verification_email_to_address(request, addr) |
| 492 |
return response.RequestEmailVerificationResponse( |
| 493 |
request, verification_sent=sent |
| 494 |
) |
| 495 |
|
| 496 |
def get_input_class(self) -> type[Input] | None: |
| 497 |
if self.verification_stage_process: |
| 498 |
return ChangeEmailInput |
| 499 |
return super().get_input_class() |
| 500 |
|
| 501 |
def get_input_kwargs(self) -> dict: |
| 502 |
if self.verification_stage_process: |
| 503 |
return {"email": self.verification_stage_process.email} |
| 504 |
return {"user": self.user} |
| 505 |
|
| 506 |
|
| 507 |
class ManagePhoneView(APIView): |
| 508 |
input_class = ChangePhoneInput |
| 509 |
input: ChangePhoneInput |
| 510 |
|
| 511 |
def dispatch( |
| 512 |
self, request: HttpRequest, *args: Any, **kwargs: Any |
| 513 |
) -> HttpResponseBase: |
| 514 |
self.verification_stage_process = None |
| 515 |
if request.user.is_authenticated: |
| 516 |
self.user = request.user |
| 517 |
elif request.method == "GET": |
| 518 |
return AuthenticationResponse(request) |
| 519 |
elif request.method == "POST": |
| 520 |
stage = LoginStageController.enter(request, PhoneVerificationStage.key) |
| 521 |
if stage: |
| 522 |
self.verification_stage_process = PhoneVerificationStageProcess.resume( |
| 523 |
stage |
| 524 |
) |
| 525 |
if ( |
| 526 |
not self.verification_stage_process |
| 527 |
or not self.verification_stage_process.can_change |
| 528 |
): |
| 529 |
return ConflictResponse(request) |
| 530 |
self.user = self.verification_stage_process.user # type:ignore[assignment] |
| 531 |
resp = ratelimit.consume_or_429(request, action="change_phone", user=self.user) |
| 532 |
if resp: |
| 533 |
return resp |
| 534 |
return super().dispatch(request, *args, **kwargs) |
| 535 |
|
| 536 |
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 537 |
phone_numbers = [] |
| 538 |
phone_verified = get_account_adapter().get_phone( |
| 539 |
authenticated_user(self.request) |
| 540 |
) |
| 541 |
if phone_verified: |
| 542 |
phone_numbers.append( |
| 543 |
{"phone": phone_verified[0], "verified": phone_verified[1]} |
| 544 |
) |
| 545 |
return response.PhoneNumbersResponse(self.request, phone_numbers) |
| 546 |
|
| 547 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 548 |
phone = self.input.cleaned_data["phone"] |
| 549 |
if self.verification_stage_process: |
| 550 |
self.verification_stage_process.change_to( |
| 551 |
phone=phone, account_already_exists=self.input.account_already_exists |
| 552 |
) |
| 553 |
else: |
| 554 |
flows.phone_verification.ChangePhoneVerificationProcess.initiate( |
| 555 |
self.request, |
| 556 |
phone, |
| 557 |
) |
| 558 |
return response.PhoneNumbersResponse( |
| 559 |
self.request, |
| 560 |
[ |
| 561 |
{ |
| 562 |
"phone": phone, |
| 563 |
"verified": False, |
| 564 |
} |
| 565 |
], |
| 566 |
status=HTTPStatus.ACCEPTED, |
| 567 |
) |
| 568 |
|
| 569 |
def get_input_kwargs(self) -> dict: |
| 570 |
phone = None |
| 571 |
phone_verified = get_account_adapter().get_phone(self.user) |
| 572 |
if phone_verified: |
| 573 |
phone = phone_verified[0] |
| 574 |
return {"phone": phone, "user": self.user} |
| 575 |
|
| 576 |
|
| 577 |
@method_decorator(rate_limit(action="reauthenticate"), name="handle") |
| 578 |
class ReauthenticateView(AuthenticatedAPIView): |
| 579 |
input_class = ReauthenticateInput |
| 580 |
|
| 581 |
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse: |
| 582 |
flows.reauthentication.reauthenticate_by_password(self.request) |
| 583 |
return AuthenticationResponse(self.request) |
| 584 |
|
| 585 |
def get_input_kwargs(self) -> dict: |
| 586 |
return {"user": self.request.user} |