@adminforth/two-factors-auth 7.1.1 → 7.2.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 +1 -1
- package/dist/services/totpService.js +3 -2
- package/dist/types.d.ts +8 -1
- package/package.json +1 -1
- package/services/totpService.ts +3 -3
- package/tests/totpService.test.mjs +10 -2
- package/types.ts +9 -1
package/build.log
CHANGED
|
@@ -10,16 +10,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
import { RateLimiter } from "adminforth";
|
|
11
11
|
import twofactor from 'node-2fa';
|
|
12
12
|
import { TwoFactorErrorCode } from "../utils/errors.js";
|
|
13
|
-
const
|
|
13
|
+
const DEFAULT_TOTP_RATE_LIMIT = ['30/1m', '300/1h', '1000/1d'];
|
|
14
14
|
const VERIFIED_CODE_JWT_TYPE = 'verifiedTotpCode';
|
|
15
15
|
const VERIFIED_CODE_TOKEN_TTL = '1m';
|
|
16
16
|
const TOTP_STEP_SECONDS = 30;
|
|
17
17
|
export class TotpService {
|
|
18
18
|
constructor(options, userRepository, adminforth) {
|
|
19
|
+
var _a;
|
|
19
20
|
this.options = options;
|
|
20
21
|
this.userRepository = userRepository;
|
|
21
22
|
this.adminforth = adminforth;
|
|
22
|
-
this.codeAttemptLimiters =
|
|
23
|
+
this.codeAttemptLimiters = ((_a = this.options.totpRateLimit) !== null && _a !== void 0 ? _a : DEFAULT_TOTP_RATE_LIMIT).map((rate) => new RateLimiter(rate));
|
|
23
24
|
this.lastUsedCodeSteps = new Map();
|
|
24
25
|
}
|
|
25
26
|
generateSecret(issuerName, userName) {
|
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AdminUser, KeyValueAdapter } from "adminforth";
|
|
2
|
-
import { type PluginsCommonOptions, PeriodString } from "adminforth";
|
|
2
|
+
import { type PluginsCommonOptions, PeriodString, RateLimitString } from "adminforth";
|
|
3
3
|
export interface PluginOptions extends PluginsCommonOptions {
|
|
4
4
|
/**
|
|
5
5
|
* Name of the field in the auth resource which will store 2FA secret.
|
|
@@ -24,6 +24,13 @@ export interface PluginOptions extends PluginsCommonOptions {
|
|
|
24
24
|
* code and enter the code. Default is '5m'.
|
|
25
25
|
*/
|
|
26
26
|
totpSetupValidityPeriod?: PeriodString;
|
|
27
|
+
/**
|
|
28
|
+
* Rate limits for TOTP code attempts of each user, counted by user (not by IP).
|
|
29
|
+
* Every TOTP check counts as an attempt: at login and for step-up verification.
|
|
30
|
+
* Format: "attempts/period", where period is "1s", "1m", "1h", or "1d".
|
|
31
|
+
* Default: ['30/1m', '300/1h', '1000/1d']
|
|
32
|
+
*/
|
|
33
|
+
totpRateLimit?: RateLimitString[];
|
|
27
34
|
customBrandPrefix?: string;
|
|
28
35
|
/**
|
|
29
36
|
* Passkeys (WebAuthn) configuration.
|
package/package.json
CHANGED
package/services/totpService.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { RateLimiter, type AdminUser, type IAdminForth } from "adminforth";
|
|
1
|
+
import { RateLimiter, type AdminUser, type IAdminForth, type RateLimitString } from "adminforth";
|
|
2
2
|
import twofactor from 'node-2fa';
|
|
3
3
|
import type { PluginOptions } from "../types.js";
|
|
4
4
|
import { UserRepository } from "../repositories/userRepository.js";
|
|
5
5
|
import { TwoFactorErrorCode } from "../utils/errors.js";
|
|
6
6
|
import type { VerificationFailure, VerificationResult } from "../utils/types.js";
|
|
7
7
|
|
|
8
|
-
const
|
|
8
|
+
const DEFAULT_TOTP_RATE_LIMIT: RateLimitString[] = ['30/1m', '300/1h', '1000/1d'];
|
|
9
9
|
const VERIFIED_CODE_JWT_TYPE = 'verifiedTotpCode';
|
|
10
10
|
const VERIFIED_CODE_TOKEN_TTL = '1m';
|
|
11
11
|
const TOTP_STEP_SECONDS = 30;
|
|
12
12
|
|
|
13
13
|
export class TotpService {
|
|
14
|
-
private readonly codeAttemptLimiters =
|
|
14
|
+
private readonly codeAttemptLimiters = (this.options.totpRateLimit ?? DEFAULT_TOTP_RATE_LIMIT).map((rate) => new RateLimiter(rate));
|
|
15
15
|
private readonly lastUsedCodeSteps = new Map<string, number>();
|
|
16
16
|
|
|
17
17
|
constructor(
|
|
@@ -7,7 +7,7 @@ import { TwoFactorErrorCode } from '../dist/utils/errors.js';
|
|
|
7
7
|
const SECRET = 'JBSWY3DPEHPK3PXP';
|
|
8
8
|
const userRepository = { getAuthUser: async (pk) => ({ pk }), getSecret: () => SECRET, getUserPk: (adminUser) => adminUser.pk };
|
|
9
9
|
|
|
10
|
-
function createService() {
|
|
10
|
+
function createService(options = {}) {
|
|
11
11
|
const jwts = new Map();
|
|
12
12
|
const auth = {
|
|
13
13
|
issueJWT: (payload, type) => {
|
|
@@ -17,7 +17,7 @@ function createService() {
|
|
|
17
17
|
},
|
|
18
18
|
verify: async (token, type) => (jwts.get(token)?.type === type ? jwts.get(token) : null),
|
|
19
19
|
};
|
|
20
|
-
return new TotpService({ timeStepWindow: 1 }, userRepository, { auth });
|
|
20
|
+
return new TotpService({ timeStepWindow: 1, ...options }, userRepository, { auth });
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
test('limits code attempts of each user to stop brute force of a stolen session', async () => {
|
|
@@ -29,6 +29,14 @@ test('limits code attempts of each user to stop brute force of a stolen session'
|
|
|
29
29
|
assert.equal((await service.verifyUserCode('user-2', 'wrong')).code, TwoFactorErrorCode.INVALID);
|
|
30
30
|
});
|
|
31
31
|
|
|
32
|
+
test('uses totpRateLimit from plugin options instead of the default limits', async () => {
|
|
33
|
+
const service = createService({ totpRateLimit: ['3/1m'] });
|
|
34
|
+
for (let attempt = 0; attempt < 3; attempt++) {
|
|
35
|
+
assert.equal((await service.verifyUserCode('user-1', 'wrong')).code, TwoFactorErrorCode.INVALID);
|
|
36
|
+
}
|
|
37
|
+
assert.equal((await service.verifyUserCode('user-1', 'wrong')).code, TwoFactorErrorCode.RATE_LIMITED);
|
|
38
|
+
});
|
|
39
|
+
|
|
32
40
|
test('a correct code gives a token that confirms the code only for its user', async () => {
|
|
33
41
|
const service = createService();
|
|
34
42
|
const { verifiedCodeToken } = await service.verifyAdminUserCode({ pk: 'user-1' }, twofactor.generateToken(SECRET).token);
|
package/types.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AdminUser, KeyValueAdapter } from "adminforth";
|
|
2
|
-
import {type PluginsCommonOptions, PeriodString } from "adminforth";
|
|
2
|
+
import {type PluginsCommonOptions, PeriodString, RateLimitString } from "adminforth";
|
|
3
3
|
|
|
4
4
|
export interface PluginOptions extends PluginsCommonOptions {
|
|
5
5
|
|
|
@@ -30,6 +30,14 @@ export interface PluginOptions extends PluginsCommonOptions {
|
|
|
30
30
|
*/
|
|
31
31
|
totpSetupValidityPeriod?: PeriodString; // e.g. '5m', '10m', '15m'. Default is '1m'
|
|
32
32
|
|
|
33
|
+
/**
|
|
34
|
+
* Rate limits for TOTP code attempts of each user, counted by user (not by IP).
|
|
35
|
+
* Every TOTP check counts as an attempt: at login and for step-up verification.
|
|
36
|
+
* Format: "attempts/period", where period is "1s", "1m", "1h", or "1d".
|
|
37
|
+
* Default: ['30/1m', '300/1h', '1000/1d']
|
|
38
|
+
*/
|
|
39
|
+
totpRateLimit?: RateLimitString[];
|
|
40
|
+
|
|
33
41
|
customBrandPrefix?: string;
|
|
34
42
|
|
|
35
43
|
/**
|