Skip to content

Commit f9ced7d

Browse files
authored
docs: add Cap CAPTCHA guide (#1571)
* docs: add Cap CAPTCHA guide * docs: update Cap server prerequisite link
1 parent f5f77d8 commit f9ced7d

3 files changed

Lines changed: 124 additions & 0 deletions

File tree

‎docs/customization/bring-your-ui/README.mdx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ Send the returned provider token as `captchaToken` in the `PUT /api/experience`
118118
- [Cloudflare Turnstile with Bring your UI](/security/captcha/turnstile#bring-your-ui)
119119
- [GeeTest with Bring your UI](/security/captcha/geetest#bring-your-ui)
120120
- [Friendly Captcha with Bring your UI](/security/captcha/friendly-captcha#bring-your-ui)
121+
- [Cap with Bring your UI](/security/captcha/cap#bring-your-ui)
121122

122123
### Sample projects \{#sample-projects}
123124

‎docs/security/captcha/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ Currently, we support:
4343
- **Cloudflare Turnstile**
4444
- **[GeeTest](/security/captcha/geetest)**
4545
- **[Friendly Captcha](/security/captcha/friendly-captcha)**
46+
- **[Cap](/security/captcha/cap)**

‎docs/security/captcha/cap.md‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
---
2+
slug: /security/captcha/cap
3+
sidebar_label: Cap
4+
---
5+
6+
# Cap
7+
8+
[Cap](https://capjs.js.org) is an open-source, self-hosted CAPTCHA alternative based on proof-of-work without visual puzzles or third-party service dependencies, making it well suited for regions where external CAPTCHA services are unreachable. This guide walks you through setting up Cap with Logto.
9+
10+
## Prerequisites {#prerequisites}
11+
12+
- A publicly reachable [Cap Standalone](https://capjs.js.org/guide/capd/index.html) instance over HTTPS, since both the browser sign-in page and Logto need access to it.
13+
14+
## Create a site key {#create-a-site-key}
15+
16+
1. Open the dashboard of your Cap Standalone instance and log in with your admin key.
17+
2. Create a new site key. If you restrict CORS origins, add Logto's endpoint domain, such as `https://[tenant-id].logto.app`.
18+
3. Copy the **Site key** and **Secret key**. The secret key is only shown once.
19+
20+
## Configure Logto {#configure-logto}
21+
22+
In **Console > Security > CAPTCHA**, choose **Cap** and fill in the following details:
23+
24+
- **Cap endpoint**: The base URL of your Cap Standalone instance, e.g., `https://cap.example.com`
25+
- **Site key**: The site key you copied above
26+
- **Secret key**: The secret key you copied above
27+
28+
Alternatively, you can configure Cap through the Management API with `PUT /api/captcha-provider`:
29+
30+
```json
31+
{
32+
"config": {
33+
"type": "Cap",
34+
"endpoint": "https://cap.example.com",
35+
"siteKey": "<siteKey>",
36+
"secretKey": "<secretKey>"
37+
}
38+
}
39+
```
40+
41+
:::note
42+
Use HTTPS for your Cap endpoint in production. Browsers block mixed HTTP requests from an HTTPS sign-in page, and HTTP sends the secret key unencrypted.
43+
:::
44+
45+
## Security note {#security-note}
46+
47+
Cap's instrumentation challenge evaluates scripts in the browser to detect bots. While Cap is enabled, Logto's built-in sign-in page allows the Cap origin, blob workers, WebAssembly (`'wasm-unsafe-eval'`), and dynamic JavaScript evaluation (`'unsafe-eval'`) in its Content Security Policy.
48+
49+
## Bring your UI {#bring-your-ui}
50+
51+
If you use [Bring your UI](/customization/bring-your-ui/), Logto can't inject or run Cap in your custom frontend automatically. After enabling CAPTCHA in Logto Console, your custom UI must load `@cap.js/widget`, render the widget, and send the solved token to the Experience API.
52+
53+
Load `@cap.js/widget` in your custom UI (or install and bundle it in your frontend build):
54+
55+
```html
56+
<script src="https://cdn.jsdelivr.net/npm/@cap.js/widget" async></script>
57+
```
58+
59+
:::note
60+
If you load `@cap.js/widget` from a CDN instead of bundling it, add the CDN origin (such as `https://cdn.jsdelivr.net`) to `scriptSrc` and `connectSrc` in your [custom CSP sources](/customization/bring-your-ui/#configure-custom-csp-sources) so the widget script and its WebAssembly solver can load.
61+
:::
62+
63+
Add the `<cap-widget>` element pointing to your Cap Standalone endpoint and site key:
64+
65+
```html
66+
<cap-widget data-cap-api-endpoint="https://cap.example.com/<siteKey>/"></cap-widget>
67+
```
68+
69+
Listen for the `solve` event (`event.detail.token`), send the single-use token as `captchaToken` in `PUT /api/experience`, and reset the widget with `widget.reset()` after consuming it:
70+
71+
```js
72+
await customElements.whenDefined('cap-widget');
73+
const widget = document.querySelector('cap-widget');
74+
75+
const captchaToken =
76+
widget.token ??
77+
(await new Promise((resolve, reject) => {
78+
const cleanup = () => {
79+
widget.removeEventListener('solve', onSolve);
80+
widget.removeEventListener('error', onError);
81+
};
82+
83+
const onSolve = (event) => {
84+
cleanup();
85+
resolve(event.detail.token);
86+
};
87+
88+
const onError = (event) => {
89+
cleanup();
90+
reject(new Error(event.detail?.message ?? 'Cap verification failed'));
91+
};
92+
93+
widget.addEventListener('solve', onSolve);
94+
widget.addEventListener('error', onError);
95+
widget.solve().catch((error) => {
96+
cleanup();
97+
reject(error);
98+
});
99+
}));
100+
101+
try {
102+
await fetch('/api/experience', {
103+
method: 'PUT',
104+
headers: {
105+
'Content-Type': 'application/json',
106+
},
107+
body: JSON.stringify({
108+
interactionEvent: 'SignIn',
109+
captchaToken,
110+
}),
111+
});
112+
} finally {
113+
// Cap tokens are single-use; reset the widget after consuming the token.
114+
widget.reset();
115+
}
116+
```
117+
118+
## Enable CAPTCHA {#enable-captcha}
119+
120+
Remember to enable CAPTCHA bot protection after you have set up the CAPTCHA provider.
121+
122+
Go to the Security page, find the CAPTCHA tab, and switch on the toggle button of "Enable CAPTCHA".

0 commit comments

Comments
 (0)