2022-09-17 21:27:08 +03:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import type { UsersRepository } from '@/models/index.js';
|
2022-09-20 23:33:11 +03:00
|
|
|
import type { Config } from '@/config.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import { HttpRequestService } from './HttpRequestService.js';
|
|
|
|
|
|
|
|
type CaptchaResponse = {
|
|
|
|
success: boolean;
|
|
|
|
'error-codes'?: string[];
|
|
|
|
};
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class CaptchaService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
private httpRequestService: HttpRequestService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-09-18 21:11:50 +03:00
|
|
|
private async getCaptchaResponse(url: string, secret: string, response: string): Promise<CaptchaResponse> {
|
2022-09-17 21:27:08 +03:00
|
|
|
const params = new URLSearchParams({
|
|
|
|
secret,
|
|
|
|
response,
|
|
|
|
});
|
|
|
|
|
|
|
|
const res = await fetch(url, {
|
|
|
|
method: 'POST',
|
|
|
|
body: params,
|
|
|
|
headers: {
|
|
|
|
'User-Agent': this.config.userAgent,
|
|
|
|
},
|
|
|
|
// TODO
|
|
|
|
//timeout: 10 * 1000,
|
|
|
|
agent: (url, bypassProxy) => this.httpRequestService.getAgentByUrl(url, bypassProxy),
|
|
|
|
}).catch(err => {
|
|
|
|
throw `${err.message ?? err}`;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
throw `${res.status}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return await res.json() as CaptchaResponse;
|
|
|
|
}
|
|
|
|
|
2022-12-03 12:42:05 +02:00
|
|
|
public async verifyRecaptcha(secret: string, response: string | null | undefined): Promise<void> {
|
|
|
|
if (response == null) {
|
|
|
|
throw 'recaptcha-failed: no response provided';
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await this.getCaptchaResponse('https://www.recaptcha.net/recaptcha/api/siteverify', secret, response).catch(err => {
|
|
|
|
throw `recaptcha-request-failed: ${err}`;
|
2022-09-17 21:27:08 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (result.success !== true) {
|
|
|
|
const errorCodes = result['error-codes'] ? result['error-codes'].join(', ') : '';
|
|
|
|
throw `recaptcha-failed: ${errorCodes}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-03 12:42:05 +02:00
|
|
|
public async verifyHcaptcha(secret: string, response: string | null | undefined): Promise<void> {
|
|
|
|
if (response == null) {
|
|
|
|
throw 'hcaptcha-failed: no response provided';
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await this.getCaptchaResponse('https://hcaptcha.com/siteverify', secret, response).catch(err => {
|
|
|
|
throw `hcaptcha-request-failed: ${err}`;
|
2022-09-17 21:27:08 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (result.success !== true) {
|
|
|
|
const errorCodes = result['error-codes'] ? result['error-codes'].join(', ') : '';
|
|
|
|
throw `hcaptcha-failed: ${errorCodes}`;
|
|
|
|
}
|
|
|
|
}
|
2022-10-13 03:19:57 +03:00
|
|
|
|
2022-12-03 12:42:05 +02:00
|
|
|
public async verifyTurnstile(secret: string, response: string | null | undefined): Promise<void> {
|
|
|
|
if (response == null) {
|
|
|
|
throw 'turnstile-failed: no response provided';
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await this.getCaptchaResponse('https://challenges.cloudflare.com/turnstile/v0/siteverify', secret, response).catch(err => {
|
|
|
|
throw `turnstile-request-failed: ${err}`;
|
2022-10-13 03:19:57 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (result.success !== true) {
|
|
|
|
const errorCodes = result['error-codes'] ? result['error-codes'].join(', ') : '';
|
|
|
|
throw `turnstile-failed: ${errorCodes}`;
|
|
|
|
}
|
|
|
|
}
|
2022-09-17 21:27:08 +03:00
|
|
|
}
|
|
|
|
|