推进P1账号应用浏览器生命周期与自动化隔离

This commit is contained in:
Christ committed 2026-08-15 22:57:10 +08:00
1 parent bcdc163306
commit e53177ccd1
14 files changed
+677 -50

No files matched your search

@@ -2,8 +2,10 @@ package server
import (
"context"
"errors"
"time"
accountapp "xianyu-go/internal/application/account"
"xianyu-go/internal/db"
"xianyu-go/internal/xianyu/cookierefresh"
)
@@ -40,6 +42,36 @@ type storeAccountLoginRepository struct {
store *db.Store
}
// storeAccountProfileRepository 将 Server 的摘要查询适配为账号应用层 Port。
type storeAccountProfileRepository struct {
// store 提供账号摘要查询能力;凭证字段不会通过该适配器读取。
store *db.Store
}
// GetOwnedSummary 查询指定用户拥有的非敏感账号摘要。
func (r storeAccountProfileRepository) GetOwnedSummary(ctx context.Context, userID int64, accountID string) (accountapp.Summary, error) {
// summary 保存数据库返回的非敏感账号摘要。
summary, err := r.store.Cookies.GetSummaryOwned(ctx, userID, accountID)
if err != nil {
if errors.Is(err, db.ErrForbidden) {
return accountapp.Summary{}, accountapp.ErrForbidden
}
if errors.Is(err, db.ErrNotFound) {
// ownerID 和 ownerErr 用于区分不存在账号与跨用户账号,保持 HTTP 兼容状态码。
ownerID, ownerErr := r.store.Cookies.GetOwnerID(ctx, accountID)
if ownerErr == nil && ownerID != userID {
return accountapp.Summary{}, accountapp.ErrForbidden
}
return accountapp.Summary{}, accountapp.ErrNotFound
}
return accountapp.Summary{}, err
}
return accountapp.Summary{
ID: summary.ID, UserID: summary.UserID, Remark: summary.Remark,
Nickname: summary.Nickname, AvatarURL: summary.AvatarURL,
}, nil
}
// LockCredentials 委托账号凭证锁。
func (r storeAccountLoginRepository) LockCredentials(accountID string) func() {
return r.store.LockAccountCredentials(accountID)
+32 -22
View File
@@ -7,6 +7,7 @@ import (
"sync"
"time"
accountapp "xianyu-go/internal/application/account"
"xianyu-go/internal/db"
"xianyu-go/internal/xianyu/cookierefresh"
"xianyu-go/internal/xianyu/protocol"
@@ -20,6 +21,37 @@ type accountLoginService struct {
repository accountLoginRepository
}
// serverAccountProfilePort 将平台资料刷新适配为账号应用层 Port。
type serverAccountProfilePort struct {
// server 提供平台会话、凭证锁、资料保存和运行时同步能力。
server *Server
}
// RefreshProfile 执行平台资料刷新,并把平台结果转换为应用层 DTO。
func (p serverAccountProfilePort) RefreshProfile(ctx context.Context, input accountapp.ProfileInput) (accountapp.ProfileResult, error) {
// detail 是兼容 Server 资料刷新器所需的非敏感账号摘要模型。
detail := &db.CookieDetail{
ID: input.Summary.ID, UserID: input.Summary.UserID,
Remark: input.Summary.Remark, Nickname: input.Summary.Nickname,
AvatarURL: input.Summary.AvatarURL,
}
// nickname、avatarURL 和 profileErr 保存平台资料刷新后的展示结果。
nickname, avatarURL, profileErr := p.server.refreshAccountProfile(ctx, detail)
return accountapp.ProfileResult{
AccountID: input.AccountID, Nickname: nickname,
AvatarURL: avatarURL, ErrorMessage: profileErr,
}, nil
}
// newAccountProfileApplication 从 Server 的数据库和平台能力构造账号资料应用服务。
func newAccountProfileApplication(server *Server) (*accountapp.ProfileService, error) {
// repository 是只返回非敏感摘要的数据库适配器。
repository := storeAccountProfileRepository{store: server.Store}
// profilePort 是负责平台请求和资料持久化的 Server 适配器。
profilePort := serverAccountProfilePort{server: server}
return accountapp.NewProfileService(repository, profilePort)
}
// accountLoginApplication 返回当前 Server 绑定的账号登录应用服务。
func (s *Server) accountLoginApplication() *accountLoginService {
return s.applicationServiceSet().accountLogin
@@ -54,16 +86,6 @@ type accountCookieUpdateInput struct {
LoginMethod string
}
// accountProfileResult 是账号资料刷新后的展示结果。
type accountProfileResult struct {
// Nickname 是平台昵称或本地兜底昵称。
Nickname string
// AvatarURL 是平台头像地址或本地兜底地址。
AvatarURL string
// ErrorMessage 是资料刷新失败时的可展示错误。
ErrorMessage string
}
// CreateCookie 创建账号凭证并完成登录审计、资料刷新和运行时重启。
func (svc *accountLoginService) CreateCookie(ctx context.Context, input accountLoginInput) error {
// s 是当前账号登录应用服务依赖的 Server。
@@ -127,18 +149,6 @@ func (svc *accountLoginService) UpdateCookie(ctx context.Context, input accountC
return nil
}
// RefreshProfile 按用户归属读取账号摘要并刷新平台资料。
func (svc *accountLoginService) RefreshProfile(ctx context.Context, userID int64, accountID string) (accountProfileResult, error) {
// detail 和 err 保存资料刷新所需的非敏感账号摘要。
detail, err := svc.server.loadCookieSummaryDetail(ctx, userID, accountID)
if err != nil {
return accountProfileResult{}, err
}
// nickname、avatarURL 和 profileErr 保存平台资料刷新结果。
nickname, avatarURL, profileErr := svc.server.refreshAccountProfile(ctx, detail)
return accountProfileResult{Nickname: nickname, AvatarURL: avatarURL, ErrorMessage: profileErr}, nil
}
// PersistQRLoginSuccess 持久化扫码登录结果,复用会话级幂等锁和账号重启流程。
func (svc *accountLoginService) PersistQRLoginSuccess(ctx context.Context, userID int64, sessionID string, result map[string]any, targetAccountID string) (qrLoginPersistence, error) {
return svc.persistQRLoginSuccessCore(ctx, userID, sessionID, result, targetAccountID)
@@ -19,18 +19,6 @@ func TestAccountLoginServiceValidateCookieInput(t *testing.T) {
}
}
// TestAccountLoginServiceRefreshProfileNotFound 验证资料刷新服务不会越权读取不存在账号。
func TestAccountLoginServiceRefreshProfileNotFound(t *testing.T) {
// srv、store 和 cleanup 构造并释放测试服务。
srv, _, cleanup := newTestServer(t)
defer cleanup()
// err 保存资料刷新服务返回的账号查询错误。
_, err := srv.accountLoginApplication().RefreshProfile(context.Background(), 1, "missing-account")
if err == nil {
t.Fatal("不存在账号的资料刷新应该返回错误")
}
}
// TestAccountLoginServicePersistQRLoginRejectsIncompleteResult 验证扫码结果缺少凭证时不会写入账号。
func TestAccountLoginServicePersistQRLoginRejectsIncompleteResult(t *testing.T) {
// service 使用空 Server 即可在凭证校验前验证失败结果。
+18 -2
View File
@@ -1,7 +1,10 @@
package server
import orderapp "xianyu-go/internal/application/orders"
import itemapp "xianyu-go/internal/application/items"
import (
accountapp "xianyu-go/internal/application/account"
itemapp "xianyu-go/internal/application/items"
orderapp "xianyu-go/internal/application/orders"
)
// applicationServices 聚合 Server 使用的应用服务实例,统一管理共享基础设施依赖。
type applicationServices struct {
@@ -13,6 +16,8 @@ type applicationServices struct {
itemSinglePublish *itemapp.Service
// accountLogin 是账号登录应用服务。
accountLogin *accountLoginService
// accountProfile 是账号资料刷新应用服务。
accountProfile *accountapp.ProfileService
// communication 是聊天、通知和账号任务应用服务。
communication *communicationService
// analytics 是订单分析应用服务。
@@ -27,16 +32,27 @@ func newApplicationServices(server *Server) *applicationServices {
orderRuntime := newServerOrderRuntime(server)
// orderServices 保存应用层统一构造的订单业务服务集合。
orderServices := orderapp.NewServiceSet(orderRepository, orderRepository, orderRuntime, orderRuntime, newStoreOrderRefreshJobRepository(server.Store), refreshOrderChunkSize)
// accountProfile 保存账号资料刷新用例的构造结果。
accountProfile, profileErr := newAccountProfileApplication(server)
if profileErr != nil {
panic(profileErr)
}
return &applicationServices{
orders: &orderHTTPAdapter{services: orderServices, repository: orderRepository},
itemPublish: &itemPublishService{server: server, repository: newStoreItemPublishRepository(server.Store)},
itemSinglePublish: newItemPublishApplication(server),
accountLogin: &accountLoginService{server: server, repository: newStoreAccountLoginRepository(server.Store)},
accountProfile: accountProfile,
communication: &communicationService{server: server, repository: newStoreCommunicationRepository(server.Store)},
analytics: &analyticsService{repository: newStoreAnalyticsRepository(server.Store)},
}
}
// accountProfileApplication 返回当前 Server 绑定的账号资料应用服务。
func (s *Server) accountProfileApplication() *accountapp.ProfileService {
return s.applicationServiceSet().accountProfile
}
// applicationServiceSet 返回当前 Server 的应用服务集合,并兼容测试 Server。
func (s *Server) applicationServiceSet() *applicationServices {
if s.applications != nil {
+12 -6
View File
@@ -11,6 +11,7 @@ import (
"github.com/go-chi/chi/v5"
accountapp "xianyu-go/internal/application/account"
"xianyu-go/internal/auth"
"xianyu-go/internal/db"
"xianyu-go/internal/engine"
@@ -484,14 +485,19 @@ func (s *Server) getCookieDetails(w http.ResponseWriter, r *http.Request) {
func (s *Server) refreshCookieProfile(w http.ResponseWriter, r *http.Request) {
cid := chi.URLParam(r, "cid") // cid 是请求路径中的账号 ID。
sess := auth.SessionFromContext(r.Context()) // sess 是当前认证会话。
if !s.cookieOwnedByUser(r.Context(), sess.UserID, cid) {
writeErr(w, http.StatusForbidden, "无权限操作该账号")
return
}
// profile、err 保存profile、err,供当前处理流程使用
profile, err := s.accountLoginApplication().RefreshProfile(r.Context(), sess.UserID, cid)
profile, err := s.accountProfileApplication().RefreshProfile(r.Context(), sess.UserID, cid)
if err != nil {
writeErr(w, http.StatusNotFound, "账号不存在")
if errors.Is(err, accountapp.ErrForbidden) {
writeErr(w, http.StatusForbidden, "无权限操作该账号")
return
}
if errors.Is(err, accountapp.ErrNotFound) {
// 旧路径将不存在与无权账号统一视为不可操作,保留既有客户端状态码兼容性。
writeErr(w, http.StatusForbidden, "无权限操作该账号")
return
}
writeErr(w, http.StatusInternalServerError, "刷新账号资料失败")
return
}
writeJSON(w, http.StatusOK, cookieProfileResponse{