Files
leaudit-platform-backend/fastapi_common/fastapi_common_security/security.py
T
wren 535d97a70c chore: initial commit — leaudit-platform project skeleton
17-table PostgreSQL schema with full Chinese column comments,
FastAPI project structure (admin/common/modules),
DSL rule files, and schema migration scripts.
2026-04-27 16:48:22 +08:00

24 lines
629 B
Python

"""JWT 鉴权工具。"""
from __future__ import annotations
from typing import Any
import jwt
from fastapi import Request
from fastapi_admin.config import JWT_SECRET_KEY, JWT_ALGORITHM
def verify_access_token(RequestObj: Request) -> dict[str, Any]:
"""验证 JWT access token 并返回 payload。"""
auth = RequestObj.headers.get("Authorization", "")
if not auth.startswith("Bearer "):
return {}
token = auth.removeprefix("Bearer ").strip()
try:
payload = jwt.decode(token, JWT_SECRET_KEY, algorithms=[JWT_ALGORITHM])
return payload
except jwt.PyJWTError:
return {}