Compare commits
3
Commits
ab31d828d4
...
8b65c4aeed
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8b65c4aeed | ||
|
|
c9d6dcaf97 | ||
|
|
f8e1c77bd3 |
No files matched your search
@@ -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 实例上任意仍然有效的访问令牌。审查要求的决定已经落地,没有为此改校验代码。
|
||||
@@ -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
@@ -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"
|
||||
}
|
||||
+4
-2
@@ -25,10 +25,11 @@
|
||||
| D17 | 仓库 `gatePolicy.defaultMode` 保持 `balanced`;Tier L 保持 `prefer-strict`/`full-marionettist`。本任务曾出现的 autonomous/放宽漂移已回滚,不作为 M01 交付 | 2026-11-17 独立 review S1-5;用户授权处理必改项时回滚 |
|
||||
| D18 | Compose 容器内 `API_HOST=0.0.0.0` 仅开发且必须行内标注;宿主发布端口仍绑 `127.0.0.1` | 2026-11-17 独立 review 顺带项 |
|
||||
| D19 | M02 授权:Casdoor 分配 `watchman-admin`、`watchman-operator`、`watchman-viewer`。Watchman 只校验,不建角色表和授权管理页。登录用自有页面,后端向新建的 Watchman Application 校验密码,并接收 HXOPS 短时断言。不复用 HXOPS client。行级数据范围留到后续业务模块 | 2026-11-17 用户确认按此冻结 |
|
||||
| D20 | 地址上的访问令牌只要在这套 Casdoor 上仍然有效就接受,不另核签发应用。没有 Watchman 角色的用户仍然进不了业务界面。密码登录继续使用 Watchman Application | 2026-11-17 用户在 O11 中选择接受该实例上任意仍然有效的访问令牌 |
|
||||
|
||||
## 冻结前/相关模块实现前必须确认
|
||||
|
||||
下表保留旧细项编号用于历史定位;当前阻塞单位按映射为:O01/O07→M08,O02/O03→M09,O04→M06/M09,O05→M06,O06→M05/M06,O08→M01及所有委派,O09→M01/M07/M08/M13,O10→M02。旧依赖不是当前大任务流程。
|
||||
下表保留旧细项编号用于历史定位;当前阻塞单位按映射为:O01/O07→M08,O02/O03→M09,O04→M06/M09,O05→M06,O06→M05/M06,O08→M01及所有委派,O09→M01/M07/M08/M13,O10→M02。O11 已由 D20 确认。旧依赖不是当前大任务流程。
|
||||
|
||||
| ID | 状态 | 问题 | 建议供选择,非默认已批准 | 阻塞 |
|
||||
|---|---|---|---|---|
|
||||
@@ -41,7 +42,8 @@
|
||||
| O07 | OPEN | 停机漏跑、跨周期任务重叠、启停及改 Cron 后的旧任务 | 建议每场景不并发推进进度,逾期运行有界跳过/显式补跑且不自动补发历史消息;用户需确认具体业务行为 | P-004、A-033~A-036 |
|
||||
| O08 | OPEN | Cursor/Grok 等实际服务商能处理哪些项目资料?能否跨系列子 Agent? | 先确认交接文档、未来源码、合成数据和截图授权;默认不读取原会议或 MES 原始资料,不发真实凭证。用可用模型目录填写角色映射,不能默认已获所有服务商授权 | 所有实际模型委派 |
|
||||
| O09 | TECH_PENDING | Python/Node/依赖版本,Cron 解析器和编辑组件,AI SDK,企微库 | M01 已锁定:Python 3.12.16(uv)、FastAPI 0.163、SQLAlchemy 2.0.57、Alembic 1.20、Celery 5.7、Pydantic 2.14、Vue 3.5 / Element Plus 2.x / Vite 7 / pnpm 12。其余(Cron/AI/企微)仍待后续模块 | P-002、P-004、A-056、B-001 |
|
||||
| O10 | TECH_PENDING | Watchman Application 已在本机 `.env.local`。密码登录已用一个已有账号走过。HXOPS 真实断言签发仍未做 | 密钥不入库。本地签名测试和一次密码登录不能写成 A-074 真实入口已全部通过 | A-008~A-010、A-074 |
|
||||
| O10 | TECH_PENDING | Watchman Application 已在本机 `.env.local`。密码登录走过一次。约定跳转是地址上的 Casdoor 访问令牌,后端调用 Casdoor userinfo,这条也在本机走过一次。不接 HXOPS 自己的断言签发 | 密钥和访问令牌不入库。`POST /auth/hxops-entry` 仍要求 `casdoor_sub`,但不是当前跳转入口。模块尚未标 DONE_LOCAL | A-008~A-010、A-074 |
|
||||
| O11 | CONFIRMED | 见 D20。地址令牌不核对签发应用 | 用户选择接受该 Casdoor 实例上任意仍然有效的访问令牌 | A-008、A-074 |
|
||||
|
||||
O01~O07 不要求全在第一天解决,但其依赖切片不能带着猜测宣布完成。用户可一次批准建议,也可逐项给不同口径。无关切片在授权开工后仍可推进。
|
||||
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 11 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 108 KiB After Width: | Height: | Size: 108 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 10 KiB |
+6
-6
@@ -2,11 +2,11 @@
|
||||
|
||||
## 当前快照
|
||||
|
||||
- 当前阶段:M02 模块审查发现的必改已修。pytest 54,前端 unit 9。未标 DONE_LOCAL。HXOPS 真实跳转仍未做。
|
||||
- 当前大任务:M02 统一登录与权限。上一完成:M01 基础框架(DONE_LOCAL)。
|
||||
- 用户授权:已确认提交并推送当前 M02 实现。HXOPS 真实跳转仍未做,不把本模块标成 DONE_LOCAL。
|
||||
- Git:本轮提交 M02,并推送到 origin。
|
||||
- 下一步:用户确认是否把 M02 标为 DONE_LOCAL。真实 HXOPS 跳转继续单列未完成。
|
||||
- 当前阶段:M02 统一登录与权限已 DONE_LOCAL。
|
||||
- 当前大任务:无进行中的模块。上一完成:M02。再之前:M01 基础框架(DONE_LOCAL)。
|
||||
- 用户授权:地址携带 Casdoor 访问令牌。前端只提交该令牌,后端调用 Casdoor 校验。D20 接受该实例上任意仍然有效的访问令牌。用户已确认提交并完成 M02。
|
||||
- Git:M02 候选 `f8e1c77`。其上一次已推送的是 `ab31d82`。`f8e1c77` 尚未推送。
|
||||
- 下一步:等用户指定下一个模块。不自动开工。
|
||||
- RabbitMQ 容器可能仍在运行:`watchman-dev-rabbitmq`。
|
||||
|
||||
## 大任务状态
|
||||
@@ -14,7 +14,7 @@
|
||||
| 大任务/当前内部步骤 | 状态 | 负责人角色/实际模型 | 候选版本标识 | Review | 测试/UI 证据 | 阻塞/下一步 |
|
||||
|---|---|---|---|---|---|---|
|
||||
| M01 基础框架 | DONE_LOCAL | 实现 Composer;审查/复审异系列 | `8fb16f4` | PASS_WITH_WARNINGS(必改已关) | pytest 23;unit 8;e2e 3 | 已完成 |
|
||||
| M02 统一登录与权限 | 审查必改已修,待最终确认 | 审查 Claude;修复主 Agent | 未提交 | 整体审查 FAIL 后必改已关,未做第二次复审 | pytest 54;unit 9;此前 e2e 5 | HXOPS 真跳转未做 |
|
||||
| M02 统一登录与权限 | DONE_LOCAL | 主 Agent;复审 claude-opus | `f8e1c77` | 复审阻塞已由 D20 关闭 | pytest 58;unit 10;登录 Playwright 4 | 已完成 |
|
||||
|
||||
## 当前阻塞
|
||||
|
||||
|
||||
+6
-4
@@ -91,13 +91,15 @@ Playwright 截图写入 `docs/evidence/M01/ui/`(相对仓库根,勿再多上
|
||||
|
||||
## 登录(M02)
|
||||
|
||||
管理台先访问 `GET /auth/me`。没有会话时进入登录页。密码登录走 `POST /auth/login`,成功后浏览器只保存 HttpOnly cookie。`GET /auth/me` 返回身份和角色,`POST /auth/logout` 只结束 Watchman 会话。
|
||||
管理台加载时如果地址带 `token`,前端只把这个值交给 `POST /auth/token-login`,并马上从地址栏去掉。前端不解析、也不自行判断这个令牌。后端用它请求 Casdoor 的 userinfo,不把该令牌写入会话或响应。这条路径只要求配置了 `CASDOOR_ENDPOINT`。Casdoor 拒绝时返回 41000;Casdoor 不可用时返回 503。通过后建立的是 Watchman 会话:HttpOnly cookie,响应里另有本服务的 `access_token`。这个 `access_token` 不是 Casdoor 令牌,前端放在 sessionStorage。按 D20,userinfo 成功即可,不核对令牌是哪个应用签发的。没有 Watchman 角色的用户仍然只看到无权限。
|
||||
|
||||
没有 `token` 时先访问 `GET /auth/me`。没有会话就进入登录页。密码登录走 `POST /auth/login`。`GET /auth/me` 返回身份和角色,`POST /auth/logout` 只结束 Watchman 会话。
|
||||
|
||||
Casdoor 上的三个角色是 `watchman-admin`、`watchman-operator`、`watchman-viewer`。没有这三个角色时,页面只显示无权限。`POST /platform/audit-events` 和 `POST /platform/run-records` 需要 `watchman-admin`:未登录是 41000,已登录但角色不够是 42000。角色快照超过 5 分钟会按登录名再问 Casdoor;问失败则本次操作返回 503,不沿用旧角色。
|
||||
|
||||
`POST /auth/hxops-entry` 接收短时断言,并且必须带 `casdoor_sub`。HXOPS 令牌自己的 `sub` 不能当作 Casdoor 身份。未配置 `HXOPS_SSO_ISSUER`、`HXOPS_SSO_AUDIENCE`、`HXOPS_SSO_PUBLIC_KEY` 或 `CASDOOR_ISSUER` 时返回 503。断言里的角色不会被采用。HXOPS 尚未向 Watchman 签发真实断言,所以这条路径只有本地签名测试,不能写成真实跳转已通过。
|
||||
`POST /auth/hxops-entry` 仍接收短时断言,并且必须带 `casdoor_sub`。断言自己的 `sub` 不能当作 Casdoor 身份。未配置 `HXOPS_SSO_ISSUER`、`HXOPS_SSO_AUDIENCE`、`HXOPS_SSO_PUBLIC_KEY` 或 `CASDOOR_ISSUER` 时返回 503。断言里的角色不会被采用。当前从 HXOPS 进来不走这条接口,走上面的地址令牌。
|
||||
|
||||
本机曾用一个已有 Casdoor 账号走通密码登录、当前用户和退出。这不替代 HXOPS 真实入口验收。密钥和密码不写入仓库。前端 `VITE_AUTH_MODE=fixture` 时,登录页会显示本地身份标识。
|
||||
本机曾用一个已有 Casdoor 账号走通密码登录,并用 Casdoor 签发的访问令牌走通地址自动登录。密钥、密码和访问令牌不写入仓库。前端 `VITE_AUTH_MODE=fixture` 时,登录页会显示本地身份标识。
|
||||
|
||||
## 端口(仅开发,全部绑 127.0.0.1)
|
||||
|
||||
@@ -112,7 +114,7 @@ Casdoor 上的三个角色是 `watchman-admin`、`watchman-operator`、`watchman
|
||||
|
||||
## 当前已知限制
|
||||
|
||||
- 正式 Casdoor 密码登录已在本机走过一次;HXOPS 真实跳转仍未做。
|
||||
- 正式 Casdoor 密码登录和地址令牌登录都在本机走过一次。页面上没有「从 HXOPS 进入」按钮。
|
||||
- 平台写接口需要已登录的 `watchman-admin`。健康检查仍然匿名。
|
||||
- Compose `app` profile 依赖 Docker 构建;日常开发可用宿主机进程 + 仅 RabbitMQ 容器。
|
||||
- 容器内 `API_HOST=0.0.0.0` 仅开发监听(D18);宿主端口仍绑 `127.0.0.1`。
|
||||
+15
-13
@@ -1,6 +1,6 @@
|
||||
# M02 统一登录与权限
|
||||
|
||||
一个会话聚焦本模块。S1–S6 已实现并写入说明。模块整体独立 review 尚未做,不能标 DONE_LOCAL。
|
||||
一个会话聚焦本模块。S1–S6 和地址令牌登录已实现。测试与复审已完成。状态:DONE_LOCAL。
|
||||
|
||||
## 内部步骤(主 Agent 根据现有代码安排)
|
||||
- [x] 确认门禁、登录方式、授权落点和 Casdoor 应用策略。见 D19。
|
||||
@@ -8,21 +8,23 @@
|
||||
- [x] S1 令牌校验。critic PASS,复审 PASS。
|
||||
- [x] S2 会话与密码登录。审查 PASS_WITH_WARNINGS。随后补上真实 Casdoor 密码校验。
|
||||
- [x] S3 角色收口。匿名 41000,非管理者 42000,过期角色刷新失败则 503。
|
||||
- [x] S4 HXOPS 短时断言。验签后按 Casdoor 角色建会话。真实跳转未联调。
|
||||
- [x] S4 签名断言入口仍保留:`POST /auth/hxops-entry` 必须带 `casdoor_sub`。当前跳转不走这个按钮或这条签发。
|
||||
- [x] S5 登录壳。失败、无权限、退出已由 Playwright 覆盖。
|
||||
- [x] S6 运行说明和示例配置。`.env.example` 只有占位。
|
||||
- [ ] 模块整体独立 review、修复和复审。
|
||||
- [ ] 对最终候选再跑一轮正式测试。
|
||||
- [ ] 主 Agent 验收、进度和交接。
|
||||
- [x] 地址令牌登录。前端只提交 `?token=`,后端调用 Casdoor userinfo。本机用真实 Casdoor 访问令牌打开过。
|
||||
- [x] 对当前代码重跑后端、前端单测和登录 Playwright。
|
||||
- [x] 覆盖地址令牌登录的独立复审。结论 BLOCKED,见 `.task/2026-11-17/m02-auth/reviews/reviewer-m02-token-login.md`。
|
||||
- [x] O11 已由 D20 确认:接受该 Casdoor 实例上任意仍然有效的访问令牌。
|
||||
- [x] 用户确认后标 DONE_LOCAL。
|
||||
|
||||
当前恢复点:S6 文档已更新。下一步是模块整体 review,而不是下一个业务模块。
|
||||
当前恢复点:M02 已 DONE_LOCAL。不自动进入下一个业务模块。
|
||||
|
||||
## 目标与边界
|
||||
- 阶段/需求编号:M02;覆盖原细项 A-008~A-010、A-074。
|
||||
- 依赖及当前状态:M01 DONE_LOCAL。平台写接口现在要求 `watchman-admin`。
|
||||
- 必须已确认的决策:D01、D14、D19。门禁 selected=balanced。
|
||||
- 模块可验收行为:后端校验签名、issuer、audience、期限和角色;对象 ID 本身不能授权。
|
||||
- 明确不做:用户管理后台、组织全量同步、企微通知、场景与数据源业务功能。HXOPS 真实跳转未做,不能写成通过。
|
||||
- 模块可验收行为:密码登录和签名断言校验签名、issuer、audience、期限和角色。地址令牌按 D20 只要求 Casdoor userinfo 成功,不核对签发应用;没有 Watchman 角色仍然不能进业务界面。对象 ID 本身不能授权。
|
||||
- 明确不做:用户管理后台、组织全量同步、企微通知、场景与数据源业务功能。不接 HXOPS 自己的断言签发服务。地址上的令牌必须由 Casdoor 校验。
|
||||
- 输入/输出契约及失败语义:沿用 M01 信封。未登录 41000,无权限 42000 且带权限点。
|
||||
- 权限点:`perm:app:emp:watchman.enter`、`perm:data:emp:watchman.platform.write`、`perm:data:emp:watchman.object.read`。
|
||||
|
||||
@@ -34,8 +36,8 @@
|
||||
- UI:登录失败、无权限、退出。截图在 `docs/evidence/M02/ui/`。
|
||||
|
||||
## 证据与结论(主 Agent 汇总)
|
||||
- 后端测试:`uv run pytest services/watchman/tests -q` → **54 passed**(审查必改之后)。
|
||||
- 前端:`pnpm --filter @watchman/console test:unit` → 9 passed。Playwright 健康检查 3 在 S5 通过。登录模拟又补了成功路径:`pnpm exec playwright test e2e/login.spec.ts -g "sign in as an admin"` → 1 passed。截图 `docs/evidence/M02/ui/login-success.png`。这次是拦截接口的模拟,不是再打真实 Casdoor。
|
||||
- 本机密码登录、当前用户和退出走过一次。HXOPS 真实跳转未做。
|
||||
- 模块整体审查为 FAIL。必改是 HXOPS `sub` 冒充,已改为必须带 `casdoor_sub`。没有第二次独立复审。
|
||||
- 最终状态:必改已关,等待用户确认是否标 DONE_LOCAL。真实 HXOPS 入口仍单列未完成。
|
||||
- 后端测试:`uv run pytest services/watchman/tests -q` → **58 passed**(2026-11-17 收尾重跑,含地址令牌接口)。
|
||||
- 前端:`pnpm --filter @watchman/console test:unit` → **10 passed**。`pnpm exec playwright test e2e/login.spec.ts` → **4 passed**。Playwright 拦截了接口,截图在 `docs/evidence/M02/ui/`。
|
||||
- 本机真实 Casdoor:密码登录走过一次。地址上的 Casdoor 访问令牌也走过一次,`POST /auth/token-login` 返回 200,页面显示已登录,地址栏里的 token 被清掉。令牌本身不入库。
|
||||
- 第一次模块整体审查为 FAIL。必改是 HXOPS `sub` 冒充,已改为必须带 `casdoor_sub`。
|
||||
- 地址令牌复审曾为 BLOCKED,阻塞项是签发应用范围。用户已用 D20 确认接受该实例上任意有效令牌,并确认本模块完成。最终状态:DONE_LOCAL。
|
||||
Binary file not shown.
@@ -41,6 +41,12 @@ class HxopsEntryRequest(BaseModel):
|
||||
assertion: str = Field(min_length=1)
|
||||
|
||||
|
||||
class BearerTokenRequest(BaseModel):
|
||||
"""Casdoor access token taken from the incoming URL. Validity is checked with Casdoor."""
|
||||
|
||||
token: str = Field(min_length=1)
|
||||
|
||||
|
||||
def _public_identity(row: object, *, access_token: str | None = None) -> dict[str, object]:
|
||||
"""Map stored session data to the fields the browser is allowed to keep."""
|
||||
data: dict[str, object] = {
|
||||
@@ -93,6 +99,25 @@ def login(
|
||||
return _session_response(request, row, token)
|
||||
|
||||
|
||||
@router.post("/token-login")
|
||||
def token_login(
|
||||
request: Request,
|
||||
body: BearerTokenRequest,
|
||||
casdoor: CasdoorPort = Depends(get_casdoor_port),
|
||||
sessions: SessionStore = Depends(get_session_store),
|
||||
) -> JSONResponse:
|
||||
"""Validate a Casdoor access token by calling Casdoor, then open a Watchman session."""
|
||||
identity = casdoor.authenticate_bearer(body.token)
|
||||
session_token = issue_cookie_token()
|
||||
row = sessions.create(
|
||||
token_hash=hash_cookie_token(session_token),
|
||||
identity=identity,
|
||||
now=utc_now(),
|
||||
login_name=identity.login_name or identity.subject,
|
||||
)
|
||||
return _session_response(request, row, session_token)
|
||||
|
||||
|
||||
@router.post("/hxops-entry")
|
||||
def hxops_entry(
|
||||
request: Request,
|
||||
|
||||
@@ -21,6 +21,7 @@ class CasdoorIdentity:
|
||||
issuer: str
|
||||
display_name: str
|
||||
role_names: list[str]
|
||||
login_name: str = ""
|
||||
|
||||
|
||||
class CasdoorPort(Protocol):
|
||||
@@ -29,6 +30,9 @@ class CasdoorPort(Protocol):
|
||||
def authenticate_password(self, *, username: str, password: str) -> CasdoorIdentity:
|
||||
"""Return a verified identity or raise a semantic authentication/service error."""
|
||||
|
||||
def authenticate_bearer(self, token: str) -> CasdoorIdentity:
|
||||
"""Validate an access token with Casdoor and return the identity Casdoor reports."""
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class HttpResponse:
|
||||
@@ -158,11 +162,64 @@ class DefaultCasdoorPort:
|
||||
if not isinstance(subject, str) or not subject:
|
||||
raise UnauthorizedError("Invalid username or password")
|
||||
display_name = userinfo.get("name") or userinfo.get("preferred_username") or username
|
||||
login_name = userinfo.get("preferred_username")
|
||||
if not isinstance(login_name, str) or not login_name:
|
||||
login_name = username
|
||||
return CasdoorIdentity(
|
||||
subject=subject,
|
||||
issuer=self._settings.casdoor_issuer or self._settings.casdoor_endpoint,
|
||||
display_name=display_name if isinstance(display_name, str) else username,
|
||||
role_names=self._role_names(userinfo.get("roles")),
|
||||
login_name=login_name,
|
||||
)
|
||||
|
||||
def authenticate_bearer(self, token: str) -> CasdoorIdentity:
|
||||
"""Ask Casdoor whether this access token is valid, then use the userinfo it returns.
|
||||
|
||||
The token string is never compared to a local allow-list and is not logged.
|
||||
"""
|
||||
if not self._settings.casdoor_endpoint:
|
||||
raise ServiceUnavailableError("Casdoor login is unavailable")
|
||||
if not token.strip():
|
||||
raise UnauthorizedError()
|
||||
try:
|
||||
discovery = self._json_response(
|
||||
self._transport.request(
|
||||
method="GET",
|
||||
url=f"{self._settings.casdoor_endpoint.rstrip('/')}/.well-known/openid-configuration",
|
||||
headers={"Accept": "application/json"},
|
||||
)
|
||||
)
|
||||
userinfo_endpoint = self._required_endpoint(discovery, "userinfo_endpoint")
|
||||
userinfo_response = self._transport.request(
|
||||
method="GET",
|
||||
url=userinfo_endpoint,
|
||||
headers={
|
||||
"Accept": "application/json",
|
||||
"Authorization": f"Bearer {token}",
|
||||
},
|
||||
)
|
||||
except UnauthorizedError:
|
||||
raise
|
||||
except Exception as error:
|
||||
raise ServiceUnavailableError("Casdoor login is unavailable") from error
|
||||
if userinfo_response.status_code in {400, 401, 403}:
|
||||
raise UnauthorizedError()
|
||||
if userinfo_response.status_code < 200 or userinfo_response.status_code >= 300:
|
||||
raise ServiceUnavailableError("Casdoor login is unavailable")
|
||||
userinfo = self._decode_json(userinfo_response.body)
|
||||
subject = userinfo.get("sub")
|
||||
if not isinstance(subject, str) or not subject:
|
||||
raise UnauthorizedError()
|
||||
preferred = userinfo.get("preferred_username")
|
||||
login_name = preferred if isinstance(preferred, str) and preferred else subject
|
||||
display_name = userinfo.get("name") or login_name
|
||||
return CasdoorIdentity(
|
||||
subject=subject,
|
||||
issuer=self._settings.casdoor_issuer or self._settings.casdoor_endpoint,
|
||||
display_name=display_name if isinstance(display_name, str) else login_name,
|
||||
role_names=self._role_names(userinfo.get("roles")),
|
||||
login_name=login_name,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
|
||||
@@ -102,6 +102,9 @@ class RejectingCasdoor:
|
||||
def authenticate_password(self, *, username: str, password: str) -> CasdoorIdentity:
|
||||
raise UnauthorizedError()
|
||||
|
||||
def authenticate_bearer(self, token: str) -> CasdoorIdentity:
|
||||
raise UnauthorizedError()
|
||||
|
||||
|
||||
class FakeCasdoorTransport:
|
||||
"""Queue controlled HTTP results so Casdoor adapter tests never use the network."""
|
||||
@@ -249,6 +252,7 @@ def test_default_port_password_grant_returns_safe_identity_and_roles() -> None:
|
||||
issuer="https://issuer.casdoor.test",
|
||||
display_name="Employee",
|
||||
role_names=["watchman-admin"],
|
||||
login_name="employee-1",
|
||||
)
|
||||
assert FAKE_PASSWORD not in repr(identity)
|
||||
assert FAKE_ACCESS_TOKEN not in repr(identity)
|
||||
@@ -258,6 +262,86 @@ def test_default_port_password_grant_returns_safe_identity_and_roles() -> None:
|
||||
assert transport.calls[2][2]["Authorization"] == f"Bearer {FAKE_ACCESS_TOKEN}"
|
||||
|
||||
|
||||
def test_access_token_is_validated_by_casdoor_userinfo() -> None:
|
||||
"""A URL token is not trusted locally; Casdoor userinfo decides who it is."""
|
||||
transport = FakeCasdoorTransport(
|
||||
[
|
||||
HttpResponse(
|
||||
200,
|
||||
b'{"token_endpoint":"https://casdoor.test/token","userinfo_endpoint":"https://casdoor.test/userinfo"}',
|
||||
),
|
||||
HttpResponse(
|
||||
200,
|
||||
b'{"sub":"employee-1","name":"Employee","preferred_username":"26110002","roles":["watchman-admin"]}',
|
||||
),
|
||||
]
|
||||
)
|
||||
port = DefaultCasdoorPort(
|
||||
Settings(casdoor_endpoint="https://casdoor.test", casdoor_issuer="https://casdoor.test"),
|
||||
transport,
|
||||
)
|
||||
|
||||
identity = port.authenticate_bearer("casdoor-issued-token")
|
||||
|
||||
assert identity.subject == "employee-1"
|
||||
assert identity.login_name == "26110002"
|
||||
assert identity.role_names == ["watchman-admin"]
|
||||
assert transport.calls[1][0] == "GET"
|
||||
assert transport.calls[1][1] == "https://casdoor.test/userinfo"
|
||||
assert transport.calls[1][2]["Authorization"] == "Bearer casdoor-issued-token"
|
||||
|
||||
|
||||
def test_token_login_opens_session_from_casdoor_userinfo() -> None:
|
||||
"""The HTTP entry must pass the URL token to Casdoor and must not echo it back."""
|
||||
transport = FakeCasdoorTransport(
|
||||
[
|
||||
HttpResponse(
|
||||
200,
|
||||
b'{"token_endpoint":"https://casdoor.test/token","userinfo_endpoint":"https://casdoor.test/userinfo"}',
|
||||
),
|
||||
HttpResponse(
|
||||
200,
|
||||
b'{"sub":"employee-1","name":"Employee","preferred_username":"26110002","roles":["watchman-admin"]}',
|
||||
),
|
||||
]
|
||||
)
|
||||
port = DefaultCasdoorPort(
|
||||
Settings(casdoor_endpoint="https://casdoor.test", casdoor_issuer="https://casdoor.test"),
|
||||
transport,
|
||||
)
|
||||
app = create_app(validate_runtime=False)
|
||||
store = InMemorySessionStore()
|
||||
app.dependency_overrides[get_casdoor_port] = lambda: port
|
||||
app.dependency_overrides[get_session_store] = lambda: store
|
||||
|
||||
with TestClient(app) as client:
|
||||
response = client.post("/auth/token-login", json={"token": "casdoor-issued-token"})
|
||||
|
||||
assert response.status_code == 200
|
||||
body = response.json()["data"]
|
||||
assert body["subject"] == "employee-1"
|
||||
assert body["login_name"] == "26110002"
|
||||
assert body["roles"] == ["watchman-admin"]
|
||||
assert "casdoor-issued-token" not in response.text
|
||||
assert len(store.rows) == 1
|
||||
|
||||
|
||||
def test_rejected_token_login_is_unauthorized_and_creates_no_session() -> None:
|
||||
"""A Casdoor rejection of the URL token is 41000 and does not open a session."""
|
||||
app = create_app(validate_runtime=False)
|
||||
store = InMemorySessionStore()
|
||||
app.dependency_overrides[get_casdoor_port] = lambda: RejectingCasdoor()
|
||||
app.dependency_overrides[get_session_store] = lambda: store
|
||||
|
||||
with TestClient(app, raise_server_exceptions=False) as client:
|
||||
response = client.post("/auth/token-login", json={"token": "rejected-token"})
|
||||
|
||||
assert response.status_code == 401
|
||||
assert response.json()["error"]["code"] == 41000
|
||||
assert "rejected-token" not in response.text
|
||||
assert len(store.rows) == 0
|
||||
|
||||
|
||||
def test_default_port_invalid_grant_is_unauthorized() -> None:
|
||||
"""Casdoor invalid_grant is a generic credential rejection."""
|
||||
transport = FakeCasdoorTransport(
|
||||
|
||||
@@ -66,6 +66,51 @@ test.describe("login shell", () => {
|
||||
await expect(page.getByTestId("shell-welcome")).toHaveCount(0);
|
||||
});
|
||||
|
||||
test("return from HXOPS with a one-time assertion signs in", async ({ page }) => {
|
||||
await page.route("**/auth/me", async (route) => {
|
||||
await route.fulfill({
|
||||
status: 401,
|
||||
contentType: "application/json",
|
||||
body: JSON.stringify({
|
||||
success: false,
|
||||
error: { code: 41000, message: "Unauthorized", details: {}, retryable: false },
|
||||
request_id: "pw-anon",
|
||||
}),
|
||||
});
|
||||
});
|
||||
let postedAssertion = "";
|
||||
await page.route("**/auth/token-login", async (route) => {
|
||||
postedAssertion = route.request().postData() ?? "";
|
||||
await route.fulfill({
|
||||
status: 200,
|
||||
contentType: "application/json",
|
||||
body: envelope({
|
||||
subject: "casdoor-subject-1",
|
||||
display_name: "高昊宇",
|
||||
roles: ["watchman-admin"],
|
||||
access_token: "session-token",
|
||||
token_type: "Bearer",
|
||||
expires_in: 28800,
|
||||
}),
|
||||
});
|
||||
});
|
||||
await page.route("**/health", async (route) => {
|
||||
await route.fulfill({
|
||||
status: 200,
|
||||
contentType: "application/json",
|
||||
body: envelope({ status: "ok", app: "watchman", version: "0.1.0" }),
|
||||
});
|
||||
});
|
||||
|
||||
await page.goto("/?token=one-time-token");
|
||||
await expect(page.getByTestId("hxops-entry")).toHaveCount(0);
|
||||
await expect(page.getByTestId("shell-welcome")).toContainText("已登录:高昊宇");
|
||||
await expect(page.getByTestId("health-success")).toBeVisible();
|
||||
expect(postedAssertion).toContain("one-time-token");
|
||||
await expect(page).not.toHaveURL(/token=/);
|
||||
await page.screenshot({ path: path.join(evidenceDir, "hxops-return.png"), fullPage: true });
|
||||
});
|
||||
|
||||
test("failed sign-in stays on the form", async ({ page }) => {
|
||||
await page.route("**/auth/me", async (route) => {
|
||||
await route.fulfill({
|
||||
|
||||
@@ -82,6 +82,29 @@ async function signOut(): Promise<void> {
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const urlToken = params.get("token");
|
||||
if (urlToken) {
|
||||
params.delete("token");
|
||||
const query = params.toString();
|
||||
window.history.replaceState(null, "", query ? `/?${query}` : "/");
|
||||
try {
|
||||
applyIdentity(
|
||||
await api<SessionUser>("/auth/token-login", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ token: urlToken }),
|
||||
}),
|
||||
);
|
||||
return;
|
||||
} catch (error) {
|
||||
if (error instanceof BusinessError && error.status !== 401) {
|
||||
detail.value = String(error.code);
|
||||
}
|
||||
screen.value = "login";
|
||||
return;
|
||||
}
|
||||
}
|
||||
try {
|
||||
applyIdentity(await api<SessionUser>("/auth/me"));
|
||||
} catch (error) {
|
||||
|
||||
Vendored
+8
@@ -6,6 +6,14 @@ declare module "*.vue" {
|
||||
export default component;
|
||||
}
|
||||
|
||||
interface ImportMetaEnv {
|
||||
readonly VITE_AUTH_MODE?: string;
|
||||
}
|
||||
|
||||
interface ImportMeta {
|
||||
readonly env: ImportMetaEnv;
|
||||
}
|
||||
|
||||
declare module "*.json" {
|
||||
const value: Record<string, unknown>;
|
||||
export default value;
|
||||
|
||||
Reference in new issue
Block a user