API版本化:迁移账号凭证与登录信息入口

This commit is contained in:
Christ committed 2026-08-14 17:03:15 +08:00
1 parent eb8a54698c
commit 3d493ce2d4
6 files changed
+110 -9

No files matched your search

@@ -7,7 +7,7 @@
| 领域 | 现有路径 | 目标 `/api/v1` 路径 | 迁移状态 |
| --- | --- | --- | --- |
| 会话 | `/login`、`/initialize`、`/verify`、`/logout` | `/api/v1/session/login`、`/api/v1/session/initialize`、`/api/v1/session`、`/api/v1/session/logout` | React 会话调用方已迁移到版本化入口;旧路径保留并由同一 handler 提供 |
| 账号 | `/cookies`、`/cookies/details`、`/cookies/runtime-status`、`/cookies/{cid}/...` | `/api/v1/accounts`、`/api/v1/accounts/details`、`/api/v1/accounts/runtime-status`、`/api/v1/accounts/{cid}/...` | React 摘要、详情、运行状态、启停状态、设置、备注、暂停、自动确认、长登录和资料刷新调用已迁移;旧路径保留并由同一 handler 提供 |
| 账号 | `/cookies`、`/cookies/details`、`/cookies/runtime-status`、`/cookies/{cid}/...` | `/api/v1/accounts`、`/api/v1/accounts/details`、`/api/v1/accounts/runtime-status`、`/api/v1/accounts/{cid}/...` | React 摘要、详情、运行状态、启停状态、设置、备注、暂停、自动确认、长登录、资料刷新、Cookie 更新和登录信息设置调用已迁移;旧路径保留并由同一 handler 提供 |
| 商品 | `/items...` | `/api/v1/items...` | 旧路径保留,发布/同步/列表/详情 DTO 已落地 |
| 商品批量 | `/items/publish-batches...` | `/api/v1/items/publish-batches...` | 旧路径保留,类目推荐/预检/任务 DTO 已落地 |
| 自动化 | `/automation-rules...` | `/api/v1/automation-rules...` | 旧路径保留,规则分页/异常 DTO 已落地 |
@@ -44,7 +44,7 @@
- 管理员和统计分别使用 `adminUserResponse`/`adminCookieResponse`/`adminStatsResponse`、`dashboardStatsResponse`、`orderAnalyticsResponse` 与 `validOrdersResponse`;二维码生成使用 `qrLoginGenerateResponse`。
- 动态设置和二维码兼容边界分别使用 `settingsResponse`、`qrLoginStatusResponse` 与 `qrLoginVerificationResponse`;二维码状态契约测试确认 `cookies` 等敏感字段不会回传。
- 会话登录、初始化、校验和登出已由 React 请求层迁移到 `/api/v1/session/...`,Go 契约测试确认版本化入口与旧路径共用 handler 且旧路径仍可用。
- 账号摘要、非敏感详情、运行状态、启停状态、设置、备注、暂停、自动确认、长登录和资料刷新已由 React 请求层迁移到 `/api/v1/accounts...`;Go 契约测试确认账号详情不泄露 Cookie/密码,且旧详情与设置入口仍可用。
- 账号摘要、非敏感详情、运行状态、启停状态、设置、备注、暂停、自动确认、长登录、资料刷新、Cookie 更新和登录信息设置已由 React 请求层迁移到 `/api/v1/accounts...`;Go 契约测试确认账号详情与凭证变更响应不泄露 Cookie/密码,且旧详情、设置和登录信息入口仍可用。
- 聊天会话、消息分页和发送结果使用独立 DTO,不直接暴露数据库模型。
- React `frontend/types.ts` 与 `frontend/services/api.ts` 已同步具名成功响应类型,同时保留旧字段归一逻辑。
- 会话版本化入口已可用;除会话外的 `/api/v1` 领域入口仍未宣称可用,旧路径继续保留。
+3 -1
View File
@@ -141,7 +141,8 @@ app shell / routes
- 已完成阶段 3 第十二个 PR 切片“HTTP API 版本化兼容入口与会话调用方迁移”:新增 `/api/v1/session/login`、`/api/v1/session/initialize`、`/api/v1/session` 与 `/api/v1/session/logout` 薄适配入口,复用旧 handler;React 登录、初始化、会话校验和登出已迁移,旧路径保留;新增 Go/React 契约测试并保持独立可回滚提交;
- 已完成阶段 3 第十三个 PR 切片“账号 API 版本化兼容入口与调用方迁移”:新增 `/api/v1/accounts`、详情、运行状态、单账号详情和启停状态薄适配入口,复用旧 handler;React 账号摘要、详情、运行状态和启停状态调用已迁移,旧路径保留;新增 Go/React 契约测试并保持独立可回滚提交;
- 已完成阶段 3 第十四个 PR 切片“账号设置与资料 API 版本化兼容入口迁移”:新增账号聚合设置、备注、暂停、自动确认、长登录和资料刷新 `/api/v1/accounts/{cid}/...` 薄适配入口,复用旧 handler;React 相关调用已迁移,旧路径保留;新增 Go/React 契约测试并保持独立可回滚提交;
- 阶段 3 下一 PR 切片为“账号凭证与登录信息 API 版本化兼容入口迁移”:迁移 Cookie 更新和登录信息设置的一组调用方,继续保留敏感字段边界与旧路径,不拆分单个 handler 提交;
- 已完成阶段 3 第十五个 PR 切片“账号凭证与登录信息 API 版本化兼容入口迁移”:新增账号新增、Cookie 更新和登录信息设置 `/api/v1/accounts...` 薄适配入口,复用既有凭证锁与权限 handler;React 对应调用已迁移,旧路径保留;新增敏感字段边界契约测试并保持独立可回滚提交;
- 阶段 3 下一 PR 切片为“订单 API 版本化兼容入口与调用方迁移”:迁移订单列表、详情和更新的一组调用方,继续保留旧路径,不拆分单个 handler 提交;
- 禁止跳过当前入口直接开始 Engine、Automation 或 DB 的大规模拆分。
## 6. 阶段 0:治理文档与强约束
@@ -532,3 +533,4 @@ npm --prefix frontend run build
| 2026-08-14 | 完成阶段 3 第十二个 PR 切片“HTTP API 版本化兼容入口与会话调用方迁移” | 新增 `/api/v1/session/login`、`/api/v1/session/initialize`、`/api/v1/session` 与 `/api/v1/session/logout` 薄适配入口,全部复用既有认证 handler;React 登录、初始化、会话校验和登出调用已迁移;Go/React 契约测试确认新旧路径兼容;全量门禁通过并合并为一个可回滚提交 | 阶段 3:账号 API 版本化兼容入口与调用方迁移 |
| 2026-08-14 | 完成阶段 3 第十三个 PR 切片“账号 API 版本化兼容入口与调用方迁移” | 新增 `/api/v1/accounts`、`/api/v1/accounts/details`、`/api/v1/accounts/runtime-status`、`/api/v1/accounts/{cid}` 与启停状态薄适配入口,全部复用既有账号 handler;React 账号摘要、详情、运行状态和启停状态调用已迁移;Go/React 契约测试确认新旧路径兼容且详情不泄露凭证;全量门禁通过并合并为一个可回滚提交 | 阶段 3:账号设置与资料 API 版本化兼容入口迁移 |
| 2026-08-14 | 完成阶段 3 第十四个 PR 切片“账号设置与资料 API 版本化兼容入口迁移” | 新增 `/api/v1/accounts/{cid}/settings`、`remark`、`pause-duration`、`auto-confirm`、`long-login` 和 `refresh-profile` 薄适配入口,全部复用既有账号 handler;React 账号设置、备注、暂停、自动确认、长登录和资料刷新调用已迁移;Go/React 契约测试确认新旧路径兼容;全量门禁通过并合并为一个可回滚提交 | 阶段 3:账号凭证与登录信息 API 版本化兼容入口迁移 |
| 2026-08-14 | 完成阶段 3 第十五个 PR 切片“账号凭证与登录信息 API 版本化兼容入口迁移” | 新增 `/api/v1/accounts`、`/api/v1/accounts/{cid}`、`/api/v1/accounts/{cid}/login-info` 薄适配入口,复用既有 handler 和凭证锁;React 新增/更新 Cookie 与登录信息调用已迁移;Go/React 契约测试确认敏感字段不回传且旧路径兼容;全量门禁通过并合并为一个可回滚提交 | 阶段 3:订单 API 版本化兼容入口与调用方迁移 |
+3 -3
View File
@@ -315,7 +315,7 @@ test('account cookie APIs include login_method when provided', async () => {
vi.stubGlobal('fetch', fetchMock);
await addAccount('acc1', 'unb=acc1', 'qr_scan');
expect(fetchMock).toHaveBeenCalledWith('/cookies', expect.objectContaining({
expect(fetchMock).toHaveBeenCalledWith('/api/v1/accounts', expect.objectContaining({
method: 'POST',
credentials: 'include',
}));
@@ -326,7 +326,7 @@ test('account cookie APIs include login_method when provided', async () => {
});
await updateAccountCookie('acc1', 'unb=acc1; x=1', 'qr_scan');
expect(fetchMock).toHaveBeenCalledWith('/cookies/acc1', expect.objectContaining({
expect(fetchMock).toHaveBeenCalledWith('/api/v1/accounts/acc1', expect.objectContaining({
method: 'PUT',
credentials: 'include',
}));
@@ -382,7 +382,7 @@ test('updateAccountLoginInfo sends exactly provided fields', async () => {
vi.stubGlobal('fetch', fetchMock);
await updateAccountLoginInfo('acc1', { username: 'login-user', show_browser: false });
expect(fetchMock).toHaveBeenCalledWith('/cookies/acc1/login-info', expect.objectContaining({
expect(fetchMock).toHaveBeenCalledWith('/api/v1/accounts/acc1/login-info', expect.objectContaining({
method: 'PUT',
credentials: 'include',
}));
+3 -3
View File
@@ -48,7 +48,7 @@ export const updateLoginCredentials = async (data: {
// Accounts
export const addAccount = async (id: string, value: string, loginMethod?: string): Promise<ApiResponse> => {
return post('/cookies', { id, value, login_method: loginMethod });
return post('/api/v1/accounts', { id, value, login_method: loginMethod });
};
const accountAvatarURL = (item: AccountSummaryResponse, version: string): string => {
@@ -207,7 +207,7 @@ export const updateAccountPauseDuration = async (id: string, pauseDuration: numb
};
export const updateAccountCookie = async (id: string, value: string, loginMethod?: string): Promise<OperationResponse> => {
return put(`/cookies/${id}`, { id, value, login_method: loginMethod });
return put(`/api/v1/accounts/${id}`, { id, value, login_method: loginMethod });
};
export interface AccountSettingsUpdate {
@@ -286,7 +286,7 @@ export const updateAccountLoginInfo = async (id: string, data: {
clear_password?: boolean;
show_browser?: boolean;
}): Promise<OperationResponse> => {
return put(`/cookies/${id}/login-info`, data);
return put(`/api/v1/accounts/${id}/login-info`, data);
};
export const getAllAISettings = async (options?: RequestControlOptions): Promise<Record<string, AIReplySettings>> => {
@@ -1,6 +1,7 @@
package server
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
@@ -139,6 +140,101 @@ func TestVersionedAccountRoutesPreserveLegacyContracts(t *testing.T) {
}
}
// TestVersionedAccountCredentialRoutesPreserveLegacyContracts 验证账号凭证与登录信息版本化入口的兼容性及敏感字段边界。
func TestVersionedAccountCredentialRoutesPreserveLegacyContracts(t *testing.T) {
// srv 是用于验证账号凭证版本化路由的 HTTP 测试服务。
srv, store, cleanup := newTestServer(t)
defer cleanup()
// srv.Manager 置空后,新增和更新请求不会启动后台运行时。
srv.Manager = nil
// handler 是当前测试使用的完整路由树。
handler := srv.Router()
// sessionCookie 是管理员登录后得到的认证会话。
sessionCookie := loginHelper(t, handler)
// addReq 是通过版本化入口新增账号凭证的请求。
addReq := httptest.NewRequest(http.MethodPost, "/api/v1/accounts", strings.NewReader(`{"id":"cred-v1","value":"unb=cred-v1","login_method":"qr_scan"}`))
addReq.AddCookie(sessionCookie)
// addRecorder 是捕获新增账号响应的记录器。
addRecorder := httptest.NewRecorder()
handler.ServeHTTP(addRecorder, addReq)
if addRecorder.Code != http.StatusOK {
t.Fatalf("versioned add account status=%d body=%s", addRecorder.Code, addRecorder.Body.String())
}
// addValue 是新增账号响应中的具名结果 DTO。
var addValue accountMutationResponse
// addDecodeErr 是新增账号响应反序列化失败的原因。
if addDecodeErr := json.Unmarshal(addRecorder.Body.Bytes(), &addValue); addDecodeErr != nil {
t.Fatalf("decode versioned add account: %v", addDecodeErr)
}
if !addValue.Success || addValue.ID != "cred-v1" {
t.Fatalf("versioned add account=%+v", addValue)
}
if strings.Contains(addRecorder.Body.String(), "unb=cred-v1") || strings.Contains(addRecorder.Body.String(), "password") {
t.Fatalf("versioned add account exposes credential: %s", addRecorder.Body.String())
}
// updateReq 是通过版本化入口更新账号 Cookie 的请求。
updateReq := httptest.NewRequest(http.MethodPut, "/api/v1/accounts/cred-v1", strings.NewReader(`{"value":"unb=cred-v1; token=v2","login_method":"manual"}`))
updateReq.AddCookie(sessionCookie)
// updateRecorder 是捕获 Cookie 更新响应的记录器。
updateRecorder := httptest.NewRecorder()
handler.ServeHTTP(updateRecorder, updateReq)
if updateRecorder.Code != http.StatusOK {
t.Fatalf("versioned update account status=%d body=%s", updateRecorder.Code, updateRecorder.Body.String())
}
// updateValue 是 Cookie 更新响应中的具名结果 DTO。
var updateValue operationResponse
// updateDecodeErr 是 Cookie 更新响应反序列化失败的原因。
if updateDecodeErr := json.Unmarshal(updateRecorder.Body.Bytes(), &updateValue); updateDecodeErr != nil {
t.Fatalf("decode versioned update account: %v", updateDecodeErr)
}
if !updateValue.Success {
t.Fatalf("versioned update account=%+v", updateValue)
}
if strings.Contains(updateRecorder.Body.String(), "token=v2") || strings.Contains(updateRecorder.Body.String(), "password") {
t.Fatalf("versioned update account exposes credential: %s", updateRecorder.Body.String())
}
// loginInfoReq 是通过版本化入口更新账号登录信息的请求。
loginInfoReq := httptest.NewRequest(http.MethodPut, "/api/v1/accounts/acc1/login-info", strings.NewReader(`{"username":"versioned-login","show_browser":false}`))
loginInfoReq.AddCookie(sessionCookie)
// loginInfoRecorder 是捕获登录信息更新响应的记录器。
loginInfoRecorder := httptest.NewRecorder()
handler.ServeHTTP(loginInfoRecorder, loginInfoReq)
if loginInfoRecorder.Code != http.StatusOK {
t.Fatalf("versioned login info status=%d body=%s", loginInfoRecorder.Code, loginInfoRecorder.Body.String())
}
// loginInfoValue 是登录信息更新响应中的具名结果 DTO。
var loginInfoValue operationResponse
// loginInfoDecodeErr 是登录信息响应反序列化失败的原因。
if loginInfoDecodeErr := json.Unmarshal(loginInfoRecorder.Body.Bytes(), &loginInfoValue); loginInfoDecodeErr != nil {
t.Fatalf("decode versioned login info: %v", loginInfoDecodeErr)
}
if !loginInfoValue.Success {
t.Fatalf("versioned login info=%+v", loginInfoValue)
}
if strings.Contains(loginInfoRecorder.Body.String(), "password") || strings.Contains(loginInfoRecorder.Body.String(), "login_password") {
t.Fatalf("versioned login info exposes password: %s", loginInfoRecorder.Body.String())
}
// detail 是数据库中用于确认登录信息已持久化的完整账号详情。
detail, detailErr := store.Cookies.GetDetails(context.Background(), "acc1")
if detailErr != nil || detail == nil || detail.Username != "versioned-login" || detail.ShowBrowser {
t.Fatalf("versioned login info not persisted: detail=%+v err=%v", detail, detailErr)
}
// legacyReq 是验证旧登录信息入口仍可用的请求。
legacyReq := httptest.NewRequest(http.MethodPut, "/cookies/acc1/login-info", strings.NewReader(`{"username":"legacy-login","show_browser":false}`))
legacyReq.AddCookie(sessionCookie)
// legacyRecorder 是捕获旧登录信息响应的记录器。
legacyRecorder := httptest.NewRecorder()
handler.ServeHTTP(legacyRecorder, legacyReq)
if legacyRecorder.Code != http.StatusOK {
t.Fatalf("legacy login info status=%d body=%s", legacyRecorder.Code, legacyRecorder.Body.String())
}
}
// TestVersionedAccountSettingsRoutesPreserveLegacyContracts 验证账号设置、资料和旧路径兼容。
func TestVersionedAccountSettingsRoutesPreserveLegacyContracts(t *testing.T) {
// srv 是用于验证版本化账号设置路由的 HTTP 测试服务。
@@ -12,6 +12,7 @@ func (s *Server) mountVersionedAccounts(r chi.Router) {
r.Use(s.Auth.Middleware)
r.Use(auth.RequireAuth)
r.Get("/api/v1/accounts", s.listCookies)
r.Post("/api/v1/accounts", s.addCookie)
r.Get("/api/v1/accounts/details", s.listCookieDetails)
r.Get("/api/v1/accounts/runtime-status", s.listCookieRuntimeStatus)
r.Get("/api/v1/accounts/{cid}", s.getCookieDetails)
@@ -25,5 +26,7 @@ func (s *Server) mountVersionedAccounts(r chi.Router) {
r.Put("/api/v1/accounts/{cid}/remark", s.setCookieRemark)
r.Get("/api/v1/accounts/{cid}/pause-duration", s.getCookiePauseDuration)
r.Put("/api/v1/accounts/{cid}/pause-duration", s.setCookiePauseDuration)
r.Put("/api/v1/accounts/{cid}", s.updateCookie)
r.Put("/api/v1/accounts/{cid}/login-info", s.updateCookieLoginInfo)
})
}