@adminforth/two-factors-auth 1.5.0 → 1.6.0
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/build.log +2 -2
- package/custom/TwoFaModal.vue +9 -9
- package/custom/TwoFactorsConfirmation.vue +1 -1
- package/custom/TwoFactorsSetup.vue +1 -1
- package/dist/custom/TwoFaModal.vue +9 -9
- package/dist/custom/TwoFactorsConfirmation.vue +1 -1
- package/dist/custom/TwoFactorsSetup.vue +1 -1
- package/dist/index.js +26 -0
- package/index.ts +28 -0
- package/package.json +1 -1
package/build.log
CHANGED
|
@@ -11,5 +11,5 @@ custom/package-lock.json
|
|
|
11
11
|
custom/package.json
|
|
12
12
|
custom/tsconfig.json
|
|
13
13
|
|
|
14
|
-
sent
|
|
15
|
-
total size is 30,
|
|
14
|
+
sent 31,207 bytes received 134 bytes 62,682.00 bytes/sec
|
|
15
|
+
total size is 30,714 speedup is 0.98
|
package/custom/TwoFaModal.vue
CHANGED
|
@@ -57,14 +57,15 @@
|
|
|
57
57
|
let resolveFn: ((code: string) => void) | null = null;
|
|
58
58
|
let verifyingCallback: ((code: string) => boolean) | null = null;
|
|
59
59
|
let verifyFn: null | ((code: string) => Promise<boolean> | boolean) = null;
|
|
60
|
+
let rejectFn: ((err?: any) => void) | null = null;
|
|
60
61
|
|
|
61
62
|
window.adminforthTwoFaModal = {
|
|
62
|
-
getCode: (verifyingCallback?: () => Promise<boolean>) =>
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
63
|
+
getCode: (verifyingCallback?: (code: string) => Promise<boolean>) =>
|
|
64
|
+
new Promise((resolve, reject) => {
|
|
65
|
+
if (modelShow.value) throw new Error('Modal is already open');
|
|
66
66
|
modelShow.value = true;
|
|
67
67
|
resolveFn = resolve;
|
|
68
|
+
rejectFn = reject;
|
|
68
69
|
verifyFn = verifyingCallback ?? null;
|
|
69
70
|
}),
|
|
70
71
|
};
|
|
@@ -106,17 +107,16 @@
|
|
|
106
107
|
}
|
|
107
108
|
|
|
108
109
|
async function sendCode(value: string) {
|
|
109
|
-
if (!resolveFn)
|
|
110
|
-
throw new Error('Modal is not initialized properly');
|
|
111
|
-
}
|
|
112
|
-
|
|
110
|
+
if (!resolveFn) throw new Error('Modal is not initialized properly');
|
|
113
111
|
if (verifyFn) {
|
|
114
112
|
try {
|
|
115
113
|
const ok = await verifyFn(value);
|
|
116
114
|
if (!ok) {
|
|
115
|
+
rejectFn?.(new Error('Invalid code'));
|
|
117
116
|
return;
|
|
118
117
|
}
|
|
119
|
-
} catch {
|
|
118
|
+
} catch (err) {
|
|
119
|
+
rejectFn?.(err);
|
|
120
120
|
return;
|
|
121
121
|
}
|
|
122
122
|
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
'background-image': 'url(' + loadFile(coreStore.config?.loginBackgroundImage) + ')',
|
|
5
5
|
'background-size': 'cover',
|
|
6
6
|
'background-position': 'center',
|
|
7
|
-
'background-blend-mode': 'darken'
|
|
7
|
+
'background-blend-mode': coreStore.config?.removeBackgroundBlendMode ? 'normal' : 'darken'
|
|
8
8
|
}: {}"
|
|
9
9
|
>
|
|
10
10
|
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
'background-image': 'url(' + loadFile(coreStore.config?.loginBackgroundImage) + ')',
|
|
5
5
|
'background-size': 'cover',
|
|
6
6
|
'background-position': 'center',
|
|
7
|
-
'background-blend-mode': 'darken'
|
|
7
|
+
'background-blend-mode': coreStore.config?.removeBackgroundBlendMode ? 'normal' : 'darken'
|
|
8
8
|
}: {}"
|
|
9
9
|
>
|
|
10
10
|
|
|
@@ -57,14 +57,15 @@
|
|
|
57
57
|
let resolveFn: ((code: string) => void) | null = null;
|
|
58
58
|
let verifyingCallback: ((code: string) => boolean) | null = null;
|
|
59
59
|
let verifyFn: null | ((code: string) => Promise<boolean> | boolean) = null;
|
|
60
|
+
let rejectFn: ((err?: any) => void) | null = null;
|
|
60
61
|
|
|
61
62
|
window.adminforthTwoFaModal = {
|
|
62
|
-
getCode: (verifyingCallback?: () => Promise<boolean>) =>
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
63
|
+
getCode: (verifyingCallback?: (code: string) => Promise<boolean>) =>
|
|
64
|
+
new Promise((resolve, reject) => {
|
|
65
|
+
if (modelShow.value) throw new Error('Modal is already open');
|
|
66
66
|
modelShow.value = true;
|
|
67
67
|
resolveFn = resolve;
|
|
68
|
+
rejectFn = reject;
|
|
68
69
|
verifyFn = verifyingCallback ?? null;
|
|
69
70
|
}),
|
|
70
71
|
};
|
|
@@ -106,17 +107,16 @@
|
|
|
106
107
|
}
|
|
107
108
|
|
|
108
109
|
async function sendCode(value: string) {
|
|
109
|
-
if (!resolveFn)
|
|
110
|
-
throw new Error('Modal is not initialized properly');
|
|
111
|
-
}
|
|
112
|
-
|
|
110
|
+
if (!resolveFn) throw new Error('Modal is not initialized properly');
|
|
113
111
|
if (verifyFn) {
|
|
114
112
|
try {
|
|
115
113
|
const ok = await verifyFn(value);
|
|
116
114
|
if (!ok) {
|
|
115
|
+
rejectFn?.(new Error('Invalid code'));
|
|
117
116
|
return;
|
|
118
117
|
}
|
|
119
|
-
} catch {
|
|
118
|
+
} catch (err) {
|
|
119
|
+
rejectFn?.(err);
|
|
120
120
|
return;
|
|
121
121
|
}
|
|
122
122
|
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
'background-image': 'url(' + loadFile(coreStore.config?.loginBackgroundImage) + ')',
|
|
5
5
|
'background-size': 'cover',
|
|
6
6
|
'background-position': 'center',
|
|
7
|
-
'background-blend-mode': 'darken'
|
|
7
|
+
'background-blend-mode': coreStore.config?.removeBackgroundBlendMode ? 'normal' : 'darken'
|
|
8
8
|
}: {}"
|
|
9
9
|
>
|
|
10
10
|
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
'background-image': 'url(' + loadFile(coreStore.config?.loginBackgroundImage) + ')',
|
|
5
5
|
'background-size': 'cover',
|
|
6
6
|
'background-position': 'center',
|
|
7
|
-
'background-blend-mode': 'darken'
|
|
7
|
+
'background-blend-mode': coreStore.config?.removeBackgroundBlendMode ? 'normal' : 'darken'
|
|
8
8
|
}: {}"
|
|
9
9
|
>
|
|
10
10
|
|
package/dist/index.js
CHANGED
|
@@ -17,6 +17,32 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
|
|
|
17
17
|
instanceUniqueRepresentation(pluginOptions) {
|
|
18
18
|
return `single`;
|
|
19
19
|
}
|
|
20
|
+
verify(code, opts) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
var _a, _b, _c, _d;
|
|
23
|
+
if (!code)
|
|
24
|
+
return { error: "Code is required" };
|
|
25
|
+
const authRes = this.adminforth.config.resources
|
|
26
|
+
.find(r => r.resourceId === this.adminforth.config.auth.usersResourceId);
|
|
27
|
+
if (!authRes)
|
|
28
|
+
return { error: "Auth resource not found" };
|
|
29
|
+
const connector = this.adminforth.connectors[authRes.dataSource];
|
|
30
|
+
const pkName = (_a = authRes.columns.find(c => c.primaryKey)) === null || _a === void 0 ? void 0 : _a.name;
|
|
31
|
+
if (!pkName)
|
|
32
|
+
return { error: "Primary key not found on auth resource" };
|
|
33
|
+
const pk = (_b = opts === null || opts === void 0 ? void 0 : opts.userPk) !== null && _b !== void 0 ? _b : (_d = (_c = opts === null || opts === void 0 ? void 0 : opts.adminUser) === null || _c === void 0 ? void 0 : _c.dbUser) === null || _d === void 0 ? void 0 : _d[pkName];
|
|
34
|
+
if (!pk)
|
|
35
|
+
return { error: "User PK is required" };
|
|
36
|
+
const user = yield connector.getRecordByPrimaryKey(authRes, pk);
|
|
37
|
+
if (!user)
|
|
38
|
+
return { error: "User not found" };
|
|
39
|
+
const secret = user[this.options.twoFaSecretFieldName];
|
|
40
|
+
if (!secret)
|
|
41
|
+
return { error: "2FA is not set up for this user" };
|
|
42
|
+
const verified = twofactor.verifyToken(secret, code, this.options.timeStepWindow);
|
|
43
|
+
return verified ? { ok: true } : { error: "Wrong or expired OTP code" };
|
|
44
|
+
});
|
|
45
|
+
}
|
|
20
46
|
modifyResourceConfig(adminforth, resourceConfig) {
|
|
21
47
|
super.modifyResourceConfig(adminforth, resourceConfig);
|
|
22
48
|
this.adminforth = adminforth;
|
package/index.ts
CHANGED
|
@@ -19,6 +19,34 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
|
|
|
19
19
|
return `single`;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
public async verify(
|
|
23
|
+
code: string,
|
|
24
|
+
opts?: { adminUser?: AdminUser; userPk?: string }
|
|
25
|
+
): Promise<{ ok: true } | { error: string }> {
|
|
26
|
+
if (!code) return { error: "Code is required" };
|
|
27
|
+
|
|
28
|
+
const authRes = this.adminforth.config.resources
|
|
29
|
+
.find(r => r.resourceId === this.adminforth.config.auth.usersResourceId);
|
|
30
|
+
|
|
31
|
+
if (!authRes) return { error: "Auth resource not found" };
|
|
32
|
+
|
|
33
|
+
const connector = this.adminforth.connectors[authRes.dataSource];
|
|
34
|
+
const pkName = authRes.columns.find(c => c.primaryKey)?.name;
|
|
35
|
+
if (!pkName) return { error: "Primary key not found on auth resource" };
|
|
36
|
+
|
|
37
|
+
const pk = opts?.userPk ?? opts?.adminUser?.dbUser?.[pkName];
|
|
38
|
+
if (!pk) return { error: "User PK is required" };
|
|
39
|
+
|
|
40
|
+
const user = await connector.getRecordByPrimaryKey(authRes, pk);
|
|
41
|
+
if (!user) return { error: "User not found" };
|
|
42
|
+
|
|
43
|
+
const secret = user[this.options.twoFaSecretFieldName];
|
|
44
|
+
if (!secret) return { error: "2FA is not set up for this user" };
|
|
45
|
+
|
|
46
|
+
const verified = twofactor.verifyToken(secret, code, this.options.timeStepWindow);
|
|
47
|
+
return verified ? { ok: true } : { error: "Wrong or expired OTP code" };
|
|
48
|
+
}
|
|
49
|
+
|
|
22
50
|
modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
|
|
23
51
|
super.modifyResourceConfig(adminforth, resourceConfig);
|
|
24
52
|
this.adminforth = adminforth;
|