| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import abc |
| 4 |
import time |
| 5 |
from typing import Any |
| 6 |
|
| 7 |
from django.contrib.auth import get_user_model |
| 8 |
from django.contrib.auth.base_user import AbstractBaseUser |
| 9 |
|
| 10 |
from allauth.account.internal.userkit import str_to_user_id, user_id_to_str |
| 11 |
|
| 12 |
|
| 13 |
class AbstractCodeVerificationProcess(abc.ABC): |
| 14 |
def __init__( |
| 15 |
self, |
| 16 |
max_attempts: int, |
| 17 |
timeout: int, |
| 18 |
state: dict, |
| 19 |
user: AbstractBaseUser | None = None, |
| 20 |
) -> None: |
| 21 |
self._user = user |
| 22 |
self.max_attempts = max_attempts |
| 23 |
self.timeout = timeout |
| 24 |
self.state = state |
| 25 |
|
| 26 |
@property |
| 27 |
def user(self) -> AbstractBaseUser | None: |
| 28 |
if self._user: |
| 29 |
return self._user |
| 30 |
user_id = self.state.get("user_id") |
| 31 |
if not user_id: |
| 32 |
return None |
| 33 |
user_id = str_to_user_id(user_id) |
| 34 |
self._user = get_user_model().objects.filter(pk=user_id).first() |
| 35 |
return self._user |
| 36 |
|
| 37 |
@property |
| 38 |
def code(self) -> str: |
| 39 |
return self.state.get("code", "") |
| 40 |
|
| 41 |
@classmethod |
| 42 |
def initial_state( |
| 43 |
cls, |
| 44 |
user: AbstractBaseUser | None, |
| 45 |
email: str | None = None, |
| 46 |
phone: str | None = None, |
| 47 |
) -> dict[str, Any]: |
| 48 |
state: dict[str, Any] = { |
| 49 |
"at": time.time(), |
| 50 |
"failed_attempts": 0, |
| 51 |
"resend_count": 0, |
| 52 |
"change_count": 0, |
| 53 |
} |
| 54 |
if email: |
| 55 |
state["email"] = email |
| 56 |
if phone: |
| 57 |
state["phone"] = phone |
| 58 |
if user: |
| 59 |
state["user_id"] = user_id_to_str(user) |
| 60 |
return state |
| 61 |
|
| 62 |
def record_invalid_attempt(self) -> bool: |
| 63 |
self.state["failed_attempts"] += 1 |
| 64 |
if self.state["failed_attempts"] >= self.max_attempts: |
| 65 |
self.abort() |
| 66 |
return False |
| 67 |
self.persist() |
| 68 |
return True |
| 69 |
|
| 70 |
def abort_if_invalid(self): |
| 71 |
if not self.is_valid(): |
| 72 |
self.abort() |
| 73 |
return None |
| 74 |
return self |
| 75 |
|
| 76 |
def is_valid(self) -> bool: |
| 77 |
return time.time() - self.state["at"] <= self.timeout |
| 78 |
|
| 79 |
@abc.abstractmethod |
| 80 |
def persist(self) -> None: ... # noqa: E704 |
| 81 |
|
| 82 |
@abc.abstractmethod |
| 83 |
def send(self) -> None: ... # noqa: E704 |
| 84 |
|
| 85 |
@abc.abstractmethod |
| 86 |
def abort(self) -> None: ... # noqa: E704 |
| 87 |
|
| 88 |
def is_resend_quota_reached(self, quota: int) -> bool: |
| 89 |
return self.state["resend_count"] >= quota |
| 90 |
|
| 91 |
def is_change_quota_reached(self, quota: int) -> bool: |
| 92 |
return self.state["change_count"] >= quota |
| 93 |
|
| 94 |
def record_change( |
| 95 |
self, *, email: str | None = None, phone: str | None = None |
| 96 |
) -> None: |
| 97 |
self.state["change_count"] += 1 |
| 98 |
if email: |
| 99 |
self.state["email"] = email |
| 100 |
if phone: |
| 101 |
self.state["phone"] = phone |
| 102 |
|
| 103 |
def record_resend(self) -> None: |
| 104 |
self.state["resend_count"] += 1 |
| 105 |
|
| 106 |
@property |
| 107 |
def can_resend(self) -> bool: |
| 108 |
return False |
| 109 |
|
| 110 |
@property |
| 111 |
def can_change(self) -> bool: |
| 112 |
return False |