# Keyed challenges (laravel-rust-captcha)

> 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…

- Package: laravel-rust-captcha (Laravel for Rust, alpha), crates: `laravel-rust-captcha`
- Maintenance: active; Latest release: 0.1.0 (2026-12-04); Framework: laravel-rust 0.2.x; MSRV: Rust 1.99 or later; Licence: MIT
- Links: [Repository](https://github.com/bowerbird-si/laravel-rust-captcha), [crates.io](https://crates.io/crates/laravel-rust-captcha/0.1.0), [docs.rs](https://docs.rs/laravel-rust-captcha/0.1.0), [Changelog](https://oss.bowerbird.si/captcha/docs/changelog/), [Contributing](https://github.com/bowerbird-si/laravel-rust-captcha/blob/57c677c77d6c928790e0acaf24e0b3f0ff06f29c/CONTRIBUTING.md), [Security policy](https://github.com/bowerbird-si/laravel-rust-captcha/blob/57c677c77d6c928790e0acaf24e0b3f0ff06f29c/SECURITY.md)
- Based on: [mews/captcha](https://github.com/mewebstudio/captcha): The captcha package this ports: profiles, config keys, routes and validation, following version 3.5.1. Its backgrounds and fonts ship with their own licences.
- PHP parity: full (54 covered, 33 diverged, 0 planned of 87 abilities; 44 fixtures; follows mews/captcha 3.5.1; verified 2026-12-04)
- Community package, not affiliated with Laravel.
- HTML version: https://oss.bowerbird.si/captcha/docs/keyed/
- Source: https://github.com/bowerbird-si/laravel-rust-captcha/blob/57c677c77d6c928790e0acaf24e0b3f0ff06f29c/docs/keyed.md

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:

```json
{ "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:

```json
{ "captcha": "x7k2p", "captcha_key": "q2Zt0f…" }
```

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

## Issuing from a handler

```rust
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 }))
}
```

```html
{{ 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.
