-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
105 lines (92 loc) · 3.74 KB
/
index.ts
File metadata and controls
105 lines (92 loc) · 3.74 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { AdminForthPlugin } from "adminforth";
import type { AdminForthResource, AdminUser, IAdminForth, IHttpServer, IAdminForthHttpResponse } from "adminforth";
import type { PluginOptions } from './types.js';
export default class CaptchaPlugin extends AdminForthPlugin {
options: PluginOptions;
constructor(options: PluginOptions) {
super(options, import.meta.url);
this.options = options;
}
async modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
super.modifyResourceConfig(adminforth, resourceConfig);
if (!adminforth.config.customization?.loginPageInjections) {
adminforth.config.customization = {
...adminforth.config.customization,
loginPageInjections: { underInputs: [], panelHeader: [] }
};
};
const adapter = this.options.captchaAdapter;
const adapterName = adapter.constructor.name;
adminforth.config.customization.loginPageInjections.underInputs.push({
file: this.componentPath('CaptchaWidget.vue'),
meta: {
containerId: this.options.captchaAdapter.getWidgetId(),
adapterName: adapterName,
renderWidgetFunctionName: this.options.captchaAdapter.getRenderWidgetFunctionName(),
siteKey: this.options.captchaAdapter.getSiteKey(),
pluginInstanceId: this.pluginInstanceId
}
});
if (!adminforth.config.customization?.customHeadItems) {
adminforth.config.customization.customHeadItems = [];
}
adminforth.config.customization.customHeadItems.push(
{
tagName: 'script',
attributes: { src: this.options.captchaAdapter.getScriptSrc(), async: 'true', defer: 'true' }
},
{
tagName: 'script',
attributes: { type: 'text/javascript' },
innerCode: this.options.captchaAdapter.getRenderWidgetCode()
}
);
const beforeLoginConfirmation = this.adminforth.config.auth.beforeLoginConfirmation;
const beforeLoginConfirmationArray = Array.isArray(beforeLoginConfirmation) ? beforeLoginConfirmation : [beforeLoginConfirmation];
beforeLoginConfirmationArray.unshift(
async({ extra }: { adminUser: AdminUser, response: IAdminForthHttpResponse, extra?: any} )=> {
const rejectResult = {
body:{
allowedLogin: false,
redirectTo: '/login',
},
ok: true
};
if ( !extra || !extra.cookies ) {
return rejectResult;
}
const cookies = extra.cookies;
const token = cookies.find(
(cookie) => cookie.key === `adminforth_${adapterName}_temporaryJWT`
)?.value;
if ( !token ) {
return rejectResult;
}
const ip = this.adminforth.auth.getClientIp(extra.headers);
const validationResult = await this.options.captchaAdapter.validate(token, ip);
if (!validationResult || !validationResult.success) {
return rejectResult;
}
}
);
}
instanceUniqueRepresentation(pluginOptions: any) : string {
const adapter = this.options.captchaAdapter;
const adapterName = adapter.constructor.name;
return `CaptchaPlugin-${adapterName}-${this.options.captchaAdapter.getSiteKey()}`;
}
setupEndpoints(server: IHttpServer) {
server.endpoint({
method: 'POST',
path: `/plugin/${this.pluginInstanceId}/setToken`,
noAuth: true,
handler: async ({ body, response }) => {
const { token } = body;
const adapter = this.options.captchaAdapter;
const adapterName = adapter.constructor.name;
response.setHeader('Set-Cookie', `adminforth_${adapterName}_temporaryJWT=${token}; Path=${this.adminforth.config.baseUrl || '/'}; HttpOnly; SameSite=Strict; max-age=300; `);
return { ok: true };
}
});
}
}