Documentation
¶
Overview ¶
config.go
Package captcha provides a unified interface for various CAPTCHA services
Index ¶
- Variables
- func Init(configs ...Config) error
- func Reset()
- type Builder
- type CaptchaService
- type Config
- type DisabledService
- type GoogleCaptchaService
- func (g *GoogleCaptchaService) GenerateHTML() string
- func (g *GoogleCaptchaService) Validate(ctx context.Context, token string, remoteIP string) (bool, error)
- func (g *GoogleCaptchaService) ValidateV3WithScore(ctx context.Context, token, remoteIP, expectedAction string, minScore float64) (bool, float64, error)
- type HCaptchaResponse
- type HCaptchaService
- type RecaptchaResponse
- type TurnstileResponse
- type TurnstileService
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidConfig = errors.New("invalid configuration") ErrNotInitialized = errors.New("service not initialized") ErrProviderRequired = errors.New("captcha provider required") ErrInvalidProvider = errors.New("invalid captcha provider") ErrKeysRequired = errors.New("site key and secret key required") )
Package-specific errors
Functions ¶
Types ¶
type Builder ¶ added in v0.1.0
type Builder struct {
// contains filtered or unexported fields
}
Builder provides a way to create CAPTCHA service instances with custom prefixes
func WithPrefix ¶ added in v0.1.0
WithPrefix creates a new Builder with the specified prefix
func (*Builder) Init ¶ added in v0.1.0
Init initializes the global CAPTCHA service using the builder's prefix
func (*Builder) New ¶ added in v0.1.0
func (b *Builder) New() (CaptchaService, error)
New creates a new CAPTCHA service using the builder's prefix
type CaptchaService ¶
type CaptchaService interface {
// Validate validates a captcha token with the remote service
Validate(ctx context.Context, token string, remoteIP string) (bool, error)
// GenerateHTML generates the HTML needed to embed the captcha
GenerateHTML() string
}
CaptchaService defines the interface for all captcha services
func New ¶ added in v0.0.4
func New(cfg Config) (CaptchaService, error)
New creates a new instance with given config
func Service ¶ added in v0.0.4
func Service() CaptchaService
Service returns the global captcha service instance
type Config ¶ added in v0.0.4
type Config struct {
// Provider specifies the captcha service provider (recaptcha, hcaptcha, turnstile)
Provider string `env:"CAPTCHA_PROVIDER,default:recaptcha"`
// SiteKey is the public key for the captcha service
SiteKey string `env:"CAPTCHA_SITE_KEY"`
// SecretKey is the private key for server-side validation
SecretKey string `env:"CAPTCHA_SECRET_KEY"`
// Version specifies the captcha version (only used for recaptcha: 2 or 3)
Version int `env:"CAPTCHA_VERSION,default:2"`
// Enabled determines if captcha validation is active
Enabled bool `env:"CAPTCHA_ENABLED,default:false"`
}
Config defines captcha service configuration
type DisabledService ¶ added in v0.0.4
type DisabledService struct{}
DisabledService is a no-op captcha service
func (*DisabledService) GenerateHTML ¶ added in v0.0.4
func (d *DisabledService) GenerateHTML() string
type GoogleCaptchaService ¶
type GoogleCaptchaService struct {
// contains filtered or unexported fields
}
GoogleCaptchaService implements the CaptchaService interface for Google reCAPTCHA
func NewGoogleCaptcha ¶
func NewGoogleCaptcha(siteKey, secretKey string, version int) *GoogleCaptchaService
NewGoogleCaptcha creates a new Google reCAPTCHA service
func (*GoogleCaptchaService) GenerateHTML ¶
func (g *GoogleCaptchaService) GenerateHTML() string
GenerateHTML implements the CaptchaService interface for Google reCAPTCHA
func (*GoogleCaptchaService) Validate ¶
func (g *GoogleCaptchaService) Validate(ctx context.Context, token string, remoteIP string) (bool, error)
Validate implements the CaptchaService interface for Google reCAPTCHA
func (*GoogleCaptchaService) ValidateV3WithScore ¶
func (g *GoogleCaptchaService) ValidateV3WithScore(ctx context.Context, token, remoteIP, expectedAction string, minScore float64) (bool, float64, error)
ValidateV3WithScore validates a reCAPTCHA v3 token with specified minimum score and action
type HCaptchaResponse ¶
type HCaptchaResponse struct {
Success bool `json:"success"`
ChallengeTS string `json:"challenge_ts"`
Hostname string `json:"hostname"`
Credit bool `json:"credit,omitempty"`
ErrorCodes []string `json:"error-codes,omitempty"`
Score *float64 `json:"score,omitempty"` // Enterprise only
ScoreReason []string `json:"score_reason,omitempty"` // Enterprise only
}
HCaptchaResponse represents the response from hCaptcha's verification API
type HCaptchaService ¶
type HCaptchaService struct {
// contains filtered or unexported fields
}
HCaptchaService implements the CaptchaService interface for hCaptcha
func NewHCaptcha ¶
func NewHCaptcha(siteKey, secretKey string) *HCaptchaService
NewHCaptcha creates a new hCaptcha service
func (*HCaptchaService) GenerateHTML ¶
func (h *HCaptchaService) GenerateHTML() string
GenerateHTML implements the CaptchaService interface for hCaptcha
type RecaptchaResponse ¶
type RecaptchaResponse struct {
Success bool `json:"success"`
Score float64 `json:"score,omitempty"` // v3 only
Action string `json:"action,omitempty"` // v3 only
ChallengeTS string `json:"challenge_ts"`
Hostname string `json:"hostname,omitempty"`
ErrorCodes []string `json:"error-codes,omitempty"`
}
RecaptchaResponse represents the response from Google's reCAPTCHA API
type TurnstileResponse ¶
type TurnstileResponse struct {
Success bool `json:"success"`
ChallengeTS string `json:"challenge_ts"`
Hostname string `json:"hostname"`
ErrorCodes []string `json:"error-codes,omitempty"`
Action string `json:"action,omitempty"`
CData string `json:"cdata,omitempty"`
}
TurnstileResponse represents the response from Cloudflare's Turnstile verification API
type TurnstileService ¶
type TurnstileService struct {
// contains filtered or unexported fields
}
TurnstileService implements the CaptchaService interface for Cloudflare Turnstile
func NewTurnstile ¶
func NewTurnstile(siteKey, secretKey string) *TurnstileService
NewTurnstile creates a new Cloudflare Turnstile service
func (*TurnstileService) GenerateHTML ¶
func (t *TurnstileService) GenerateHTML() string
GenerateHTML implements the CaptchaService interface for Turnstile