feat: перевести API на password/Basic/Bearer и PostgreSQL с Redis-кэшем

Co-authored-by: aider (deepseek/deepseek-chat) <aider@aider.chat>
This commit is contained in:
Evgeny Bushkov
2026-10-23 18:07:09 +03:00
co-authored by aider
parent 3e152d4175
commit 785fde833f
18 changed files with 856 additions and 176 deletions
+29
View File
@@ -0,0 +1,29 @@
"""Хеширование паролей (Argon2id) и генерация opaque-токенов сессий."""
import hashlib
import secrets
from argon2 import PasswordHasher
from argon2.exceptions import VerifyMismatchError, VerificationError, InvalidHashError
_hasher = PasswordHasher()
def hash_password(password: str) -> str:
return _hasher.hash(password)
def verify_password(password_hash: str, password: str) -> bool:
try:
return _hasher.verify(password_hash, password)
except (VerifyMismatchError, VerificationError, InvalidHashError):
return False
def new_token() -> str:
"""256 бит энтропии, URL-safe."""
return secrets.token_urlsafe(32)
def token_hash(token: str) -> str:
"""В БД/Redis храним только SHA-256 от токена, не сам токен."""
return hashlib.sha256(token.encode("utf-8")).hexdigest()