summary refs log tree commit diff
path: root/api/src/util/utility/captcha.ts
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-07-20 15:33:27 +1000
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-07-20 15:33:27 +1000
commitaaf0499017091587037e96b61a4894a40f13e0e7 (patch)
tree6de5045a2c3b1501646b12e07a6e024a556765cd /api/src/util/utility/captcha.ts
parentCaptcha required message on login/register (diff)
parentCaptcha checking (diff)
downloadserver-aaf0499017091587037e96b61a4894a40f13e0e7.tar.xz
Merge branch 'feat/captchaVerify' into slowcord
Diffstat (limited to 'api/src/util/utility/captcha.ts')
-rw-r--r--api/src/util/utility/captcha.ts32
1 files changed, 25 insertions, 7 deletions
diff --git a/api/src/util/utility/captcha.ts b/api/src/util/utility/captcha.ts

index ba380d7a..016861fa 100644 --- a/api/src/util/utility/captcha.ts +++ b/api/src/util/utility/captcha.ts
@@ -11,18 +11,36 @@ export interface hcaptchaResponse { 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 { secret, sitekey } = security.captcha; - - const res = await fetch("https://hcaptcha.com/siteverify", { + const { service, secret, sitekey } = security.captcha; + + if (!service) throw new Error("Cannot verify captcha without service"); + + const res = await fetch(verifyEndpoints[service], { method: "POST", headers: { "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 json; + return await res.json() as hcaptchaResponse | recaptchaResponse; } \ No newline at end of file