Captcha
<x-wirekit:: asks the reader to confirm they are a person before a form is sent. It
renders Cloudflare Turnstile or ALTCHA, and hands the token of the check to a property of your
Livewire component through wire:model. A token that expires, a check that fails and a check that
times out empty that property again, so a stale token never reaches your server.
The preview uses Cloudflare's test key, which passes every check. Turnstile's script comes from
Cloudflare, and the preview waits until you use its form: a click in it or a key typed in its field
loads the check, and with it reaches Cloudflare with your IP address. Moving past it with Tab
loads nothing.
Usage
<form wire:submit="subscribe">
<x-wirekit::input label="Email" wire:model="email" />
<x-wirekit::captcha
:site-key="config('services.turnstile.key')"
wire:model="captchaToken"
label="Confirm you are a person"
/>
<x-wirekit::button type="submit">Subscribe</x-wirekit::button>
</form>
The provider's widget stands under wire:ignore, so a render of your component leaves it as the
reader left it. The token travels in a hidden field of the component's own, which carries your
wire:model: with wire:model.live it reaches the server as soon as it comes, and
wire:model. and wire:model.blur hear it as well. A form without Livewire posts the field
under name, or under the bound property's name when you give none.
The component checks nothing itself. Verify every token on your server before you act on the form.
Waiting Until the Form Is Used
A captcha loads its provider's script as soon as the page starts it, so a page that is only read
already reaches Cloudflare, or the address ALTCHA loads from. defer keeps the provider away until
the reader uses the form around the captcha: the first click in that form, entry in one of its
fields or key pressed there loads the script and draws the check. Moving through the page is not
using the form, so Tab, Shift+Tab, Escape and a key pressed with Ctrl, Alt or Cmd,
such as find in page, load nothing, while a paste is an entry and counts. A focus alone never
counts, so a field that has the focus from autofocus waits for the reader to type in it. Only
what the reader does counts, never an event your own scripts dispatch. Outside a form, a captcha
with defer waits for a click on its own label, hint or widget area.
<form wire:submit="subscribe">
<x-wirekit::input label="Email" wire:model="email" />
<x-wirekit::captcha
:site-key="config('services.turnstile.key')"
wire:model="captchaToken"
label="Confirm you are a person"
defer
/>
<x-wirekit::button type="submit">Subscribe</x-wirekit::button>
</form>
The check takes a moment once it has started. A reader whose browser fills in the form, and who presses the button first, starts the check with that press and sends the form without a token, so your validation asks them to confirm and the check is ready when they do.
components. in the configuration makes every captcha wait, and a captcha's own
:defer="false" loads with the page again. A captcha that waits carries data-wk-captcha-defer
beside data-wk-captcha-provider, for a script of the page that tells the two apart.
Turnstile
Create a widget in the Cloudflare dashboard and keep its site key and its secret key in your
configuration, for example as services. and services. in
config/, read from TURNSTILE_ and TURNSTILE_.
Verify the token with Cloudflare's siteverify endpoint before the form's work: post your secret
key, the token and the reader's IP address to challenges.
over HTTPS, and go on only when the answer's success is true. The example keeps that address
beside the keys, as services.. A token can be verified only once, so start a
new check afterwards, whatever the answer:
use Illuminate\Support\Facades\Http;
use Livewire\Component;
class Subscribe extends Component
{
public string $email = '';
public string $captchaToken = '';
public function subscribe(): void
{
$passed = Http::asForm()
->post(config('services.turnstile.verify_url'), [
'secret' => config('services.turnstile.secret'),
'response' => $this->captchaToken,
'remoteip' => request()->ip(),
])
->json('success') === true;
$this->captchaToken = '';
$this->dispatch('wirekit:captcha-reset', name: 'captchaToken');
if (! $passed) {
$this->addError('captchaToken', 'Please confirm you are a person.');
return;
}
// Subscribe $this->email here.
}
}
The error appears under the captcha, as under any field: it is filed under the property the captcha binds, and the group says it is invalid.
For local development and tests, Cloudflare's site key 1x00000000000000000000AA with the secret
key 1x0000000000000000000000000000000AA passes every check.
size sets the widget's width: normal, flexible for the width of its column, or compact.
The widget follows the page's light or dark mode. Turnstile takes the mode when it draws, so a
switch draws the widget again while it holds no token; one that already gave its token keeps its
look until the next check.
The script loads from Cloudflare's own address,
challenges., the first time a captcha needs it.
Cloudflare asks that this file be loaded from that address, never copied or cached.
ALTCHA
ALTCHA has the reader's browser solve a small proof of work against a challenge your application hands out and verifies itself, so the check needs no other service. Install its library for PHP:
composer require altcha-org/altcha
Hand out a challenge at an address of your application, signed with a secret of your own, kept as
services.:
use AltchaOrg\Altcha\Algorithm\Pbkdf2;
use AltchaOrg\Altcha\Altcha;
use AltchaOrg\Altcha\CreateChallengeOptions;
use Illuminate\Support\Facades\Route;
Route::get('/altcha/challenge', function () {
$altcha = new Altcha(hmacSignatureSecret: config('services.altcha.secret'));
return response()->json($altcha->createChallenge(new CreateChallengeOptions(
algorithm: new Pbkdf2(),
cost: 5000,
expiresAt: time() + 300,
)));
});
Name that address on the captcha:
<x-wirekit::captcha
provider="altcha"
challenge="/altcha/challenge"
wire:model="captchaToken"
label="Confirm you are a person"
/>
and verify the payload the check gives, which ALTCHA reports as malformed with an exception:
use AltchaOrg\Altcha\Algorithm\Pbkdf2;
use AltchaOrg\Altcha\Altcha;
use AltchaOrg\Altcha\VerifySolutionOptions;
use Livewire\Component;
class Subscribe extends Component
{
public string $captchaToken = '';
public function subscribe(): void
{
$altcha = new Altcha(hmacSignatureSecret: config('services.altcha.secret'));
try {
$passed = $altcha->verifySolution(new VerifySolutionOptions(
algorithm: new Pbkdf2(),
payload: $this->captchaToken,
))->verified;
} catch (\InvalidArgumentException) {
$passed = false;
}
$this->captchaToken = '';
$this->dispatch('wirekit:captcha-reset', name: 'captchaToken');
if (! $passed) {
$this->addError('captchaToken', 'Please confirm you are a person.');
return;
}
// Act on the form here.
}
}
The widget loads ALTCHA 3.5.0 from https://unpkg., a
fixed version. To serve a copy of your own, name its address in the configuration below. It takes
the page's colors, edges and accent from the theme.
Starting a New Check
wirekit:captcha-reset on window starts a new check and empties the field: for the captcha whose
field its name names, or for every captcha of the page without a name. From Livewire it is
$this->dispatch('wirekit:captcha-reset', name: 'captchaToken'), from a script of your own:
window.dispatchEvent(new CustomEvent('wirekit:captcha-reset', { detail: { name: 'captchaToken' } }));
Content Security Policy
| Provider | What the policy allows |
|---|---|
| Turnstile | challenges.cloudflare.com in script-src and frame-src, or the nonce of your scripts: the captcha hands Turnstile's script the nonce of the page's scripts, and Turnstile carries it on to what it loads |
| ALTCHA | the script's origin in script-src (https://unpkg.com for the default address), blob: in worker-src for its workers, and 'unsafe-inline' in style-src for the styles it writes |
Configuration
// config/wirekit.php
'components' => [
'captcha' => [
'provider' => 'turnstile',
'defer' => false,
'altcha' => ['script' => 'https://unpkg.com/[email protected]/dist/main/altcha.min.js'],
],
],
provider is the check a captcha without the prop renders. defer makes every captcha wait until
its form is used. altcha.script is the address ALTCHA
loads from. turnstile. holds Cloudflare's address of Turnstile's script, which Cloudflare
asks every page to load from.
A browser test that runs without a network, or should not reach Cloudflare, points
turnstile. at a stand-in of its own. The stand-in sets window. with the
render(), reset() and remove() of the real script, and its render() hands the callback it
is given a token:
// config/wirekit.php, in the configuration your browser tests read
'components' => [
'captcha' => [
'turnstile' => ['script' => '/js/turnstile-stand-in.js'],
],
],
Props
| Prop | Type | Default | Description |
|---|---|---|---|
provider |
turnstile|altcha |
turnstile |
The check, from the configuration's provider when you give none. |
site-key |
string |
Turnstile: the widget's site key. | |
challenge |
string |
ALTCHA: the address of a challenge, or the challenge as JSON. | |
name |
string|null |
the bound property | The field's name: the key of its error and what a plain form post sends. |
label |
string|null |
null |
Names the check. |
hint |
string|null |
null |
Describes it. An error takes its place. |
help |
string|null |
null |
Explained in a tooltip from a question mark beside the label, and read as a description of the check. |
error |
string|null |
null |
An error of your own; without one, the validation bag's under name. false shows none. |
announce-error |
bool|null |
null |
Whether the error stands in a polite live region. Without the prop, a form around the captcha decides, then wirekit.a11y.announce_error. |
required |
bool |
false |
Marks the label. |
size |
normal|flexible|compact |
normal |
Turnstile: the widget's width. |
language |
string|null |
null |
The widget's language, such as de; without one, the provider follows the browser. |
action |
string|null |
null |
Turnstile: a name for the check in Cloudflare's analytics. |
defer |
bool|null |
null |
Loads nothing from the provider until the reader uses the form around the captcha. Without the prop, the configuration's defer. See Waiting Until the Form Is Used. |
scope |
string|null |
null |
Scoped personalization name |
Events
| Event | On | Detail | When |
|---|---|---|---|
wirekit:captcha-reset |
window |
{ name?: string } |
You send it to start a new check and empty the field. |
Accessibility
- With a
label, the captcha is a group named by it and described by its hint or its error. Arole, anaria-labelor anaria-labelledbyof yours replaces the group's own, and anaria-describedbyof yours joins its description. - With an error the group says it is invalid.
- The widget names itself: Turnstile's frame and ALTCHA's checkbox carry labels of their own.
- An error appears in a polite live region, so a message after a Livewire round trip is announced.
Keyboard Interaction
The captcha adds no keys of its own. The widget is the provider's: Turnstile's checkbox and ALTCHA's checkbox take the focus with Tab and are ticked with Space, as a checkbox is.