Documentation
¶
Overview ¶
Package captchala provides a server-side SDK for validating Captcha tokens.
Usage:
import captchala "github.com/Captcha-La/captchala-go"
client := captchala.NewClient("your_app_key", "your_app_secret")
result, err := client.Validate(token)
if err != nil {
// handle error
}
if result.Valid {
// verification passed
}
Index ¶
- Constants
- type Client
- func (c *Client) IssueServerToken(action string) (*IssueResult, error)
- func (c *Client) IssueServerTokenWithOptions(action string, opts IssueOptions) (*IssueResult, error)
- func (c *Client) ModerationCheck(input []ModerationItem, userID string) (*ModerationResult, error)
- func (c *Client) ModerationText(text, userID string) (*ModerationResult, error)
- func (c *Client) Validate(token string) (*ValidateResult, error)
- func (c *Client) ValidateWithClientIP(token string, keepToken bool, clientIP string) (*ValidateResult, error)
- func (c *Client) ValidateWithOptions(token string, keepToken bool) (*ValidateResult, error)
- type IssueOptions
- type IssueResult
- type ModerationItem
- type ModerationResult
- type ValidateResult
Constants ¶
const ( // MainAPIURL is the primary API endpoint for pt_ tokens MainAPIURL = "https://apiv1.captcha.la/v1/validate" // BackupAPIURL is the backup API endpoint for offline_ tokens BackupAPIURL = "https://fallbackapiv1.captchala.com/api/validate" // IssueAPIURL mints a one-time server_token (sct_) — POST /v1/server/challenge/issue IssueAPIURL = "https://apiv1.captcha.la/v1/server/challenge/issue" // ModerationCheckURL multi-modal content moderation — POST /v1/moderation/check ModerationCheckURL = "https://apiv1.captcha.la/v1/moderation/check" // ModerationTextURL plain-text moderation — POST /v1/moderation/text ModerationTextURL = "https://apiv1.captcha.la/v1/moderation/text" // Token prefixes PrefixMain = "pt_" // Main API token prefix PrefixOffline = "offline_" // Backup/offline token prefix PrefixClient = "client_" // Client-only token prefix )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
AppKey string
AppSecret string
Timeout time.Duration
// BaseURL overrides the main API URL. Mainly used for tests.
// If empty, MainAPIURL is used.
BaseURL string
// BackupURL overrides the backup API URL. Mainly used for tests.
// If empty, BackupAPIURL is used.
BackupURL string
// IssueURL / ModerationCheckURL / ModerationTextURL override the
// corresponding endpoint URLs. Mainly used for tests; leave empty
// in production.
IssueURL string
ModerationCheckURLStr string
ModerationTextURLStr string
// contains filtered or unexported fields
}
Client is the Captchala SDK client
func NewClientWithTimeout ¶
NewClientWithTimeout creates a new Captchala client with custom timeout
func (*Client) IssueServerToken ¶ added in v1.0.2
func (c *Client) IssueServerToken(action string) (*IssueResult, error)
IssueServerToken mints a one-time server_token for the given action. Hand the returned sct_ token to the browser SDK via the serverToken prop.
func (*Client) IssueServerTokenWithOptions ¶ added in v1.0.2
func (c *Client) IssueServerTokenWithOptions(action string, opts IssueOptions) (*IssueResult, error)
IssueServerTokenWithOptions is the same as IssueServerToken but lets you bind the token to an IP / UID and override TTL / max_uses.
func (*Client) ModerationCheck ¶ added in v1.0.2
func (c *Client) ModerationCheck(input []ModerationItem, userID string) (*ModerationResult, error)
ModerationCheck does multi-modal content moderation. Pass a slice of ModerationItem (text and/or image_url entries). userID is optional.
func (*Client) ModerationText ¶ added in v1.0.2
func (c *Client) ModerationText(text, userID string) (*ModerationResult, error)
ModerationText is a convenience wrapper for plain-text moderation.
func (*Client) Validate ¶
func (c *Client) Validate(token string) (*ValidateResult, error)
Validate validates a captcha token and consumes it
func (*Client) ValidateWithClientIP ¶ added in v1.0.2
func (c *Client) ValidateWithClientIP(token string, keepToken bool, clientIP string) (*ValidateResult, error)
ValidateWithClientIP validates with explicit client IP (for bind_ip check). Pass the real user IP from YOUR request, not the server-side proxy IP. If the pass_token was issued with bind_ip, the backend will compare the provided client_ip against the bound IP and reject mismatches.
func (*Client) ValidateWithOptions ¶
func (c *Client) ValidateWithOptions(token string, keepToken bool) (*ValidateResult, error)
ValidateWithOptions validates a captcha token with options. If keepToken is true, the token will not be consumed and can be validated again.
type IssueOptions ¶ added in v1.0.2
type IssueOptions struct {
BindingIP string // End-user IP — backend rejects token if a different IP redeems it
TTL int // Lifetime in seconds; server enforces a hard upper bound
MaxUses int // SDK retry budget; verification is still single-pass
BindUID string // User ID to bind; pair with ValidateResult.UID on verify side
}
IssueOptions are optional knobs for IssueServerTokenWithOptions.
type IssueResult ¶ added in v1.0.2
type IssueResult struct {
OK bool `json:"ok"`
Token string `json:"token,omitempty"` // sct_<hex>; pass to browser SDK as serverToken
ExpiresIn int `json:"expires_in,omitempty"` // seconds
IssuedAt int64 `json:"issued_at,omitempty"` // unix seconds
Error string `json:"error,omitempty"`
Message string `json:"message,omitempty"`
}
IssueResult contains the server_token issuance result.
type ModerationItem ¶ added in v1.0.2
type ModerationItem struct {
Type string `json:"type"` // "text" or "image_url"
Text string `json:"text,omitempty"`
ImageURL map[string]any `json:"image_url,omitempty"` // {"url": "https://..."}
}
ModerationItem is one entry in a multi-modal moderation request. Use either Text (with Type="text") or ImageURL (with Type="image_url").
func ImageURLItem ¶ added in v1.0.2
func ImageURLItem(url string) ModerationItem
ImageURLItem returns a {type:image_url, image_url:{url}} item.
func TextItem ¶ added in v1.0.2
func TextItem(text string) ModerationItem
TextItem returns a {type:text, text} item ready for ModerationCheck input.
type ModerationResult ¶ added in v1.0.2
type ModerationResult struct {
OK bool `json:"ok"`
Flagged bool `json:"flagged"`
Categories map[string]bool `json:"categories,omitempty"` // category name → tripped
ContentType string `json:"content_type,omitempty"` // "text" / "image" / "mixed"
Raw map[string]any `json:"raw,omitempty"` // full upstream payload
Error string `json:"error,omitempty"`
Message string `json:"message,omitempty"`
}
ModerationResult contains the moderation verdict.
func (*ModerationResult) HasCategory ¶ added in v1.0.2
func (r *ModerationResult) HasCategory(names ...string) bool
HasCategory reports true if any of the named categories tripped.
type ValidateResult ¶
type ValidateResult struct {
// Valid indicates if the token is valid
Valid bool `json:"valid"`
// Offline indicates if this was an offline verification
Offline bool `json:"offline"`
// ClientOnly indicates if this is a client-only token (cannot be verified server-side)
ClientOnly bool `json:"client_only"`
// ChallengeID is the challenge identifier
ChallengeID string `json:"challenge_id,omitempty"`
// Action is the business action associated with the token
Action string `json:"action,omitempty"`
// UID is the user ID bound via bind_uid at server_token issuance time.
// Use this to verify the pass_token was issued for the expected user.
UID string `json:"uid,omitempty"`
// Error contains the error message if validation failed
Error string `json:"error,omitempty"`
// Warning contains warning message (e.g., for client-only tokens)
Warning string `json:"warning,omitempty"`
}
ValidateResult contains the validation result