+ добавила авторизацию через Bearer

This commit is contained in:
vasyukova
2026-09-30 20:19:44 +03:00
parent 67caf49b55
commit 8e3a0e22f4
8 changed files with 222 additions and 67 deletions
+23 -2
View File
@@ -1,3 +1,24 @@
from fastapi.security import APIKeyHeader
from uuid import UUID
dev_user = APIKeyHeader(name="X-User-Id", auto_error=False, scheme_name="devUser")
from fastapi import HTTPException, Security
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from ..models.schemas import User
from ..storage.memory 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