@adminforth/two-factors-auth 2.9.9 → 2.9.10

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.
Files changed (3) hide show
  1. package/dist/index.js +37 -0
  2. package/index.ts +39 -0
  3. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -20,6 +20,31 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
20
20
  instanceUniqueRepresentation(pluginOptions) {
21
21
  return `single`;
22
22
  }
23
+ checkIfSkipSetupAllowSkipVerify(adminUser) {
24
+ return __awaiter(this, void 0, void 0, function* () {
25
+ if (this.options.usersFilterToAllowSkipSetup) {
26
+ const res = yield this.options.usersFilterToAllowSkipSetup(adminUser); // recieve result of usersFilterToAllowSkipSetup
27
+ if (res === false) { // if false, user is not allowed to skip anyway, so doesn't matter if they have 2FA set up or not
28
+ return { skipAllowed: false };
29
+ }
30
+ //recieve user's record
31
+ const usersResource = this.adminforth.config.resources.find(r => r.resourceId === this.adminforth.config.auth.usersResourceId);
32
+ const usersPrimaryKeyColumn = usersResource.columns.find((col) => col.primaryKey);
33
+ const userPkFieldName = usersPrimaryKeyColumn.name;
34
+ const userRecord = yield this.adminforth.resource(this.adminforth.config.auth.usersResourceId).get([Filters.EQ(userPkFieldName, adminUser.pk)]);
35
+ //check if user has 2FA set up
36
+ const users2FASecret = userRecord[this.options.twoFaSecretFieldName];
37
+ //check if user has any passkeys registered
38
+ const passkeys = yield this.adminforth.resource(this.options.passkeys.credentialResourceID).list([Filters.EQ(this.options.passkeys.credentialUserIdFieldName, adminUser.dbUser[userPkFieldName])]);
39
+ // If user has either 2FA secret or any passkeys, they cannot skip
40
+ if (users2FASecret || (passkeys && passkeys.length > 0)) {
41
+ return { skipAllowed: false };
42
+ }
43
+ return { skipAllowed: res };
44
+ }
45
+ return { skipAllowed: false };
46
+ });
47
+ }
23
48
  verify(confirmationResult, opts) {
24
49
  return __awaiter(this, void 0, void 0, function* () {
25
50
  var _a, _b, _c, _d;
@@ -31,6 +56,12 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
31
56
  return { ok: true };
32
57
  }
33
58
  }
59
+ if (this.options.usersFilterToAllowSkipSetup) {
60
+ const res = yield this.checkIfSkipSetupAllowSkipVerify(opts.adminUser);
61
+ if (res.skipAllowed === true) {
62
+ return { ok: true };
63
+ }
64
+ }
34
65
  if (confirmationResult.mode === "totp") {
35
66
  const code = confirmationResult.result;
36
67
  const authRes = this.adminforth.config.resources
@@ -503,6 +534,12 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
503
534
  return { skipAllowed: true };
504
535
  }
505
536
  }
537
+ if (this.options.usersFilterToAllowSkipSetup) {
538
+ const res = yield this.checkIfSkipSetupAllowSkipVerify(adminUser);
539
+ if (res.skipAllowed === true) {
540
+ return { skipAllowed: true };
541
+ }
542
+ }
506
543
  return { skipAllowed: false };
507
544
  }),
508
545
  });
package/index.ts CHANGED
@@ -27,6 +27,33 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
27
27
  return `single`;
28
28
  }
29
29
 
30
+ public async checkIfSkipSetupAllowSkipVerify(adminUser: AdminUser): Promise<{ skipAllowed: boolean }> {
31
+ if (this.options.usersFilterToAllowSkipSetup) {
32
+ const res = await this.options.usersFilterToAllowSkipSetup(adminUser); // recieve result of usersFilterToAllowSkipSetup
33
+ if (res === false) { // if false, user is not allowed to skip anyway, so doesn't matter if they have 2FA set up or not
34
+ return { skipAllowed: false };
35
+ }
36
+
37
+ //recieve user's record
38
+ const usersResource = this.adminforth.config.resources.find(r => r.resourceId === this.adminforth.config.auth.usersResourceId);
39
+ const usersPrimaryKeyColumn = usersResource.columns.find((col) => col.primaryKey);
40
+ const userPkFieldName = usersPrimaryKeyColumn.name;
41
+ const userRecord = await this.adminforth.resource(this.adminforth.config.auth.usersResourceId).get([Filters.EQ(userPkFieldName, adminUser.pk)])
42
+
43
+ //check if user has 2FA set up
44
+ const users2FASecret = userRecord[this.options.twoFaSecretFieldName];
45
+ //check if user has any passkeys registered
46
+ const passkeys = await this.adminforth.resource(this.options.passkeys.credentialResourceID).list( [Filters.EQ(this.options.passkeys.credentialUserIdFieldName, adminUser.dbUser[userPkFieldName])] );
47
+
48
+ // If user has either 2FA secret or any passkeys, they cannot skip
49
+ if (users2FASecret || (passkeys && passkeys.length > 0)) {
50
+ return { skipAllowed: false };
51
+ }
52
+ return { skipAllowed: res };
53
+ }
54
+ return { skipAllowed: false };
55
+ }
56
+
30
57
  public async verify(
31
58
  confirmationResult: Record<string, any>,
32
59
  opts?: { adminUser?: AdminUser; userPk?: string; cookies?: any }
@@ -38,6 +65,12 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
38
65
  return { ok: true };
39
66
  }
40
67
  }
68
+ if (this.options.usersFilterToAllowSkipSetup) {
69
+ const res = await this.checkIfSkipSetupAllowSkipVerify(opts.adminUser);
70
+ if ( res.skipAllowed === true ) {
71
+ return { ok: true };
72
+ }
73
+ }
41
74
  if (confirmationResult.mode === "totp") {
42
75
  const code = confirmationResult.result;
43
76
  const authRes = this.adminforth.config.resources
@@ -553,6 +586,12 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
553
586
  return { skipAllowed: true };
554
587
  }
555
588
  }
589
+ if ( this.options.usersFilterToAllowSkipSetup ) {
590
+ const res = await this.checkIfSkipSetupAllowSkipVerify(adminUser);
591
+ if ( res.skipAllowed === true ) {
592
+ return { skipAllowed: true };
593
+ }
594
+ }
556
595
  return { skipAllowed: false };
557
596
  },
558
597
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adminforth/two-factors-auth",
3
- "version": "2.9.9",
3
+ "version": "2.9.10",
4
4
  "description": "AdminForth plugin for two factors authentication",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",