refactor(captcha): update Cap backend to 2.0.0 API format

pull/2573/head
WittF 2025-06-26 04:58:16 +08:00
parent 3db522609e
commit 26fde303c5
4 changed files with 14 additions and 13 deletions

View File

@ -3,6 +3,12 @@ package middleware
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/cloudreve/Cloudreve/v4/application/dependency"
"github.com/cloudreve/Cloudreve/v4/pkg/logging"
"github.com/cloudreve/Cloudreve/v4/pkg/recaptcha"
@ -11,11 +17,6 @@ import (
"github.com/cloudreve/Cloudreve/v4/pkg/setting"
"github.com/gin-gonic/gin"
"github.com/mojocn/base64Captcha"
"io"
"net/http"
"net/url"
"strings"
"time"
)
type req struct {
@ -133,7 +134,7 @@ func CaptchaRequired(enabled func(c *gin.Context) bool) gin.HandlerFunc {
break
case setting.CaptchaCap:
captchaSetting := settings.CapCaptcha(c)
if captchaSetting.InstanceURL == "" || captchaSetting.KeyID == "" || captchaSetting.KeySecret == "" {
if captchaSetting.InstanceURL == "" || captchaSetting.SiteKey == "" || captchaSetting.SecretKey == "" {
l.Warning("Cap verification failed: missing configuration")
c.JSON(200, serializer.ErrWithDetails(c, serializer.CodeCaptchaError, "Captcha configuration error", nil))
c.Abort()
@ -146,9 +147,9 @@ func CaptchaRequired(enabled func(c *gin.Context) bool) gin.HandlerFunc {
request2.WithHeader(http.Header{"Content-Type": []string{"application/json"}}),
)
capEndpoint := strings.TrimSuffix(captchaSetting.InstanceURL, "/") + "/" + captchaSetting.KeyID + "/siteverify"
capEndpoint := strings.TrimSuffix(captchaSetting.InstanceURL, "/") + "/" + captchaSetting.SiteKey + "/siteverify"
requestBody := map[string]string{
"secret": captchaSetting.KeySecret,
"secret": captchaSetting.SecretKey,
"response": service.Ticket,
}
requestData, err := json.Marshal(requestBody)

View File

@ -669,8 +669,8 @@ func (s *settingProvider) TurnstileCaptcha(ctx context.Context) *Turnstile {
func (s *settingProvider) CapCaptcha(ctx context.Context) *Cap {
return &Cap{
InstanceURL: s.getString(ctx, "captcha_cap_instance_url", ""),
KeyID: s.getString(ctx, "captcha_cap_key_id", ""),
KeySecret: s.getString(ctx, "captcha_cap_key_secret", ""),
SiteKey: s.getString(ctx, "captcha_cap_key_id", ""),
SecretKey: s.getString(ctx, "captcha_cap_key_secret", ""),
}
}

View File

@ -50,8 +50,8 @@ type Turnstile struct {
type Cap struct {
InstanceURL string
KeyID string
KeySecret string
SiteKey string
SecretKey string
}
type SMTP struct {

View File

@ -137,7 +137,7 @@ func (s *GetSettingService) GetSiteConfig(c *gin.Context) (*SiteConfig, error) {
TurnstileSiteID: settings.TurnstileCaptcha(c).Key,
ReCaptchaKey: reCaptcha.Key,
CapInstanceURL: capCaptcha.InstanceURL,
CapKeyID: capCaptcha.KeyID,
CapKeyID: capCaptcha.SiteKey,
AppPromotion: appSetting.Promotion,
}, nil
}