@adminforth/two-factors-auth 1.0.33 → 1.1.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 +42 -0
- package/CHANGELOG.md +5 -0
- package/LICENSE +21 -0
- package/build.log +14 -0
- package/custom/TwoFactorsSetup.vue +3 -1
- package/custom/tsconfig.json +19 -0
- package/dist/custom/TwoFactorsSetup.vue +3 -1
- package/dist/custom/tsconfig.json +19 -0
- package/dist/index.js +20 -13
- package/index.ts +29 -16
- package/package.json +42 -3
- package/types.ts +7 -1
- package/Changelog.md +0 -23
- package/dist/custom/TwoFactorsOtp.vue +0 -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_STEP_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,42 @@
|
|
|
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
|
+
secrets:
|
|
19
|
+
- VAULT_TOKEN
|
|
20
|
+
|
|
21
|
+
release:
|
|
22
|
+
image: node:20
|
|
23
|
+
when:
|
|
24
|
+
- event: push
|
|
25
|
+
commands:
|
|
26
|
+
- apt update && apt install -y rsync
|
|
27
|
+
- export $(cat /woodpecker/deploy.vault.env | xargs)
|
|
28
|
+
- npm clean-install
|
|
29
|
+
- /bin/bash ./.woodpecker/buildRelease.sh
|
|
30
|
+
- npm audit signatures
|
|
31
|
+
- npx semantic-release
|
|
32
|
+
|
|
33
|
+
slack-on-failure:
|
|
34
|
+
when:
|
|
35
|
+
- event: push
|
|
36
|
+
status: [failure, success]
|
|
37
|
+
- event: push
|
|
38
|
+
image: curlimages/curl
|
|
39
|
+
commands:
|
|
40
|
+
- export $(cat /woodpecker/deploy.vault.env | xargs)
|
|
41
|
+
- /bin/sh ./.woodpecker/buildSlackNotify.sh
|
|
42
|
+
|
package/CHANGELOG.md
ADDED
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,14 @@
|
|
|
1
|
+
|
|
2
|
+
> @adminforth/two-factors-auth@1.0.33 build
|
|
3
|
+
> tsc && rsync -av --exclude 'node_modules' custom dist/
|
|
4
|
+
|
|
5
|
+
sending incremental file list
|
|
6
|
+
custom/
|
|
7
|
+
custom/TwoFactorsConfirmation.vue
|
|
8
|
+
custom/TwoFactorsSetup.vue
|
|
9
|
+
custom/package-lock.json
|
|
10
|
+
custom/package.json
|
|
11
|
+
custom/tsconfig.json
|
|
12
|
+
|
|
13
|
+
sent 25,933 bytes received 115 bytes 52,096.00 bytes/sec
|
|
14
|
+
total size is 25,510 speedup is 0.98
|
|
@@ -76,6 +76,8 @@ import { showErrorTost } from '@/composables/useFrontendApi';
|
|
|
76
76
|
import { Button, LinkButton } from '@/afcl';
|
|
77
77
|
import Vue2FACodeInput from '@loltech/vue3-2fa-code-input';
|
|
78
78
|
import VOtpInput from "vue3-otp-input";
|
|
79
|
+
import adminforth from '@/adminforth';
|
|
80
|
+
|
|
79
81
|
|
|
80
82
|
const code = ref(null);
|
|
81
83
|
const handleOnComplete = (value) => {
|
|
@@ -122,7 +124,7 @@ function parseJwt(token) {
|
|
|
122
124
|
|
|
123
125
|
function onCopyClick(){
|
|
124
126
|
navigator.clipboard.writeText(totp.value.newSecret);
|
|
125
|
-
|
|
127
|
+
adminforth.alert({message: 'Copied to clipboard', variant: 'success'})
|
|
126
128
|
}
|
|
127
129
|
|
|
128
130
|
onMounted(async () => {
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"baseUrl": ".", // This should point to your project root
|
|
4
|
+
"paths": {
|
|
5
|
+
"@/*": [
|
|
6
|
+
// "node_modules/adminforth/dist/spa/src/*"
|
|
7
|
+
"../../../spa/src/*"
|
|
8
|
+
],
|
|
9
|
+
"*": [
|
|
10
|
+
// "node_modules/adminforth/dist/spa/node_modules/*"
|
|
11
|
+
"../../../spa/node_modules/*"
|
|
12
|
+
],
|
|
13
|
+
"@@/*": [
|
|
14
|
+
// "node_modules/adminforth/dist/spa/src/*"
|
|
15
|
+
"."
|
|
16
|
+
]
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -76,6 +76,8 @@ import { showErrorTost } from '@/composables/useFrontendApi';
|
|
|
76
76
|
import { Button, LinkButton } from '@/afcl';
|
|
77
77
|
import Vue2FACodeInput from '@loltech/vue3-2fa-code-input';
|
|
78
78
|
import VOtpInput from "vue3-otp-input";
|
|
79
|
+
import adminforth from '@/adminforth';
|
|
80
|
+
|
|
79
81
|
|
|
80
82
|
const code = ref(null);
|
|
81
83
|
const handleOnComplete = (value) => {
|
|
@@ -122,7 +124,7 @@ function parseJwt(token) {
|
|
|
122
124
|
|
|
123
125
|
function onCopyClick(){
|
|
124
126
|
navigator.clipboard.writeText(totp.value.newSecret);
|
|
125
|
-
|
|
127
|
+
adminforth.alert({message: 'Copied to clipboard', variant: 'success'})
|
|
126
128
|
}
|
|
127
129
|
|
|
128
130
|
onMounted(async () => {
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"baseUrl": ".", // This should point to your project root
|
|
4
|
+
"paths": {
|
|
5
|
+
"@/*": [
|
|
6
|
+
// "node_modules/adminforth/dist/spa/src/*"
|
|
7
|
+
"../../../spa/src/*"
|
|
8
|
+
],
|
|
9
|
+
"*": [
|
|
10
|
+
// "node_modules/adminforth/dist/spa/node_modules/*"
|
|
11
|
+
"../../../spa/node_modules/*"
|
|
12
|
+
],
|
|
13
|
+
"@@/*": [
|
|
14
|
+
// "node_modules/adminforth/dist/spa/src/*"
|
|
15
|
+
"."
|
|
16
|
+
]
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -39,6 +39,10 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
|
|
|
39
39
|
if (typeof this.options.twoFaSecretFieldName !== 'string') {
|
|
40
40
|
throw new Error('twoFaSecretFieldName must be a string');
|
|
41
41
|
}
|
|
42
|
+
if (this.options.timeStepWindow === undefined
|
|
43
|
+
|| typeof this.options.timeStepWindow !== 'number'
|
|
44
|
+
|| this.options.timeStepWindow < 0)
|
|
45
|
+
this.options.timeStepWindow = 1;
|
|
42
46
|
this.authResource = resourceConfig;
|
|
43
47
|
if (!this.authResource.columns.some((col) => col.name === this.options.twoFaSecretFieldName)) {
|
|
44
48
|
throw new Error(`Column ${this.options.twoFaSecretFieldName} not found in ${this.authResource.label}`);
|
|
@@ -47,6 +51,7 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
|
|
|
47
51
|
const secret = adminUser.dbUser[this.options.twoFaSecretFieldName];
|
|
48
52
|
const userName = adminUser.dbUser[adminforth.config.auth.usernameField];
|
|
49
53
|
const brandName = adminforth.config.customization.brandName;
|
|
54
|
+
const brandNameSlug = adminforth.config.customization.brandNameSlug;
|
|
50
55
|
const authResource = adminforth.config.resources.find((res) => res.resourceId === adminforth.config.auth.usersResourceId);
|
|
51
56
|
const authPk = authResource.columns.find((col) => col.primaryKey).name;
|
|
52
57
|
const userPk = adminUser.dbUser[authPk];
|
|
@@ -62,7 +67,7 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
|
|
|
62
67
|
}
|
|
63
68
|
else {
|
|
64
69
|
const value = this.adminforth.auth.issueJWT({ userName, issuer: brandName, pk: userPk, userCanSkipSetup }, 'tempTotp', '2h');
|
|
65
|
-
response.setHeader('Set-Cookie', `
|
|
70
|
+
response.setHeader('Set-Cookie', `adminforth_${brandNameSlug}_totpTemporaryJWT=${value}; Path=${this.adminforth.config.baseUrl || '/'}; HttpOnly; SameSite=Strict; max-age=3600; `);
|
|
66
71
|
return {
|
|
67
72
|
body: {
|
|
68
73
|
loginAllowed: false,
|
|
@@ -72,7 +77,7 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
|
|
|
72
77
|
};
|
|
73
78
|
}
|
|
74
79
|
const totpTemporaryJWT = this.adminforth.auth.issueJWT({ userName, newSecret, issuer: brandName, pk: userPk, userCanSkipSetup }, 'tempTotp', '2h');
|
|
75
|
-
response.setHeader('Set-Cookie', `
|
|
80
|
+
response.setHeader('Set-Cookie', `adminforth_${brandNameSlug}_totpTemporaryJWT=${totpTemporaryJWT}; Path=${this.adminforth.config.baseUrl || '/'}; HttpOnly; SameSite=Strict; Expires=${new Date(Date.now() + '1h').toUTCString()} `);
|
|
76
81
|
return {
|
|
77
82
|
body: {
|
|
78
83
|
loginAllowed: false,
|
|
@@ -90,7 +95,8 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
|
|
|
90
95
|
handler: (server) => __awaiter(this, void 0, void 0, function* () {
|
|
91
96
|
var _a;
|
|
92
97
|
const toReturn = { totpJWT: null, status: 'ok', };
|
|
93
|
-
const
|
|
98
|
+
const brandNameSlug = this.adminforth.config.customization.brandNameSlug;
|
|
99
|
+
const totpTemporaryJWT = (_a = server.cookies.find((cookie) => cookie.key === `adminforth_${brandNameSlug}_totpTemporaryJWT`)) === null || _a === void 0 ? void 0 : _a.value;
|
|
94
100
|
if (totpTemporaryJWT) {
|
|
95
101
|
toReturn.totpJWT = totpTemporaryJWT;
|
|
96
102
|
}
|
|
@@ -101,15 +107,15 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
|
|
|
101
107
|
method: 'POST',
|
|
102
108
|
path: `/plugin/twofa/confirmSetup`,
|
|
103
109
|
noAuth: true,
|
|
104
|
-
handler: (
|
|
105
|
-
var
|
|
106
|
-
const
|
|
110
|
+
handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, response, cookies }) {
|
|
111
|
+
var _b;
|
|
112
|
+
const brandNameSlug = this.adminforth.config.customization.brandNameSlug;
|
|
113
|
+
const totpTemporaryJWT = (_b = cookies.find((cookie) => cookie.key === `adminforth_${brandNameSlug}_totpTemporaryJWT`)) === null || _b === void 0 ? void 0 : _b.value;
|
|
107
114
|
const decoded = yield this.adminforth.auth.verify(totpTemporaryJWT, 'tempTotp');
|
|
108
|
-
if (!decoded)
|
|
115
|
+
if (!decoded)
|
|
109
116
|
return { status: 'error', message: 'Invalid token' };
|
|
110
|
-
}
|
|
111
117
|
if (decoded.newSecret) {
|
|
112
|
-
const verified = body.skip && decoded.userCanSkipSetup ? true : twofactor.verifyToken(decoded.newSecret, body.code);
|
|
118
|
+
const verified = body.skip && decoded.userCanSkipSetup ? true : twofactor.verifyToken(decoded.newSecret, body.code, this.options.timeStepWindow);
|
|
113
119
|
if (verified) {
|
|
114
120
|
this.connectors = this.adminforth.connectors;
|
|
115
121
|
if (!body.skip) {
|
|
@@ -129,7 +135,7 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
|
|
|
129
135
|
this.connectors = this.adminforth.connectors;
|
|
130
136
|
const connector = this.connectors[this.authResource.dataSource];
|
|
131
137
|
const user = yield connector.getRecordByPrimaryKey(this.authResource, decoded.pk);
|
|
132
|
-
const verified = twofactor.verifyToken(user[this.options.twoFaSecretFieldName], body.code);
|
|
138
|
+
const verified = twofactor.verifyToken(user[this.options.twoFaSecretFieldName], body.code, this.options.timeStepWindow);
|
|
133
139
|
if (verified) {
|
|
134
140
|
this.adminforth.auth.removeCustomCookie({ response, name: 'totpTemporaryJWT' });
|
|
135
141
|
this.adminforth.auth.setAuthCookie({ response, username: decoded.userName, pk: decoded.pk });
|
|
@@ -145,9 +151,10 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
|
|
|
145
151
|
method: "GET",
|
|
146
152
|
path: "/plugin/twofa/skip-allow",
|
|
147
153
|
noAuth: true,
|
|
148
|
-
handler: (
|
|
149
|
-
var
|
|
150
|
-
const
|
|
154
|
+
handler: (_a) => __awaiter(this, [_a], void 0, function* ({ cookies }) {
|
|
155
|
+
var _b;
|
|
156
|
+
const brandNameSlug = this.adminforth.config.customization.brandNameSlug;
|
|
157
|
+
const totpTemporaryJWT = (_b = cookies.find((cookie) => cookie.key === `adminforth_${brandNameSlug}_totpTemporaryJWT`)) === null || _b === void 0 ? void 0 : _b.value;
|
|
151
158
|
const decoded = yield this.adminforth.auth.verify(totpTemporaryJWT, "tempTotp");
|
|
152
159
|
if (!decoded) {
|
|
153
160
|
return { status: "error", message: "Invalid token" };
|
package/index.ts
CHANGED
|
@@ -23,6 +23,7 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
|
|
|
23
23
|
super.modifyResourceConfig(adminforth, resourceConfig);
|
|
24
24
|
this.adminforth = adminforth;
|
|
25
25
|
this.adminForthAuth = adminforth.auth;
|
|
26
|
+
|
|
26
27
|
const customPages = this.adminforth.config.customization.customPages
|
|
27
28
|
customPages.push({
|
|
28
29
|
path:'/confirm2fa',
|
|
@@ -42,6 +43,13 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
|
|
|
42
43
|
if (typeof this.options.twoFaSecretFieldName !=='string'){
|
|
43
44
|
throw new Error('twoFaSecretFieldName must be a string')
|
|
44
45
|
}
|
|
46
|
+
if (
|
|
47
|
+
this.options.timeStepWindow === undefined
|
|
48
|
+
|| typeof this.options.timeStepWindow !== 'number'
|
|
49
|
+
|| this.options.timeStepWindow < 0
|
|
50
|
+
)
|
|
51
|
+
this.options.timeStepWindow = 1;
|
|
52
|
+
|
|
45
53
|
this.authResource = resourceConfig
|
|
46
54
|
if(!this.authResource.columns.some((col)=>col.name === this.options.twoFaSecretFieldName)){
|
|
47
55
|
throw new Error(`Column ${this.options.twoFaSecretFieldName} not found in ${this.authResource.label}`)
|
|
@@ -50,7 +58,8 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
|
|
|
50
58
|
async({ adminUser, response }: { adminUser: AdminUser, response: IAdminForthHttpResponse} )=> {
|
|
51
59
|
const secret = adminUser.dbUser[this.options.twoFaSecretFieldName]
|
|
52
60
|
const userName = adminUser.dbUser[adminforth.config.auth.usernameField]
|
|
53
|
-
const brandName = adminforth.config.customization.brandName
|
|
61
|
+
const brandName = adminforth.config.customization.brandName;
|
|
62
|
+
const brandNameSlug = adminforth.config.customization.brandNameSlug;
|
|
54
63
|
const authResource = adminforth.config.resources.find((res)=>res.resourceId === adminforth.config.auth.usersResourceId )
|
|
55
64
|
const authPk = authResource.columns.find((col)=>col.primaryKey).name
|
|
56
65
|
const userPk = adminUser.dbUser[authPk]
|
|
@@ -68,7 +77,7 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
|
|
|
68
77
|
newSecret = tempSecret.secret
|
|
69
78
|
} else {
|
|
70
79
|
const value = this.adminforth.auth.issueJWT({userName, issuer:brandName, pk:userPk, userCanSkipSetup }, 'tempTotp', '2h');
|
|
71
|
-
response.setHeader('Set-Cookie', `
|
|
80
|
+
response.setHeader('Set-Cookie', `adminforth_${brandNameSlug}_totpTemporaryJWT=${value}; Path=${this.adminforth.config.baseUrl || '/'}; HttpOnly; SameSite=Strict; max-age=3600; `);
|
|
72
81
|
|
|
73
82
|
return {
|
|
74
83
|
body:{
|
|
@@ -78,13 +87,13 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
|
|
|
78
87
|
ok: true
|
|
79
88
|
}
|
|
80
89
|
}
|
|
81
|
-
const totpTemporaryJWT = this.adminforth.auth.issueJWT({userName, newSecret, issuer:brandName, pk:userPk, userCanSkipSetup },'tempTotp', '2h');
|
|
82
|
-
response.setHeader('Set-Cookie', `
|
|
83
|
-
|
|
84
|
-
return {
|
|
90
|
+
const totpTemporaryJWT = this.adminforth.auth.issueJWT({userName, newSecret, issuer:brandName, pk:userPk, userCanSkipSetup }, 'tempTotp', '2h');
|
|
91
|
+
response.setHeader('Set-Cookie', `adminforth_${brandNameSlug}_totpTemporaryJWT=${totpTemporaryJWT}; Path=${this.adminforth.config.baseUrl || '/'}; HttpOnly; SameSite=Strict; Expires=${new Date(Date.now() + '1h').toUTCString() } `);
|
|
92
|
+
|
|
93
|
+
return {
|
|
85
94
|
body:{
|
|
86
95
|
loginAllowed: false,
|
|
87
|
-
redirectTo: secret ? '/confirm2fa' : '/setup2fa',
|
|
96
|
+
redirectTo: secret ? '/confirm2fa' : '/setup2fa',
|
|
88
97
|
},
|
|
89
98
|
ok: true
|
|
90
99
|
}
|
|
@@ -98,8 +107,9 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
|
|
|
98
107
|
noAuth: true,
|
|
99
108
|
handler: async (server) => {
|
|
100
109
|
const toReturn = {totpJWT:null,status:'ok',}
|
|
110
|
+
const brandNameSlug = this.adminforth.config.customization.brandNameSlug;
|
|
101
111
|
|
|
102
|
-
const totpTemporaryJWT = server.cookies.find((cookie)=>cookie.key ===
|
|
112
|
+
const totpTemporaryJWT = server.cookies.find((cookie)=>cookie.key === `adminforth_${brandNameSlug}_totpTemporaryJWT`)?.value;
|
|
103
113
|
if (totpTemporaryJWT){
|
|
104
114
|
toReturn.totpJWT = totpTemporaryJWT
|
|
105
115
|
}
|
|
@@ -111,13 +121,15 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
|
|
|
111
121
|
path: `/plugin/twofa/confirmSetup`,
|
|
112
122
|
noAuth: true,
|
|
113
123
|
handler: async ({ body, adminUser, response, cookies }) => {
|
|
114
|
-
const
|
|
124
|
+
const brandNameSlug = this.adminforth.config.customization.brandNameSlug;
|
|
125
|
+
const totpTemporaryJWT = cookies.find((cookie)=>cookie.key === `adminforth_${brandNameSlug}_totpTemporaryJWT`)?.value;
|
|
115
126
|
const decoded = await this.adminforth.auth.verify(totpTemporaryJWT, 'tempTotp');
|
|
116
|
-
|
|
127
|
+
|
|
128
|
+
if (!decoded)
|
|
117
129
|
return {status:'error',message:'Invalid token'}
|
|
118
|
-
|
|
130
|
+
|
|
119
131
|
if (decoded.newSecret) {
|
|
120
|
-
const verified = body.skip && decoded.userCanSkipSetup ? true : twofactor.verifyToken(decoded.newSecret, body.code);
|
|
132
|
+
const verified = body.skip && decoded.userCanSkipSetup ? true : twofactor.verifyToken(decoded.newSecret, body.code, this.options.timeStepWindow);
|
|
121
133
|
if (verified) {
|
|
122
134
|
this.connectors = this.adminforth.connectors
|
|
123
135
|
if (!body.skip) {
|
|
@@ -135,8 +147,8 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
|
|
|
135
147
|
this.connectors = this.adminforth.connectors
|
|
136
148
|
const connector = this.connectors[this.authResource.dataSource];
|
|
137
149
|
const user = await connector.getRecordByPrimaryKey(this.authResource, decoded.pk)
|
|
138
|
-
const verified = twofactor.verifyToken(user[this.options.twoFaSecretFieldName], body.code);
|
|
139
|
-
if (verified) {
|
|
150
|
+
const verified = twofactor.verifyToken(user[this.options.twoFaSecretFieldName], body.code, this.options.timeStepWindow);
|
|
151
|
+
if (verified) {
|
|
140
152
|
this.adminforth.auth.removeCustomCookie({response, name:'totpTemporaryJWT'})
|
|
141
153
|
this.adminforth.auth.setAuthCookie({response, username:decoded.userName, pk:decoded.pk})
|
|
142
154
|
return { status: 'ok', allowedLogin: true }
|
|
@@ -151,12 +163,13 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
|
|
|
151
163
|
path: "/plugin/twofa/skip-allow",
|
|
152
164
|
noAuth: true,
|
|
153
165
|
handler: async ({ cookies }) => {
|
|
166
|
+
const brandNameSlug = this.adminforth.config.customization.brandNameSlug;
|
|
154
167
|
const totpTemporaryJWT = cookies.find(
|
|
155
|
-
(cookie) => cookie.key ===
|
|
168
|
+
(cookie) => cookie.key === `adminforth_${brandNameSlug}_totpTemporaryJWT`
|
|
156
169
|
)?.value;
|
|
157
170
|
const decoded = await this.adminforth.auth.verify(
|
|
158
171
|
totpTemporaryJWT,
|
|
159
|
-
"tempTotp"
|
|
172
|
+
"tempTotp",
|
|
160
173
|
);
|
|
161
174
|
if (!decoded) {
|
|
162
175
|
return { status: "error", message: "Invalid token" };
|
package/package.json
CHANGED
|
@@ -1,17 +1,56 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adminforth/two-factors-auth",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "AdminForth plugin for two factors authentication",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/devforth/adminforth-two-factors-auth.git"
|
|
10
|
+
},
|
|
7
11
|
"type": "module",
|
|
8
12
|
"scripts": {
|
|
9
|
-
"
|
|
10
|
-
"
|
|
13
|
+
"prepare": "npm link adminforth",
|
|
14
|
+
"build": "tsc && rsync -av --exclude 'node_modules' custom dist/"
|
|
11
15
|
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"adminforth",
|
|
18
|
+
"two-factors-auth",
|
|
19
|
+
"2fa"
|
|
20
|
+
],
|
|
12
21
|
"author": "devforth",
|
|
13
22
|
"license": "ISC",
|
|
14
23
|
"dependencies": {
|
|
15
24
|
"node-2fa": "^2.0.3"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/node": "^22.10.7",
|
|
28
|
+
"semantic-release": "^24.2.1",
|
|
29
|
+
"semantic-release-slack-bot": "^4.0.2",
|
|
30
|
+
"typescript": "^5.7.3"
|
|
31
|
+
},
|
|
32
|
+
"release": {
|
|
33
|
+
"plugins": [
|
|
34
|
+
"@semantic-release/commit-analyzer",
|
|
35
|
+
"@semantic-release/release-notes-generator",
|
|
36
|
+
"@semantic-release/npm",
|
|
37
|
+
"@semantic-release/github",
|
|
38
|
+
[
|
|
39
|
+
"semantic-release-slack-bot",
|
|
40
|
+
{
|
|
41
|
+
"notifyOnSuccess": true,
|
|
42
|
+
"notifyOnFail": true,
|
|
43
|
+
"slackIcon": ":package:",
|
|
44
|
+
"markdownReleaseNotes": true
|
|
45
|
+
}
|
|
46
|
+
]
|
|
47
|
+
],
|
|
48
|
+
"branches": [
|
|
49
|
+
"main",
|
|
50
|
+
{
|
|
51
|
+
"name": "next",
|
|
52
|
+
"prerelease": true
|
|
53
|
+
}
|
|
54
|
+
]
|
|
16
55
|
}
|
|
17
56
|
}
|
package/types.ts
CHANGED
|
@@ -3,13 +3,19 @@ import { AdminUser } from "adminforth";
|
|
|
3
3
|
export type PluginOptions = {
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* Name of the field in the auth resource which will store 2FA secret.
|
|
6
|
+
* Name of the field in the auth resource which will store 2FA secret.
|
|
7
7
|
*
|
|
8
8
|
* Resource mandatory should have one columns which defined {@link AdminForthResourceColumn} which
|
|
9
9
|
* name should be equal to the value .
|
|
10
10
|
*/
|
|
11
11
|
twoFaSecretFieldName: string;
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Not-negative optional time step window for 2FA. This value means that the user will be able to enter the old code
|
|
15
|
+
* for the next 30 seconds after the new code was generated.
|
|
16
|
+
*/
|
|
17
|
+
timeStepWindow?: number;
|
|
18
|
+
|
|
13
19
|
/**
|
|
14
20
|
* Optional function to filter users to apply 2FA.
|
|
15
21
|
* Should return true if 2FA should be applied to the user and false if AdminForth should not challenge the user with 2FA.
|
package/Changelog.md
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
## [1.0.30] - 2024-12-13
|
|
2
|
-
|
|
3
|
-
### Fixed
|
|
4
|
-
|
|
5
|
-
- Fixed skip 2fa
|
|
6
|
-
|
|
7
|
-
## [1.0.20] - 2024-12-05
|
|
8
|
-
|
|
9
|
-
### Added
|
|
10
|
-
|
|
11
|
-
- Added usersFilterToAllowSkipSetup to TwoFactorsAuthPlugin to allow skip 2FA setup for specific users
|
|
12
|
-
|
|
13
|
-
## [1.0.19] - 2024-12-02
|
|
14
|
-
|
|
15
|
-
### Fixed
|
|
16
|
-
|
|
17
|
-
- Fixed copy hash for 2fa
|
|
18
|
-
|
|
19
|
-
## [1.0.18] - 2024-09-13
|
|
20
|
-
|
|
21
|
-
### Added
|
|
22
|
-
|
|
23
|
-
- add usersFilterToApply to TwoFactorsAuthPlugin to disable 2FA for specific users
|
|
File without changes
|