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§
- Asset
Set - Decoded fonts and backgrounds.
- Captcha
- The captcha validation rule.
- Captcha
Config - The
captchaconfiguration section. - Captcha
Manager - The captcha service. Resolve it with
Inject<CaptchaManager>orlaravel::resolve. - Captcha
Service Provider - Registers the captcha: its config section, the
CaptchaManagerservice, both routes (unlessCAPTCHA_DISABLE), the MiniJinja functions (featureminijinja), 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).
- Config
Error - The captcha configuration is invalid.
- Default
Renderer - The built-in renderer.
- Image
- An encoded image.
- Issued
Image - 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.
- Render
Input - 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.
- Image
Format - 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 preboundImageManagerdoes in PHP.
Functions§
- api
GET /captcha/api/{profile?}: issues a keyed challenge,{sensitive, key, img}as in PHP, wherekeyis 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, withCache-Control: no-store(PHP:no-cache, private). An unknown profile is a 404 (PHP served its class defaults). AHEADrequest 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:
Nonewhen the raw input is overMAX_INPUT_BYTESor 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, unlesssensitive, 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).