diff --git a/common/constants.go b/common/constants.go index 5a15f7301a85..8caba2a805e0 100644 --- a/common/constants.go +++ b/common/constants.go @@ -89,6 +89,7 @@ var LinuxDOOAuthEnabled = false var WeChatAuthEnabled = false var TelegramOAuthEnabled = false var TurnstileCheckEnabled = false +var RegisterPageWithCaptchaEnabled = false var RegisterEnabled = true var EmailDomainRestrictionEnabled = false // 是否启用邮箱域名限制 diff --git a/controller/misc.go b/controller/misc.go index eada4909a090..5e5fe2864c51 100644 --- a/controller/misc.go +++ b/controller/misc.go @@ -69,6 +69,7 @@ func GetStatus(c *gin.Context) { "wechat_login": common.WeChatAuthEnabled, "server_address": system_setting.ServerAddress, "turnstile_check": common.TurnstileCheckEnabled, + "register_page_with_captcha": common.RegisterPageWithCaptchaEnabled, "turnstile_site_key": common.TurnstileSiteKey, "docs_link": operation_setting.GetGeneralSetting().DocsLink, "quota_per_unit": common.QuotaPerUnit, diff --git a/controller/option.go b/controller/option.go index a97f07b841b7..a65f8be321f0 100644 --- a/controller/option.go +++ b/controller/option.go @@ -205,6 +205,15 @@ func UpdateOption(c *gin.Context) { "message": "无法启用 Turnstile 校验,请先填入 Turnstile 校验相关配置信息!", }) + return + } + case "RegisterPageWithCaptchaEnabled": + if option.Value == "true" && common.TurnstileSiteKey == "" { + c.JSON(http.StatusOK, gin.H{ + "success": false, + "message": "无法启用注册人机验证,请先填入 Turnstile 校验相关配置信息!", + }) + return } case "TelegramOAuthEnabled": diff --git a/middleware/turnstile-check.go b/middleware/turnstile-check.go index af87fad4423c..5e0fd4c0a8aa 100644 --- a/middleware/turnstile-check.go +++ b/middleware/turnstile-check.go @@ -16,7 +16,19 @@ type turnstileCheckResponse struct { func TurnstileCheck() gin.HandlerFunc { return func(c *gin.Context) { - if common.TurnstileCheckEnabled { + enabled := common.TurnstileCheckEnabled + if !enabled && common.RegisterPageWithCaptchaEnabled { + path := c.Request.URL.Path + // /api/user/register is always a registration flow. + // /api/verification is shared: registration (unauthenticated) and + // email binding (authenticated). Only enforce for the former. + if path == "/api/user/register" { + enabled = true + } else if path == "/api/verification" && c.GetHeader("Authorization") == "" { + enabled = true + } + } + if enabled { session := sessions.Default(c) turnstileChecked := session.Get("turnstile") if turnstileChecked != nil { diff --git a/model/option.go b/model/option.go index 8e8587f271c8..0c85beb0d66e 100644 --- a/model/option.go +++ b/model/option.go @@ -44,6 +44,7 @@ func InitOptionMap() { common.OptionMap["TelegramOAuthEnabled"] = strconv.FormatBool(common.TelegramOAuthEnabled) common.OptionMap["WeChatAuthEnabled"] = strconv.FormatBool(common.WeChatAuthEnabled) common.OptionMap["TurnstileCheckEnabled"] = strconv.FormatBool(common.TurnstileCheckEnabled) + common.OptionMap["RegisterPageWithCaptchaEnabled"] = strconv.FormatBool(common.RegisterPageWithCaptchaEnabled) common.OptionMap["RegisterEnabled"] = strconv.FormatBool(common.RegisterEnabled) common.OptionMap["AutomaticDisableChannelEnabled"] = strconv.FormatBool(common.AutomaticDisableChannelEnabled) common.OptionMap["AutomaticEnableChannelEnabled"] = strconv.FormatBool(common.AutomaticEnableChannelEnabled) @@ -296,6 +297,8 @@ func updateOptionMap(key string, value string) (err error) { common.TelegramOAuthEnabled = boolValue case "TurnstileCheckEnabled": common.TurnstileCheckEnabled = boolValue + case "RegisterPageWithCaptchaEnabled": + common.RegisterPageWithCaptchaEnabled = boolValue case "RegisterEnabled": common.RegisterEnabled = boolValue case "EmailDomainRestrictionEnabled": diff --git a/web/classic/src/components/settings/SystemSetting.jsx b/web/classic/src/components/settings/SystemSetting.jsx index de0962c1b0b1..7fed7320932c 100644 --- a/web/classic/src/components/settings/SystemSetting.jsx +++ b/web/classic/src/components/settings/SystemSetting.jsx @@ -78,6 +78,7 @@ const SystemSetting = () => { WeChatServerToken: '', WeChatAccountQRCodeImageURL: '', TurnstileCheckEnabled: '', + RegisterPageWithCaptchaEnabled: '', TurnstileSiteKey: '', TurnstileSecretKey: '', RegisterEnabled: '', @@ -181,6 +182,7 @@ const SystemSetting = () => { case 'TelegramOAuthEnabled': case 'RegisterEnabled': case 'TurnstileCheckEnabled': + case 'RegisterPageWithCaptchaEnabled': case 'EmailDomainRestrictionEnabled': case 'EmailAliasRestrictionEnabled': case 'SMTPSSLEnabled': @@ -1070,6 +1072,15 @@ const SystemSetting = () => { > {t('允许 Turnstile 用户校验')} +