Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶ added in v0.2.1
type Config struct {
Secret []byte // secret key used for HMAC token signing
Length int // number of characters in the captcha answer
Expiry time.Duration // lifetime of a captcha token before it expires
Image bool // whether to generate a PNG image for the captcha
Width uint16 // image width in pixels
Height uint16 // image height in pixels
Noise uint16 // number of random noise lines drawn on the image
CaseSensitive bool // whether captcha answers are case-sensitive
AllowActions []string // optional list of allowed action identifiers
FG color.Color // foreground color for text
BG color.Color // background color of the captcha image
Font FontConfig // font configuration for text rendering
}
Config defines all configurable parameters for the TokenCaptcha service. It controls how tokens are generated, verified, and optionally rendered as images. Missing or zero values are automatically normalized to defaults when creating a new Service instance using New(Config).
type FontConfig ¶ added in v0.2.1
type FontConfig struct {
Name string // name of the built-in font (e.g. "noto-sans" or "jetbrains-mono")
TTF []byte // optional raw TTF font data for custom fonts
Size float64 // font size in points
DPI float64 // dots per inch used for text rendering
}
FontConfig defines the font settings used when rendering captcha images. It allows either selecting a built-in font by name or providing a custom TrueType font as raw byte data. The Size and DPI fields control text scaling and resolution during rendering.
type IssueResult ¶ added in v1.0.0
type IssueResult struct {
TokenB64 string // Base64URL JSON tokenPayload
PNGB64 string // optional field, only set if cfg.Image is true
}
IssueResult represents the response of a generated captcha. TokenB64 contains the Base64URL-encoded JSON token payload. PNGB64 contains the optional captcha image in Base64 PNG format if image generation is enabled.
type Service ¶ added in v0.2.1
type Service struct {
// contains filtered or unexported fields
}
Service represents the main captcha generator and verifier. It operates in a stateless manner, meaning that no data needs to be stored on the server. The service uses an HMAC signature to verify captcha tokens without maintaining sessions.
func New ¶
New creates a new captcha service using the provided configuration. It automatically normalizes the configuration by filling in missing or invalid values with secure and reasonable defaults before returning the initialized Service instance.
func (*Service) IssueCaptcha ¶ added in v1.0.0
func (s *Service) IssueCaptcha(action string) (IssueResult, error)
IssueCaptcha creates a new stateless captcha token and optionally an image. It generates a random answer, computes the HMAC signature, encodes the payload, and returns the token along with the image if configured. The returned captcha is completely stateless and can be verified later using the Verify method without any stored session data.
func (*Service) Verify ¶ added in v1.0.0
Verify validates a user provided captcha answer against a given token. It decodes the token, checks expiration and action values, recalculates the expected HMAC signature, and performs a constant-time comparison. Returns true if the captcha is valid and false otherwise.

