Skip to main content

Crate laravel_rust_captcha

Crate laravel_rust_captcha 

Source
Expand description

Self-hosted image captcha for Laravel for Rust, based on the PHP package mews/captcha (MIT). Community package, not affiliated with Laravel.

It covers what mews/captcha does (profiles, the image and JSON routes, session and keyed checks, math captchas, every drawing option, the helpers and the rules) and fixes its weak points: an opaque id per challenge, at most one pass per challenge even under concurrency, nothing secret on the client, an HMAC instead of bcrypt per image, escaped HTML, and limits on image generation.

use laravel::Application;
use laravel_rust_captcha::{CaptchaConfig, CaptchaServiceProvider, Profile};

let _builder = Application::configure()
    .with_providers(|providers| {
        providers.add(CaptchaServiceProvider::new());
    })
    .with_middleware(|middleware, config| {
        CaptchaServiceProvider::configure_input(middleware, config, &[]);
    })
    .with_config(|config, _| {
        config.section_mut::<CaptchaConfig>().profile("contact", Profile::default().length(5u32).size(180, 54));
    });

§Database cache stores

In production, database cache stores require CAPTCHA_TRUST_STORE=true, the database feature and a writer-only cache connection. SQLite needs its own file, even when the app uses a different connection name. See Laravel’s database cache replica reads. Enable database-sqlite to turn on these checks and Laravel’s SQLite driver together. Laravel 0.2 requires at least one driver when its database feature is enabled. See Laravel’s driverless database build.

Modules§

testing
Helpers for testing forms that use the captcha.

Structs§

AssetSet
Decoded fonts and backgrounds.
Captcha
The captcha validation rule.
CaptchaConfig
The captcha configuration section.
CaptchaManager
The captcha service. Resolve it with Inject<CaptchaManager> or laravel::resolve.
CaptchaServiceProvider
Registers the captcha: its config section, the CaptchaManager service, both routes (unless CAPTCHA_DISABLE), the MiniJinja functions (feature minijinja), and the boot checks.
Challenge
A keyed challenge, ready to send to a client: the JSON route’s body, an Inertia prop, or the input of MiniJinja’s captcha_field.
Characters
The characters answers are drawn from. Each entry is one drawn unit (usually one character).
ConfigError
The captcha configuration is invalid.
DefaultRenderer
The built-in renderer.
Image
An encoded image.
IssuedImage
An issued image for the session route.
Profile
One profile: how a challenge looks and how it may be answered. PHP: one named array in config/captcha.php.
Profiles
The named profiles.
RenderInput
What to draw.

Enums§

Assets
Where fonts or backgrounds come from.
Attempts
How many answers a challenge accepts before it is spent.
Error
A captcha operation failed for a reason other than the answer.
ImageFormat
The image format.
Verdict
The result of checking an answer.

Constants§

MAX_INPUT_BYTES
Submitted answers longer than this many bytes are always wrong, before any other work.
VERIFIER_LABEL
The domain-separation label for the subkey.

Traits§

Renderer
Draws challenges. The default is DefaultRenderer; an app can register its own, as a prebound ImageManager does in PHP.

Functions§

api
GET /captcha/api/{profile?}: issues a keyed challenge, {sensitive, key, img} as in PHP, where key is an opaque id instead of the bcrypt hash. Runs without the session.
check
Checks an answer against the session’s current challenge for profile. PHP: captcha_check($value).
check_keyed
Checks an answer against a keyed challenge. PHP: captcha_api_check($value, $key, $config).
draw
Draws the image without encoding it.
encode
Encodes in the profile’s format; JPEG at the profile’s quality (PHP always uses 75).
image
GET /captcha/{profile?}: issues a session challenge and returns its image, inline; filename= "image.jpg" as in PHP, with Cache-Control: no-store (PHP: no-cache, private). An unknown profile is a 404 (PHP served its class defaults). A HEAD request issues nothing (PHP issued a challenge).
img
An escaped <img> tag for a profile’s image. PHP: captcha_img($config, $attrs).
issue
Issues a keyed challenge for a handler to pass to a view or an Inertia page. PHP: Captcha::create($config, true).
normalize
Normalizes an answer or a submitted input: None when the raw input is over MAX_INPUT_BYTES or when it contains U+FFFD (the framework’s replacement for malformed UTF-8). Otherwise it is trimmed of ASCII whitespace (space, tab, LF, FF, CR) and, unless sensitive, lowercased one character at a time with Unicode’s full lowercase mapping and no context.
src
The image URL for a profile. PHP: captcha_src($config).