feat: ЛР2 - PostgreSQL, Redis сессии и keyset-пагинация

This commit is contained in:
Ильяс
2026-10-23 12:45:58 +03:00
parent 5893d051c1
commit 9ddb2244bc
25 changed files with 1332 additions and 423 deletions
+21 -17
View File
@@ -1,26 +1,30 @@
from __future__ import annotations
from uuid import UUID
from fastapi import Header, Request
from fastapi import Depends, Header, Request
from app.errors import UnauthorizedError
from app.schemas import UserResponse
from app.storage import UnauthorizedError, storage
from app.storage import storage
async def get_current_user(
request: Request,
x_user_id: str | None = Header(default=None, alias="X-User-Id"),
) -> UserResponse:
if not x_user_id or not x_user_id.strip():
raise UnauthorizedError("Отсутствует заголовок X-User-Id")
async def get_current_token(
authorization: str | None = Header(default=None, alias="Authorization"),
) -> str:
if not authorization or not authorization.strip():
raise UnauthorizedError("Отсутствует заголовок Authorization")
# валидация формата UUID
try:
UUID(x_user_id)
except ValueError:
raise UnauthorizedError("Неверный формат заголовка X-User-Id")
parts = authorization.strip().split(maxsplit=1)
if len(parts) != 2 or parts[0].lower() != "bearer":
raise UnauthorizedError("Неверная схема авторизации (ожидается Bearer)")
# проверка существования пользователя
user = await storage.get_user_by_id(x_user_id)
token = parts[1].strip()
if not token:
raise UnauthorizedError("Пустой Bearer токен")
return token
async def get_current_user(token: str = Depends(get_current_token)) -> UserResponse:
user = await storage.get_session_user(token)
if not user:
raise UnauthorizedError("Недействительная или истекшая сессия")
return user