Welcome to the Captcha API, a simple tool for generating and validating captchas. This API is written in Rust and powered by the Rocket framework, utilizing the captcha crate for captcha generation.
To set up the Captcha API, ensure you have Rust installed. Then, follow these steps:
- Clone this repository.
- Run
cargo build --releaseto build the API executable, which will be located intarget/release/captcha. - Start the Captcha API by running the executable.
Additionally, there is an example app written in PHP that demonstrates the usage of the Captcha API. To run this example app:
- Make sure you have PHP installed.
- Run
php -S localhost:8001to start a web server on port 8001. - Follow the previous steps to run the Captcha API using
cargo build --releaseandtarget/release/captcha. - Access the example app by opening
http://localhost:8001/in your web browser.
To create a captcha, make a GET request to /api/v1/new with the following query parameters:
All optional parameters have default values defined in the .env file.
auth: The authentication token.len: The captcha code's length (optional).width: The captcha image's width (optional).height: The captcha image's height (optional).filters: Filters to apply to the captcha image (optional). More information about filters can be found in the Filters section.
The response will be a JSON object like this:
{
"id": "8c6b00702ab445f29b25e8d09d0734c8c848a75d7dcd4ec19cae8c238836cf17",
"expire_time": "2023-10-28T11:21:10.037393300Z",
"url": "http://localhost:8000/api/v1/img/9ecdcd60-3148-49ae-8313-e820c8fcd713"
}To validate a captcha, send a POST request to /api/v1/verify with the following query parameters:
id: The captcha ID obtained from the/api/v1/newendpoint.auth: The authentication token.code: The captcha code.
The response will be a JSON object with a message indicating whether the captcha was valid or not ("ok"), raised a warning ("warn"), or encountered an error ("error").
Filters can be applied to modify the captcha image and make it more challenging for bots to solve. Available filters include:
dot: Adds dots to the image.grid: Adds a grid to the image.wave: Adds a wave to the image.noise: Adds noise to the image.
Filters are applied in the order they are specified in the filters query parameter, using the following syntax:
filter1:arg1,arg2,arg3;filter2:arg1,arg2;filter3:arg1,...Please note that the filters query parameter must be valid; otherwise, the Captcha API will return an error message. Here are some valid examples of the filters query parameter:
dot:10grid:10,10wave:10,10,hnoise:0.1dot:10;grid:10,10;wave:10,10,h;noise:0.1
You can configure the Captcha API using the .env file, where you can set the following options:
# Authentication token
AUTH_TOKEN=secret
# Base URL of the Captcha API
BASE_URL=http://localhost:8000
# Captcha settings
CAPTCHA_EXPIRE_TIME=300 # 5 minutes in seconds
CAPTCHA_LENGTH=6
CAPTCHA_WIDTH=200
CAPTCHA_HEIGHT=100This project is licensed under the MIT license. For more details, please read the LICENSE file.