21 lines
953 B
Python
21 lines
953 B
Python
from __future__ import annotations
|
|
|
|
from fastapi import Depends, Header, HTTPException, status
|
|
|
|
from app.db.platform_store import get_platform_store
|
|
from app.modules.auth.service import decode_access_token
|
|
|
|
|
|
def get_current_user(authorization: str | None = Header(default=None)) -> dict:
|
|
"""从 Bearer 令牌解析出当前登录用户,供受保护接口依赖使用。"""
|
|
if not authorization or not authorization.lower().startswith("bearer "):
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="缺少认证令牌")
|
|
token = authorization.split(" ", 1)[1].strip()
|
|
user_id = decode_access_token(token)
|
|
if not user_id:
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="令牌无效或已过期")
|
|
user = get_platform_store().user_by_id(user_id)
|
|
if not user:
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="用户不存在")
|
|
return user
|