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,49 @@
|
||||
package captcha
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type CaptchaType string
|
||||
|
||||
const (
|
||||
CaptchaTypeLocal CaptchaType = "local"
|
||||
CaptchaTypeTurnstile CaptchaType = "turnstile"
|
||||
)
|
||||
|
||||
// Service defines the captcha service interface
|
||||
type Service interface {
|
||||
// Generate generates a new captcha
|
||||
// For local captcha: returns id and base64 image
|
||||
// For turnstile: returns empty strings
|
||||
Generate(ctx context.Context) (id string, image string, err error)
|
||||
|
||||
// Verify verifies the captcha
|
||||
// For local captcha: token is captcha id, code is user input
|
||||
// For turnstile: token is cf-turnstile-response, code is ignored
|
||||
Verify(ctx context.Context, token string, code string, ip string) (bool, error)
|
||||
|
||||
// GetType returns the captcha type
|
||||
GetType() CaptchaType
|
||||
}
|
||||
|
||||
// Config holds the configuration for captcha service
|
||||
type Config struct {
|
||||
Type CaptchaType
|
||||
RedisClient *redis.Client
|
||||
TurnstileSecret string
|
||||
}
|
||||
|
||||
// NewService creates a new captcha service based on the config
|
||||
func NewService(config Config) Service {
|
||||
switch config.Type {
|
||||
case CaptchaTypeTurnstile:
|
||||
return newTurnstileService(config.TurnstileSecret)
|
||||
case CaptchaTypeLocal:
|
||||
fallthrough
|
||||
default:
|
||||
return newLocalService(config.RedisClient)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user