@basaltkit/auth 4.0.0 → 4.1.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/README.md CHANGED
@@ -241,7 +241,7 @@ Flow (all routes require login):
241
241
 
242
242
  1. `POST /auth/mfa/enroll` → returns `{ secret, otpauthUri }`; show the `otpauthUri` as a QR code.
243
243
  2. `POST /auth/mfa/activate` with `{ code }` (code from the app) → activates and returns `{ recoveryCodes }` — 10 single-use recovery codes, **shown only once**.
244
- 3. From then on, `POST /auth/login` requires the extra `mfaCode` field (TOTP code or a recovery code). Correct password without a code → `AUTH_MFA_REQUIRED` error.
244
+ 3. From then on, `POST /auth/login` requires the extra `mfaCode` field (TOTP code or a recovery code). Correct password without a code → `AUTH_MFA_REQUIRED` error (counted by the login throttle — see [Brute-force lockout](#brute-force-lockout-loginthrottle)).
245
245
  4. `GET /auth/mfa/status` and `POST /auth/mfa/disable` (with `{ code }`) complete the cycle.
246
246
 
247
247
  **Requiring MFA.** `authPlugin({ requireMfa: true })` — or a policy
@@ -529,6 +529,8 @@ declare `meta.scopes`; opt out with `allowNarrowKeysOnUnscopedRoutes: true`) and
529
529
 
530
530
  Active by default: 5 failed attempts per email within a 15-minute window → `AUTH_LOCKED` error (HTTP 429). A successful login clears the counter.
531
531
 
532
+ **`AUTH_MFA_REQUIRED` counts as an attempt.** On an MFA account, that answer is only given for the *right* password, so it is a password oracle (the standard two-step MFA login has the same property). To keep it from allowing unthrottled guessing, it spends the per-account and per-IP budgets exactly like a wrong password. A user who then signs in with the code clears the account counter; the IP slot of the first step expires with the window, so under a shared NAT with many MFA users, size `ipLoginThrottle` accordingly.
533
+
532
534
  ```ts
533
535
  import { authPlugin, LoginThrottle, MemoryUserSource } from '@basaltkit/auth'
534
536
 
@@ -709,5 +711,6 @@ If you implement your own store, do the same. Returning `void` keeps the older r
709
711
  - **Use the browser session cookie for browser UIs.** It is `HttpOnly` by default; keep JWTs out of `localStorage` and use same-origin requests so the browser sends the cookie automatically.
710
712
  - **Don't increase `accessTtl`.** Short access tokens limit the damage from a stolen token; renewal via refresh token already provides convenience for the user.
711
713
  - **Show the API key and recovery codes only once** — that's how the module works; don't store them in plain text on your side.
712
- - **Don't disable `loginThrottle` in production**, and keep the "always 200" responses on the forgot/verify routes (already the default), so as not to reveal which emails have an account.
714
+ - **Don't disable `loginThrottle` / `ipLoginThrottle` in production.** Besides wrong passwords, they bound the `AUTH_MFA_REQUIRED` password oracle on MFA accounts (it is only returned for a correct password and is counted like a failure).
715
+ - **Keep the "always 200" responses** on the forgot/verify routes (already the default), so as not to reveal which emails have an account.
713
716
  - **In a cluster (multiple machines)**, use shared stores (database/Redis) instead of the `Memory*` ones, or sessions and lockouts won't be shared across processes.
package/dist/auth.d.ts CHANGED
@@ -339,8 +339,10 @@ export declare class Auth {
339
339
  *
340
340
  * When the account has MFA enabled, `mfaCode` (a TOTP or recovery code) is
341
341
  * required: a correct password with a missing code throws
342
- * {@link MfaRequiredError} (without counting as a failed attempt), and a
343
- * wrong code throws {@link MfaInvalidCodeError}.
342
+ * {@link MfaRequiredError}, and a wrong code throws {@link MfaInvalidCodeError}.
343
+ * Because `MfaRequiredError` reveals that the password was right, it counts
344
+ * against the per-account and per-IP login budgets exactly like a failure
345
+ * (a later successful login with the code clears the account counter).
344
346
  *
345
347
  * `amr` lists the authentication methods used — `['pwd']`, or
346
348
  * `['pwd', 'mfa']` when a second factor was verified. It is also embedded in
package/dist/auth.js CHANGED
@@ -444,8 +444,10 @@ export class Auth {
444
444
  *
445
445
  * When the account has MFA enabled, `mfaCode` (a TOTP or recovery code) is
446
446
  * required: a correct password with a missing code throws
447
- * {@link MfaRequiredError} (without counting as a failed attempt), and a
448
- * wrong code throws {@link MfaInvalidCodeError}.
447
+ * {@link MfaRequiredError}, and a wrong code throws {@link MfaInvalidCodeError}.
448
+ * Because `MfaRequiredError` reveals that the password was right, it counts
449
+ * against the per-account and per-IP login budgets exactly like a failure
450
+ * (a later successful login with the code clears the account counter).
449
451
  *
450
452
  * `amr` lists the authentication methods used — `['pwd']`, or
451
453
  * `['pwd', 'mfa']` when a second factor was verified. It is also embedded in
@@ -475,11 +477,6 @@ export class Auth {
475
477
  throw error;
476
478
  }
477
479
  }
478
- const release = async () => {
479
- await this.throttle?.release(key);
480
- if (ipKey)
481
- await this.ipThrottle?.release(ipKey);
482
- };
483
480
  const user = await this.attempt(key, password);
484
481
  if (!user) {
485
482
  await this.hooks?.emit('auth:login_failed', { email: key });
@@ -488,7 +485,11 @@ export class Auth {
488
485
  const amr = ['pwd'];
489
486
  if (await this.isMfaEnabled(user.id)) {
490
487
  if (!mfaCode) {
491
- await release(); // password was correct — not a failure
488
+ // The password was right, and this answer says so — a password oracle
489
+ // on MFA accounts (the industry-standard two-step flow). Keep BOTH
490
+ // reservations (per-account and per-IP) so the oracle is throttled like
491
+ // any other guess: a success with the code resets the account counter
492
+ // and frees its own IP slot; this step's IP slot expires with the window.
492
493
  throw new MfaRequiredError();
493
494
  }
494
495
  if (!(await this.verifyMfaCode(user.id, mfaCode))) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@basaltkit/auth",
3
- "version": "4.0.0",
3
+ "version": "4.1.0",
4
4
  "engines": {
5
5
  "node": ">=22.5.0"
6
6
  },
@@ -19,7 +19,7 @@
19
19
  ],
20
20
  "dependencies": {
21
21
  "@basaltkit/core": "^1.5.0",
22
- "@basaltkit/http": "^2.6.0"
22
+ "@basaltkit/http": "^2.7.0"
23
23
  },
24
24
  "peerDependencies": {
25
25
  "zod": "^4.0.0"