Перенесла полный код из предыдущей лабораторной работы

This commit is contained in:
vasyukova
2026-10-24 19:01:13 +03:00
parent 470d9d6f1e
commit 84908b7d7c
57 changed files with 6946 additions and 8 deletions
+24
View File
@@ -0,0 +1,24 @@
from uuid import UUID
from fastapi import HTTPException, Security
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from ..models.schemas import User
from ..storage.postgres import storage
bearer_scheme = HTTPBearer(auto_error=False, scheme_name="bearerAuth")
def current_user(
credentials: HTTPAuthorizationCredentials | None = Security(bearer_scheme),
) -> User:
"""Достаёт Bearer-токен и возвращает владельца сессии.
Basic на бизнес-endpoint не принимается, X-User-Id игнорируется.
"""
if credentials is None or credentials.scheme.lower() != "bearer":
raise HTTPException(status_code=401, detail="unauthorized")
user = storage.get_session_user(credentials.credentials)
if user is None:
raise HTTPException(status_code=401, detail="unauthorized")
return user