阶段八:完成登录审计资料repository收口
This commit is contained in:
1 parent
98023744fa
commit
a1bf5078d8
6 files changed
+214
-186
No files matched your search
@@ -8,7 +8,7 @@ import (
|
||||
"xianyu-go/internal/xianyu/cookierefresh"
|
||||
)
|
||||
|
||||
// accountLoginRepository 定义账号登录服务写入凭证、清理 Token 和读取账号状态所需的最小能力。
|
||||
// accountLoginRepository 定义账号登录服务写入凭证、审计登录、更新资料和读取账号状态所需的最小能力。
|
||||
type accountLoginRepository interface {
|
||||
// LockCredentials 串行化账号登录凭证变更。
|
||||
LockCredentials(accountID string) func()
|
||||
@@ -24,6 +24,14 @@ type accountLoginRepository interface {
|
||||
ClearTokens(ctx context.Context, accountID string) error
|
||||
// GetStatus 返回账号是否启用。
|
||||
GetStatus(ctx context.Context, accountID string) bool
|
||||
// MarkLogin 保存账号最近一次成功登录方式。
|
||||
MarkLogin(ctx context.Context, accountID, method string, at int64) error
|
||||
// SetStatusWithReason 更新账号启用状态及禁用原因。
|
||||
SetStatusWithReason(ctx context.Context, accountID string, enabled bool, reason string) error
|
||||
// AddLoginLog 写入账号登录审计记录。
|
||||
AddLoginLog(ctx context.Context, log db.AccountLoginLog) error
|
||||
// UpdateProfile 保存账号展示资料。
|
||||
UpdateProfile(ctx context.Context, accountID, nickname, avatarURL string) error
|
||||
}
|
||||
|
||||
// storeAccountLoginRepository 将完整 Store 适配为账号登录服务窄 repository。
|
||||
@@ -80,6 +88,29 @@ func (r storeAccountLoginRepository) GetStatus(ctx context.Context, accountID st
|
||||
return r.store.Cookies.GetStatus(ctx, accountID)
|
||||
}
|
||||
|
||||
// MarkLogin 委托账号最近一次成功登录方式保存。
|
||||
func (r storeAccountLoginRepository) MarkLogin(ctx context.Context, accountID, method string, at int64) error {
|
||||
return r.store.Cookies.MarkLogin(ctx, accountID, method, at)
|
||||
}
|
||||
|
||||
// SetStatusWithReason 委托账号启用状态更新。
|
||||
func (r storeAccountLoginRepository) SetStatusWithReason(ctx context.Context, accountID string, enabled bool, reason string) error {
|
||||
return r.store.Cookies.SetStatusWithReason(ctx, accountID, enabled, reason)
|
||||
}
|
||||
|
||||
// AddLoginLog 委托账号登录审计记录写入;未启用审计 repository 时保持兼容空操作。
|
||||
func (r storeAccountLoginRepository) AddLoginLog(ctx context.Context, log db.AccountLoginLog) error {
|
||||
if r.store.LoginLogs == nil {
|
||||
return nil
|
||||
}
|
||||
return r.store.LoginLogs.Add(ctx, log)
|
||||
}
|
||||
|
||||
// UpdateProfile 委托账号展示资料更新。
|
||||
func (r storeAccountLoginRepository) UpdateProfile(ctx context.Context, accountID, nickname, avatarURL string) error {
|
||||
return r.store.Cookies.UpdateProfile(ctx, accountID, nickname, avatarURL)
|
||||
}
|
||||
|
||||
// newStoreAccountLoginRepository 从完整 Store 构造账号登录服务窄 repository。
|
||||
func newStoreAccountLoginRepository(store *db.Store) accountLoginRepository {
|
||||
if store == nil || store.Cookies == nil {
|
||||
|
||||
@@ -20,6 +20,16 @@ type accountLoginService struct {
|
||||
repository accountLoginRepository
|
||||
}
|
||||
|
||||
// accountLoginApplication 返回当前 Server 绑定的账号登录应用服务。
|
||||
func (s *Server) accountLoginApplication() *accountLoginService {
|
||||
return s.applicationServiceSet().accountLogin
|
||||
}
|
||||
|
||||
// accountLoginRepositoryForServer 返回当前 Server 装配的账号登录持久化边界。
|
||||
func (s *Server) accountLoginRepositoryForServer() accountLoginRepository {
|
||||
return s.accountLoginApplication().repository
|
||||
}
|
||||
|
||||
// accountLoginInput 是新增账号登录凭证用例的业务输入。
|
||||
type accountLoginInput struct {
|
||||
// AccountID 是待创建的账号标识。
|
||||
@@ -54,11 +64,6 @@ type accountProfileResult struct {
|
||||
ErrorMessage string
|
||||
}
|
||||
|
||||
// accountLoginApplication 返回当前 Server 绑定的账号登录应用服务。
|
||||
func (s *Server) accountLoginApplication() *accountLoginService {
|
||||
return s.applicationServiceSet().accountLogin
|
||||
}
|
||||
|
||||
// CreateCookie 创建账号凭证并完成登录审计、资料刷新和运行时重启。
|
||||
func (svc *accountLoginService) CreateCookie(ctx context.Context, input accountLoginInput) error {
|
||||
// s 是当前账号登录应用服务依赖的 Server。
|
||||
|
||||
@@ -171,20 +171,11 @@ func (s *Server) persistLongLoginCookies(ctx context.Context, detail *db.CookieD
|
||||
return credentialChanged, nil
|
||||
}
|
||||
|
||||
// updateFlatCookieOwnedLocked 用新的扁平 Cookie 覆盖账号时同步移除旧浏览器
|
||||
// 快照,避免下一次浏览器取 token 又把旧 Cookie 注入回来。调用方必须持有
|
||||
// 对应账号的凭证锁,并已验证 detail 的归属。
|
||||
func (s *Server) updateFlatCookieOwnedLocked(ctx context.Context, detail *db.CookieDetail, value string) error {
|
||||
if detail == nil {
|
||||
return db.ErrNotFound
|
||||
}
|
||||
metadata := cookierefresh.MetadataWithoutSnapshot(detail.MetadataJSON)
|
||||
return s.Store.Cookies.UpdateRenewalCookie(ctx, detail.ID, value, metadata, time.Now().Unix())
|
||||
}
|
||||
|
||||
func (s *Server) updateRunningCookie(ctx context.Context, cookieID, value string) {
|
||||
s.wakeCredentialBlockedAutomation(ctx, cookieID)
|
||||
if s.Manager == nil || !s.Store.Cookies.GetStatus(ctx, cookieID) {
|
||||
// repository 提供运行时更新前的账号启用状态读取。
|
||||
repository := s.accountLoginRepositoryForServer()
|
||||
if s.Manager == nil || repository == nil || !repository.GetStatus(ctx, cookieID) {
|
||||
return
|
||||
}
|
||||
if sender, ok := s.Manager.GetInstance(cookieID); ok {
|
||||
@@ -750,8 +741,13 @@ func (s *Server) refreshAccountProfile(ctx context.Context, d *db.CookieDetail)
|
||||
return cachedAccountNickname(d), d.AvatarURL, "账号资料客户端未初始化"
|
||||
}
|
||||
|
||||
credentialUnlock := s.Store.LockAccountCredentials(d.ID)
|
||||
latest, latestErr := s.loadCookiePlatformDetail(ctx, d.ID)
|
||||
// repository 提供资料刷新所需的凭证锁、平台视图和资料持久化能力。
|
||||
repository := s.accountLoginRepositoryForServer()
|
||||
if repository == nil {
|
||||
return cachedAccountNickname(d), d.AvatarURL, "账号登录持久化未初始化"
|
||||
}
|
||||
credentialUnlock := repository.LockCredentials(d.ID)
|
||||
latest, latestErr := repository.LoadPlatformDetail(ctx, d.ID)
|
||||
if latestErr != nil || latest == nil || latest.UserID != d.UserID {
|
||||
credentialUnlock()
|
||||
if latestErr == nil {
|
||||
@@ -778,7 +774,7 @@ func (s *Server) refreshAccountProfile(ctx context.Context, d *db.CookieDetail)
|
||||
} else if callErr == nil && profile != nil && profile.UpdatedCookies != "" && profile.UpdatedCookies != latest.Value {
|
||||
// 注入 mock 或没有权威快照的历史账号继续沿用扁平 Cookie 路径;
|
||||
// 该路径必须清除旧 snapshot,不能伪造完整 Jar。
|
||||
if err := s.updateFlatCookieOwnedLocked(ctx, latest, profile.UpdatedCookies); err != nil {
|
||||
if err := repository.UpdateFlatCookieOwned(ctx, latest, profile.UpdatedCookies); err != nil {
|
||||
if s.Logger != nil {
|
||||
s.Logger.Warn("保存账号刷新 cookie 失败", "account", d.ID, "err", err)
|
||||
}
|
||||
@@ -806,7 +802,7 @@ func (s *Server) refreshAccountProfile(ctx context.Context, d *db.CookieDetail)
|
||||
|
||||
apiNickname := strings.TrimSpace(profile.Nickname)
|
||||
apiAvatarURL := normalizeProfileAvatarURL(profile.AvatarURL)
|
||||
if err := s.Store.Cookies.UpdateProfile(ctx, d.ID, apiNickname, apiAvatarURL); err != nil && s.Logger != nil {
|
||||
if err := repository.UpdateProfile(ctx, d.ID, apiNickname, apiAvatarURL); err != nil && s.Logger != nil {
|
||||
s.Logger.Warn("保存账号资料失败", "account", d.ID, "err", err)
|
||||
}
|
||||
if apiNickname == "" {
|
||||
|
||||
@@ -30,19 +30,24 @@ func normalizeLoginMethod(method string) string {
|
||||
}
|
||||
|
||||
func (s *Server) markSuccessfulLogin(ctx context.Context, cookieID string, userID int64, method, message string) {
|
||||
// repository 提供登录审计和账号状态持久化能力。
|
||||
repository := s.accountLoginRepositoryForServer()
|
||||
if repository == nil {
|
||||
return
|
||||
}
|
||||
method = normalizeLoginMethod(method)
|
||||
if method == "" {
|
||||
return
|
||||
}
|
||||
at := time.Now().Unix()
|
||||
if err := s.Store.Cookies.MarkLogin(ctx, cookieID, method, at); err != nil {
|
||||
if err := repository.MarkLogin(ctx, cookieID, method, at); err != nil {
|
||||
if s.Logger != nil {
|
||||
s.Logger.Warn("记录账号登录方式失败", "cookie_id", cookieID, "method", method, "err", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if method == loginMethodPassword || method == loginMethodQRScan {
|
||||
if err := s.Store.Cookies.SetStatusWithReason(ctx, cookieID, true, ""); err != nil && s.Logger != nil {
|
||||
if err := repository.SetStatusWithReason(ctx, cookieID, true, ""); err != nil && s.Logger != nil {
|
||||
s.Logger.Warn("成功登录后启用账号失败", "cookie_id", cookieID, "method", method, "err", err)
|
||||
}
|
||||
}
|
||||
@@ -50,13 +55,15 @@ func (s *Server) markSuccessfulLogin(ctx context.Context, cookieID string, userI
|
||||
}
|
||||
|
||||
func (s *Server) addLoginLog(ctx context.Context, cookieID string, userID int64, method, status, failureReason, message string, at int64) {
|
||||
if s.Store == nil || s.Store.LoginLogs == nil {
|
||||
// repository 提供登录日志持久化能力。
|
||||
repository := s.accountLoginRepositoryForServer()
|
||||
if repository == nil {
|
||||
return
|
||||
}
|
||||
if at == 0 {
|
||||
at = time.Now().Unix()
|
||||
}
|
||||
if err := s.Store.LoginLogs.Add(ctx, db.AccountLoginLog{
|
||||
if err := repository.AddLoginLog(ctx, db.AccountLoginLog{
|
||||
CookieID: cookieID,
|
||||
UserID: userID,
|
||||
OwnerID: userID,
|
||||
|
||||
Reference in new issue
Block a user