From d786e96c2bb6d637be7289efdb6766ae4406af1f Mon Sep 17 00:00:00 2001 From: Marie Date: Sat, 2 Nov 2024 02:20:35 +0100 Subject: upd: add FriendlyCaptcha as a captcha solution FriendlyCaptcha is a german captcha solution which is GDPR compliant and has a non-commerical free license --- packages/backend/src/core/CaptchaService.ts | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'packages/backend/src/core/CaptchaService.ts') diff --git a/packages/backend/src/core/CaptchaService.ts b/packages/backend/src/core/CaptchaService.ts index f6b7955cd2..5a424890f2 100644 --- a/packages/backend/src/core/CaptchaService.ts +++ b/packages/backend/src/core/CaptchaService.ts @@ -10,6 +10,7 @@ import { bindThis } from '@/decorators.js'; type CaptchaResponse = { success: boolean; 'error-codes'?: string[]; + 'errors'?: string[]; }; @Injectable() @@ -73,6 +74,35 @@ export class CaptchaService { } } + @bindThis + public async verifyFriendlyCaptcha(secret: string, response: string | null | undefined): Promise { + if (response == null) { + throw new Error('recaptcha-failed: no response provided'); + } + + const result = await this.httpRequestService.send('https://api.friendlycaptcha.com/api/v1/siteverify', { + method: 'POST', + body: JSON.stringify({ + secret: secret, + solution: response, + }), + headers: { + 'Content-Type': 'application/json', + }, + }); + + if (result.status !== 200) { + throw new Error('frc-failed: frc didn\'t return 200 OK'); + } + + const resp = await result.json() as CaptchaResponse; + + if (resp.success !== true) { + const errorCodes = resp['errors'] ? resp['errors'].join(', ') : ''; + throw new Error(`frc-failed: ${errorCodes}`); + } + } + // https://codeberg.org/Gusted/mCaptcha/src/branch/main/mcaptcha.go @bindThis public async verifyMcaptcha(secret: string, siteKey: string, instanceHost: string, response: string | null | undefined): Promise { -- cgit v1.2.3-freya