| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import Optional |
| 4 |
|
| 5 |
from django.contrib import messages |
| 6 |
from django.contrib.auth.base_user import AbstractBaseUser |
| 7 |
from django.http import HttpRequest |
| 8 |
|
| 9 |
from allauth.account import app_settings |
| 10 |
from allauth.account.adapter import get_adapter |
| 11 |
from allauth.account.internal.flows.code_verification import ( |
| 12 |
AbstractCodeVerificationProcess, |
| 13 |
) |
| 14 |
from allauth.account.internal.flows.reauthentication import ( |
| 15 |
raise_if_reauthentication_required, |
| 16 |
) |
| 17 |
from allauth.account.internal.stagekit import stash_login |
| 18 |
from allauth.account.internal.userkit import did_user_login |
| 19 |
from allauth.core import context, ratelimit |
| 20 |
from allauth.core.internal.httpkit import authenticated_user |
| 21 |
|
| 22 |
|
| 23 |
PHONE_VERIFICATION_STATE_KEY = "phone_verification" |
| 24 |
PHONE_VERIFICATION_SESSION_KEY = "account_phone_verification" |
| 25 |
|
| 26 |
|
| 27 |
def verify_phone_indirectly( |
| 28 |
request: HttpRequest, user: AbstractBaseUser, phone: str |
| 29 |
) -> None: |
| 30 |
get_adapter().set_phone_verified(user, phone) |
| 31 |
|
| 32 |
|
| 33 |
class PhoneVerificationProcess(AbstractCodeVerificationProcess): |
| 34 |
def __init__(self, user: AbstractBaseUser, state) -> None: |
| 35 |
super().__init__( |
| 36 |
user=user, |
| 37 |
state=state, |
| 38 |
timeout=app_settings.PHONE_VERIFICATION_TIMEOUT, |
| 39 |
max_attempts=app_settings.PHONE_VERIFICATION_MAX_ATTEMPTS, |
| 40 |
) |
| 41 |
|
| 42 |
@property |
| 43 |
def phone(self) -> str: |
| 44 |
return self.state["phone"] |
| 45 |
|
| 46 |
def send(self, skip_enumeration_sms: bool = False) -> None: |
| 47 |
ratelimit.consume( |
| 48 |
context.request, |
| 49 |
action="verify_phone", |
| 50 |
key=self.phone, |
| 51 |
raise_exception=True, |
| 52 |
) |
| 53 |
adapter = get_adapter() |
| 54 |
if self.user: |
| 55 |
code = adapter._generate_phone_verification_code_compat( |
| 56 |
user=self.user, |
| 57 |
phone=self.phone, |
| 58 |
) |
| 59 |
else: |
| 60 |
code = "" |
| 61 |
self.state["code"] = code |
| 62 |
self.send_sms(skip_enumeration_sms) |
| 63 |
get_adapter().add_message( |
| 64 |
context.request, |
| 65 |
messages.INFO, |
| 66 |
"account/messages/phone_verification_sent.txt", |
| 67 |
{"phone": self.phone}, |
| 68 |
) |
| 69 |
self.persist() |
| 70 |
|
| 71 |
def send_sms(self, skip_enumeration_sms: bool) -> None: |
| 72 |
adapter = get_adapter() |
| 73 |
if not self.user or self.state.get("account_already_exists"): |
| 74 |
if not skip_enumeration_sms: |
| 75 |
if self.state.get("account_already_exists") or self.state.get("signup"): |
| 76 |
adapter.send_account_already_exists_sms(self.phone) |
| 77 |
else: |
| 78 |
adapter.send_unknown_account_sms(self.phone) |
| 79 |
return |
| 80 |
adapter.send_verification_code_sms( |
| 81 |
user=self.user, |
| 82 |
code=self.code, |
| 83 |
phone=self.phone, |
| 84 |
) |
| 85 |
|
| 86 |
def finish(self) -> None: |
| 87 |
phone = self.state["phone"] |
| 88 |
adapter = get_adapter() |
| 89 |
assert self.user # nosec |
| 90 |
adapter.set_phone_verified(self.user, phone) |
| 91 |
adapter.add_message( |
| 92 |
context.request, |
| 93 |
messages.SUCCESS, |
| 94 |
"account/messages/phone_verified.txt", |
| 95 |
{"phone": phone}, |
| 96 |
) |
| 97 |
|
| 98 |
|
| 99 |
class PhoneVerificationStageProcess(PhoneVerificationProcess): |
| 100 |
def __init__(self, stage) -> None: |
| 101 |
self.stage = stage |
| 102 |
super().__init__(user=stage.login.user, state=stage.state) |
| 103 |
|
| 104 |
def abort(self) -> None: |
| 105 |
pass |
| 106 |
|
| 107 |
def persist(self) -> None: |
| 108 |
stash_login(self.stage.request, self.stage.login) |
| 109 |
|
| 110 |
@classmethod |
| 111 |
def initiate(cls, *, stage, phone: str) -> PhoneVerificationStageProcess: |
| 112 |
stage.state.update(cls.initial_state(user=stage.login.user, phone=phone)) |
| 113 |
process = PhoneVerificationStageProcess(stage=stage) |
| 114 |
process.state["signup"] = stage.login.signup |
| 115 |
process.send(skip_enumeration_sms=False) |
| 116 |
return process |
| 117 |
|
| 118 |
@classmethod |
| 119 |
def resume(cls, stage) -> Optional["PhoneVerificationStageProcess"]: |
| 120 |
state = stage.state |
| 121 |
if not state: |
| 122 |
return None |
| 123 |
process = PhoneVerificationStageProcess(stage) |
| 124 |
return process.abort_if_invalid() |
| 125 |
|
| 126 |
def change_to(self, phone: str, account_already_exists: bool) -> None: |
| 127 |
self.state["account_already_exists"] = account_already_exists |
| 128 |
self.record_change(phone=phone) |
| 129 |
adapter = get_adapter() |
| 130 |
if not account_already_exists: |
| 131 |
assert self.user # nosec |
| 132 |
adapter.set_phone(self.user, phone, False) |
| 133 |
self.send(skip_enumeration_sms=False) |
| 134 |
self.persist() |
| 135 |
|
| 136 |
def resend(self) -> None: |
| 137 |
self.record_resend() |
| 138 |
self.send(skip_enumeration_sms=True) |
| 139 |
|
| 140 |
@property |
| 141 |
def can_resend(self) -> bool: |
| 142 |
return not self.is_resend_quota_reached( |
| 143 |
app_settings.PHONE_VERIFICATION_MAX_RESEND_COUNT |
| 144 |
) |
| 145 |
|
| 146 |
@property |
| 147 |
def can_change(self) -> bool: |
| 148 |
# TODO: Prevent enumeration flaw: if we don't have a user, we cannot |
| 149 |
# change the phone. To fix this, we would need to serialize |
| 150 |
# the user and perform an on-the-fly signup here. |
| 151 |
return ( |
| 152 |
not self.is_change_quota_reached( |
| 153 |
app_settings.PHONE_VERIFICATION_MAX_CHANGE_COUNT |
| 154 |
) |
| 155 |
and bool(self.user) |
| 156 |
and not did_user_login(self.user) |
| 157 |
) |
| 158 |
|
| 159 |
|
| 160 |
class ChangePhoneVerificationProcess(PhoneVerificationProcess): |
| 161 |
def __init__(self, request: HttpRequest, state: dict) -> None: |
| 162 |
self.request = request |
| 163 |
super().__init__( |
| 164 |
user=authenticated_user(request), |
| 165 |
state=state, |
| 166 |
) |
| 167 |
|
| 168 |
def abort(self) -> None: |
| 169 |
self.request.session.pop(PHONE_VERIFICATION_SESSION_KEY, None) |
| 170 |
|
| 171 |
def persist(self) -> None: |
| 172 |
self.request.session[PHONE_VERIFICATION_SESSION_KEY] = self.state |
| 173 |
|
| 174 |
def finish(self) -> None: |
| 175 |
super().finish() |
| 176 |
self.request.session.pop(PHONE_VERIFICATION_SESSION_KEY, None) |
| 177 |
|
| 178 |
@classmethod |
| 179 |
def initiate( |
| 180 |
cls, request: HttpRequest, phone: str |
| 181 |
) -> ChangePhoneVerificationProcess: |
| 182 |
if app_settings.REAUTHENTICATION_REQUIRED: |
| 183 |
raise_if_reauthentication_required(request) |
| 184 |
|
| 185 |
state = cls.initial_state(user=authenticated_user(request), phone=phone) |
| 186 |
process = ChangePhoneVerificationProcess(request, state=state) |
| 187 |
process.send() |
| 188 |
return process |
| 189 |
|
| 190 |
@classmethod |
| 191 |
def resume(cls, request: HttpRequest) -> Optional["ChangePhoneVerificationProcess"]: |
| 192 |
state = request.session.get(PHONE_VERIFICATION_SESSION_KEY) |
| 193 |
if not state: |
| 194 |
return None |
| 195 |
process = ChangePhoneVerificationProcess(request, state=state) |
| 196 |
return process.abort_if_invalid() |
| 197 |
|
| 198 |
|
| 199 |
def phone_already_exists( |
| 200 |
user: AbstractBaseUser | None, phone: str, always_raise: bool = False |
| 201 |
) -> bool: |
| 202 |
""" |
| 203 |
Throws a validation error (if allowed by enumeration prevention rules). |
| 204 |
Otherwise, returns True iff another account already exists. |
| 205 |
""" |
| 206 |
adapter = get_adapter() |
| 207 |
other_user = adapter.get_user_by_phone(phone) |
| 208 |
already_exists = bool(other_user and (not user or user.pk != other_user.pk)) |
| 209 |
if already_exists and (not app_settings.PREVENT_ENUMERATION or always_raise): |
| 210 |
raise adapter.validation_error("phone_taken") |
| 211 |
return already_exists |