H0-05 用户 JWT 和 Admin JWT 密钥彻底隔离 #50
Closed
opened 2026-05-24 21:49:32 +08:00 by wangdl
·
4 comments
Labels
Clear labels
area:activity
活动/统计
area:admin
管理后台
area:admin-api
area:ai
AI/RAG
area:ai-runtime
AI Runtime / AI 分析体系相关
area:analytics
area:api
API 接口
area:auth
认证与授权
area:cos
对象存储
area:database
数据库/Migration
area:import
文件导入/解析
area:knowledge
知识库/知识点
area:learning-info
area:learning-session
area:quiz
测验/自测
area:reading-event
area:reading-progress
area:review
复习系统
area:security
安全相关
audit:api-admin-info
audit:api-info
audit:planned
已完成宏观规划,尚未代码审查
audit:reviewed
blocked-by:api-info-aggregation
blocked-by:api-info-core
blocked-by:api-info-ops
blocked-by:api-info-schema
blocked-by:processor
blocked-by:schema
priority:p0
最高优先级,阻塞发布
priority:p1
高优先级,里程碑必需
priority:p2
中优先级,后续版本
repo:api
API 仓库 Issue
status:blocked
被阻塞
status:done
已完成
status:partial
status:todo
type:aggregation
type:bug
缺陷修复
type:design
设计
type:docs
文档
type:feature
新功能
type:migration
type:refactor
重构
type:test
work:admin-api
work:aggregation
work:api
work:artifact
题目/卡片产物
work:audit
work:circuit-breaker
熔断
work:contract
work:design
架构/协议设计工作
work:docs
work:export
work:extend-existing
work:internal-api
Runtime 内部接口
work:job
Job 调度相关
work:new-module
work:new-table
work:ops
work:query
work:quota
额度/限流
work:schema
Prisma Schema 设计
work:security
work:service
Service 层实现
work:snapshot
Snapshot 构建
work:test
No Label
Milestone
No items
No Milestone
H0:iOS 对接阻断修复(P0)
Projects
Clear projects
No project
No Assignees
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: wangdl/api-server#50
Reference in New Issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
目标
彻底隔离 C 端用户 JWT 和 Admin JWT 的密钥体系,防止权限提升。
背景说明
当前生产环境可能未配置独立的 ADMIN_JWT_ACCESS_SECRET,存在 fallback 到 JWT_SECRET 的风险。user token 和 admin token 必须通过不同的密钥签发和验证。
模块职责
请设计和实现以下隔离措施:
禁止事项
✅ H0-05 修复完成
问题
用户 JWT 和管理员 JWT 共用同一密钥(
JWT_SECRET),ADMIN_JWT_ACCESS_SECRET未在生产环境配置。用户 token 无type字段,无法在网关层区分。修改
src/modules/auth/token.service.ts:9-14type: "user"字段src/common/guards/jwt-auth.guard.ts:47-50payload.type === "admin"→ 拒绝管理员 token 访问 CAPIsrc/config/jwt.config.ts:25-29ADMIN_JWT_ACCESS_SECRET必须显式设置,否则启动失败test/h0.e2e-spec.tsADMIN_JWT_ACCESS_SECRET隔离矩阵
/api/*type ≠ admin/api/*type === admin拒绝/admin-api/*type !== admin拒绝/admin-api/*type === admin放行JWT payload 对比
subtype"user"(新增)"admin"roleemailsessionId上线注意
必须在生产 env 中新增
ADMIN_JWT_ACCESS_SECRET,值必须与JWT_SECRET不同。建议使用openssl rand -hex 32生成:若不配置此项,API 服务将启动失败并报错:
测试
已实现 ✅ — 无需修改
该需求在
jwt.config.ts和JwtAuthGuard中已经完整实现:启动时检查 —
src/config/jwt.config.ts:23-27:运行时隔离 —
src/common/guards/jwt-auth.guard.ts:44-47:覆盖的检查项:
ADMIN_JWT_ACCESS_SECRETtype字段区分/admin-api/*(AdminAuthGuard 拒绝type: "user")/api/*(JwtAuthGuard 拒绝type: "admin")建议关闭此 issue。
H0-05 补充:结构化错误码体系
JWT 隔离已在之前完成,本次新增了 iOS 端可编程使用的语义错误码。
新增文件
src/common/errors/capi-error-codes.ts— 定义所有 CAPI 错误码常量src/common/errors/capi.exception.ts— 携带 errorCode 的 HttpException 子类修改文件
GlobalExceptionFiltererrorCode字段JwtAuthGuardAUTH_UNAUTHORIZED/AUTH_USER_DISABLED/AUTH_USER_DELETED/AUTH_WRONG_TOKEN_TYPEAuthService.refresh()AUTH_REFRESH_TOKEN_EXPIRED/AUTH_REFRESH_TOKEN_REVOKED/AUTH_USER_DISABLED/AUTH_USER_DELETEDAuthService.devLogin()AUTH_DEV_LOGIN_FORBIDDENAppleAuthServiceAUTH_INVALID_APPLE_TOKEN错误响应格式
iOS 端现在可以通过
errorCode做强类型分支判断,不再依赖中文 message 字符串。wangdl referenced this issue2026-06-05 19:34:44 +08:00
wangdl referenced this issue2026-06-05 19:36:08 +08:00
关闭
jwt.config.ts 已实现生产环境强制检查 ADMIN_JWT_ACCESS_SECRET。Admin/User JWT 使用独立密钥体系。