feat(auth): 用 Casdoor 校验地址令牌并完成 M02

HXOPS 打开带 token 的地址时,前端只把令牌交给后端,后端向 Casdoor userinfo 校验。D20 接受该实例上任意仍然有效的访问令牌。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
unknownandCursor committed 2026-11-17 19:41:31 +08:00
1 parent ab31d828d4
commit f8e1c77bd3
17 files changed
+322 -114

No files matched your search

+33 -81
View File
@@ -2,7 +2,7 @@
## Task Goal
M02 用 Casdoor 做身份和三个粗角色。当前只实现 S2:可替换的 Casdoor 端口、服务端会话、密码登录、当前用户和退出。不保护平台写接口,不接收 HXOPS 断言,不做界面。
M02 用 Casdoor 做身份和三个粗角色。密码登录、会话、角色收口、登录页和地址令牌登录已经实现。D20 已确认地址令牌不核对签发应用。用户已确认完成,状态为 DONE_LOCAL。
## Knowledge Posture
@@ -11,18 +11,14 @@ M02 用 Casdoor 做身份和三个粗角色。当前只实现 S2:可替换的
## Current Slice Or Group
S2 会话与密码登录。用户已确认继续。S1 已复审 PASS。S3 及以后未批准开做。
收尾。地址令牌登录已由用户确认做法并实现。不开始下一个业务模块。
## Gate Policy
- Recommended: strict
- Selected: balanced
- finalApprovalRequired: true
- Policy Notes: S2 是 high-risk、risk_score 4、critic-required。做完必须停,不能续跑 S3。S1 的验签模块保持不动,除非 S2 必须调用它且不改变其拒绝规则。
## Bugfix Context
不适用。
- Policy Notes: 认证是 high-risk。O11 已由 D20 确认。标 DONE_LOCAL 仍要用户点头。
## Requirement Source
@@ -30,114 +26,70 @@ S2 会话与密码登录。用户已确认继续。S1 已复审 PASS。S3 及以
## Implementation Source
`.task/2026-11-17/m02-auth/implementation-plan.md` 的 Slice 2
`.task/2026-11-17/m02-auth/implementation-plan.md`。地址令牌是其后按用户指令追加的入口,不在原切片正文里。
## Involved Modules Or Areas
`services/watchman` 内新建 `watchman.auth` 校验模块和对应测试。
## Loaded Context
### Global Rules
AGENTS.md 的安全底线:秘密不入库、不进日志。D19 已确认授权模型。
### Knowledge Map Matches
无单独认证知识区。以任务需求和 M01 当前代码为准。
### Path-Proximity Rules
`services/watchman` 下没有 `MODULE_RULES.md`。
### Excluded Context
未加载 Cron、报警、企微、syops 实现源码。S1 不读 `.env.local`。
`services/watchman` 的 `watchman.auth` 与 `/auth` 路由;`web/apps/console` 的登录壳。
## Loaded Rules
- `standards://api/api-envelope?version=1.1.0`(本切片不新增 HTTP)
- `standards://api/api-envelope?version=1.1.0`
- `standards://security/red-line?version=1.1.0`
- `standards://engineering/code-comments?version=1.1.1`(实现时检索原文)
- D14、D19
- `standards://engineering/code-comments?version=1.1.1`
- D14、D19、O10、O11
## Loaded Docs
- M02 requirement 与 implementation plan
- `docs/decisions.md` 的 D19、O10
## Execution Mode
- Mode: sequential
- Current Slice Or Group: S2
- Parallel Members: 无
- Fallback Order: 2
- Shared Files: `app.py`、`config.py`、迁移目录
- Merge Owner: 主 Agent
- Conflict Resolution Rule: 不并行
- Validation Level: slice
## Execution Chain
用户确认 S2 → critic → 只实现 S2 → 跑 pytest → 异系列审查 → 停在 S3 之前。
- `docs/tasks/M02.md`
- `docs/runbook.md` 的登录节
- `docs/decisions.md` 的 D19、O10、O11
- `.task/2026-11-17/m02-auth/reviews/reviewer-m02-token-login.md`
## Allowed Modification Scope
- `services/watchman/src/watchman/auth/`
- `services/watchman/src/watchman/api/` 的登录路由和注册
- `services/watchman/src/watchman/core/config.py`
- `services/watchman/src/watchman/models/` 与 `services/watchman/migrations/`,仅限会话表
- `services/watchman/src/watchman/api/routes/auth.py`
- `services/watchman/tests/` 的登录与会话测试
- `.env.example` 已有占位,不要写入真实值
- `web/apps/console` 的登录壳、会话和登录 e2e
- `docs/tasks/M02.md`、`docs/progress.md`、`docs/runbook.md`、`docs/decisions.md`
- 本任务目录下的审查记录和本 context pack
## Forbidden Modification Scope
- 前端页面、平台写接口的授权、HXOPS 断言
- 不要再给地址令牌加签发应用校验,D20 已经否掉这条
- `D:\PROJECT\syops` 的任何写入
- 读取、打印或复制 `.env.local` 里的密钥
- 新的直接依赖和 `uv.lock` 改写
- 读取、打印或复制 `.env.local`、访问令牌
- 角色表、用户授权表、密码哈希
- 下一个业务模块
## Key Existing Classes Or Entrypoints
## Key Existing Entrypoints
- S1:`watchman.auth.token_verifier.verify_rs256_token`
- `UnauthorizedError` 为 41000,`ServiceUnavailableError` 为 503 且 `retryable`
- 信封:`watchman.core.response.success_body`
- RLS:`migrations/versions/20261117_0001_platform_rls.py`,`set_tenant` 只用服务端 `WATCHMAN_TENANT_ID`
- 路由注册在 `watchman.api.app.create_app`
- `POST /auth/login`:密码登录
- `POST /auth/token-login`:地址令牌,后端调用 Casdoor userinfo
- `POST /auth/hxops-entry`:签名断言,必须带 `casdoor_sub`
- `GET /auth/me`、`POST /auth/logout`
## Required Behavior
密码登录通过可注入端口。成功写入会话并保存端口返回的角色,失败 41000。端口未配置或不可用返回 503,且不建立会话。当前用户不返回秘密。退出后同一会话不能再读到当前用户。健康检查仍匿名。租户只来自服务端配置。不保存密码、client secret 和 Casdoor token。
## Non-goals
平台写接口收口、对象检查、HXOPS 断言、登录页、真实 Casdoor 网络测试。
## Implementation Steps
1. 增加带 RLS 的会话表和迁移,字段只包括身份主体、签发方、显示名、角色快照、角色确认时间、过期时间、服务端租户。
2. 登录、当前用户、退出。Casdoor 调用可替换,测试不访问网络。
3. 断言响应里没有密码、secret 和 token。
前端发现 `?token=` 后只提交给后端,并从地址栏去掉。后端不把该令牌写入会话、响应或日志。Casdoor 拒绝为 41000,不可用为 503。通过后的会话令牌是 Watchman 自己的。
## Validation Commands
`uv run pytest services/watchman/tests -q`
- `uv run pytest services/watchman/tests -q`
- `pnpm --filter @watchman/console test:unit`
- `pnpm exec playwright test e2e/login.spec.ts`(在 `web/apps/console`)
## Assumptions
- 会话 8 小时。角色快照可以写入,但 5 分钟后的重新确认属于 S3。
- 浏览器会话用 HttpOnly cookie。本地 HTTP 开发不强制 Secure。
- 真实 Casdoor 客户端可以写,但测试必须注入替身,不能读取 `.env.local`。
- 会话 8 小时。浏览器同时有 HttpOnly cookie 和 sessionStorage 里的 Watchman access token。
- 真实 Casdoor 只在用户明确要求时调用。测试注入替身。
## Risks
- 迁移一旦执行,回滚要有 down。
- 测试若加载本机 `.env.local`,可能误打真实 Casdoor。
- S2 结束时平台写接口仍然匿名可写。
- D20:同一 Casdoor 实例上其他应用的有效访问令牌可以换 Watchman 会话。这是已确认行为。
- 地址上的令牌会出现在承接页面的访问日志里。
## Stop Conditions
- critic 判定不能安全开做时停止。
- S2 审查完成或需要用户决定时停止。
- 不开始 S3。
- M02 已 DONE_LOCAL。不开始下一个模块,除非用户另行指定。
@@ -0,0 +1,8 @@
# Review — M02 地址令牌登录
- 审查:[M02 token login review](e5113ddc-6c2c-4e01-8e00-2381e1b5e072),claude-opus-5-thinking-high。结论:BLOCKED。
- 代码路径与已确认行为一致:前端只发现 `?token=` 并交给 `POST /auth/token-login`;后端只拿该令牌请求 Casdoor userinfo。没有本地白名单,也没有把令牌写进日志、会话或响应。`POST /auth/hxops-entry` 仍要求 `casdoor_sub`,这次 diff 没有改它。
- 审查当时重跑:pytest 58 passed,前端 unit 10 passed,`vue-tsc` 通过。Playwright 4 passed 是主 Agent 在同一工作区重跑的,审查没有再跑。
- 必改不是实现方式,而是口径:`authenticate_bearer` 只证明令牌在这套 Casdoor 上仍然有效,不核对是哪个应用签发的。同一实例里其他应用的访问令牌,只要用户有 Watchman 角色,就能换到 Watchman 会话。签名断言那条路径会核对 issuer 和 audience,这条不会。标 DONE_LOCAL 前要用户选择:只接受指定应用的令牌,或明确接受该 Casdoor 实例上任意仍然有效的访问令牌。见 O11。未替用户做这个决定。
- 审查看到的 `docs/progress.md` 和 `docs/runbook.md` 还是旧的 HXOPS 按钮口径。这两处已在审查之后改成地址令牌。任务记录当时仍停在 S2/S6,已补到当前收尾。
- 2026-11-17 用户确认 D20:接受该 Casdoor 实例上任意仍然有效的访问令牌。审查要求的决定已经落地,没有为此改校验代码。
+3 -3
View File
@@ -1,6 +1,6 @@
{
"taskId": "2026-11-17/m02-auth",
"phase": "review",
"phase": "done",
"allowedToCode": false,
"gatePolicy": {
"recommended": "strict",
@@ -10,6 +10,6 @@
},
"requirement": ".task/2026-11-17/m02-auth/requirement.md",
"implementationPlan": ".task/2026-11-17/m02-auth/implementation-plan.md",
"currentSlice": "S2",
"nextGate": "user-confirm-s3"
"currentSlice": "done",
"nextGate": "none"
}
+5 -5
View File
@@ -1,10 +1,10 @@
{
"taskId": "2026-11-17/m02-auth",
"type": "feature",
"phase": "finalization",
"phase": "done",
"allowedToCode": false,
"currentSlice": "S6",
"lastGate": "s6-docs",
"nextCommand": "module-wide independent review before DONE_LOCAL",
"updatedAt": "2026-11-17T09:50:00Z"
"currentSlice": "done",
"lastGate": "user-confirm-done-local",
"nextCommand": "wait for the next module",
"updatedAt": "2026-11-17T11:40:00Z"
}