add migrations, create domain and crud layers

This commit is contained in:
2026-10-26 21:59:40 +03:00
parent 76b34fca4b
commit 3082b84f61
27 changed files with 481 additions and 278 deletions
+9 -2
View File
@@ -13,15 +13,18 @@ TOKEN_BYTES = 32
_hasher = PasswordHasher()
_executor = ThreadPoolExecutor(
max_workers=os.cpu_count() or 1, thread_name_prefix="argon2"
max_workers=max(1, (os.cpu_count() or 1) // _hasher.parallelism),
thread_name_prefix="argon2",
)
def _hash_password_sync(password: str) -> str:
"""Блокирующий расчёт Argon2id-хеша со случайной солью в формате PHC."""
return _hasher.hash(password)
def _verify_password_sync(password: str, password_hash: str) -> bool:
"""Блокирующая проверка пароля по PHC-строке; False при несовпадении или повреждённом хеше."""
try:
return _hasher.verify(password_hash, password)
except (VerificationError, InvalidHashError):
@@ -32,11 +35,13 @@ _DUMMY_HASH = _hash_password_sync(secrets.token_urlsafe(TOKEN_BYTES))
async def hash_password(password: str) -> str:
"""Хеширует пароль в пуле потоков, не блокируя event loop."""
loop = asyncio.get_running_loop()
return await loop.run_in_executor(_executor, _hash_password_sync, password)
async def verify_password(password: str, password_hash: str) -> bool:
"""Проверяет пароль в пуле потоков, не блокируя event loop."""
loop = asyncio.get_running_loop()
return await loop.run_in_executor(
_executor, _verify_password_sync, password, password_hash
@@ -44,13 +49,15 @@ async def verify_password(password: str, password_hash: str) -> bool:
async def dummy_verify_password(password: str) -> None:
"""Выравнивает время ответа для несуществующего пользователя."""
"""Проверка против фиктивного хеша: время ответа не выдаёт отсутствие пользователя."""
await verify_password(password, _DUMMY_HASH)
def generate_token() -> str:
"""Непрозрачный токен сессии: 32 случайных байта в base64url."""
return secrets.token_urlsafe(TOKEN_BYTES)
def hash_token(token: str) -> str:
"""SHA-256 токена в hex: детерминированный ключ для хранилища сессий."""
return hashlib.sha256(token.encode()).hexdigest()