from __future__ import annotations from fastapi import Depends, Header, Request from app.errors import UnauthorizedError from app.schemas import UserResponse from app.storage import storage async def get_current_token( authorization: str | None = Header(default=None, alias="Authorization"), ) -> str: if not authorization or not authorization.strip(): raise UnauthorizedError("Отсутствует заголовок Authorization") parts = authorization.strip().split(maxsplit=1) if len(parts) != 2 or parts[0].lower() != "bearer": raise UnauthorizedError("Неверная схема авторизации (ожидается Bearer)") 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