added alphabet setting

This commit is contained in:
leca 2025-05-11 14:15:32 +03:00
parent ceb8642365
commit 044f4094f0
3 changed files with 13 additions and 2 deletions

View File

@ -10,4 +10,6 @@ APP_PORT=3000
CAPTCHA_SOURCE_URL=https://example.com
DATA_DIR=/opt/captcha_aggregator
ADMIN_TOKEN=GENERATE_A_STRONG_TOKEN_HERE
SECRET=GENERATE_A_STRONG_SECRET_HERE
SECRET=GENERATE_A_STRONG_SECRET_HERE
#Allowed symbols. Must be a regex
ALPHABET="[123456789ABCDEFGHIJKLMNPQRSTUVWXZY]"

View File

@ -16,7 +16,8 @@ const config = {
captcha_source_url: process.env.CAPTCHA_SOURCE_URL,
data_dir: process.env.DATA_DIR,
admin_token: process.env.ADMIN_TOKEN,
secret: process.env.SECRET
secret: process.env.SECRET,
alphabet: process.env.ALPHABET
}
export default config;

View File

@ -1,11 +1,19 @@
import CaptchaService from "../services/captcha.js";
import jwt from 'jsonwebtoken';
import config from '../config.js';
class CaptchaController {
async submit(req, res) {
const {image, solution} = req.body;
if (!image) return res.status(400).send({"message":"You must send image blob"});
if (!solution || solution.length != 6) return res.status(400).send({"message":"You must send a valid solution"});
for (let i = 0; i < solution.length; i ++) {
let char = solution[i];
if (!char.match(config.alphabet)) {
console.log("Illegal symbol: " + char);
return res.status(400).send({"message": `Illegal symbol ${char} at position ${i + 1}`});
}
}
try {
await CaptchaService.new(image, solution, jwt.decode(req.token).id);
} catch (e) {