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
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user