@adminforth/login-captcha 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.woodpecker/buildRelease.sh +13 -0
- package/.woodpecker/buildSlackNotify.sh +44 -0
- package/.woodpecker/release.yml +40 -0
- package/LICENSE +21 -0
- package/build.log +11 -0
- package/custom/CaptchaWidget.vue +57 -0
- package/custom/tsconfig.json +16 -0
- package/dist/custom/CaptchaWidget.vue +57 -0
- package/dist/custom/tsconfig.json +16 -0
- package/dist/index.js +105 -0
- package/dist/types.js +1 -0
- package/index.ts +114 -0
- package/package.json +50 -0
- package/tsconfig.json +13 -0
- package/types.ts +5 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
|
|
2
|
+
#!/bin/bash
|
|
3
|
+
|
|
4
|
+
# write npm run output both to console and to build.log
|
|
5
|
+
npm run build 2>&1 | tee build.log
|
|
6
|
+
build_status=${PIPESTATUS[0]}
|
|
7
|
+
|
|
8
|
+
# if exist status from the npm run build is not 0
|
|
9
|
+
# then exit with the status code from the npm run build
|
|
10
|
+
if [ $build_status -ne 0 ]; then
|
|
11
|
+
echo "Build failed. Exiting with status code $build_status"
|
|
12
|
+
exit $build_status
|
|
13
|
+
fi
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
|
|
3
|
+
set -x
|
|
4
|
+
|
|
5
|
+
COMMIT_SHORT_SHA=$(echo $CI_COMMIT_SHA | cut -c1-8)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
if [ "$CI_PREV_PIPELINE_STATUS" = "success" ]; then
|
|
9
|
+
MESSAGE="Did a build without issues on \`$CI_REPO_NAME/$CI_COMMIT_BRANCH\`. Commit: _${CI_COMMIT_MESSAGE}_ (<$CI_COMMIT_URL|$COMMIT_SHORT_SHA>)"
|
|
10
|
+
|
|
11
|
+
curl -s -X POST -H "Content-Type: application/json" -d '{
|
|
12
|
+
"username": "'"$CI_COMMIT_AUTHOR"'",
|
|
13
|
+
"icon_url": "'"$CI_COMMIT_AUTHOR_AVATAR"'",
|
|
14
|
+
"attachments": [
|
|
15
|
+
{
|
|
16
|
+
"mrkdwn_in": ["text", "pretext"],
|
|
17
|
+
"color": "#36a64f",
|
|
18
|
+
"text": "'"$MESSAGE"'"
|
|
19
|
+
}
|
|
20
|
+
]
|
|
21
|
+
}' "$DEVELOPERS_SLACK_WEBHOOK"
|
|
22
|
+
exit 0
|
|
23
|
+
fi
|
|
24
|
+
export BUILD_LOG=$(cat ./build.log)
|
|
25
|
+
|
|
26
|
+
BUILD_LOG=$(echo $BUILD_LOG | sed 's/"/\\"/g')
|
|
27
|
+
|
|
28
|
+
MESSAGE="Broke \`$CI_REPO_NAME/$CI_COMMIT_BRANCH\` with commit _${CI_COMMIT_MESSAGE}_ (<$CI_COMMIT_URL|$COMMIT_SHORT_SHA>)"
|
|
29
|
+
CODE_BLOCK="\`\`\`$BUILD_LOG\n\`\`\`"
|
|
30
|
+
|
|
31
|
+
echo "Sending slack message to developers $MESSAGE"
|
|
32
|
+
# Send the message
|
|
33
|
+
curl -sS -X POST -H "Content-Type: application/json" -d '{
|
|
34
|
+
"username": "'"$CI_COMMIT_AUTHOR"'",
|
|
35
|
+
"icon_url": "'"$CI_COMMIT_AUTHOR_AVATAR"'",
|
|
36
|
+
"attachments": [
|
|
37
|
+
{
|
|
38
|
+
"mrkdwn_in": ["text", "pretext"],
|
|
39
|
+
"color": "#8A1C12",
|
|
40
|
+
"text": "'"$CODE_BLOCK"'",
|
|
41
|
+
"pretext": "'"$MESSAGE"'"
|
|
42
|
+
}
|
|
43
|
+
]
|
|
44
|
+
}' "$DEVELOPERS_SLACK_WEBHOOK" 2>&1
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
clone:
|
|
2
|
+
git:
|
|
3
|
+
image: woodpeckerci/plugin-git
|
|
4
|
+
settings:
|
|
5
|
+
partial: false
|
|
6
|
+
depth: 5
|
|
7
|
+
|
|
8
|
+
steps:
|
|
9
|
+
init-secrets:
|
|
10
|
+
when:
|
|
11
|
+
- event: push
|
|
12
|
+
image: infisical/cli
|
|
13
|
+
environment:
|
|
14
|
+
INFISICAL_TOKEN:
|
|
15
|
+
from_secret: VAULT_TOKEN
|
|
16
|
+
commands:
|
|
17
|
+
- infisical export --domain https://vault.devforth.io/api --format=dotenv-export --env="prod" > /woodpecker/deploy.vault.env
|
|
18
|
+
|
|
19
|
+
release:
|
|
20
|
+
image: node:20
|
|
21
|
+
when:
|
|
22
|
+
- event: push
|
|
23
|
+
commands:
|
|
24
|
+
- apt update && apt install -y rsync
|
|
25
|
+
- export $(cat /woodpecker/deploy.vault.env | xargs)
|
|
26
|
+
- npm clean-install
|
|
27
|
+
- /bin/bash ./.woodpecker/buildRelease.sh
|
|
28
|
+
- npm audit signatures
|
|
29
|
+
- npx semantic-release
|
|
30
|
+
|
|
31
|
+
slack-on-failure:
|
|
32
|
+
when:
|
|
33
|
+
- event: push
|
|
34
|
+
status: [failure, success]
|
|
35
|
+
- event: push
|
|
36
|
+
image: curlimages/curl
|
|
37
|
+
commands:
|
|
38
|
+
- export $(cat /woodpecker/deploy.vault.env | xargs)
|
|
39
|
+
- /bin/sh ./.woodpecker/buildSlackNotify.sh
|
|
40
|
+
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Devforth.io
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/build.log
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
|
|
2
|
+
> @adminforth/login-captcha@1.0.55 build
|
|
3
|
+
> tsc && rsync -av --exclude 'node_modules' custom dist/
|
|
4
|
+
|
|
5
|
+
sending incremental file list
|
|
6
|
+
custom/
|
|
7
|
+
custom/CaptchaWidget.vue
|
|
8
|
+
custom/tsconfig.json
|
|
9
|
+
|
|
10
|
+
sent 1,753 bytes received 58 bytes 3,622.00 bytes/sec
|
|
11
|
+
total size is 1,536 speedup is 0.85
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
|
|
3
|
+
<div :id="props.meta.containerId"></div>
|
|
4
|
+
|
|
5
|
+
</template>
|
|
6
|
+
|
|
7
|
+
<script setup lang="ts">
|
|
8
|
+
import { onMounted, ref } from 'vue';
|
|
9
|
+
import { callAdminForthApi } from '@/utils';
|
|
10
|
+
|
|
11
|
+
interface Props {
|
|
12
|
+
meta: {
|
|
13
|
+
renderWidgetFunctionName: string;
|
|
14
|
+
containerId: string;
|
|
15
|
+
adapterName: string;
|
|
16
|
+
siteKey: string;
|
|
17
|
+
pluginInstanceId: string;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const props = defineProps<Props>();
|
|
22
|
+
|
|
23
|
+
const token = ref(null);
|
|
24
|
+
const emit = defineEmits(
|
|
25
|
+
["update:allowLoginClick"]
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
onMounted(() => {
|
|
29
|
+
const fnName = props.meta.renderWidgetFunctionName;
|
|
30
|
+
|
|
31
|
+
const renderFn = (window as any)[fnName];
|
|
32
|
+
if (typeof renderFn === 'function') {
|
|
33
|
+
renderFn(props.meta.containerId, props.meta.siteKey, (receivedToken: string) => {
|
|
34
|
+
emit("update:allowLoginClick", true);
|
|
35
|
+
token.value = receivedToken;
|
|
36
|
+
setJWT(receivedToken);
|
|
37
|
+
});
|
|
38
|
+
} else {
|
|
39
|
+
console.warn(`Function ${fnName} not found on window`);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
async function setJWT(token: string) {
|
|
44
|
+
try {
|
|
45
|
+
const res = await callAdminForthApi({
|
|
46
|
+
path: `/plugin/${props.meta.pluginInstanceId}/setToken`,
|
|
47
|
+
method: 'POST',
|
|
48
|
+
body: {
|
|
49
|
+
token,
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.error('Failed to validate token:', error);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
</script>
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"baseUrl": ".", // This should point to your project root
|
|
4
|
+
"paths": {
|
|
5
|
+
"@/*": [
|
|
6
|
+
"../node_modules/adminforth/dist/spa/src/*"
|
|
7
|
+
],
|
|
8
|
+
"*": [
|
|
9
|
+
"../node_modules/adminforth/dist/spa/node_modules/*"
|
|
10
|
+
],
|
|
11
|
+
"@@/*": [
|
|
12
|
+
"."
|
|
13
|
+
]
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
|
|
3
|
+
<div :id="props.meta.containerId"></div>
|
|
4
|
+
|
|
5
|
+
</template>
|
|
6
|
+
|
|
7
|
+
<script setup lang="ts">
|
|
8
|
+
import { onMounted, ref } from 'vue';
|
|
9
|
+
import { callAdminForthApi } from '@/utils';
|
|
10
|
+
|
|
11
|
+
interface Props {
|
|
12
|
+
meta: {
|
|
13
|
+
renderWidgetFunctionName: string;
|
|
14
|
+
containerId: string;
|
|
15
|
+
adapterName: string;
|
|
16
|
+
siteKey: string;
|
|
17
|
+
pluginInstanceId: string;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const props = defineProps<Props>();
|
|
22
|
+
|
|
23
|
+
const token = ref(null);
|
|
24
|
+
const emit = defineEmits(
|
|
25
|
+
["update:allowLoginClick"]
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
onMounted(() => {
|
|
29
|
+
const fnName = props.meta.renderWidgetFunctionName;
|
|
30
|
+
|
|
31
|
+
const renderFn = (window as any)[fnName];
|
|
32
|
+
if (typeof renderFn === 'function') {
|
|
33
|
+
renderFn(props.meta.containerId, props.meta.siteKey, (receivedToken: string) => {
|
|
34
|
+
emit("update:allowLoginClick", true);
|
|
35
|
+
token.value = receivedToken;
|
|
36
|
+
setJWT(receivedToken);
|
|
37
|
+
});
|
|
38
|
+
} else {
|
|
39
|
+
console.warn(`Function ${fnName} not found on window`);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
async function setJWT(token: string) {
|
|
44
|
+
try {
|
|
45
|
+
const res = await callAdminForthApi({
|
|
46
|
+
path: `/plugin/${props.meta.pluginInstanceId}/setToken`,
|
|
47
|
+
method: 'POST',
|
|
48
|
+
body: {
|
|
49
|
+
token,
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.error('Failed to validate token:', error);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
</script>
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"baseUrl": ".", // This should point to your project root
|
|
4
|
+
"paths": {
|
|
5
|
+
"@/*": [
|
|
6
|
+
"../node_modules/adminforth/dist/spa/src/*"
|
|
7
|
+
],
|
|
8
|
+
"*": [
|
|
9
|
+
"../node_modules/adminforth/dist/spa/node_modules/*"
|
|
10
|
+
],
|
|
11
|
+
"@@/*": [
|
|
12
|
+
"."
|
|
13
|
+
]
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { AdminForthPlugin } from "adminforth";
|
|
11
|
+
export default class CaptchaPlugin extends AdminForthPlugin {
|
|
12
|
+
constructor(options) {
|
|
13
|
+
super(options, import.meta.url);
|
|
14
|
+
this.options = options;
|
|
15
|
+
}
|
|
16
|
+
modifyResourceConfig(adminforth, resourceConfig) {
|
|
17
|
+
const _super = Object.create(null, {
|
|
18
|
+
modifyResourceConfig: { get: () => super.modifyResourceConfig }
|
|
19
|
+
});
|
|
20
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
21
|
+
var _a;
|
|
22
|
+
_super.modifyResourceConfig.call(this, adminforth, resourceConfig);
|
|
23
|
+
if (!((_a = adminforth.config.customization) === null || _a === void 0 ? void 0 : _a.loginPageInjections)) {
|
|
24
|
+
adminforth.config.customization = Object.assign(Object.assign({}, adminforth.config.customization), { loginPageInjections: { underInputs: [], panelHeader: [] } });
|
|
25
|
+
}
|
|
26
|
+
;
|
|
27
|
+
const adapter = this.options.captchaAdapter;
|
|
28
|
+
const adapterName = adapter.constructor.name;
|
|
29
|
+
adminforth.config.customization.loginPageInjections.underInputs.push({
|
|
30
|
+
file: this.componentPath('CaptchaWidget.vue'),
|
|
31
|
+
meta: {
|
|
32
|
+
containerId: this.options.captchaAdapter.getWidgetId(),
|
|
33
|
+
adapterName: adapterName,
|
|
34
|
+
renderWidgetFunctionName: this.options.captchaAdapter.getRenderWidgetFunctionName(),
|
|
35
|
+
siteKey: this.options.captchaAdapter.getSiteKey(),
|
|
36
|
+
pluginInstanceId: this.pluginInstanceId
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
adminforth.config.customization.customHeadItems.push({
|
|
40
|
+
tagName: 'script',
|
|
41
|
+
attributes: { src: this.options.captchaAdapter.getScriptSrc(), async: 'true', defer: 'true' }
|
|
42
|
+
}, {
|
|
43
|
+
tagName: 'script',
|
|
44
|
+
attributes: { type: 'text/javascript' },
|
|
45
|
+
innerCode: this.options.captchaAdapter.getRenderWidgetCode()
|
|
46
|
+
});
|
|
47
|
+
const beforeLoginConfirmation = this.adminforth.config.auth.beforeLoginConfirmation;
|
|
48
|
+
const beforeLoginConfirmationArray = Array.isArray(beforeLoginConfirmation) ? beforeLoginConfirmation : [beforeLoginConfirmation];
|
|
49
|
+
beforeLoginConfirmationArray.push((_a) => __awaiter(this, [_a], void 0, function* ({ extra }) {
|
|
50
|
+
var _b;
|
|
51
|
+
if (!extra || !extra.cookies) {
|
|
52
|
+
return {
|
|
53
|
+
body: {
|
|
54
|
+
allowedLogin: false,
|
|
55
|
+
redirectTo: '/login',
|
|
56
|
+
},
|
|
57
|
+
ok: true
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
const cookies = extra.cookies;
|
|
61
|
+
const token = (_b = cookies.find((cookie) => cookie.key === `adminforth_${adapterName}_temporaryJWT`)) === null || _b === void 0 ? void 0 : _b.value;
|
|
62
|
+
if (!token) {
|
|
63
|
+
return {
|
|
64
|
+
body: {
|
|
65
|
+
allowedLogin: false,
|
|
66
|
+
redirectTo: '/login',
|
|
67
|
+
},
|
|
68
|
+
ok: true
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
const ip = this.adminforth.auth.getClientIp(extra.headers);
|
|
72
|
+
const validationResult = yield this.options.captchaAdapter.validate(token, ip);
|
|
73
|
+
console.log('Validation result:', validationResult);
|
|
74
|
+
if (!validationResult || !validationResult.success) {
|
|
75
|
+
return {
|
|
76
|
+
body: {
|
|
77
|
+
allowedLogin: false,
|
|
78
|
+
redirectTo: '/login',
|
|
79
|
+
},
|
|
80
|
+
ok: true
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
}));
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
instanceUniqueRepresentation(pluginOptions) {
|
|
87
|
+
const adapter = this.options.captchaAdapter;
|
|
88
|
+
const adapterName = adapter.constructor.name;
|
|
89
|
+
return `CaptchaPlugin-${adapterName}-${this.options.captchaAdapter.getSiteKey()}`;
|
|
90
|
+
}
|
|
91
|
+
setupEndpoints(server) {
|
|
92
|
+
server.endpoint({
|
|
93
|
+
method: 'POST',
|
|
94
|
+
path: `/plugin/${this.pluginInstanceId}/setToken`,
|
|
95
|
+
noAuth: true,
|
|
96
|
+
handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, response }) {
|
|
97
|
+
const { token } = body;
|
|
98
|
+
const adapter = this.options.captchaAdapter;
|
|
99
|
+
const adapterName = adapter.constructor.name;
|
|
100
|
+
response.setHeader('Set-Cookie', `adminforth_${adapterName}_temporaryJWT=${token}; Path=${this.adminforth.config.baseUrl || '/'}; HttpOnly; SameSite=Strict; max-age=300; `);
|
|
101
|
+
return { ok: true };
|
|
102
|
+
})
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/index.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { AdminForthPlugin } from "adminforth";
|
|
2
|
+
import type { AdminForthResource, AdminUser, IAdminForth, IHttpServer, IAdminForthHttpResponse } from "adminforth";
|
|
3
|
+
import type { PluginOptions } from './types.js';
|
|
4
|
+
|
|
5
|
+
export default class CaptchaPlugin extends AdminForthPlugin {
|
|
6
|
+
options: PluginOptions;
|
|
7
|
+
|
|
8
|
+
constructor(options: PluginOptions) {
|
|
9
|
+
super(options, import.meta.url);
|
|
10
|
+
this.options = options;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
|
|
14
|
+
super.modifyResourceConfig(adminforth, resourceConfig);
|
|
15
|
+
if (!adminforth.config.customization?.loginPageInjections) {
|
|
16
|
+
adminforth.config.customization = {
|
|
17
|
+
...adminforth.config.customization,
|
|
18
|
+
loginPageInjections: { underInputs: [], panelHeader: [] }
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const adapter = this.options.captchaAdapter;
|
|
23
|
+
const adapterName = adapter.constructor.name;
|
|
24
|
+
|
|
25
|
+
adminforth.config.customization.loginPageInjections.underInputs.push({
|
|
26
|
+
file: this.componentPath('CaptchaWidget.vue'),
|
|
27
|
+
meta: {
|
|
28
|
+
containerId: this.options.captchaAdapter.getWidgetId(),
|
|
29
|
+
adapterName: adapterName,
|
|
30
|
+
renderWidgetFunctionName: this.options.captchaAdapter.getRenderWidgetFunctionName(),
|
|
31
|
+
siteKey: this.options.captchaAdapter.getSiteKey(),
|
|
32
|
+
pluginInstanceId: this.pluginInstanceId
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
adminforth.config.customization.customHeadItems.push(
|
|
37
|
+
{
|
|
38
|
+
tagName: 'script',
|
|
39
|
+
attributes: { src: this.options.captchaAdapter.getScriptSrc(), async: 'true', defer: 'true' }
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
tagName: 'script',
|
|
43
|
+
attributes: { type: 'text/javascript' },
|
|
44
|
+
innerCode: this.options.captchaAdapter.getRenderWidgetCode()
|
|
45
|
+
}
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
const beforeLoginConfirmation = this.adminforth.config.auth.beforeLoginConfirmation;
|
|
49
|
+
const beforeLoginConfirmationArray = Array.isArray(beforeLoginConfirmation) ? beforeLoginConfirmation : [beforeLoginConfirmation];
|
|
50
|
+
beforeLoginConfirmationArray.push(
|
|
51
|
+
async({ extra }: { adminUser: AdminUser, response: IAdminForthHttpResponse, extra?: any} )=> {
|
|
52
|
+
if ( !extra || !extra.cookies ) {
|
|
53
|
+
return {
|
|
54
|
+
body:{
|
|
55
|
+
allowedLogin: false,
|
|
56
|
+
redirectTo: '/login',
|
|
57
|
+
},
|
|
58
|
+
ok: true
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const cookies = extra.cookies;
|
|
62
|
+
const token = cookies.find(
|
|
63
|
+
(cookie) => cookie.key === `adminforth_${adapterName}_temporaryJWT`
|
|
64
|
+
)?.value;
|
|
65
|
+
if ( !token ) {
|
|
66
|
+
return {
|
|
67
|
+
body:{
|
|
68
|
+
allowedLogin: false,
|
|
69
|
+
redirectTo: '/login',
|
|
70
|
+
},
|
|
71
|
+
ok: true
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const ip = this.adminforth.auth.getClientIp(extra.headers);
|
|
76
|
+
const validationResult = await this.options.captchaAdapter.validate(token, ip);
|
|
77
|
+
console.log('Validation result:', validationResult);
|
|
78
|
+
|
|
79
|
+
if (!validationResult || !validationResult.success) {
|
|
80
|
+
return {
|
|
81
|
+
body:{
|
|
82
|
+
allowedLogin: false,
|
|
83
|
+
redirectTo: '/login',
|
|
84
|
+
},
|
|
85
|
+
ok: true
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
instanceUniqueRepresentation(pluginOptions: any) : string {
|
|
93
|
+
const adapter = this.options.captchaAdapter;
|
|
94
|
+
const adapterName = adapter.constructor.name;
|
|
95
|
+
return `CaptchaPlugin-${adapterName}-${this.options.captchaAdapter.getSiteKey()}`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
setupEndpoints(server: IHttpServer) {
|
|
99
|
+
server.endpoint({
|
|
100
|
+
method: 'POST',
|
|
101
|
+
path: `/plugin/${this.pluginInstanceId}/setToken`,
|
|
102
|
+
noAuth: true,
|
|
103
|
+
handler: async ({ body, response }) => {
|
|
104
|
+
const { token } = body;
|
|
105
|
+
|
|
106
|
+
const adapter = this.options.captchaAdapter;
|
|
107
|
+
const adapterName = adapter.constructor.name;
|
|
108
|
+
|
|
109
|
+
response.setHeader('Set-Cookie', `adminforth_${adapterName}_temporaryJWT=${token}; Path=${this.adminforth.config.baseUrl || '/'}; HttpOnly; SameSite=Strict; max-age=300; `);
|
|
110
|
+
return { ok: true };
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@adminforth/login-captcha",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc && rsync -av --exclude 'node_modules' custom dist/"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [],
|
|
14
|
+
"author": "",
|
|
15
|
+
"license": "ISC",
|
|
16
|
+
"description": "",
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/node": "latest",
|
|
19
|
+
"semantic-release": "^24.2.1",
|
|
20
|
+
"semantic-release-slack-bot": "^4.0.2",
|
|
21
|
+
"typescript": "^5.7.3"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"adminforth": "^2.4.0-next.184"
|
|
25
|
+
},
|
|
26
|
+
"release": {
|
|
27
|
+
"plugins": [
|
|
28
|
+
"@semantic-release/commit-analyzer",
|
|
29
|
+
"@semantic-release/release-notes-generator",
|
|
30
|
+
"@semantic-release/npm",
|
|
31
|
+
"@semantic-release/github",
|
|
32
|
+
[
|
|
33
|
+
"semantic-release-slack-bot",
|
|
34
|
+
{
|
|
35
|
+
"notifyOnSuccess": true,
|
|
36
|
+
"notifyOnFail": true,
|
|
37
|
+
"slackIcon": ":package:",
|
|
38
|
+
"markdownReleaseNotes": true
|
|
39
|
+
}
|
|
40
|
+
]
|
|
41
|
+
]
|
|
42
|
+
},
|
|
43
|
+
"branches": [
|
|
44
|
+
"main",
|
|
45
|
+
{
|
|
46
|
+
"name": "next",
|
|
47
|
+
"prerelease": true
|
|
48
|
+
}
|
|
49
|
+
]
|
|
50
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include*/
|
|
4
|
+
"module": "node16", /* Specify what module code is generated. */
|
|
5
|
+
"outDir": "./dist", /* Specify an output folder for all emitted files. */
|
|
6
|
+
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. */
|
|
7
|
+
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
|
8
|
+
"strict": false, /* Enable all strict type-checking options. */
|
|
9
|
+
"skipLibCheck": true, /* Skip type checking all .d.ts files. */
|
|
10
|
+
},
|
|
11
|
+
"exclude": ["node_modules", "dist", "custom"], /* Exclude files from compilation. */
|
|
12
|
+
}
|
|
13
|
+
|