-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
75 lines (63 loc) · 1.95 KB
/
index.ts
File metadata and controls
75 lines (63 loc) · 1.95 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
72
73
74
75
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 CaptchaAdapterCloudflare implements CaptchaAdapter {
options: AdapterOptions;
private token: string;
constructor(options: AdapterOptions) {
this.options = options;
}
getScriptSrc(): string {
return `https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit`;
}
getSiteKey(): string {
return this.options.siteKey;
}
getWidgetId(): string {
return 'turnstile-container';
}
getRenderWidgetCode(): string {
return `
window.renderCaptchaWidgetCloudflare = function(containerId, siteKey, onSuccess) {
if (!window.turnstile) {
console.error('Turnstile script is not loaded');
return;
}
return window.turnstile.render('#' + containerId, {
sitekey: siteKey,
callback: function(token) {
if (typeof onSuccess === 'function') {
onSuccess(token);
}
}
});
};
`;
}
getRenderWidgetFunctionName(): string {
return 'renderCaptchaWidgetCloudflare';
}
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://challenges.cloudflare.com/turnstile/v0/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'] };
}
}
}