Skip to content

Commit 6150802

Browse files
authored
Merge pull request #14 from devforth/AdminForth/968
fix: allow to skip 2FA reauthorization if users role mentioned in use…
2 parents 7a26163 + 78bb2da commit 6150802

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

index.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,33 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
2727
return `single`;
2828
}
2929

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+
3057
public async verify(
3158
confirmationResult: Record<string, any>,
3259
opts?: { adminUser?: AdminUser; userPk?: string; cookies?: any }
@@ -38,6 +65,12 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
3865
return { ok: true };
3966
}
4067
}
68+
if (this.options.usersFilterToAllowSkipSetup) {
69+
const res = await this.checkIfSkipSetupAllowSkipVerify(opts.adminUser);
70+
if ( res.skipAllowed === true ) {
71+
return { ok: true };
72+
}
73+
}
4174
if (confirmationResult.mode === "totp") {
4275
const code = confirmationResult.result;
4376
const authRes = this.adminforth.config.resources
@@ -553,6 +586,12 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin {
553586
return { skipAllowed: true };
554587
}
555588
}
589+
if ( this.options.usersFilterToAllowSkipSetup ) {
590+
const res = await this.checkIfSkipSetupAllowSkipVerify(adminUser);
591+
if ( res.skipAllowed === true ) {
592+
return { skipAllowed: true };
593+
}
594+
}
556595
return { skipAllowed: false };
557596
},
558597
});

0 commit comments

Comments
 (0)