Files
sub2api/backend/internal/service/aliyun_captcha_service_test.go
T
feeeei 26e0a89323 人机验证增加阿里云验证码 2.0
沿用腾讯天御验证码引入的多服务商模型:aliyun_captcha_enabled 作为独立
开关,与 Cloudflare Turnstile、腾讯天御三方互斥(保存校验 + 运行时
CAPTCHA_PROVIDER_CONFLICT)。后台「安全与认证」合并为单张人机验证卡片:
总开关 + 服务商单选(Turnstile / 腾讯天御 / 阿里云),选中即启用该家并
关闭其它,落库仍是三个独立开关键,由前端映射保证互斥。

阿里云侧同时支持 aliyun 中国站与国际站(alibabacloud.com):两站前端脚本、
region 取值与服务端 API 完全一致,仅账号与 AccessKey 相互独立,因此由
「服务地域」决定线路即可——中国内地走 captcha.cn-shanghai.aliyuncs.com,
非中国内地(新加坡)走 captcha.ap-southeast-1.aliyuncs.com,AccessKey
取自持有该实例的账号,无需在配置中区分站点。

- AliyunCaptchaService 对称 TencentCaptchaService:服务端校验走官方 SDK
  VerifyIntelligentCaptcha,调用异常按 fail-closed 拦截,与 Turnstile
  网络错误行为对称;保存设置时真实探测 AK/SK 有效性
- 保护面对齐腾讯扩展入口:VerifyTencentCaptchaIfEnabled 通用化为
  VerifyActionCaptchaIfEnabled,OAuth 登录启动、passkey 登录在阿里云
  启用时同样拦截;Turnstile 维持既有覆盖不扩大
- 前端 AliyunCaptchaWidget 为表单内预验证按钮(popup 模式),同时暴露
  verify() 供 OAuth 启动、passkey 等动作入口程序化弹窗;未预验证直接
  提交时弹窗兜底。SDK 按钮绑定异步完成,弹窗未出现前按 tick 重试触发,
  并轮询弹窗可见性识别用户关闭
- captchaVerifyParam 复用 turnstile_token 请求字段提交;公开设置下发
  aliyun_captcha_enabled / scene_id / prefix / region
- CSP 放行验证码 CDN:script-src/style-src 加 *.alicdn.com
2026-08-04 20:57:15 +08:00

234 lines
7.8 KiB
Go

