管理台可以密码登录、按角色进入,并把 Watchman 会话 token 交给浏览器。HXOPS 真实跳转仍未联调。 Co-authored-by: Cursor <cursoragent@cursor.com>
399 lines
13 KiB
Markdown
399 lines
13 KiB
Markdown
# M02 统一登录与权限 Implementation Plan
|
||
|
||
## Requirement Source
|
||
|
||
`.task/2026-11-17/m02-auth/requirement.md`
|
||
|
||
## Scope Summary
|
||
|
||
按已冻结需求接入 Casdoor 身份、三个粗角色和 Watchman 会话,收口 M01 平台写接口,并加上登录壳。不建角色表,不改 syops,不实现业务模块。
|
||
|
||
## Involved Modules Or Areas
|
||
|
||
Watchman 认证与现有平台 API、管理台薄壳。Casdoor 仅作为外部身份与角色来源。HXOPS 只作为短时断言的签发方,本仓库实现接收端。
|
||
|
||
## Loaded Rules
|
||
|
||
- 本任务 `gatePolicy.selected=balanced`,`finalApprovalRequired=true`。分析到编码仍须用户确认。
|
||
- `high-risk`、`boundary-sensitive`、critic 要求、`risk_score >= 4` 的切片完成后必须停。balanced 不能跳过这些停点。
|
||
- `standards://api/api-envelope?version=1.1.0`
|
||
- `standards://api/error-handling?version=1.1.1`
|
||
- `standards://api/permission-nodes?version=1.0.0`
|
||
- `standards://security/red-line?version=1.1.0`
|
||
- `standards://frontend/frontend-stack?version=2.1.0`
|
||
- `standards://frontend/element-plus?version=1.0.1`
|
||
- Watchman D01、D12、D13、D14、D19。
|
||
|
||
## Loaded Docs
|
||
|
||
- `.task/2026-11-17/m02-auth/requirement.md`
|
||
- `docs/tasks/INDEX.md` 的 M02
|
||
- `docs/decisions.md`
|
||
- syops 认证说明只作对照
|
||
|
||
## Global Forbidden Scope
|
||
|
||
- `D:\PROJECT\syops` 的任何写入,包括为了打通入口而改 HXOPS。
|
||
- 复制 syops `.env.dev` 的密钥、client id 或 audience 进仓库、文档、测试夹具。
|
||
- 用户表、角色表、授权管理页、密码哈希。
|
||
- 数据源、查询、规则、报警、通知、场景、AI。
|
||
- Redis、vben5、把健康检查改成必须登录。
|
||
- 未确认就新增三枚以外的权限点。
|
||
|
||
## Execution Strategy
|
||
|
||
- Complexity: complex
|
||
- Default Execution: sequential
|
||
- Parallel Execution: not-needed
|
||
- Fallback Execution: sequential
|
||
- Merge Owner: 主 Agent
|
||
- Conflict Resolution Rule: 认证切片按 S1 到 S6 的顺序写同一服务包。不并行。
|
||
|
||
selected `balanced` 对 S1–S5 不自动续跑:这些切片是 `high-risk` 或 `boundary-sensitive`,且要求 critic。S6 虽是 `simple`,也要等 S5 的停点结束之后再开始。
|
||
|
||
## Slice Dependency Graph
|
||
|
||
| Slice | Depends On | Can Run In Parallel With | Fallback Order | Shared Files | Merge Owner | Conflict Risk |
|
||
| --- | --- | --- | --- | --- | --- | --- |
|
||
| S1 令牌校验 | 无 | 无 | 1 | 无 | 主 Agent | low |
|
||
| S2 会话与密码登录 | S1 | 无 | 2 | `app.py`、`config.py` | 主 Agent | high |
|
||
| S3 角色收口 | S2 | 无 | 3 | 平台路由 | 主 Agent | high |
|
||
| S4 HXOPS 断言 | S2、S3 | 无 | 4 | `app.py`、会话 | 主 Agent | high |
|
||
| S5 登录壳 | S4 | 无 | 5 | 前端 API 客户端 | 主 Agent | medium |
|
||
| S6 文档与示例配置 | S5 | 无 | 6 | 任务卡、runbook、`.env.example` | 主 Agent | low |
|
||
|
||
## Implementation Slices
|
||
|
||
### Slice 1: 令牌校验
|
||
|
||
#### Goal
|
||
|
||
本地密钥下拒绝坏签名、错误 issuer、错误 audience 和过期令牌。这一步不发 HTTP 登录,不访问真实 Casdoor。
|
||
|
||
#### Allowed Modification Scope
|
||
|
||
- `services/watchman/src/watchman/auth/` 中的校验模块
|
||
- `services/watchman/tests/` 中对应该校验的测试
|
||
|
||
#### Forbidden Scope
|
||
|
||
- 路由、迁移、前端、`.env.local`、syops
|
||
|
||
#### Execution
|
||
|
||
- Mode: sequential
|
||
- Depends On: 无
|
||
- Can Run With: 无
|
||
- Must Not Run With: S2–S6
|
||
- Fallback Order: 1
|
||
- Shared Files: 无
|
||
- Merge Owner: 主 Agent
|
||
- Conflict Risk: low
|
||
- Gate Class: high-risk
|
||
- Risk Score: 4
|
||
- Gate Reasons: security-logic, critic-required
|
||
- Validation Level: slice
|
||
- Recommended Agent Strategy: 实现用编码模型;本切片审查用另一系列,并先读冻结需求
|
||
|
||
#### Steps
|
||
|
||
1. 实现只接受已验签声明的校验:issuer、audience、有效期、签名。
|
||
2. 用测试生成的密钥覆盖四类失败和一类成功。
|
||
3. 确认失败结果不能被当成已登录。
|
||
|
||
#### Validation
|
||
|
||
`uv run pytest services/watchman/tests -q`
|
||
|
||
#### Done Criteria
|
||
|
||
验收标准 1 在单元测试中有对应断言。仓库里没有真实密钥。
|
||
|
||
#### Rollback Notes
|
||
|
||
删除新增的 auth 校验模块和测试即可。无迁移。
|
||
|
||
### Slice 2: 会话与密码登录
|
||
|
||
#### Goal
|
||
|
||
用可替换的 Casdoor 替身完成密码登录、当前用户和退出。会话写入服务端,遵守 RLS。未配置真实应用时登录失败为 503,测试不依赖内网 Casdoor。
|
||
|
||
#### 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/` 与对应 Alembic 迁移,仅限会话
|
||
- `services/watchman/tests/` 的登录与会话测试
|
||
- `.env.example` 只加占位键名,值为空或 CHANGE-ME
|
||
|
||
#### Forbidden Scope
|
||
|
||
- 前端页面、平台写接口的授权、HXOPS 断言、syops、真实 secret
|
||
|
||
#### Execution
|
||
|
||
- Mode: sequential
|
||
- Depends On: S1
|
||
- Can Run With: 无
|
||
- Must Not Run With: S3–S6
|
||
- Fallback Order: 2
|
||
- Shared Files: `app.py`、`config.py`、迁移目录
|
||
- Merge Owner: 主 Agent
|
||
- Conflict Risk: high
|
||
- Gate Class: high-risk
|
||
- Risk Score: 4
|
||
- Gate Reasons: security-logic, schema-change, critic-required
|
||
- Validation Level: slice
|
||
- Recommended Agent Strategy: 实现与审查分属不同模型系列
|
||
|
||
#### Steps
|
||
|
||
1. 增加会话存储。字段限于需求中的数据规则。租户只用服务端配置。
|
||
2. 密码登录调用可注入的 Casdoor 端口。成功写会话并取回角色;失败 41000;端口不可用或未配置 503。
|
||
3. 提供当前用户与退出。退出后会话不可再用于后续切片的平台写接口。
|
||
4. 测试断言响应和日志路径不包含密码、secret、Casdoor token。
|
||
|
||
#### Validation
|
||
|
||
`uv run pytest services/watchman/tests -q`
|
||
|
||
#### Done Criteria
|
||
|
||
验收标准 2、8 的会话部分、9 有自动化证据。健康检查仍匿名可用。
|
||
|
||
#### Rollback Notes
|
||
|
||
回滚本切片迁移,并移除登录路由。不改动 S1 的纯校验测试。
|
||
|
||
### Slice 3: 角色收口与对象检查
|
||
|
||
#### Goal
|
||
|
||
平台写接口改为必须是 `watchman-admin`。进入能力与对象 ID 检查按冻结的三枚权限点拒绝。角色快照过期后,Casdoor 不可用则拒绝。
|
||
|
||
#### Allowed Modification Scope
|
||
|
||
- `services/watchman/src/watchman/auth/`
|
||
- `services/watchman/src/watchman/api/routes/platform.py`
|
||
- `services/watchman/tests/` 中权限与对象检查测试
|
||
|
||
#### Forbidden Scope
|
||
|
||
- 新的业务表、授权管理接口、前端、syops
|
||
|
||
#### Execution
|
||
|
||
- Mode: sequential
|
||
- Depends On: S2
|
||
- Can Run With: 无
|
||
- Must Not Run With: S4–S6
|
||
- Fallback Order: 3
|
||
- Shared Files: 平台路由、auth 依赖
|
||
- Merge Owner: 主 Agent
|
||
- Conflict Risk: high
|
||
- Gate Class: high-risk
|
||
- Risk Score: 4
|
||
- Gate Reasons: security-logic, permission-nodes, critic-required
|
||
- Validation Level: slice
|
||
- Recommended Agent Strategy: 实现与审查分属不同模型系列
|
||
|
||
#### Steps
|
||
|
||
1. 为三个权限点接上角色判断。无角色不升成查看者。
|
||
2. 平台两个写接口接入 `perm:data:emp:watchman.platform.write`。
|
||
3. 对象检查对两个合成 ID 在无授权时都返回 42000,权限点为 `perm:data:emp:watchman.object.read`。不新增公开的对象管理 API。
|
||
4. 角色确认超过 5 分钟且替身失败时,返回 503,不放行。
|
||
|
||
#### Validation
|
||
|
||
`uv run pytest services/watchman/tests -q`
|
||
|
||
#### Done Criteria
|
||
|
||
验收标准 3、4 的服务端部分、5、7 通过。41000 的响应里没有 `permission_node`。
|
||
|
||
#### Rollback Notes
|
||
|
||
恢复平台路由的 M01 行为会重新变成匿名可写。回滚时要连同本切片测试一起撤下,并在进度里写明平台写接口重新失去保护。
|
||
|
||
### Slice 4: HXOPS 短时断言
|
||
|
||
#### Goal
|
||
|
||
接收端校验短时断言,建立与密码登录相同的会话。角色只从 Casdoor 端口读取。
|
||
|
||
#### Allowed Modification Scope
|
||
|
||
- `services/watchman/src/watchman/auth/`
|
||
- `services/watchman/src/watchman/api/` 的断言入口
|
||
- `services/watchman/tests/` 的断言测试
|
||
|
||
#### Forbidden Scope
|
||
|
||
- 修改 syops、在断言中信任 HXOPS 权限列表、前端以外的业务模块
|
||
|
||
#### Execution
|
||
|
||
- Mode: sequential
|
||
- Depends On: S2、S3
|
||
- Can Run With: 无
|
||
- Must Not Run With: S5–S6
|
||
- Fallback Order: 4
|
||
- Shared Files: `app.py`、会话创建
|
||
- Merge Owner: 主 Agent
|
||
- Conflict Risk: high
|
||
- Gate Class: high-risk
|
||
- Risk Score: 4
|
||
- Gate Reasons: security-logic, external-identity, critic-required
|
||
- Validation Level: slice
|
||
- Recommended Agent Strategy: 实现与审查分属不同模型系列
|
||
|
||
#### Steps
|
||
|
||
1. 校验断言的签名、issuer、audience、有效期和 Casdoor 身份引用。
|
||
2. 缺少该引用,或只用 HXOPS 本地主键冒充时,返回 41000。
|
||
3. 通过后调用与密码登录相同的角色读取。断言内的权限字段不写入会话。
|
||
|
||
#### Validation
|
||
|
||
`uv run pytest services/watchman/tests -q`
|
||
|
||
#### Done Criteria
|
||
|
||
验收标准 6 通过。真实 HXOPS 跳转未配置时,证据里写明未做真实联调。
|
||
|
||
#### Rollback Notes
|
||
|
||
移除断言路由。密码登录和平台收口保持可用。
|
||
|
||
### Slice 5: 登录壳
|
||
|
||
#### Goal
|
||
|
||
管理台可以登录、看到无权限、会话失效后离开、退出。本地身份模式有可见标识。
|
||
|
||
#### Allowed Modification Scope
|
||
|
||
- `web/apps/console/`
|
||
- `web/packages/api/`
|
||
- 对应的单元测试与 Playwright
|
||
|
||
#### Forbidden Scope
|
||
|
||
- 后端授权规则、vben5、把真实 secret 写进前端环境
|
||
|
||
#### Execution
|
||
|
||
- Mode: sequential
|
||
- Depends On: S4
|
||
- Can Run With: 无
|
||
- Must Not Run With: S6
|
||
- Fallback Order: 5
|
||
- Shared Files: 前端 API 客户端、路由
|
||
- Merge Owner: 主 Agent
|
||
- Conflict Risk: medium
|
||
- Gate Class: boundary-sensitive
|
||
- Risk Score: 4
|
||
- Gate Reasons: auth-ui, session-boundary, critic-required
|
||
- Validation Level: slice
|
||
- Recommended Agent Strategy: 实现后用浏览器走登录失败、无权限和退出;审查用另一系列
|
||
|
||
#### Steps
|
||
|
||
1. 登录页提交到 S2 的接口。失败展示通用文案。
|
||
2. 按当前用户角色进入壳页或无权限页。
|
||
3. 退出并确认再访问受保护调用会失败。
|
||
4. 本地身份模式显示固定标识。
|
||
5. 为 HXOPS 断言准备提交入口,不把断言写进日志或本地长期存储。
|
||
|
||
#### Validation
|
||
|
||
- `pnpm test:unit`
|
||
- `pnpm test:e2e`
|
||
- 浏览器实际走一遍登录失败、无权限和退出。只看截图不算完成。
|
||
|
||
#### Done Criteria
|
||
|
||
验收标准 11 有断言和浏览器操作证据。前端包里没有 client secret。
|
||
|
||
#### Rollback Notes
|
||
|
||
撤下登录页后,后端会话仍在。进度里要写明界面与接口暂时不一致。
|
||
|
||
### Slice 6: 文档与示例配置
|
||
|
||
#### Goal
|
||
|
||
任务卡、运行说明和示例环境变量与已实现行为一致,并写清真实联调尚未完成的条件。
|
||
|
||
#### Allowed Modification Scope
|
||
|
||
- `docs/tasks/M02.md`
|
||
- `docs/runbook.md`
|
||
- `docs/progress.md`
|
||
- `docs/decisions.md`
|
||
- `.env.example`
|
||
|
||
#### Forbidden Scope
|
||
|
||
- 业务代码、真实密钥、把本地测试写成真实 Casdoor 已通过
|
||
|
||
#### Execution
|
||
|
||
- Mode: sequential
|
||
- Depends On: S5
|
||
- Can Run With: 无
|
||
- Must Not Run With: 无
|
||
- Fallback Order: 6
|
||
- Shared Files: 进度和任务卡
|
||
- Merge Owner: 主 Agent
|
||
- Conflict Risk: low
|
||
- Gate Class: simple
|
||
- Risk Score: 2
|
||
- Gate Reasons: docs-only
|
||
- Validation Level: final
|
||
- Recommended Agent Strategy: 主 Agent 直接改文档,并对照验收标准 12
|
||
|
||
#### Steps
|
||
|
||
1. 写上 Watchman Application 需要哪些键,值保持占位。
|
||
2. 写明真实密码登录和真实 HXOPS 跳转的完成条件。
|
||
3. 更新 M02 任务卡证据。没有命令输出就写未运行。
|
||
|
||
#### Validation
|
||
|
||
对照需求验收标准逐条看证据路径是否存在。不新跑业务测试,除非文档改动同时改了示例配置的键名。
|
||
|
||
#### Done Criteria
|
||
|
||
验收标准 12 在进度和任务卡里有明确状态。示例文件没有真实 secret。
|
||
|
||
#### Rollback Notes
|
||
|
||
文档回退不影响已发布的会话行为。
|
||
|
||
## Parallel Slice Groups
|
||
|
||
本任务不设并行组。会话、路由注册和权限依赖是同一条链。
|
||
|
||
## Final Validation
|
||
|
||
- `uv run pytest services/watchman/tests -q`
|
||
- `pnpm test:unit`
|
||
- `pnpm test:e2e`
|
||
- 浏览器走登录失败、无权限、退出
|
||
- 检查响应与日志样本,确认没有密码和 client secret
|
||
- 真实 Casdoor 与真实 HXOPS 入口未就绪时保持未完成,不并入上述通过结论
|
||
|
||
## Documentation Update Requirement
|
||
|
||
S6 更新运行说明和任务卡。不把本计划扩写成新的架构文档。若实现时发现会话规则和冻结需求不一致,先停下来改需求,再改代码。
|
||
|
||
## Risks
|
||
|
||
- S2 的迁移一旦执行,回滚需要明确的 down 步骤。
|
||
- S3 之前平台写接口仍匿名可写。不要在 S2 做完时宣称模块已经收口。
|
||
- S5 的 `risk_score` 为 4,即使界面工作看起来较小,也不能在 S4 通过后自动开做。
|