推进P1聊天通知与凭证应用端口迁移

This commit is contained in:
Christ committed 2026-08-16 00:55:03 +08:00
1 parent 25531c05cb
commit 550aee8aae
17 files changed
+1677 -289

No files matched your search

@@ -6,6 +6,9 @@ import (
"strings"
)
// ErrCredentialConflict 表示凭证写入前后检测到更新版本已变化,调用方应重新读取后重试。
var ErrCredentialConflict = errors.New("账号凭证已发生变化,请重试")
// CreateCookieInput 是手动录入账号 Cookie 用例的非敏感输入;明文 Cookie 由调用方专用端口封装。
type CreateCookieInput struct {
// AccountID 是待创建的平台账号稳定标识。
@@ -16,12 +19,30 @@ type CreateCookieInput struct {
LoginMethod string
}
// UpdateCookieInput 是更新既有账号凭证用例的非敏感输入;明文 Cookie 由调用方专用端口封装。
type UpdateCookieInput struct {
// AccountID 是待更新的平台账号稳定标识。
AccountID string
// UserID 是当前用户的本地身份标识。
UserID int64
// LoginMethod 是可选登录方式;空值表示普通凭证替换且不写入登录审计。
LoginMethod string
// ExpectedRevision 是调用方读取到的最近 Cookie 刷新时间;零值关闭兼容客户端的乐观冲突检查。
ExpectedRevision int64
}
// CookieWriter 定义一次性写入账号凭证的端口;实现方负责在最小作用域内持有明文 Cookie。
type CookieWriter interface {
// CreateOwnedCookie 原子校验账号归属并写入凭证;明文不应进入应用层 DTO 或日志。
CreateOwnedCookie(context.Context, string, int64) error
}
// CookieUpdater 定义更新既有账号凭证的专用端口;实现方负责在最小作用域内接收明文 Cookie。
type CookieUpdater interface {
// UpdateOwnedCookie 原子校验账号归属、可选版本并写入新凭证;明文不应进入应用层输入或日志。
UpdateOwnedCookie(context.Context, string, int64, int64) error
}
// LoginLifecyclePort 定义凭证成功写入后的审计、启用、资料刷新和运行时同步边界。
type LoginLifecyclePort interface {
// AfterSuccessfulLogin 在凭证写入并释放凭证锁后完成后续登录编排。
@@ -64,6 +85,31 @@ func (s *LoginService) CreateCookie(ctx context.Context, input CreateCookieInput
return nil
}
// UpdateCookie 执行既有账号的凭证更新;写入成功后才触发登录审计、资料刷新与运行时同步。
func (s *LoginService) UpdateCookie(ctx context.Context, input UpdateCookieInput, updater CookieUpdater) error {
if s == nil || s.lifecycle == nil {
return errors.New("账号登录服务未初始化")
}
if updater == nil {
return errors.New("账号 Cookie 更新端口未初始化")
}
if strings.TrimSpace(input.AccountID) == "" {
return errors.New("缺少账号 ID")
}
// updateErr 保存凭证适配器完成归属校验、短锁写入和冲突检测后的结果。
updateErr := updater.UpdateOwnedCookie(ctx, input.AccountID, input.UserID, input.ExpectedRevision)
if updateErr != nil {
return updateErr
}
// method 是归一化后的登录方式;更新凭证未声明登录方式时必须保持普通替换,不写入登录审计。
method := ""
if strings.TrimSpace(input.LoginMethod) != "" {
method = normalizeLoginMethod(input.LoginMethod)
}
s.lifecycle.AfterSuccessfulLogin(ctx, input.UserID, input.AccountID, method)
return nil
}
// normalizeLoginMethod 将兼容的登录方式别名归一化为审计使用的稳定值。
func normalizeLoginMethod(method string) string {
switch strings.ToLower(strings.TrimSpace(method)) {
@@ -16,6 +16,24 @@ type fakeCookieWriter struct {
called bool
}
// fakeCookieUpdater 是更新凭证应用服务测试用的专用端口替身,不接收明文 Cookie。
type fakeCookieUpdater struct {
// err 是更新端口需要模拟的失败,包括归属失败、版本冲突和持久化失败。
err error
// accountID 和 userID 保存最近一次更新请求,用于验证应用层只传递非敏感身份。
accountID string
userID int64
called bool
}
// UpdateOwnedCookie 记录更新请求并返回预设结果。
func (u *fakeCookieUpdater) UpdateOwnedCookie(_ context.Context, accountID string, userID, _ int64) error {
u.called = true
u.accountID = accountID
u.userID = userID
return u.err
}
// CreateOwnedCookie 记录写入请求但不接收明文 Cookie。
func (w *fakeCookieWriter) CreateOwnedCookie(_ context.Context, accountID string, userID int64) error {
w.called = true
@@ -132,3 +150,94 @@ func TestLoginServiceCreateCookiePropagatesOwnershipFailure(t *testing.T) {
t.Fatal("归属校验失败时不应触发登录成功后续编排")
}
}
// TestLoginServiceUpdateCookieSuccess 验证更新成功后才触发统一登录后续编排。
func TestLoginServiceUpdateCookieSuccess(t *testing.T) {
// lifecycle 记录更新成功后的审计、资料和运行时同步调用。
lifecycle := &fakeLoginLifecycle{}
// updater 只接收账号身份,不接收明文 Cookie。
updater := &fakeCookieUpdater{}
// service 保存使用有效生命周期端口构造的登录应用服务;serviceErr 保存构造错误。
service, serviceErr := NewLoginService(lifecycle)
if serviceErr != nil {
t.Fatalf("构造登录服务失败: %v", serviceErr)
}
// updateErr 保存应用服务返回的更新结果。
updateErr := service.UpdateCookie(context.Background(), UpdateCookieInput{AccountID: "acc1", UserID: 7, LoginMethod: "qr_scan"}, updater)
if updateErr != nil {
t.Fatalf("更新 Cookie 失败: %v", updateErr)
}
if !updater.called || updater.accountID != "acc1" || updater.userID != 7 {
t.Fatalf("更新端口输入异常: %+v", updater)
}
if lifecycle.calls != 1 || lifecycle.method != "qr_scan" {
t.Fatalf("成功更新后未触发正确后续编排: %+v", lifecycle)
}
}
// TestLoginServiceUpdateCookiePropagatesOwnershipFailure 验证归属失败不会触发成功后续编排。
func TestLoginServiceUpdateCookiePropagatesOwnershipFailure(t *testing.T) {
// ownershipErr 是凭证适配器完成账号归属校验后返回的越权错误。
ownershipErr := errors.New("账号不属于当前用户")
// lifecycle 用于确认归属失败时不会记录登录成功。
lifecycle := &fakeLoginLifecycle{}
// updater 模拟归属校验失败。
updater := &fakeCookieUpdater{err: ownershipErr}
// service 保存有效应用服务;serviceErr 保存构造错误。
service, serviceErr := NewLoginService(lifecycle)
if serviceErr != nil {
t.Fatalf("构造登录服务失败: %v", serviceErr)
}
// updateErr 保存归属失败结果。
updateErr := service.UpdateCookie(context.Background(), UpdateCookieInput{AccountID: "other", UserID: 7}, updater)
if !errors.Is(updateErr, ownershipErr) {
t.Fatalf("应保留归属失败错误,got %v", updateErr)
}
if lifecycle.calls != 0 {
t.Fatal("归属失败时不应触发登录成功后续编排")
}
}
// TestLoginServiceUpdateCookiePropagatesVersionConflict 验证并发版本冲突会原样返回并阻止旧响应生效。
func TestLoginServiceUpdateCookiePropagatesVersionConflict(t *testing.T) {
// lifecycle 用于确认版本冲突不会触发运行时重启。
lifecycle := &fakeLoginLifecycle{}
// updater 模拟凭证快照已被其他请求更新后的冲突结果。
updater := &fakeCookieUpdater{err: ErrCredentialConflict}
// service 保存有效应用服务;serviceErr 保存构造错误。
service, serviceErr := NewLoginService(lifecycle)
if serviceErr != nil {
t.Fatalf("构造登录服务失败: %v", serviceErr)
}
// updateErr 保存并发版本冲突结果。
updateErr := service.UpdateCookie(context.Background(), UpdateCookieInput{AccountID: "acc1", UserID: 7}, updater)
if !errors.Is(updateErr, ErrCredentialConflict) {
t.Fatalf("应返回版本冲突,got %v", updateErr)
}
if lifecycle.calls != 0 {
t.Fatal("版本冲突时不应触发登录成功后续编排")
}
}
// TestLoginServiceUpdateCookiePropagatesWriteFailure 验证凭证持久化失败不会伪造登录成功。
func TestLoginServiceUpdateCookiePropagatesWriteFailure(t *testing.T) {
// writeErr 是凭证适配器返回的底层写入故障。
writeErr := errors.New("凭证写入失败")
// lifecycle 用于确认写入失败时未触发后续编排。
lifecycle := &fakeLoginLifecycle{}
// updater 模拟数据库写入失败。
updater := &fakeCookieUpdater{err: writeErr}
// service 保存有效应用服务;serviceErr 保存构造错误。
service, serviceErr := NewLoginService(lifecycle)
if serviceErr != nil {
t.Fatalf("构造登录服务失败: %v", serviceErr)
}
// updateErr 保存写入失败结果。
updateErr := service.UpdateCookie(context.Background(), UpdateCookieInput{AccountID: "acc1", UserID: 7}, updater)
if !errors.Is(updateErr, writeErr) {
t.Fatalf("应返回凭证写入错误,got %v", updateErr)
}
if lifecycle.calls != 0 {
t.Fatal("写入失败时不应触发登录成功后续编排")
}
}