feat: добавить каркас приложения с PostgreSQL, сессиями и Redis-кэшем

This commit is contained in:
Evgeny Bushkov
2026-11-06 00:22:27 +03:00
committed by Evgeny Bushkov (aider)
parent 0ab3a341aa
commit d8ba046033
6 changed files with 237 additions and 0 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()