The captcha reads the CaptchaConfig section. Its keys follow PHP's config/captcha.php.
Environment variables
| Variable | Default | What |
|---|---|---|
CAPTCHA_DISABLE | false | No routes, and the rules pass. For tests and local work. PHP: disable. |
CAPTCHA_STORE | the default cache store | The cache store that holds challenges and both quota counters. |
CAPTCHA_PATH | captcha | The URL prefix of both routes. |
CAPTCHA_THROTTLE | 30 | At most this many images per client per clock minute. |
CAPTCHA_FAILURE_LIMIT | 10 | At most this many wrong answers per client and profile per clock minute. |
CAPTCHA_MAX_RENDERS | CPU count | Images drawn at once. |
CAPTCHA_SINGLE_PROCESS | false | Accept the process-local memory store in production. |
CAPTCHA_TRUST_STORE | false | Accept a non-memory store in production after verifying its backend and atomic operations. See Deployment. |
CAPTCHA_ANSWER_FIELDS | captcha | Comma-separated answer field names. Use dot paths for nested fields, such as contact.human_code. |
CAPTCHA_KEY_FIELD | captcha_key | The form field that carries a keyed challenge's id. |
The provider registers the answer fields and key field with Laravel's old-input filter.
Set the input middleware exceptions in your application's
with_middleware callback. If a keyed rule uses a .key_field(…) override, include that field
in CAPTCHA_ANSWER_FIELDS.
Profiles
A profile says how a challenge looks and how it may be answered. Five ship with PHP's values:
default (6 characters, 345×65), flat, mini, inverse and math. Change them or add your own in
src/app.rs:
use std::time::Duration;
use laravel_rust_captcha::{Attempts, CaptchaConfig, Profile};
Application::configure().with_config(|config, _| {
config.section_mut::<CaptchaConfig>().profile(
"contact",
Profile::default()
.length(5u32)
.size(180, 54)
.expire(Some(Duration::from_secs(120)))
.font_colors(["#111827", "#991b1b", "#0f766e"])
.lines(8u32)
.line_color("#64748b")
.angle(18u32)
.contrast(-8)
.sharpen(6u32)
.blur(1u32)
.quality(95),
);
})
A new profile starts from PHP's class defaults (Profile::default(): 5 characters, 120×36), not from
the default profile, as in PHP.
| Rust | PHP key | Range | Default |
|---|---|---|---|
length | length | 1–32 | 5 |
width, height (size) | width, height | 1–1000, 16–400 | 120, 36 |
quality | quality | 1–100 | 90 (honoured; PHP always used 75) |
math | math | false | |
expire | expire (seconds) | ≥ 1 s or None | 60 s |
lines, line_width, line_color | lines, lineWidth, lineColor | 0–100, 1–20, colour | 3, 2, ff00ff |
angle | angle | 0–180 | 15 |
bg_image, bg_color | bgImage, bgColor | true, #ffffff | |
contrast, sharpen, blur, invert | same | −100–100, 0–100, 0–10, | 0, 0, 0, false |
sensitive | sensitive | false | |
font_colors | fontColors | colours | any colour |
text_left_padding, margin_top | textLeftPadding, marginTop | 4, 0 | |
characters, fonts, backgrounds | profile keys of the same name | the global ones | |
attempts | new | Limited(n) or Unlimited. Math requires Limited(1..=3). | Limited(1) |
retention | new | ≥ 1 s | 24 h, for challenges without a deadline |
format | new | Jpeg or Png | Jpeg |
Colours accept Intervention Image's named colours, hex with or without # (3, 4, 6 or 8
digits), RGB and sRGB, HSL, HSV and HSB, CMYK, Oklab and Oklch. Invalid settings stop the app at
boot with the profile and key named. The profile's longest answer must fit within 256 bytes.
For a text profile, multiply its length by the byte length of its longest effective alphabet entry.
Characters, fonts and backgrounds
use laravel_rust_captcha::{Assets, Characters};
let captcha = config.section_mut::<CaptchaConfig>();
captcha.characters = Characters::from_chars("ABCDEFGHJKMNPQRSTUWXYZ23456789");
captcha.fonts = Assets::Directory("resources/captcha/fonts".into());
captcha.backgrounds = Assets::Bundled;
With the default bundled-assets feature, the PHP package's 6 fonts and 12 backgrounds are embedded.
Without it, set fonts (and optionally backgrounds). Files are read once at boot. Asset files are limited to 8 MiB each and 32 MiB in total across
global and profile loads. Background decoding limits dimensions to 4096 × 4096 and sets a 64 MiB
allocation limit.
Attempts and deadlines
// A long form: five tries within 30 minutes.
Profile::default().attempts(Attempts::Limited(5)).expire(Some(Duration::from_secs(30 * 60)))
// No deadline and unlimited tries, as a common fork of the PHP package does. The challenge is kept
// for `retention` (24 hours), and the failure limit per client still applies.
Profile::default().attempts(Attempts::Unlimited).expire(None)Loading a PHP config
Export your PHP config with json_encode(config('captcha')) and load it:
let captcha = CaptchaConfig::from_php_json(include_str!("captcha.json"))?;
It accepts PHP's key names and integers in characters. It maps paths into
vendor/mews/captcha/assets to the bundled assets. It ignores driver and encrypt, and rejects any
other unknown key.