Skip to content

Commit 73e9e1b

Browse files
committed
2FA in devDemo adjustments
1 parent b6d365e commit 73e9e1b

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

adminforth/documentation/docs/tutorial/05-Plugins/02-TwoFactorsAuth.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ The Two-Factor Authentication Plugin provides an additional layer of security to
44

55
## Installation
66

7-
8-
```
7+
``` bash
98
npm i @adminforth/two-factors-auth --save
109
```
1110

1211
Plugin is already installed into adminforth, to import:
13-
12+
1413
```ts title="/users.ts"
1514
import TwoFactorsAuthPlugin from '@adminforth/two-factors-auth';
1615
```
@@ -20,9 +19,9 @@ Plugin required some additional setup, to make it work properly. It should be ad
2019
```ts title='./schema.prisma'
2120
model users {
2221
id String @id
23-
created_at DateTime
22+
created_at DateTime
2423
email String @unique
25-
role String
24+
role String
2625
password_hash String
2726
//diff-add
2827
secret2fa String?
@@ -36,13 +35,14 @@ npx --yes prisma migrate dev --name init
3635
```
3736
3837
And add it to `users.ts`
38+
3939
```ts tittle="./resources/users.ts"
4040
{
4141
table: 'users',
4242
//diff-add
4343
plugins: [
4444
//diff-add
45-
new TwoFactorsAuthPlugin ({ twoFaSecretFieldName: 'secret2fa' }),
45+
new TwoFactorsAuthPlugin ({ twoFaSecretFieldName: 'secret2fa', timeStepWindow: 1 }),
4646
//diff-add
4747
],
4848
columns: [
@@ -61,18 +61,28 @@ And add it to `users.ts`
6161
}
6262
```
6363
64-
Thats it! Two-Factor Authentication is now enabled:
64+
> 💡 **Note**: Time-Step Size
65+
>
66+
> By default, `timeStepWindow` is set to `1`, which means the Two-Factor Authentication Plugin will check the current 30-second time-step, as well as one step before and after, to validate a TOTP code. This aligns with [RFC 6238](https://www.rfc-editor.org/rfc/rfc6238) best practices to accommodate slight clock drift between the server and the user's device.
67+
>
68+
> For example, if a code is generated between **12:00:00** and **12:00:30**, it will typically expire at **12:00:30**. However, with a `timeStepWindow` of `1`, the plugin will continue to accept it up to **12:00:59** (the “next” 30-second step), preventing users from being locked out if their device clock is a few seconds off. Once the clock hits **12:01:00**, that previous code will be treated as expired.
69+
>
70+
> If you find users frequently encountering code mismatches due to clock drift, you can increase `timeStepWindow` to `2`. **However, be cautious: larger windows can reduce overall security!**
71+
>
72+
> ❗ With a `timeStepWindow` set to `0`, the plugin will pass all the expired codes, which is not secure and should only be used for testing purposes.
73+
74+
Thats it! Two-Factor Authentication is now enabled:
6575
![alt text](image-1.png)
6676
6777
## Disabling Two-Factor Authentication locally
6878
69-
If it is not convenient to enter the code every time you log in during local development, you can disable Two-Factor Authentication
79+
If it is not convenient to enter the code every time you log in during local development, you can disable Two-Factor Authentication
7080
for the dev environment using `usersFilterToApply` option.
7181
7282
```ts title='./index.ts'
7383

7484
plugins: [
75-
new TwoFactorsAuthPlugin ({
85+
new TwoFactorsAuthPlugin ({
7686
twoFaSecretFieldName: 'secret2fa',
7787
//diff-add
7888
usersFilterToApply: (adminUser: AdminUser) => {
@@ -92,10 +102,9 @@ for the dev environment using `usersFilterToApply` option.
92102
],
93103
```
94104
95-
96105
## Select which users should use Two-Factor Authentication
97106
98-
By default plugin enforces Two-Factor Authentication for all users.
107+
By default plugin enforces Two-Factor Authentication for all users.
99108
100109
If you wish to enforce 2FA only for specific users, you can again use `usersFilterToApply` option:
101110
@@ -105,7 +114,7 @@ If you wish to enforce 2FA only for specific users, you can again use `usersFilt
105114
return !(['adminforth', 'adminguest'].includes(adminUser.dbUser.email));
106115
},
107116
```
108-
117+
109118
You can even add a boolean column to the user table to store whether the user should use 2FA or not:
110119
111120
```ts title='./users.ts'
@@ -137,7 +146,7 @@ You can even add a boolean column to the user table to store whether the user sh
137146
}
138147
},
139148
plugins: [
140-
new TwoFactorsAuthPlugin ({
149+
new TwoFactorsAuthPlugin ({
141150
twoFaSecretFieldName: 'secret2fa',
142151
usersFilterToApply: (adminUser: AdminUser) => {
143152
return adminUser.dbUser.use2fa;
@@ -156,7 +165,7 @@ If you want to allow specific users to skip the 2FA setup, you can use the `user
156165
```ts title='./users.ts'
157166
...
158167
plugins: [
159-
new TwoFactorsAuthPlugin ({
168+
new TwoFactorsAuthPlugin ({
160169
twoFaSecretFieldName: 'secret2fa',
161170
...
162171
//diff-add

dev-demo/resources/users.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export default {
3838
}),
3939
new TwoFactorsAuthPlugin({
4040
twoFaSecretFieldName: "secret2fa",
41+
timeStepWindow: 1, // optional time step window for 2FA
4142
// optional callback to define which users should be enforced to use 2FA
4243
usersFilterToApply: (adminUser: AdminUser) => {
4344
if (process.env.NODE_ENV === "development") {
@@ -207,4 +208,4 @@ export default {
207208
// }
208209
// },
209210
},
210-
} as AdminForthResourceInput;
211+
} as AdminForthResourceInput;

0 commit comments

Comments
 (0)