Merge branch 'feat/captchaVerify' into slowcord

This commit is contained in:
Madeline 2022-07-20 15:33:27 +10:00
commit 8a569d884a
9 changed files with 43 additions and 25 deletions

View File

@ -5,14 +5,14 @@
"main": "dist/index.js", "main": "dist/index.js",
"types": "src/index.ts", "types": "src/index.ts",
"scripts": { "scripts": {
"test:only": "jest --coverage --verbose --forceExit ./tests", "test:only": "npx jest --coverage --verbose --forceExit ./tests",
"test:routes": "jest --coverage --verbose --forceExit ./routes.test.ts", "test:routes": "npx jest --coverage --verbose --forceExit ./routes.test.ts",
"test": "npm run build && npm run test:only", "test": "npm run build && npm run test:only",
"test:watch": "jest --watch", "test:watch": "npx jest --watch",
"start": "npm run build && node dist/start", "start": "npm run build && node dist/start",
"build": "npx tsc -p .", "build": "npx tsc -p .",
"dev": "tsnd --respawn src/start.ts", "dev": "npx tsnd --respawn src/start.ts",
"patch": "ts-patch install -s && npx patch-package", "patch": "npx ts-patch install -s && npx patch-package",
"postinstall": "npm run patch", "postinstall": "npm run patch",
"generate:docs": "node scripts/generate_openapi", "generate:docs": "node scripts/generate_openapi",
"generate:schema": "node scripts/generate_schema" "generate:schema": "node scripts/generate_schema"

View File

@ -1,5 +1,5 @@
import { Request, Response, Router } from "express"; import { Request, Response, Router } from "express";
import { route, getIpAdress, verifyHcaptcha } from "@fosscord/api"; import { route, getIpAdress, verifyCaptcha } from "@fosscord/api";
import bcrypt from "bcrypt"; import bcrypt from "bcrypt";
import { Config, User, generateToken, adjustEmail, FieldErrors } from "@fosscord/util"; import { Config, User, generateToken, adjustEmail, FieldErrors } from "@fosscord/util";
@ -23,7 +23,7 @@ router.post("/", route({ body: "LoginSchema" }), async (req: Request, res: Respo
const config = Config.get(); const config = Config.get();
if (config.login.requireCaptcha && config.security.captcha.enabled) { if (config.login.requireCaptcha && config.security.captcha.enabled) {
const { sitekey, service, secret } = config.security.captcha; const { sitekey, service } = config.security.captcha;
if (!captcha_key) { if (!captcha_key) {
return res.status(400).json({ return res.status(400).json({
captcha_key: ["captcha-required"], captcha_key: ["captcha-required"],
@ -33,7 +33,7 @@ router.post("/", route({ body: "LoginSchema" }), async (req: Request, res: Respo
} }
const ip = getIpAdress(req); const ip = getIpAdress(req);
const verify = await verifyHcaptcha(captcha_key, ip); const verify = await verifyCaptcha(captcha_key, ip);
if (!verify.success) { if (!verify.success) {
return res.status(400).json({ return res.status(400).json({
captcha_key: verify["error-codes"], captcha_key: verify["error-codes"],

View File

@ -1,6 +1,6 @@
import { Request, Response, Router } from "express"; import { Request, Response, Router } from "express";
import { Config, generateToken, Invite, FieldErrors, User, adjustEmail } from "@fosscord/util"; import { Config, generateToken, Invite, FieldErrors, User, adjustEmail } from "@fosscord/util";
import { route, getIpAdress, IPAnalysis, isProxy, verifyHcaptcha } from "@fosscord/api"; import { route, getIpAdress, IPAnalysis, isProxy, verifyCaptcha } from "@fosscord/api";
import "missing-native-js-functions"; import "missing-native-js-functions";
import bcrypt from "bcrypt"; import bcrypt from "bcrypt";
import { HTTPError } from "lambert-server"; import { HTTPError } from "lambert-server";
@ -67,16 +67,16 @@ router.post("/", route({ body: "RegisterSchema" }), async (req: Request, res: Re
} }
if (register.requireCaptcha && security.captcha.enabled) { if (register.requireCaptcha && security.captcha.enabled) {
const { sitekey, service, secret } = security.captcha; const { sitekey, service } = security.captcha;
if (!body.captcha_key) { if (!body.captcha_key) {
return res.status(400).json({ return res?.status(400).json({
captcha_key: ["captcha-required"], captcha_key: ["captcha-required"],
captcha_sitekey: sitekey, captcha_sitekey: sitekey,
captcha_service: service captcha_service: service
}); });
} }
const verify = await verifyHcaptcha(body.captcha_key, ip); const verify = await verifyCaptcha(body.captcha_key, ip);
if (!verify.success) { if (!verify.success) {
return res.status(400).json({ return res.status(400).json({
captcha_key: verify["error-codes"], captcha_key: verify["error-codes"],

View File

@ -11,18 +11,36 @@ export interface hcaptchaResponse {
score_reason: string[]; // enterprise only score_reason: string[]; // enterprise only
} }
export async function verifyHcaptcha(response: string, ip?: string) { export interface recaptchaResponse {
success: boolean;
score: number; // between 0 - 1
action: string;
challenge_ts: string;
hostname: string;
"error-codes"?: string[];
}
const verifyEndpoints = {
hcaptcha: "https://hcaptcha.com/siteverify",
recaptcha: "https://www.google.com/recaptcha/api/siteverify",
}
export async function verifyCaptcha(response: string, ip?: string) {
const { security } = Config.get(); const { security } = Config.get();
const { secret, sitekey } = security.captcha; const { service, secret, sitekey } = security.captcha;
const res = await fetch("https://hcaptcha.com/siteverify", { if (!service) throw new Error("Cannot verify captcha without service");
const res = await fetch(verifyEndpoints[service], {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/x-www-form-urlencoded", "Content-Type": "application/x-www-form-urlencoded",
}, },
body: `response=${response}&secret=${secret}&remoteip=${ip}&sitekey=${sitekey}`, body: `response=${encodeURIComponent(response)}`
+ `&secret=${encodeURIComponent(secret!)}`
+ `&sitekey=${encodeURIComponent(sitekey!)}`
+ ip ? `&remoteip=${encodeURIComponent(ip!)}` : "",
}) })
const json = await res.json() as hcaptchaResponse; return await res.json() as hcaptchaResponse | recaptchaResponse;
return json;
} }

View File

@ -4,7 +4,7 @@
"description": "", "description": "",
"main": "src/start.js", "main": "src/start.js",
"scripts": { "scripts": {
"setup": "node scripts/install.js && npm install --no-optional && ts-patch install -s && patch-package --patch-dir ../api/patches/ && npm run build", "setup": "node scripts/install.js && npm install --no-optional && npx ts-patch install -s && npx patch-package --patch-dir ../api/patches/ && npm run build",
"build": "node scripts/build.js", "build": "node scripts/build.js",
"start": "node scripts/build.js && node dist/bundle/src/start.js", "start": "node scripts/build.js && node dist/bundle/src/start.js",
"start:bundle": "node dist/bundle/src/start.js", "start:bundle": "node dist/bundle/src/start.js",
@ -112,4 +112,4 @@
"typescript-json-schema": "^0.50.1", "typescript-json-schema": "^0.50.1",
"ws": "^7.4.2" "ws": "^7.4.2"
} }
} }

View File

@ -5,7 +5,7 @@
"main": "dist/index.js", "main": "dist/index.js",
"types": "src/index.ts", "types": "src/index.ts",
"scripts": { "scripts": {
"test": "npm run build && jest --coverage ./tests", "test": "npm run build && npx jest --coverage ./tests",
"build": "npx tsc -p .", "build": "npx tsc -p .",
"start": "node dist/start.js" "start": "node dist/start.js"
}, },

View File

@ -5,7 +5,7 @@
"main": "dist/index.js", "main": "dist/index.js",
"types": "src/index.ts", "types": "src/index.ts",
"scripts": { "scripts": {
"test": "npm run build && jest --coverage ./tests", "test": "npm run build && npx jest --coverage ./tests",
"build": "npx tsc -p .", "build": "npx tsc -p .",
"start": "node dist/start.js" "start": "node dist/start.js"
}, },

View File

@ -9,7 +9,7 @@
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"start": "npm run build && node dist/start.js", "start": "npm run build && node dist/start.js",
"build": "npx tsc -p .", "build": "npx tsc -p .",
"dev": "tsnd --respawn src/start.ts" "dev": "npx tsnd --respawn src/start.ts"
}, },
"keywords": [], "keywords": [],
"author": "Fosscord", "author": "Fosscord",

View File

@ -6,7 +6,7 @@
"types": "src/index.ts", "types": "src/index.ts",
"scripts": { "scripts": {
"start": "npm run build && node dist/", "start": "npm run build && node dist/",
"test": "npm run build && jest", "test": "npm run build && npx jest",
"postinstall": "npm run build", "postinstall": "npm run build",
"build": "npx tsc -p .", "build": "npx tsc -p .",
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js" "typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js"