Files
deerflow-code/parallel-agents-vue/docs/04-免密登录与鉴权.md
T
2026-06-08 22:52:18 +08:00

107 lines
3.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 测试用例 · 免密登录与鉴权
覆盖:username 免密自动登录、token 优先级、token 缓存与 401 重登、口令门、CSRF 绕过。
> 背景:面板优先用宿主传入的 token;没有 token 时,用宿主传入的 **username** 走 DeerFlow 公共免密端点
> `POST /api/v1/auth/login/username` 换取 `access_token`,以 `Authorization: Bearer` 发起后续请求
> (带 Bearer 会绕过 CSRF)。token 解析优先级:`token` > `getToken` > 缓存 > `username` 登录。
---
## PA-AUTH-01 · username 免密自动登录(P0)
**前置条件**:后端 `auth_login.username_login.require_password = false`(默认)。
**操作步骤**
1. 只传 `:username="demo"`(不传 token),打开面板。
2. 开始分析 / 打开历史。
**预期结果**
- 面板能正常拉历史、能发起并行流式——说明已用 username 换到 token 并附带在请求头。
- 旁证(手工 curl):
```bash
curl -s -X POST -H "Content-Type: application/json" \
-d '{"username":"demo"}' http://localhost:8001/api/v1/auth/login/username
# → 返回 {"access_token":"...","token_type":"bearer",...}
```
---
## PA-AUTH-02 · token 优先级高于 username(P1)
**操作步骤**
1. 同时传 `:token="<有效token>"` 和 `:username="demo"`。
2. 触发请求(开始分析 / 历史)。
**预期结果**
- 直接用传入的 `token`(不会再去调登录端点)。
- `:get-token` 同理优先于 username(动态取 token)。
---
## PA-AUTH-03 · token 缓存(不重复登录)(P1)
**操作步骤**
1. 只传 username,先后触发多次请求(拉历史 → 开始分析 → 保存)。
**预期结果**
- 登录端点**只调用一次**(首个请求),后续请求复用缓存 token(按 `baseUrl::username` 缓存)。
- 可在浏览器 Network 面板核对 `login/username` 只出现一次。
---
## PA-AUTH-04 · 401 自动重登重试(P1)
**前置条件**:能制造 token 失效(例如等待过期,或后端清理)。
**操作步骤**
1. 用 username 登录并缓存 token 后,使其失效,再触发一次 CRUD 请求。
**预期结果**
- 首次请求收到 401 → 面板**清缓存并用 username 重登一次**,再自动重试该请求。
- 用户无感知(不需手动重新登录),请求最终成功。
---
## PA-AUTH-05 · 口令门(require_password=true)(P2)
**前置条件**:后端把 `auth_login.username_login.require_password` 设为 `true`、配置 `password`。
**操作步骤**
1. 只传 username、不传 `loginPassword` → 触发请求。
2. 再传上正确的 `:login-password="<口令>"` → 触发请求。
**预期结果**
- 不传口令时:登录返回 401「口令错误」,面板顶部红色错误提示,不崩溃。
- 传对口令时:登录成功,后续请求正常。
---
## PA-AUTH-06 · Bearer 绕过 CSRF(P1)
**目的**:核对带 Bearer 的 POST 不被 CSRF 拦截。
**操作步骤(手工 curl 旁证)**
```bash
TOKEN=... # 上面登录拿到的 access_token
# 带 Bearer 的 POST:参数非法应为 400/422,而非 403(403=CSRF 拦截)
curl -s -o /dev/null -w "%{http_code}\n" -X POST \
-H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
-d '{}' http://localhost:8001/api/parallel-agents/run/stream
```
**预期结果**
- 返回 **400/422**(业务参数校验),**不是 403**——证明 Bearer 已绕过 CSRF。
- 不带任何鉴权时该 POST 返回 401/403(被拦截),属正常。
---
## PA-AUTH-07 · 未传 token 且未传 username(P2)
**操作步骤**
1. 既不传 token / getToken,也不传 username,触发需要鉴权的请求。
**预期结果**
- 请求因缺少鉴权失败(401/403),面板给出可读的错误提示(不白屏)。
- 控制台/提示能反映「缺少 token 且未提供 username,无法登录」之类信息。