hi-server/pkg/captcha/turnstile.go
EUForest 0dbcff85f1 feat(captcha): add captcha service interface and implementations
- Add captcha service interface with Generate and Verify methods
- Implement local image captcha using base64Captcha library
- Implement Cloudflare Turnstile verification wrapper
- Support Redis-based captcha storage with 5-minute expiration
- Add factory method for creating captcha service instances
2026-03-09 22:53:13 +08:00

38 lines
785 B
Go

package captcha
import (
"context"
"github.com/perfect-panel/server/pkg/turnstile"
)
type turnstileService struct {
service turnstile.Service
}
func newTurnstileService(secret string) Service {
return &turnstileService{
service: turnstile.New(turnstile.Config{
Secret: secret,
}),
}
}
func (s *turnstileService) Generate(ctx context.Context) (id string, image string, err error) {
// Turnstile doesn't need server-side generation
return "", "", nil
}
func (s *turnstileService) Verify(ctx context.Context, token string, code string, ip string) (bool, error) {
if token == "" {
return false, nil
}
// Verify with Cloudflare Turnstile
return s.service.Verify(ctx, token, ip)
}
func (s *turnstileService) GetType() CaptchaType {
return CaptchaTypeTurnstile
}