Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Added some CAPTCHA documentation

Blue Mouse7e3ba32b1d3f
diff --git a/docs/captcha/client-side-usage.md b/docs/captcha/client-side-usage.md
new file mode 100644
index 000000000..472cf28cb
--- /dev/null
+++ b/docs/captcha/client-side-usage.md
@@ -0,0 +1,48 @@
+---
+title: Client-side usage
+slug: /captcha/client-side-usage
+---
+
+## Introduction
+
+You can use the CAPTCHA widget on any page of your website by using our CAPTCHA loader widget. Swetrix CAPTCHA relies on JavaScript to work, so you have to include the widget on your page.
+
+## Installation
+
+### Using CDN
+The easiest way to add the CAPTCHA script to your website is to edit the HTML code of your website. To do this, paste the following code snippet into the header section (`<head>` tag) of your website:
+
+```html
+<script src="https://swetrix.org/captcha.js" defer></script>
+```
+
+### Using NPM
+__Not yet supported__
+
+## Usage
+Once the CAPTCHA script is installed (or embedded) on your website, you can use the CAPTCHA widget by adding the following HTML code to your page:
+
+```html
+<div class="swecaptcha" data-project-id="YOUR_PROJECT_ID"></div>
+```
+
+Inside the `.swecaptcha` element, the widget will automatically render a frame with the CAPTCHA challenge and an input field where the token will be stored. The CAPTCHA is designed to be embedded to HTML forms, but you can use it anywhere on your website.
+
+### Customisation
+You can customise the appearance of the CAPTCHA widget by adding the following attributes to the `.swecaptcha` element:
+
+| Attribute | Type | Description |
+| --- | --- | --- |
+| `data-project-id` | string | The ID of your CAPTCHA project (REQUIRED). |
+| `data-theme` | string | The theme of the CAPTCHA widget. Possible values: `light` (default), `dark`. |
+| `data-response-input-name` | string | Name of the token input element. Default: `swetrix-captcha-response`. |
+
+### Widget sizes
+The CAPTCHA widget has several sizes based on the state of the CAPTCHA challenge and the widget will automatically adjust its size.
+
+Here's a table with the possible sizes of the widget that can help you when designing your website:
+
+| State | Width | Height |
+| --- | --- | --- |
+| Default | 300px | 66px |
+| Manual challenge | 300px | 200px |
diff --git a/docs/captcha/introduction.md b/docs/captcha/introduction.md
new file mode 100644
index 000000000..97aeea735
--- /dev/null
+++ b/docs/captcha/introduction.md
@@ -0,0 +1,6 @@
+---
+title: Introduction
+slug: /captcha/introduction
+---
+
+TODO
diff --git a/docs/captcha/server-side-validation.md b/docs/captcha/server-side-validation.md
new file mode 100644
index 000000000..ead2d0553
--- /dev/null
+++ b/docs/captcha/server-side-validation.md
@@ -0,0 +1,98 @@
+---
+title: Server-side validation
+slug: /captcha/server-side-validation
+---
+
+## Introduction
+
+You have to validate the user's response to the CAPTCHA on your server. The token should be considered valid only if you validated it on your backend, otherwise it can be easily spoofed (the presence of the token in the request is not enough to consider it valid as an attacker can easily send a request with any string as a token).
+
+The validation endpoint accepts the CAPTHCA token and your project-specific secret key and returns a JSON response with the validation result.
+
+:::info
+Token validation can only be performed once, if you try to validate the same token twice, the second validation will fail. This is done to prevent token reuse attacks.
+:::
+
+## Validation via HTTP request
+
+To validate the token, you have to send a POST request to the `https://api.swetrix.com/v1/captcha/validate` URL, with the following parameters:
+
+| Parameter | Type | Description |
+| --- | --- | --- |
+| token | string | The token returned by the CAPTCHA widget. |
+| secret | string | Your CAPTCHA project secret key. |
+
+<br />
+
+The successfull response will have the `success` field set to `true` and the `data` field will contain the following fields:
+
+| Field | Type | Description |
+| --- | --- | --- |
+| hash | string | The hash of CAPTCHA user passed. |
+| timestamp | number | The timestamp of CAPTCHA user passed. |
+| autoVerified | boolean | Whether the CAPTCHA was auto-verified (i.e. user was NOT presented a CAPTCHA challenge). |
+| pid | string | The ID of the CAPTCHA project. |
+
+```json
+{
+ success: true,
+ data: {
+ hash, timestamp, autoVerified, pid,
+ }
+}
+```
+
+<br />
+
+The failed response will throw an error that will look like the following example. The HTTP status code will correspond to the `statusCode` in JSON.
+```json
+{
+ "statusCode": 400,
+ "message": "Could not decrypt token",
+ "error": "Bad Request"
+}
+```
+
+<br />
+
+Here's an example of how to validate the token using `curl`:
+
+```bash
+curl -X POST\
+ -H "Content-Type: application/json"\
+ -d '{"token": "<token>", "secret": "<secret>"}'\
+ https://api.swetrix.com/v1/captcha/validate
+```
+
+## Validation via Node.js library
+
+You can use the [@swetrix/captcha-validator](https://www.npmjs.com/package/@swetrix/captcha-validator) package to validate the token in your Node.js project.
+
+You can use this package in your Node.js (or NestJS, ExpressJS, etc.) project to validate the user's response to the CAPTCHA.
+The package exports a single function `validateToken` which takes arguments listed below and returns a Promise.
+
+| Argument | Type | Description | Required |
+| --- | --- | --- | --- |
+| token | string | The token returned by the CAPTCHA widget. | YES |
+| secretKey | string | Your CAPTCHA project secret key. | YES |
+| apiURL | string | The API URL to use (default: https://api.swetrix.com/captcha), this one may be used in case you're selfhosting the API. | NO |
+
+<br />
+
+Here's an example of how to use the package in your project:
+```js
+const { validateToken } = require('@swetrix/captcha-validator')
+// or
+import { validateToken } from '@swetrix/captcha-validator'
+
+// somewhere inside the controller of your form where the CAPTCHA is used
+validateToken(
+ 'CAPTCHA_TOKEN',
+ 'YOUR_SECRET_KEY',
+).then((result) => {
+ const [status, data] = result
+ // status is a boolean, true if the token is valid, false otherwise
+ // data is an error message if status is false
+ console.log(status, data)
+})
+```
diff --git a/sidebars.js b/sidebars.js
index e015e1806..d1ad51ba4 100644
--- a/sidebars.js
+++ b/sidebars.js
@@ -19,5 +19,10 @@ module.exports = {
'API': [
'api/stats',
],
+ 'CAPTCHA': [
+ 'captcha/introduction',
+ 'captcha/client-side-usage',
+ 'captcha/server-side-validation',
+ ]
},
}