fix: принимать токен без префикса Bearer в current_user

Co-authored-by: aider (deepseek/deepseek-chat) <aider@aider.chat>
This commit is contained in:
Evgeny Bushkov
2026-11-06 14:11:13 +03:00
co-authored by aider
parent 9b3eabd3e4
commit 05d85eaf87
+8 -3
View File
@@ -20,10 +20,15 @@ def get_db():
def current_user(request: Request, db: Session = Depends(get_db)) -> User:
auth = request.headers.get("authorization", "")
if not auth.lower().startswith("bearer "):
auth = request.headers.get("authorization", "").strip()
if not auth:
raise ApiError(401, "unauthorized", "Требуется Bearer-токен")
token = auth[7:].strip()
# Swagger UI для bearerAuth подставляет токен без префикса "Bearer ".
# Принимаем оба варианта: "Bearer <token>" и "<token>".
if auth.lower().startswith("bearer "):
token = auth[7:].strip()
else:
token = auth
if not token:
raise ApiError(401, "unauthorized", "Пустой токен")
row = db.get(SessionRow, token_hash(token))