@adminforth/two-factors-auth 7.3.0 → 7.3.1

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 CHANGED
@@ -21,5 +21,5 @@ custom/tsconfig.json
21
21
  custom/use2faApi.ts
22
22
  custom/utils.js
23
23
 
24
- sent 300,012 bytes received 324 bytes 600,672.00 bytes/sec
24
+ sent 300,029 bytes received 324 bytes 600,706.00 bytes/sec
25
25
  total size is 298,778 speedup is 0.99
@@ -13,6 +13,7 @@ export declare class TotpService {
13
13
  verifySecret(secret: string, code: string): boolean;
14
14
  private findCodeStep;
15
15
  verifyUserCode(userPk: string, code: string, extra?: HttpExtra): Promise<VerificationResult>;
16
+ private refundCodeAttempt;
16
17
  verifyAdminUserCode(adminUser: AdminUser, code: string, extra: HttpExtra): Promise<{
17
18
  ok: true;
18
19
  verifiedCodeToken: string;
@@ -56,10 +56,17 @@ export class TotpService {
56
56
  return { error: "This code has already been used. Wait for the next one.", code: TwoFactorErrorCode.INVALID };
57
57
  }
58
58
  this.lastUsedCodeSteps.set(userPk, step);
59
+ yield this.refundCodeAttempt(userPk);
59
60
  yield ((_e = (_d = this.options).onOtpSucceeded) === null || _e === void 0 ? void 0 : _e.call(_d, hookParams));
60
61
  return { ok: true };
61
62
  });
62
63
  }
64
+ // attempt is consumed before the check so parallel requests can't bypass the limit, and given back when the code is correct
65
+ refundCodeAttempt(userPk) {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ yield Promise.all(this.codeAttemptLimiters.map((limiter) => limiter.rateLimiter.reward(userPk, 1)));
68
+ });
69
+ }
63
70
  verifyAdminUserCode(adminUser, code, extra) {
64
71
  return __awaiter(this, void 0, void 0, function* () {
65
72
  const result = yield this.verifyUserCode(this.userRepository.getUserPk(adminUser), code, extra);
package/dist/types.d.ts CHANGED
@@ -45,7 +45,7 @@ export interface PluginOptions extends PluginsCommonOptions {
45
45
  totpSetupValidityPeriod?: PeriodString;
46
46
  /**
47
47
  * Rate limits for TOTP code attempts of each user, counted by user (not by IP).
48
- * Every TOTP check counts as an attempt: at login and for step-up verification.
48
+ * Only unsuccessful TOTP checks count as attempts (wrong or reused code), at login and for step-up verification.
49
49
  * Format: "attempts/period", where period is "1s", "1m", "1h", or "1d".
50
50
  * Default: ['30/1m', '300/1h', '1000/1d']
51
51
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adminforth/two-factors-auth",
3
- "version": "7.3.0",
3
+ "version": "7.3.1",
4
4
  "description": "AdminForth plugin for two factors authentication",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -56,10 +56,16 @@ export class TotpService {
56
56
  return { error: "This code has already been used. Wait for the next one.", code: TwoFactorErrorCode.INVALID };
57
57
  }
58
58
  this.lastUsedCodeSteps.set(userPk, step);
59
+ await this.refundCodeAttempt(userPk);
59
60
  await this.options.onOtpSucceeded?.(hookParams);
60
61
  return { ok: true };
61
62
  }
62
63
 
64
+ // attempt is consumed before the check so parallel requests can't bypass the limit, and given back when the code is correct
65
+ private async refundCodeAttempt(userPk: string): Promise<void> {
66
+ await Promise.all(this.codeAttemptLimiters.map((limiter) => limiter.rateLimiter.reward(userPk, 1)));
67
+ }
68
+
63
69
  public async verifyAdminUserCode(adminUser: AdminUser, code: string, extra: HttpExtra): Promise<{ ok: true; verifiedCodeToken: string } | VerificationFailure> {
64
70
  const result = await this.verifyUserCode(this.userRepository.getUserPk(adminUser), code, extra);
65
71
  if ('error' in result) return result;
package/types.ts CHANGED
@@ -52,7 +52,7 @@ export interface PluginOptions extends PluginsCommonOptions {
52
52
 
53
53
  /**
54
54
  * Rate limits for TOTP code attempts of each user, counted by user (not by IP).
55
- * Every TOTP check counts as an attempt: at login and for step-up verification.
55
+ * Only unsuccessful TOTP checks count as attempts (wrong or reused code), at login and for step-up verification.
56
56
  * Format: "attempts/period", where period is "1s", "1m", "1h", or "1d".
57
57
  * Default: ['30/1m', '300/1h', '1000/1d']
58
58
  */