summary refs log tree commit diff
path: root/api/src/util/utility/captcha.ts
blob: ba380d7a7c8e50a9fa27e9b7fd31c254053f141e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { Config } from "@fosscord/util";
import fetch from "node-fetch";

export interface hcaptchaResponse {
	success: boolean;
	challenge_ts: string;
	hostname: string;
	credit: boolean;
	"error-codes": string[];
	score: number;	// enterprise only
	score_reason: string[];	// enterprise only
}

export async function verifyHcaptcha(response: string, ip?: string) {
	const { security } = Config.get();
	const { secret, sitekey } = security.captcha;
	
	const res = await fetch("https://hcaptcha.com/siteverify", {
		method: "POST",
		headers: {
			"Content-Type": "application/x-www-form-urlencoded",
		},
		body: `response=${response}&secret=${secret}&remoteip=${ip}&sitekey=${sitekey}`,
	})

	const json = await res.json() as hcaptchaResponse;
	return json;
}