captchafox

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: Apache-2.0 Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

CaptchaFox for Go

Official CaptchaFox integration for Go. It verifies the answers of the widget with CaptchaFox.

Compatibility

Go Library
1.26, 1.27 1.x

Go 1.25 and older are not supported: the module requires Go 1.26, so the go command refuses it or, where GOTOOLCHAIN allows, switches your module to Go 1.26.

Installation

go get github.com/CaptchaFox/captchafox-go

The library needs nothing but the standard library.

Configuration

Get your keys

In the CaptchaFox portal, open your website under Sites > Site Details and copy the site key and the secret key.

Set up the library

Create a Client once with the secret key of your site and reuse it; it is safe for concurrent use. Keep the secret key in your configuration or environment, not in source code:

client, err := captchafox.NewClient(os.Getenv("CAPTCHAFOX_SECRET"))
if err != nil {
	// The secret key is empty.
}
Options
Option Default Meaning
timeout 5 seconds How long a verification may take, from connecting to CaptchaFox to the end of its answer: captchafox.NewClientWithTimeout(secret, 3*time.Second)

Usage

The widget adds the token to the form as the POST parameter cf-captcha-response. Verify it on the server before the form is processed:

import "github.com/CaptchaFox/captchafox-go"

token := r.PostFormValue("cf-captcha-response")
verification, err := client.VerifyWith(r.Context(), token, siteKey, remoteIP)
if err != nil {
	// CaptchaFox gave no usable answer: decide whether the form passes.
} else if _, passed := verification.(*captchafox.Passed); !passed {
	// Reject the form.
}

Verify(ctx, token) sends the token alone; VerifyWith(ctx, token, sitekey, remoteIP) also sends the site key the token was issued for and the visitor's IP address, each only when it is not empty. A verification ends when the context ends or the timeout passes, whichever comes first.

A verification has one of three outcomes:

  • *captchafox.Passed: CaptchaFox accepted the token. ChallengeTS and Hostname hold the time of the challenge and the hostname of the site, where CaptchaFox sent them.
  • *captchafox.Rejected: CaptchaFox rejected the token; ErrorCodes holds its error codes, such as timeout-or-duplicate, possibly none. An empty token is rejected with missing-input-response without asking CaptchaFox.
  • *captchafox.UnavailableError: CaptchaFox gave no usable answer; see If CaptchaFox is unreachable.

Operation

If CaptchaFox is unreachable

When CaptchaFox cannot be reached, does not complete its answer within the timeout, answers with an HTTP status other than 200, or with a body that is no valid answer, Verify and VerifyWith return an *UnavailableError; it is the only error they return, and errors.As finds it. It wraps the cause, so errors.Is(err, context.Canceled) tells a cancelled context. Your code decides whether the form passes then: letting it through keeps your forms available during an outage, rejecting it keeps them protected. Tokens that CaptchaFox rejects are always *captchafox.Rejected.

Log

The library logs nothing. Its errors never contain the secret key, and printing a Client with fmt does not show it.

Visitor IP

The visitor's IP address is sent only when you pass it as remoteIP. Behind a reverse proxy or load balancer, pass the visitor's address that the proxy forwards, not the address of the proxy.

Privacy

CaptchaFox is operated by Scoria Labs GmbH, Germany. How CaptchaFox processes data and how to get a data processing agreement is described in the CaptchaFox Privacy Center. A suggested paragraph for your privacy policy is on the GDPR page.

License

Apache-2.0. See LICENSE.

Documentation

Overview

Package captchafox verifies tokens of the CaptchaFox widget with siteverify, with the secret key of a site.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client verifies tokens with the secret key of a site. It is safe for concurrent use; create it once and reuse it.

func NewClient

func NewClient(secret string) (*Client, error)

NewClient returns a Client that gives a verification 5 seconds, from connecting to siteverify to the end of its answer. It returns an error if the secret key is empty.

func NewClientWithTimeout

func NewClientWithTimeout(secret string, timeout time.Duration) (*Client, error)

NewClientWithTimeout returns a Client that gives a verification the timeout instead of 5 seconds, from connecting to siteverify to the end of its answer. It returns an error if the secret key is empty or the timeout is not positive.

func (*Client) Verify

func (c *Client) Verify(ctx context.Context, token string) (Verification, error)

Verify verifies a token. It returns *Passed or *Rejected; an empty token is rejected with missing-input-response without a request. Every error it returns is an *UnavailableError.

func (*Client) VerifyWith

func (c *Client) VerifyWith(ctx context.Context, token, sitekey, remoteIP string) (Verification, error)

VerifyWith is Verify for a token that was issued for a site key to a visitor's IP address; it sends the site key and the IP address only when they are not empty.

type Passed

type Passed struct {
	// The time of the challenge as siteverify sent it, an ISO 8601 timestamp; empty if it sent none.
	ChallengeTS string
	// The hostname of the site where the challenge was solved; empty if siteverify sent none.
	Hostname string
}

Passed means siteverify answered "success": true.

type Rejected

type Rejected struct {
	// The error codes of siteverify, such as timeout-or-duplicate; possibly none.
	ErrorCodes []string
}

Rejected means siteverify answered "success": false, or the token was empty.

type UnavailableError

type UnavailableError struct {
	// contains filtered or unexported fields
}

UnavailableError means siteverify could not be evaluated: it was not reached, did not answer in time, answered with an HTTP status other than 200, or with a body that is no JSON object with a boolean success. Whether the protected request passes then is the caller's decision. Its message never contains the secret key.

func (*UnavailableError) Error

func (e *UnavailableError) Error() string

func (*UnavailableError) Unwrap

func (e *UnavailableError) Unwrap() error

Unwrap returns the error that made siteverify unavailable, such as that of the network or of a cancelled context.

type Verification

type Verification interface {
	// contains filtered or unexported methods
}

Verification is the outcome of a token that siteverify evaluated: *Passed or *Rejected, never both and nothing else. A token whose verification cannot be evaluated has no Verification; Verify returns an *UnavailableError instead.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL