# Configuration (laravel-go-captcha)

> Laravel for Go has no config files: the package reads a few environment variables when it registers, and you change everything else in code, in the function…

- Package: laravel-go-captcha (Laravel for Go, alpha), go modules: `github.com/bowerbird-si/laravel-go-captcha`
- Maintenance: active; Latest release: 0.1.0 (2026-12-04); Framework: go.laravel.com v0.2.0 or later; Go: Go 1.27 or later; Licence: MIT
- Links: [Repository](https://github.com/bowerbird-si/laravel-go-captcha), [pkg.go.dev](https://pkg.go.dev/github.com/bowerbird-si/laravel-go-captcha@v0.1.0), [Changelog](https://oss.bowerbird.si/go-captcha/docs/changelog/), [Contributing](https://github.com/bowerbird-si/laravel-go-captcha/blob/7a10d1c56d2e1a3e3e6618d025657dda5d5385cf/CONTRIBUTING.md), [Security policy](https://github.com/bowerbird-si/laravel-go-captcha/blob/7a10d1c56d2e1a3e3e6618d025657dda5d5385cf/SECURITY.md)
- Based on: [mews/captcha](https://github.com/mewebstudio/captcha): The captcha package this ports: profiles, config keys, routes and validation, following version 3.5.1. Its bundled fonts ship with their own licences.
- PHP parity: core (52 covered, 35 diverged, 0 planned of 87 abilities; 44 fixtures; follows mews/captcha 3.5.1; verified 2026-12-04)
- Community package, not affiliated with Laravel.
- HTML version: https://oss.bowerbird.si/go-captcha/docs/configuration/
- Source: https://github.com/bowerbird-si/laravel-go-captcha/blob/7a10d1c56d2e1a3e3e6618d025657dda5d5385cf/docs/configuration.md

Laravel for Go has no config files: the package reads a few environment variables when it registers, and
you change everything else in code, in the function you pass to `captcha.New`.

## Environment

| Variable | Default | What it does |
| --- | --- | --- |
| `CAPTCHA_DISABLE` | `false` | Turns the captcha off: no routes, and every rule passes. |
| `CAPTCHA_STORE` | the default cache store | The cache store for challenges and the failure limit. |
| `CAPTCHA_THROTTLE` | `30` | Images a client may request per minute, on every entry point. `0` turns it off. |
| `CAPTCHA_FAILURES` | `10` | Wrong answers a client may give per minute and profile. `0` turns it off. |
| `CAPTCHA_SINGLE_PROCESS` | `false` | Allows the process-local `memory` store, for an application that runs exactly one process. |

## Profiles

A profile is one captcha style, like a named array in mews/captcha's `config/captcha.php`. Start every
profile from `captcha.Defaults()`, which holds the PHP class defaults:

```go
captcha.New(func(cfg *captcha.Config) {
	contact := captcha.Defaults()
	contact.Length = 5
	contact.Width, contact.Height = 180, 54
	contact.Expire = 2 * time.Minute
	cfg.Profiles["contact"] = contact
})
```

Like PHP, a profile doesn't inherit from the `default` profile: keys you don't set keep the class defaults.

| Field | Range | Meaning |
| --- | --- | --- |
| `Length` | 1 to 32 | Number of characters. |
| `Width`, `Height` | 1 to 1000, 16 to 400 | Image size in pixels. |
| `Quality` | 1 to 100 | JPEG quality. |
| `Math` | | Draws `x + y = ` and expects the sum. Needs `Attempts` from 1 to 3. |
| `Sensitive` | | Keeps the case of answers. |
| `Characters` | 1 to 256 entries | The alphabet for this profile; an entry may be several characters. |
| `Expire` | | How long a challenge may be answered, or `captcha.NoDeadline`, which still ends after `Config.NoDeadlineLifetime` (24 hours by default). |
| `Attempts` | | Answers a challenge accepts: `1` (default), any number, or `captcha.Unlimited`. |
| `Lines`, `LineWidth`, `LineColor` | 0 to 100, 1 to 20 | Noise lines. mews/captcha draws `Lines + 1`. |
| `Angle` | 0 to 180 | The largest rotation of a character, in degrees. |
| `Contrast`, `Sharpen`, `Blur`, `Invert` | −100 to 100, 0 to 100, 0 to 10 | Image filters, as in PHP. |
| `BgImage`, `BgColor` | | A random background image, or a plain colour. |
| `FontColors` | | A palette for the characters; empty means a random colour each. |
| `Fonts`, `Backgrounds` | | Replace the bundled fonts or backgrounds for this profile. |
| `TextLeftPadding`, `MarginTop` | | Where the characters start. `MarginTop` 0 means `Height / Length`. |

Colours are hex (with or without `#`), `rgb()`, `rgba()` or an HTML colour name. Every value is checked
when the application starts: a value out of range, an unknown colour or a profile named `api` stops it
with an error that names the setting.

An unknown profile in a URL is a 404.

### From a PHP config

`captcha.ParsePHPConfig` reads the output of `json_encode(config('captcha'))`, so you can move an existing
config as it is:

```go
cfg, err := captcha.ParsePHPConfig(data)
```

Paths into `vendor/mews/captcha/assets` map to the bundled assets; any other path must exist.

## A custom renderer

The images are drawn by the package's own pipeline, which follows mews/captcha's. To draw them yourself,
set `Config.Renderer`:

```go
captcha.New(func(cfg *captcha.Config) {
	cfg.Renderer = myRenderer{}
})
```

`Render(ctx, profile, settings, text)` returns a JPEG; the package refuses any other output. It is called
by several requests at once, within the render limit below, and `ctx` carries the request's deadline.

`(*Captcha).Config()` returns a copy of the settings the package registered with, so changing it has no
effect.

## Fonts and backgrounds

The six fonts and twelve backgrounds of mews/captcha are bundled with their licences. To build without
them, use the build tag `captcha_noassets` and set `Config.Fonts` and `Config.Backgrounds` to your own
files or directories.

## The challenge store

Each challenge is a record in a cache store. A check consumes it with an atomic compare-and-swap, so of many
simultaneous correct answers exactly one passes. The store must therefore be shared by every process of
your application:

- **Redis** and the **database** cache on Postgres or on a SQLite file of its own are tested and accepted.
- `discard` and `failover` are refused when the application starts.
- `memory` is refused unless `CAPTCHA_SINGLE_PROCESS=true`.
- Any other store, including a custom one registered under a built-in name, needs `Config.TrustStore = true`.
  That states the store is shared by every process and that its compare-and-swap and counters are atomic.

**Durability.** A used challenge can't pass again as long as the store keeps the write that marked it used.
If a Redis primary fails over to a replica that missed that write, or the store is restored from an older
backup, an open copy of the challenge can come back. Where that matters, configure the store to keep
acknowledged writes, or clear the captcha keys after a failover or restore.

**Redis retries.** The Redis client retries a command when the connection drops, even after Redis ran it.
A retried check still never passes a used challenge: the repeated swap finds the record already changed
and fails. A retried give-back of the wrong-answer limit can return one extra unit, though. To make the
limit exact, add `max_retries=-1` to the Redis URL, which turns the client's retries off.

Store operations run outside the caller's database transaction and read from the writer, so a rollback
can't bring a used challenge back and a lagging replica can't serve an old one. Each operation waits at most
2 seconds; if the store doesn't answer, the check fails.

On SQLite, check the captcha before you open a write transaction: inside one, the check can't reach the
store and fails.

## Limits

- **Issuance:** `CAPTCHA_THROTTLE` images per minute per client. Beyond that, a 429.
- **Wrong answers:** at most `CAPTCHA_FAILURES` committed wrong answers per client and profile in each clock
  minute. Every check reserves one unit before it reads anything, and gets it back unless the answer was
  wrong or the outcome is unknown, so simultaneous guesses can't exceed the limit. Because the window is a
  clock minute, up to twice the limit can land within 60 seconds across a minute boundary. It is a rate
  bound, not a guarantee: a math profile has only 29 possible sums, so keep its `Attempts` low (at most 3).
- **The client** is the request's IP address, as the framework resolves it through your trusted proxies. A
  proxy you trust must set the forwarded address correctly. Checks made without a request share one bucket.
- **Rendering:** at most `Config.RenderConcurrency` images are drawn at once (default: `GOMAXPROCS`). When
  all are busy for 50 ms, the image routes answer 503 with `Retry-After: 1`.
