From c3667fe00d76c0f363fa15bee1ca62c7f003c510 Mon Sep 17 00:00:00 2001 From: duxingchen Date: Mon, 21 Sep 2026 13:05:41 +0800 Subject: [PATCH] =?UTF-8?q?fix(audit):=20=E5=88=B7=E6=96=B0=E4=BB=A4?= =?UTF-8?q?=E7=89=8C=E8=AE=B0=E5=BD=95=E4=B8=8D=E5=86=8D=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E3=80=8C=E6=9C=AA=E8=AE=A4=E8=AF=81=E3=80=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题:审计列表里 POST /auth/refresh 的操作人恒为「未认证」。 根因:本接口刻意不挂 get_current_user —— 能用到这里,正是因为 access token 已过期、请求里没有 Authorization 头,JWT 依赖不执行,request.state 里 从未写入操作人。而"谁在何时尝试刷新"恰恰是该留痕的信息。 修复: - core/security.py 新增 peek_token_identity(),与 decode_token 的唯一区别是 关闭过期校验(刷新场景令牌本就过期,若因过期解不出来还是会漏记)。 签名校验照常进行,伪造令牌解不出任何东西。 ⚠️ docstring 中明确:该函数只许用于写审计字段,鉴权一律走 get_current_user - refresh 接口解码 refresh token 取得 sub/username/display_name/role 写入 state。 refresh token 的载荷与 access token 完全一致,只有 type 字段不同。 附带效果:活动打点读的正是 request.state.audit_user,修复后刷新请求也会 被计为一次活动 —— 语义正确(会刷新说明用户正在使用)。 验证(7/7):正常刷新记到中文人名与角色;过期令牌虽被拒 401 但仍能记到人; 伪造令牌不认人、显示未认证。历史记录不追溯。 --- backend/app/api/v1/endpoints/auth.py | 14 +++++++++++++- backend/app/core/security.py | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/backend/app/api/v1/endpoints/auth.py b/backend/app/api/v1/endpoints/auth.py index da1e198..20e42de 100644 --- a/backend/app/api/v1/endpoints/auth.py +++ b/backend/app/api/v1/endpoints/auth.py @@ -7,6 +7,7 @@ from app.schemas.user import ( RefreshResponse, UserResponse, ) +from app.core.security import peek_token_identity from app.services.auth_service import login, refresh_access_token, get_current_user router = APIRouter(prefix="/auth", tags=["认证"]) @@ -34,8 +35,19 @@ def login_endpoint(data: LoginRequest, request: Request): @router.post("/refresh", response_model=RefreshResponse) -def refresh_endpoint(data: RefreshRequest): +def refresh_endpoint(data: RefreshRequest, request: Request): """刷新 Access Token — 使用 Refresh Token 换取新的 Access Token""" + # 本接口刻意不挂 get_current_user:能用到这里,正是因为 access token 已经 + # 过期/缺失,请求里没有 Authorization 头,JWT 依赖不会执行 → 审计拿不到操作人, + # 记录只能显示「未认证」。 + # 但 refresh token 里本来就带着完整身份(sub/username/display_name/role), + # 解出来写进 state,审计才能记到人 —— 而"谁在何时尝试刷新"正是要留痕的。 + # 注意 peek 只用于审计标注,鉴权判断一律走 get_current_user。 + identity = peek_token_identity(data.refresh_token) + if identity: + request.state.audit_user = identity.get("username") or identity.get("sub") + request.state.audit_display_name = identity.get("display_name") or "" + request.state.audit_role = identity.get("role") or "" return refresh_access_token(data.refresh_token) diff --git a/backend/app/core/security.py b/backend/app/core/security.py index a080707..aa3fbd0 100644 --- a/backend/app/core/security.py +++ b/backend/app/core/security.py @@ -39,6 +39,28 @@ def decode_token(token: str) -> dict: return jwt.decode(token, settings.SECRET_KEY, algorithms=[ALGORITHM]) +def peek_token_identity(token: str) -> dict | None: + """读出令牌里的用户身份 —— **仅供审计标注,绝不可用于授权**。 + + 与 decode_token 的唯一区别:**关闭过期校验**。 + + 为什么需要它:刷新令牌接口正是"access token 过期了才来"的场景, + 请求里不带 Authorization 头,JWT 依赖根本不执行,审计只能记成 + 「未认证」—— 而"谁在什么时候尝试刷新"恰恰是该留痕的信息。 + 签名校验照常进行,伪造的令牌解不出任何东西。 + + ⚠️ 返回值只允许写进 request.state 的审计字段; + 任何鉴权判断一律走 get_current_user,不要用本函数。 + """ + try: + return jwt.decode( + token, settings.SECRET_KEY, algorithms=[ALGORITHM], + options={"verify_exp": False}, + ) + except JWTError: + return None + + def verify_password(plain_password: str, hashed_password: str) -> bool: """验证明文密码 vs 哈希密码""" return pwd_context.verify(plain_password, hashed_password)