Keyed challenges

laravel-rust-captcha Alpha Laravel for Rust

In session mode the server remembers the visitor's current image. In keyed mode the client carries the challenge's id instead, in a hidden field. Use it for Inertia pages, JSON APIs, mobile apps, pages with several captchas, or routes without a session.

The JSON route

GET /captcha/api/{profile?} returns a new challenge:

{ "sensitive": false, "key": "q2Zt0f…", "img": "data:image/jpeg;base64,…" }

The shape is PHP's. key is an opaque random id; in PHP it was the bcrypt hash of the answer, which a client could check guesses against offline. The route runs without session middleware.

Post the answer with the key:

{ "captcha": "x7k2p", "captcha_key": "q2Zt0f…" }
#[laravel::input]
#[validate(anyone)]
pub struct Login {
    #[validate(required, Captcha::keyed().profile("math"))]
    captcha: String,
    captcha_key: String,
}

Issuing from a handler

use laravel::http::Request;
use laravel::view::{View, data, view};

pub async fn create(request: Request) -> laravel::Result<View> {
    let challenge = laravel_rust_captcha::issue(&request, "contact").await?;
    Ok(view("contact", data! { challenge }))
}
{{ captcha_field(challenge) }}
<input type="text" name="captcha" autocomplete="off">

captcha_field renders the image and the hidden captcha_key input. For Inertia, pass the challenge as a prop; it serializes as {sensitive, key, img}.

Challenges issued from a handler count against the same per-client image limit as the routes.