Documentation
¶
Index ¶
- Variables
- func InitGlobalManager(config *Config)
- func ValidatePayload(id, typ string, value interface{}) error
- func VerifyPayload(p Payload) (bool, error)
- type CaptchaFields
- type CharMarker
- type ClickCaptcha
- type Config
- type ImageCaptcha
- type JigsawCaptcha
- type Manager
- type MathCaptcha
- type MemoryStore
- func (s *MemoryStore) Delete(id string) error
- func (s *MemoryStore) Get(id string) (interface{}, error)
- func (s *MemoryStore) Set(id string, data interface{}, expires time.Time) error
- func (s *MemoryStore) VerifyWithFunc(id string, input interface{}, compareFunc func(stored, input interface{}) bool) (bool, error)
- func (s *MemoryStore) VerifyWithFuncWithoutDelete(id string, input interface{}, compareFunc func(stored, input interface{}) bool) (bool, error)
- type Payload
- type Point
- type Result
- type RotateCaptcha
- type SliderCaptcha
- type Store
- type Type
Constants ¶
This section is empty.
Variables ¶
var ( ErrPayloadRequired = errors.New("captcha: id and type are required") ErrPayloadInvalid = errors.New("captcha: verification failed") )
Sentinel errors returned by ValidatePayload so callers can map to i18n responses.
var LoginCaptchaTypes = []Type{TypeImage, TypeMath}
LoginCaptchaTypes are the kinds randomly issued for auth flows. Slider, click, jigsaw, and rotate are excluded: poor UX on mobile and low-quality visuals.
Functions ¶
func InitGlobalManager ¶
func InitGlobalManager(config *Config)
InitGlobalManager initializes GlobalManager once.
func ValidatePayload ¶
ValidatePayload trims and validates a captcha proof without touching any HTTP context. Returns nil on success, or ErrPayloadRequired / ErrPayloadInvalid so the caller can decide how to surface the error.
func VerifyPayload ¶
VerifyPayload validates using GlobalManager.
Types ¶
type CaptchaFields ¶
type CaptchaFields struct {
CaptchaID string `json:"captchaId"`
CaptchaType string `json:"captchaType"`
CaptchaValue interface{} `json:"captchaValue"`
}
CaptchaFields is embedded in public auth requests that require human verification.
type CharMarker ¶
CharMarker is one character rendered on the click-captcha canvas.
type ClickCaptcha ¶
type ClickCaptcha struct {
// contains filtered or unexported fields
}
ClickCaptcha is an ordered click challenge: the user must click on the target characters in the displayed order.
func NewClickCaptcha ¶
func NewClickCaptcha(width, height, count, tolerance int, expiration time.Duration, store Store) *ClickCaptcha
NewClickCaptcha creates a click captcha generator.
func (*ClickCaptcha) Generate ¶
func (cc *ClickCaptcha) Generate() (*Result, error)
Generate creates a click challenge. The client renders the characters; the user must click the targets in the specified order.
type Config ¶
type Config struct {
ImageWidth int
ImageHeight int
ImageLength int
ClickWidth int
ClickHeight int
ClickCount int
ClickTolerance int
SliderTrackWidth int
SliderPassRatio float64
JigsawWidth int
JigsawHeight int
JigsawPieceSize int
JigsawTolerance int
RotateSize int
RotateTolerance int
Expiration time.Duration
Store Store
}
Config holds captcha settings.
type ImageCaptcha ¶
type ImageCaptcha struct {
// contains filtered or unexported fields
}
ImageCaptcha generates distorted-text image challenges.
func NewImageCaptcha ¶
func NewImageCaptcha(width, height, length int, expiration time.Duration, store Store) *ImageCaptcha
NewImageCaptcha creates an image captcha generator.
func (*ImageCaptcha) Generate ¶
func (ic *ImageCaptcha) Generate() (*Result, error)
Generate produces a new image captcha challenge.
func (*ImageCaptcha) Verify ¶
func (ic *ImageCaptcha) Verify(id, code string) (bool, error)
Verify checks the user's answer against the stored code (case-insensitive).
func (*ImageCaptcha) VerifyWithoutDelete ¶
func (ic *ImageCaptcha) VerifyWithoutDelete(id, code string) (bool, error)
VerifyWithoutDelete checks without consuming the captcha (for pre-verification).
type JigsawCaptcha ¶
type JigsawCaptcha struct {
// contains filtered or unexported fields
}
JigsawCaptcha is a slider puzzle challenge: a piece is cut from the image and the user must drag it back to the correct horizontal position.
func NewJigsawCaptcha ¶
func NewJigsawCaptcha(width, height, pieceSize, tolerance int, expiration time.Duration, store Store) *JigsawCaptcha
NewJigsawCaptcha creates a jigsaw captcha generator.
func (*JigsawCaptcha) Generate ¶
func (jc *JigsawCaptcha) Generate() (*Result, error)
Generate creates a jigsaw challenge. The response includes the background image (with the piece area masked) and the puzzle piece image, both as PNG data URLs. The user drags the piece horizontally to fill the gap.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager is the unified captcha manager.
var GlobalManager *Manager
GlobalManager is the process-wide captcha manager.
func EnsureGlobalManager ¶
func EnsureGlobalManager() *Manager
EnsureGlobalManager lazily initializes GlobalManager.
func (*Manager) GenerateRandom ¶
GenerateRandom creates a captcha using RandomType.
type MathCaptcha ¶
type MathCaptcha struct {
// contains filtered or unexported fields
}
MathCaptcha generates arithmetic problems (e.g., "3 + 5 = ?"). The user must solve the problem and submit the numeric answer.
func NewMathCaptcha ¶
func NewMathCaptcha(expiration time.Duration, store Store) *MathCaptcha
NewMathCaptcha creates a math captcha generator.
func (*MathCaptcha) Generate ¶
func (mc *MathCaptcha) Generate() (*Result, error)
Generate produces a new arithmetic challenge.
func (*MathCaptcha) Verify ¶
func (mc *MathCaptcha) Verify(id string, answer int) (bool, error)
Verify checks the user's numeric answer.
func (*MathCaptcha) VerifyString ¶
func (mc *MathCaptcha) VerifyString(id, answer string) (bool, error)
VerifyString checks a string answer (parsed as integer).
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore is an in-process implementation of Store backed by a map.
func NewMemoryStore ¶
func NewMemoryStore() *MemoryStore
NewMemoryStore creates a new in-memory store.
func (*MemoryStore) Delete ¶
func (s *MemoryStore) Delete(id string) error
func (*MemoryStore) Get ¶
func (s *MemoryStore) Get(id string) (interface{}, error)
func (*MemoryStore) Set ¶
func (s *MemoryStore) Set(id string, data interface{}, expires time.Time) error
func (*MemoryStore) VerifyWithFunc ¶
func (s *MemoryStore) VerifyWithFunc(id string, input interface{}, compareFunc func(stored, input interface{}) bool) (bool, error)
VerifyWithFunc checks the captcha and deletes it on success.
func (*MemoryStore) VerifyWithFuncWithoutDelete ¶
func (s *MemoryStore) VerifyWithFuncWithoutDelete(id string, input interface{}, compareFunc func(stored, input interface{}) bool) (bool, error)
VerifyWithFuncWithoutDelete checks the captcha without removing it (for pre-verification).
type Payload ¶
type Payload struct {
ID string `json:"captchaId"`
Type Type `json:"captchaType"`
Value interface{} `json:"captchaValue"`
}
Payload is the client proof submitted with protected actions.
type Result ¶
type Result struct {
ID string `json:"id"`
Type Type `json:"type"`
Data map[string]interface{} `json:"data"`
Expires time.Time `json:"expires"`
}
Result is returned when a captcha challenge is created.
type RotateCaptcha ¶
type RotateCaptcha struct {
// contains filtered or unexported fields
}
RotateCaptcha is a rotation challenge: an image is rotated by a random angle and the user must rotate it back to upright (0 degrees). The server stores the angle the image was rotated by; the user submits the angle they rotated it back. A pass requires the residual to be within tolerance of 0 (mod 360).
func NewRotateCaptcha ¶
func NewRotateCaptcha(size, tolerance int, expiration time.Duration, store Store) *RotateCaptcha
NewRotateCaptcha creates a rotate captcha generator.
func (*RotateCaptcha) Generate ¶
func (rc *RotateCaptcha) Generate() (*Result, error)
Generate creates a rotate challenge. The response includes a circular image rotated by a random angle; the user must rotate it back to upright.
func (*RotateCaptcha) Verify ¶
func (rc *RotateCaptcha) Verify(id string, userAngle int) (bool, error)
Verify checks that the user's rotation angle brings the image within tolerance of upright. The user submits the angle they rotated; the residual is (storedAngle - userAngle) mod 360, which must be within tolerance of 0.
type SliderCaptcha ¶
type SliderCaptcha struct {
// contains filtered or unexported fields
}
SliderCaptcha is a drag-to-end slider challenge.
func NewSliderCaptcha ¶
func NewSliderCaptcha(trackWidth int, passRatio float64, expiration time.Duration, store Store) *SliderCaptcha
NewSliderCaptcha creates a slider captcha manager.
func (*SliderCaptcha) Generate ¶
func (sc *SliderCaptcha) Generate() (*Result, error)
Generate creates a slider challenge.
type Store ¶
type Store interface {
Set(id string, data interface{}, expires time.Time) error
Get(id string) (interface{}, error)
Delete(id string) error
VerifyWithFunc(id string, input interface{}, compareFunc func(stored, input interface{}) bool) (bool, error)
VerifyWithFuncWithoutDelete(id string, input interface{}, compareFunc func(stored, input interface{}) bool) (bool, error)
}
Store is the interface for captcha storage backends.
type Type ¶
type Type string
Type is the captcha challenge kind.
const ( TypeImage Type = "image" // distorted text image TypeClick Type = "click" // ordered click on characters TypeSlider Type = "slider" // drag slider to the end TypeMath Type = "math" // arithmetic problem TypeJigsaw Type = "jigsaw" // drag puzzle piece to fit TypeRotate Type = "rotate" // rotate image to upright position TypeRandom Type = "random" )