//go:build unit
package service
import (
"context"
"errors"
"testing"
"github.com/Wei-Shaw/sub2api/internal/config"
"github.com/stretchr/testify/require"
)
type aliyunVerifierSpy struct {
called int
lastCred AliyunCaptchaCredentials
lastParam string
result *AliyunCaptchaVerifyResult
err error
}
func (s *aliyunVerifierSpy) VerifyCaptcha(_ context.Context, cred AliyunCaptchaCredentials, param string) (*AliyunCaptchaVerifyResult, error) {
s.called++
s.lastCred = cred
s.lastParam = param
if s.err != nil {
return nil, s.err
}
if s.result != nil {
return s.result, nil
}
return &AliyunCaptchaVerifyResult{VerifyResult: true}, nil
}
func aliyunEnabledSettings() map[string]string {
return map[string]string{
SettingKeyAliyunCaptchaEnabled: "true",
SettingKeyAliyunCaptchaAccessKeyID: "ak-id",
SettingKeyAliyunCaptchaAccessKeySecret: "ak-secret",
SettingKeyAliyunCaptchaSceneID: "scene-1",
SettingKeyAliyunCaptchaPrefix: "prefix-1",
}
}
func aliyunTestConfig() AliyunCaptchaConfig {
return AliyunCaptchaConfig{
Enabled: true,
AccessKeyID: "ak-id",
AccessKeySecret: "ak-secret",
SceneID: "scene-1",
Region: AliyunCaptchaRegionCN,
}
}
func newAliyunAuthServiceForTest(cfg *config.Config, settings map[string]string, aliyunSpy *aliyunVerifierSpy) *AuthService {
settingService := NewSettingService(&settingPublicRepoStub{values: settings}, cfg)
authService := NewAuthService(
nil, // entClient
nil, // userRepo
nil, // redeemRepo
nil, // refreshTokenCache
cfg,
settingService,
nil, // emailService
NewTurnstileService(settingService, &turnstileVerifierSpy{}),
nil, // emailQueueService
nil, // promoService
nil, // defaultSubAssigner
nil, // affiliateService
nil, // userPlatformQuotaRepo
)
authService.SetAliyunCaptchaService(NewAliyunCaptchaService(settingService, aliyunSpy))
return authService
}
func TestAliyunCaptchaServiceVerifyParamDispatch(t *testing.T) {
spy := &aliyunVerifierSpy{}
svc := NewAliyunCaptchaService(nil, spy)
err := svc.VerifyParamWithConfig(context.Background(), aliyunTestConfig(), "captcha-verify-param")
require.NoError(t, err)
require.Equal(t, 1, spy.called)
require.Equal(t, "captcha-verify-param", spy.lastParam)
require.Equal(t, "ak-id", spy.lastCred.AccessKeyID)
require.Equal(t, "scene-1", spy.lastCred.SceneID)
require.Equal(t, "captcha.cn-shanghai.aliyuncs.com", spy.lastCred.Endpoint)
}
func TestAliyunCaptchaServiceSgpEndpoint(t *testing.T) {
spy := &aliyunVerifierSpy{}
svc := NewAliyunCaptchaService(nil, spy)
cfg := aliyunTestConfig()
cfg.Region = AliyunCaptchaRegionSGP
err := svc.VerifyParamWithConfig(context.Background(), cfg, "captcha-verify-param")
require.NoError(t, err)
require.Equal(t, "captcha.ap-southeast-1.aliyuncs.com", spy.lastCred.Endpoint)
}
func TestAliyunCaptchaServiceFailsClosedOnVerifierError(t *testing.T) {
spy := &aliyunVerifierSpy{err: errors.New("network down")}
svc := NewAliyunCaptchaService(nil, spy)
err := svc.VerifyParamWithConfig(context.Background(), aliyunTestConfig(), "captcha-verify-param")
require.ErrorIs(t, err, ErrAliyunCaptchaVerificationFailed)
}
func TestAliyunCaptchaServiceRejectsVerifyResultFalse(t *testing.T) {
spy := &aliyunVerifierSpy{result: &AliyunCaptchaVerifyResult{VerifyResult: false, VerifyCode: "F001"}}
svc := NewAliyunCaptchaService(nil, spy)
err := svc.VerifyParamWithConfig(context.Background(), aliyunTestConfig(), "captcha-verify-param")
require.ErrorIs(t, err, ErrAliyunCaptchaVerificationFailed)
}
func TestAliyunCaptchaServiceRejectsIncompleteCredentials(t *testing.T) {
spy := &aliyunVerifierSpy{}
svc := NewAliyunCaptchaService(nil, spy)
cfg := aliyunTestConfig()
cfg.AccessKeySecret = ""
err := svc.VerifyParamWithConfig(context.Background(), cfg, "captcha-verify-param")
require.ErrorIs(t, err, ErrAliyunCaptchaNotConfigured)
require.Zero(t, spy.called)
}
func TestAliyunCaptchaServiceRejectsEmptyParam(t *testing.T) {
spy := &aliyunVerifierSpy{}
svc := NewAliyunCaptchaService(nil, spy)
err := svc.VerifyParamWithConfig(context.Background(), aliyunTestConfig(), "")
require.ErrorIs(t, err, ErrAliyunCaptchaVerificationFailed)
require.Zero(t, spy.called)
}
func TestAliyunCaptchaServiceValidateCredentials(t *testing.T) {
t.Run("invalid credential code", func(t *testing.T) {
spy := &aliyunVerifierSpy{err: &AliyunCaptchaAPIError{Code: "SignatureDoesNotMatch", Message: "bad sk"}}
svc := NewAliyunCaptchaService(nil, spy)
err := svc.ValidateCredentials(context.Background(), "id", "sk", "scene", "cn")
require.ErrorIs(t, err, ErrCaptchaInvalidCredentials)
})
t.Run("network error surfaces", func(t *testing.T) {
spy := &aliyunVerifierSpy{err: errors.New("timeout")}
svc := NewAliyunCaptchaService(nil, spy)
err := svc.ValidateCredentials(context.Background(), "id", "sk", "scene", "cn")
require.Error(t, err)
require.NotErrorIs(t, err, ErrCaptchaInvalidCredentials)
})
t.Run("verify result false means credentials valid", func(t *testing.T) {
spy := &aliyunVerifierSpy{result: &AliyunCaptchaVerifyResult{VerifyResult: false}}
svc := NewAliyunCaptchaService(nil, spy)
err := svc.ValidateCredentials(context.Background(), "id", "sk", "scene", "sgp")
require.NoError(t, err)
require.Equal(t, "captcha.ap-southeast-1.aliyuncs.com", spy.lastCred.Endpoint)
})
}
func TestAuthServiceVerifyCaptchaDispatchesAliyun(t *testing.T) {
spy := &aliyunVerifierSpy{}
authService := newAliyunAuthServiceForTest(&config.Config{}, aliyunEnabledSettings(), spy)
// 阿里云 captchaVerifyParam 复用 turnstile_token 请求字段
err := authService.VerifyCaptcha(context.Background(), CaptchaProof{TurnstileToken: "captcha-verify-param"}, "127.0.0.1")
require.NoError(t, err)
require.Equal(t, 1, spy.called)
require.Equal(t, "captcha-verify-param", spy.lastParam)
}
func TestAuthServiceVerifyCaptchaRejectsProviderConflict(t *testing.T) {
settings := aliyunEnabledSettings()
settings[SettingKeyTurnstileEnabled] = "true"
settings[SettingKeyTurnstileSecretKey] = "secret"
spy := &aliyunVerifierSpy{}
authService := newAliyunAuthServiceForTest(&config.Config{}, settings, spy)
err := authService.VerifyCaptcha(context.Background(), CaptchaProof{TurnstileToken: "param"}, "127.0.0.1")
require.ErrorIs(t, err, ErrCaptchaProviderConflict)
require.Zero(t, spy.called)
}
func TestAuthServiceVerifyCaptchaRequiredModeWithAliyun(t *testing.T) {
cfg := &config.Config{
Server: config.ServerConfig{Mode: "release"},
Turnstile: config.TurnstileConfig{Required: true},
}
spy := &aliyunVerifierSpy{}
authService := newAliyunAuthServiceForTest(cfg, aliyunEnabledSettings(), spy)
// required 模式 + 阿里云启用且凭证齐全:不误报 NOT_CONFIGURED,正常走阿里云校验
err := authService.VerifyCaptcha(context.Background(), CaptchaProof{TurnstileToken: "captcha-verify-param"}, "127.0.0.1")
require.NoError(t, err)
require.Equal(t, 1, spy.called)
}
func TestAuthServiceVerifyActionCaptchaIfEnabledDispatchesAliyun(t *testing.T) {
spy := &aliyunVerifierSpy{}
authService := newAliyunAuthServiceForTest(&config.Config{}, aliyunEnabledSettings(), spy)
err := authService.VerifyActionCaptchaIfEnabled(context.Background(), CaptchaProof{TurnstileToken: "captcha-verify-param"}, "127.0.0.1")
require.NoError(t, err)
require.Equal(t, 1, spy.called)
require.Equal(t, "captcha-verify-param", spy.lastParam)
}
func TestAuthServiceVerifyActionCaptchaIfEnabledSkipsWhenOnlyTurnstile(t *testing.T) {
spy := &aliyunVerifierSpy{}
authService := newAliyunAuthServiceForTest(&config.Config{}, map[string]string{
SettingKeyTurnstileEnabled: "true",
SettingKeyTurnstileSecretKey: "secret",
}, spy)
// Turnstile 不扩大既有覆盖:扩展入口不拦截
err := authService.VerifyActionCaptchaIfEnabled(context.Background(), CaptchaProof{}, "127.0.0.1")
require.NoError(t, err)
require.Zero(t, spy.called)
}