Johann150
2fbd31abe6
Some checks failed
ci/woodpecker/push/lint-foundkey-js Pipeline was successful
ci/woodpecker/push/lint-client Pipeline failed
ci/woodpecker/push/lint-backend Pipeline failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/lint-sw Pipeline failed
ci/woodpecker/push/test Pipeline failed
- The `timeout` parameter does not exist in `node-fetch`, so the timeout was not working properly. - Refactor the User-Agent header to be set in a central place instead of several different places. - Refactor more places to use getResult which handles the timeout and everything else already instead of the normal `fetch` provided by `node-fetch`.
52 lines
1.5 KiB
TypeScript
52 lines
1.5 KiB
TypeScript
import { URLSearchParams } from 'node:url';
|
|
import { getResponse } from '@/misc/fetch.js';
|
|
import config from '@/config/index.js';
|
|
|
|
export async function verifyRecaptcha(secret: string, response: string): Promise<void> {
|
|
const result = await getCaptchaResponse('https://www.recaptcha.net/recaptcha/api/siteverify', secret, response).catch(e => {
|
|
throw new Error(`recaptcha-request-failed: ${e.message}`);
|
|
});
|
|
|
|
if (result.success !== true) {
|
|
const errorCodes = result['error-codes'] ? result['error-codes'].join(', ') : '';
|
|
throw new Error(`recaptcha-failed: ${errorCodes}`);
|
|
}
|
|
}
|
|
|
|
export async function verifyHcaptcha(secret: string, response: string): Promise<void> {
|
|
const result = await getCaptchaResponse('https://hcaptcha.com/siteverify', secret, response).catch(e => {
|
|
throw new Error(`hcaptcha-request-failed: ${e.message}`);
|
|
});
|
|
|
|
if (result.success !== true) {
|
|
const errorCodes = result['error-codes'] ? result['error-codes'].join(', ') : '';
|
|
throw new Error(`hcaptcha-failed: ${errorCodes}`);
|
|
}
|
|
}
|
|
|
|
type CaptchaResponse = {
|
|
success: boolean;
|
|
'error-codes'?: string[];
|
|
};
|
|
|
|
async function getCaptchaResponse(url: string, secret: string, response: string): Promise<CaptchaResponse> {
|
|
const params = new URLSearchParams({
|
|
secret,
|
|
response,
|
|
});
|
|
|
|
const res = await getResponse({
|
|
url,
|
|
method: 'POST',
|
|
body: params,
|
|
}).catch(e => {
|
|
throw new Error(`${e.message || e}`);
|
|
});
|
|
|
|
if (!res.ok) {
|
|
throw new Error(`${res.status}`);
|
|
}
|
|
|
|
return await res.json() as CaptchaResponse;
|
|
}
|