TLDR: ALTCHA Server Integration
This guide offers a concise overview of integrating ALTCHA on your server, covering both modes: with and without the spam filter. By following these steps, you’ll ensure secure and efficient verification of user submissions, whether you need basic challenge verification or advanced spam filtering.
For easy integration, consider using official libraries:
Setup Overview
-
Retrieve Challenge:
- Client-side fetches a challenge using the ALTCHA widget.
-
Solve Challenge:
- Client-side solves the challenge.
-
Submit Solution:
- Solution and user data are submitted to the server.
-
Verify on Server:
- Server validates the solution and, if using the spam filter, validates the spam classification.
Without Spam Filter
Full docs: Server Integration
Creating a Challenge (Server-side)
- Generate Parameters:
maxnumber
: Maximum random number (adjusts difficulty).salt
: Random string (≥10 characters).secret_number
: Random integer (0…maxnumber).challenge
: SHA-256 hash ofsalt + secret_number
.signature
: HMAC-SHA-256 ofchallenge
with a secret key.
Validating a Solution (Server-side)
-
Decode Payload:
- Extract
algorithm
,challenge
,number
,salt
,signature
from Base64-JSON payload.
- Extract
-
Validation Steps:
alg_ok
: Check algorithm is ‘SHA-256’.challenge_ok
: Verifychallenge
matchessha2_hex(concat(salt, number))
.signature_ok
: Verifysignature
matcheshmac_sha2_hex(challenge, hmac_key)
.
With Spam Filter
Full docs: Server Integration
Using the API with the Widget
- Widget Configuration:
- Set
challengeurl
with your API key. - Enable
spamfilter
for spam classification.
- Set
Server Verification
- Payload Format:
- You receive a base64-encoded JSON object with
algorithm
,signature
,verificationData
, andverified
properties.
- You receive a base64-encoded JSON object with
Verifying the Signature
- Calculate the SHA hash of
verificationData
. - Compute the HMAC signature using the secret part of your API key.
Example pseudo-code:
- Verification:
- The payload is verified if the computed signature matches the
signature
property.
- The payload is verified if the computed signature matches the
Verifying Fields (Spam Filter)
- Check
fields
andfieldsHash
:- Concatenate field values with
\n
and compute the SHA hash.
- Concatenate field values with
Example pseudo-code:
- Validation:
- Data is correct if the computed hash matches the
fieldsHash
.
- Data is correct if the computed hash matches the
Example Code
- Node.js Example:
Security Recommendations
-
Prevent Replay Attacks:
- Invalidate previously solved challenges.
-
Challenge Expiration:
- Include a timestamp in the
salt
to limit the validity period. - Use
salt = '<random_salt>?expires=<unix_ts>'
.
- Include a timestamp in the
API Endpoints
-
Creating a New Challenge:
GET /api/v1/challenge
-
Verifying Solution:
POST /api/v1/challenge/verify
-
Validating Server Signature:
POST /api/v1/challenge/verify_server_signature