Updated javascript example usage in README.md for 2.0.0 changes.

Signed-off-by: jacobp <jacobpozaic@gmail.com>
This commit is contained in:
JacobPozaic 2022-04-05 10:30:31 -04:00 committed by jacobp
parent 41afb10cb5
commit a43b18587e

View File

@ -2,12 +2,12 @@
LibreCaptcha is a framework that allows developers to create their own [CAPTCHA](https://en.wikipedia.org/wiki/CAPTCHA)s. LibreCaptcha is a framework that allows developers to create their own [CAPTCHA](https://en.wikipedia.org/wiki/CAPTCHA)s.
The framework defines the API for a CAPTCHA generator and takes care of mundane details The framework defines the API for a CAPTCHA generator and takes care of mundane details
such as: such as:
* An HTTP interface for serving CAPTCHAs * An HTTP interface for serving CAPTCHAs
* Background workers to pre-compute CAPTCHAs and to store them in a database * Background workers to pre-compute CAPTCHAs and to store them in a database
* Managing secrets for the CAPTCHAs (tokens, expected answers, etc) * Managing secrets for the CAPTCHAs (tokens, expected answers, etc)
* Safe re-impressions of CAPTCHA images (by creating unique tokens for every impression) * Safe re-impressions of CAPTCHA images (by creating unique tokens for every impression)
* Garbage collection of stale CAPTCHAs * Garbage collection of stale CAPTCHAs
* Sandboxed plugin architecture (TBD) * Sandboxed plugin architecture (TBD)
Some sample CAPTCHA generators are included in the distribution (see below). We will continue adding more samples to the list. For quick Some sample CAPTCHA generators are included in the distribution (see below). We will continue adding more samples to the list. For quick
deployments the samples themselves might be sufficient. Projects with more resources might want create their own CAPTCHAs deployments the samples themselves might be sufficient. Projects with more resources might want create their own CAPTCHAs
@ -124,48 +124,48 @@ If a sufficient number of users agree on their answer to the unknown word, it is
The service can be accessed using a simple HTTP API. The service can be accessed using a simple HTTP API.
### - `/v1/captcha`: `POST` ### - `/v1/captcha`: `POST`
- Parameters: - Parameters:
- `level`: `String` - - `level`: `String` -
The difficulty level of a captcha The difficulty level of a captcha
- easy - easy
- medium - medium
- hard - hard
- `input_type`: `String` - - `input_type`: `String` -
The type of input option for a captcha The type of input option for a captcha
- text - text
- (More to come) - (More to come)
- `media`: `String` - - `media`: `String` -
The type of media of a captcha The type of media of a captcha
- image/png - image/png
- image/gif - image/gif
- (More to come) - (More to come)
- `size`: String - - `size`: String -
The dimensions of a captcha. It needs to be a string in the format `"widthxheight"` in pixels, and will be matched The dimensions of a captcha. It needs to be a string in the format `"widthxheight"` in pixels, and will be matched
with the `allowedSizes` config setting. Example: `size: "450x200"` which requests an image of width 450 and height with the `allowedSizes` config setting. Example: `size: "450x200"` which requests an image of width 450 and height
200 pixels. 200 pixels.
- Returns: - Returns:
- `id`: `String` - The uuid of the captcha generated - `id`: `String` - The uuid of the captcha generated
### - `/v1/media`: `GET` ### - `/v1/media`: `GET`
- Parameters: - Parameters:
- `id`: `String` - The uuid of the captcha - `id`: `String` - The uuid of the captcha
- Returns: - Returns:
- `image`: `Array[Byte]` - The requested media as bytes - `image`: `Array[Byte]` - The requested media as bytes
### - `/v1/answer`: `POST` ### - `/v1/answer`: `POST`
- Parameter: - Parameter:
- `id`: `String` - The uuid of the captcha that needs to be solved - `id`: `String` - The uuid of the captcha that needs to be solved
- `answer`: `String` - The answer to the captcha that needs to be validated - `answer`: `String` - The answer to the captcha that needs to be validated
- Returns: - Returns:
- `result`: `String` - The result after validation/checking of the answer - `result`: `String` - The result after validation/checking of the answer
- True - If the answer is correct - True - If the answer is correct
- False - If the answer is incorrect - False - If the answer is incorrect
- Expired - If the time limit to solve the captcha exceeds - Expired - If the time limit to solve the captcha exceeds
## Example usage ## Example usage
@ -173,9 +173,9 @@ The service can be accessed using a simple HTTP API.
In javascript: In javascript:
```js ```js
const resp = await fetch("/v1/captcha", { const resp = await fetch("/v2/captcha", {
method: 'POST', method: 'POST',
body: JSON.stringify({level: "easy", media: "image/png", "input_type" : "text"}) body: JSON.stringify({level: "easy", media: "image/png", "input_type" : "text", size: "350x100"})
}) })
const respJson = await resp.json(); const respJson = await resp.json();
@ -183,19 +183,19 @@ const respJson = await resp.json();
let captchaId = null; let captchaId = null;
if (resp.ok) { if (resp.ok) {
// The CAPTCHA can be displayed using the data in respJson. // The CAPTCHA can be displayed using the data in respJson.
console.log(respJson); console.log(respJson);
// Store the id somewhere so that it can be used later for answer verification // Store the id somewhere so that it can be used later for answer verification
captchaId = respJson.id; captchaId = respJson.id;
} else { } else {
console.err(respJson); console.err(respJson);
} }
// When user submits an answer it can be sent to the server for verification thusly: // When user submits an answer it can be sent to the server for verification thusly:
const resp = await fetch("/v1/answer", { const resp = await fetch("/v2/answer", {
method: 'POST', method: 'POST',
body: JSON.stringify({id: captchaId, answer: "user input"}) body: JSON.stringify({id: captchaId, answer: "user input"})
}); });
const respJson = await resp.json(); const respJson = await resp.json();
console.log(respJson.result); console.log(respJson.result);