# Captcha

The server's half of a challenge: the token a page's Cloudflare Turnstile
(or hCaptcha) widget gave, checked with the provider before a form is
taken. An [extension](../extensions/README.md): `extensions/captcha/`.
Back to the [map](README.md).

```mote
use "pkg:captcha/captcha.mote"

server fn signUp(email: string, token: string): string requires anyone limit 10 per hour by ip
  captchaRequire(token, "")?
  return "welcome"
```

```sh
CAPTCHA_SECRET=0x4AAAAAAA… CAPTCHA_HOSTNAME=shop.example bun dist/server/main.js
```

The widget is the provider's script in the page with its public site key,
and gives a token, sent with the form; only the check here makes it mean
anything.

| Setting | |
|---|---|
| `CAPTCHA_PROVIDER` | `turnstile` (unsaid) or `hcaptcha`. |
| `CAPTCHA_SECRET` | The secret key (a [secret](back-end.md#secrets)). |
| `CAPTCHA_HOSTNAME` | The site's host: a token solved on another site is refused (`hostname-mismatch`). |
| `CAPTCHA_API` | A stand-in. |

- `captchaCheck(token, ip)`: `{ok, errors, hostname, action}`; the
  provider's reasons in `errors` (`timeout-or-duplicate`: a token is good
  once). Turnstile is given an idempotency key, so a check sent again is the
  same check.
- `captchaRequire(token, ip)`: fails `Please show you are not a robot.`
  (kind `"captcha"`) unless it is good.
- A provider that cannot be reached fails the check (`"offline"`): a form
  is not taken on no word at all.

Not met Turnstile or hCaptcha: run against a stand-in (`tests/all.sh
real_svc_more`). To check: Cloudflare's test keys (a site key that always
passes, `1x00000000000000000000AA`, and its secret
`1x0000000000000000000000000000000AA`) give `ok: true`; the secret that
always fails gives `ok: false`.
