Configuration

laravel-rust-captcha Alpha Laravel for Rust

The captcha reads the CaptchaConfig section. Its keys follow PHP's config/captcha.php.

Environment variables

VariableDefaultWhat
CAPTCHA_DISABLEfalseNo routes, and the rules pass. For tests and local work. PHP: disable.
CAPTCHA_STOREthe default cache storeThe cache store that holds challenges and both quota counters.
CAPTCHA_PATHcaptchaThe URL prefix of both routes.
CAPTCHA_THROTTLE30At most this many images per client per clock minute.
CAPTCHA_FAILURE_LIMIT10At most this many wrong answers per client and profile per clock minute.
CAPTCHA_MAX_RENDERSCPU countImages drawn at once.
CAPTCHA_SINGLE_PROCESSfalseAccept the process-local memory store in production.
CAPTCHA_TRUST_STOREfalseAccept a non-memory store in production after verifying its backend and atomic operations. See Deployment.
CAPTCHA_ANSWER_FIELDScaptchaComma-separated answer field names. Use dot paths for nested fields, such as contact.human_code.
CAPTCHA_KEY_FIELDcaptcha_keyThe 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.

RustPHP keyRangeDefault
lengthlength1–325
width, height (size)width, height1–1000, 16–400120, 36
qualityquality1–10090 (honoured; PHP always used 75)
mathmathfalse
expireexpire (seconds)≥ 1 s or None60 s
lines, line_width, line_colorlines, lineWidth, lineColor0–100, 1–20, colour3, 2, ff00ff
angleangle0–18015
bg_image, bg_colorbgImage, bgColortrue, #ffffff
contrast, sharpen, blur, invertsame−100–100, 0–100, 0–10,0, 0, 0, false
sensitivesensitivefalse
font_colorsfontColorscoloursany colour
text_left_padding, margin_toptextLeftPadding, marginTop4, 0
characters, fonts, backgroundsprofile keys of the same namethe global ones
attemptsnewLimited(n) or Unlimited. Math requires Limited(1..=3).Limited(1)
retentionnew≥ 1 s24 h, for challenges without a deadline
formatnewJpeg or PngJpeg

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.