修复 token 与 Cookie 绑定并完善认证恢复

This commit is contained in:
Christ committed 2026-07-15 09:03:20 +08:00
1 parent 16d63dc7c9
commit 3cc1d4094d
30 files changed
+990 -998

No files matched your search

+31 -2
View File
@@ -22,7 +22,7 @@ type riskCountingMTop struct {
func (m *riskCountingMTop) RefreshTokenWithDeviceIDContext(context.Context, string, string) (*mtop.RefreshResult, error) {
m.calls++
if m.calls > 1 {
return &mtop.RefreshResult{AccessToken: "standard-token", UpdatedCookies: "unb=123; _m_h5_tk=recovered;"}, nil
return &mtop.RefreshResult{AccessToken: "standard-token", AccessTokenExpireAt: time.Now().Add(time.Hour).Unix(), UpdatedCookies: "unb=123; _m_h5_tk=recovered;"}, nil
}
return nil, &mtop.RiskVerificationError{Ret: []string{"FAIL_SYS_USER_VALIDATE"}, VerificationURL: "https://verify.example"}
}
@@ -33,6 +33,16 @@ func (h *tokenRecoveredHandler) OnTokenCaptchaVerification(context.Context, stri
return &mtop.RefreshResult{AccessToken: "recovered-token", UpdatedCookies: "unb=123; _m_h5_tk=recovered;"}, true
}
type rejectingTokenCaptchaHandler struct {
recordingHandler
calls int
}
func (h *rejectingTokenCaptchaHandler) OnTokenCaptchaVerification(context.Context, string, string, string, string) (*mtop.RefreshResult, bool) {
h.calls++
return nil, false
}
type capturingCaptchaHandler struct {
recordingHandler
cookieStr string
@@ -70,7 +80,7 @@ func (m *responseCookieRiskMTop) RefreshTokenWithDeviceIDContext(_ context.Conte
Ret: []string{"FAIL_SYS_USER_VALIDATE"}, VerificationURL: "https://verify.example/punish",
}
}
return &mtop.RefreshResult{AccessToken: "standard-after-captcha", UpdatedCookies: cookieStr}, nil
return &mtop.RefreshResult{AccessToken: "standard-after-captcha", AccessTokenExpireAt: time.Now().Add(time.Hour).Unix(), UpdatedCookies: cookieStr}, nil
}
func TestRefreshTokenRetriesStandardRequestAfterCaptchaRecovery(t *testing.T) {
@@ -93,6 +103,25 @@ func TestRefreshTokenRetriesStandardRequestAfterCaptchaRecovery(t *testing.T) {
}
}
func TestRefreshTokenCaptchaFailureEntersCallerCooldown(t *testing.T) {
acc, _, _, cleanup := newAccountForTest(t)
defer cleanup()
client := &riskCountingMTop{}
handler := &rejectingTokenCaptchaHandler{}
acc.mtop = client
acc.handler = handler
if _, _, err := acc.refreshToken(context.Background()); !mtop.IsRiskVerificationErr(err) {
t.Fatalf("first refresh error=%v want risk verification", err)
}
if _, _, err := acc.refreshToken(context.Background()); !errors.Is(err, errTokenCaptchaCooldown) {
t.Fatalf("second refresh error=%v want cooldown", err)
}
if client.calls != 1 || handler.calls != 1 {
t.Fatalf("cooldown must suppress repeated API/solver calls: api=%d solver=%d", client.calls, handler.calls)
}
}
func TestRefreshTokenPersistsResponseCookiesBeforeCaptchaRecovery(t *testing.T) {
acc, _, store, cleanup := newRunAccount(t, &responseCookieRiskMTop{})
defer cleanup()