forked from mtusi2026/mtusi2026lab6
24 lines
905 B
Python
24 lines
905 B
Python
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 |