-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
71 lines (59 loc) · 1.83 KB
/
index.ts
File metadata and controls
71 lines (59 loc) · 1.83 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import type { AdapterOptions } from "./types.js";
import type { CaptchaAdapter } from "adminforth";
declare global {
interface Window {
turnstile: {
render: (element: HTMLElement, options: { sitekey: string; callback: (token: string) => void }) => void;
};
}
}
export default class CaptchaAdapterReCaptcha implements CaptchaAdapter {
options: AdapterOptions;
private token: string;
constructor(options: AdapterOptions) {
this.options = options;
}
getScriptSrc(): string {
return `https://www.google.com/recaptcha/api.js`;
}
getSiteKey(): string {
return this.options.siteKey;
}
getWidgetId(): string {
return 'recaptcha-container';
}
getRenderWidgetCode(): string {
return `
window.renderCaptchaWidgetReCaptcha = function(containerId, siteKey, onSuccess) {
grecaptcha.render('recaptcha-container', {
'sitekey' : '6LdI09UrAAAAAAKUsWJRh7k5Bca4RUw_OOeX2q-Z',
'callback' : function(token) {
if (typeof onSuccess === 'function') {
onSuccess(token);
}
},
});
};
`;
}
getRenderWidgetFunctionName(): string {
return 'renderCaptchaWidgetReCaptcha';
}
async validate(token: string, ip: string): Promise<Record<string, any>> {
const formData = new FormData();
formData.append('secret', this.options.secretKey);
formData.append('response', token);
formData.append('remoteip', ip);
try {
const response = await fetch('https://www.google.com/recaptcha/api/siteverify', {
method: 'POST',
body: formData
});
const result = await response.json();
return result;
} catch (error) {
console.error('Turnstile validation error:', error);
return { success: false, 'error-codes': ['internal-error'] };
}
}
}