fix: принимать токен без префикса Bearer в current_user
Co-authored-by: aider (deepseek/deepseek-chat) <aider@aider.chat>
This commit is contained in:
+8
-3
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user