@warlock.js/auth 5.52.0 → 5.53.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.
Files changed (164) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/esm/commands/auth-cleanup-command.mjs.map +1 -1
  3. package/esm/commands/auth-purge-never-expiring-command.mjs.map +1 -1
  4. package/esm/commands/jwt-secret-generator-command.mjs.map +1 -1
  5. package/esm/contracts/index.d.mts +2 -2
  6. package/esm/contracts/providers.d.mts +53 -0
  7. package/esm/contracts/providers.d.mts.map +1 -1
  8. package/esm/contracts/types.d.mts +53 -0
  9. package/esm/contracts/types.d.mts.map +1 -1
  10. package/esm/contracts/types.mjs.map +1 -1
  11. package/esm/errors/auth-provider-sdk-missing.error.mjs.map +1 -1
  12. package/esm/errors/email-not-verified.error.mjs.map +1 -1
  13. package/esm/errors/index.d.mts +2 -1
  14. package/esm/errors/index.mjs +2 -1
  15. package/esm/errors/invalid-one-time-token.error.mjs.map +1 -1
  16. package/esm/errors/invalid-passkey.error.mjs.map +1 -1
  17. package/esm/errors/invalid-provider-callback.error.mjs.map +1 -1
  18. package/esm/errors/notifications-unavailable.error.mjs.map +1 -1
  19. package/esm/errors/provider-account-link-blocked.error.d.mts +16 -0
  20. package/esm/errors/provider-account-link-blocked.error.d.mts.map +1 -0
  21. package/esm/errors/provider-account-link-blocked.error.mjs +22 -0
  22. package/esm/errors/provider-account-link-blocked.error.mjs.map +1 -0
  23. package/esm/errors/provider-email-not-verified.error.mjs.map +1 -1
  24. package/esm/index.d.mts +3 -2
  25. package/esm/index.mjs +2 -1
  26. package/esm/localization/ar.translations.mjs.map +1 -1
  27. package/esm/localization/auth-default-translations.mjs.map +1 -1
  28. package/esm/localization/en.translations.mjs.map +1 -1
  29. package/esm/localization/init.mjs.map +1 -1
  30. package/esm/localization/register-auth-translations.mjs.map +1 -1
  31. package/esm/middleware/auth.middleware.mjs +1 -1
  32. package/esm/middleware/auth.middleware.mjs.map +1 -1
  33. package/esm/middleware/csrf-origin-check.mjs.map +1 -1
  34. package/esm/middleware/localized-login-path.mjs.map +1 -1
  35. package/esm/middleware/login-throttle.middleware.d.mts +38 -8
  36. package/esm/middleware/login-throttle.middleware.d.mts.map +1 -1
  37. package/esm/middleware/login-throttle.middleware.mjs +93 -29
  38. package/esm/middleware/login-throttle.middleware.mjs.map +1 -1
  39. package/esm/middleware/one-time-token-throttle.middleware.mjs.map +1 -1
  40. package/esm/middleware/otp-throttle.middleware.mjs.map +1 -1
  41. package/esm/middleware/require-verified-email.middleware.mjs.map +1 -1
  42. package/esm/models/access-token/access-token.model.d.mts +14 -3
  43. package/esm/models/access-token/access-token.model.d.mts.map +1 -1
  44. package/esm/models/access-token/access-token.model.mjs +15 -8
  45. package/esm/models/access-token/access-token.model.mjs.map +1 -1
  46. package/esm/models/access-token/family-association.migration.mjs.map +1 -1
  47. package/esm/models/access-token/migration.mjs.map +1 -1
  48. package/esm/models/auth-token-family/auth-token-family.model.mjs.map +1 -1
  49. package/esm/models/auth-token-family/migration.mjs.map +1 -1
  50. package/esm/models/auth.model.d.mts +8 -0
  51. package/esm/models/auth.model.d.mts.map +1 -1
  52. package/esm/models/auth.model.mjs +3 -0
  53. package/esm/models/auth.model.mjs.map +1 -1
  54. package/esm/models/index.mjs.map +1 -1
  55. package/esm/models/one-time-token/migration.mjs.map +1 -1
  56. package/esm/models/one-time-token/one-time-token.model.d.mts +7 -2
  57. package/esm/models/one-time-token/one-time-token.model.d.mts.map +1 -1
  58. package/esm/models/one-time-token/one-time-token.model.mjs +8 -7
  59. package/esm/models/one-time-token/one-time-token.model.mjs.map +1 -1
  60. package/esm/models/passkey-credential/migration.mjs.map +1 -1
  61. package/esm/models/passkey-credential/passkey-credential.model.mjs.map +1 -1
  62. package/esm/models/provider-account/migration.mjs.map +1 -1
  63. package/esm/models/provider-account/provider-account.model.mjs.map +1 -1
  64. package/esm/models/refresh-token/migration.mjs.map +1 -1
  65. package/esm/models/refresh-token/refresh-token.model.d.mts +15 -3
  66. package/esm/models/refresh-token/refresh-token.model.d.mts.map +1 -1
  67. package/esm/models/refresh-token/refresh-token.model.mjs +17 -12
  68. package/esm/models/refresh-token/refresh-token.model.mjs.map +1 -1
  69. package/esm/models/refresh-token/successor-association.migration.mjs.map +1 -1
  70. package/esm/otp/otp.d.mts +5 -1
  71. package/esm/otp/otp.d.mts.map +1 -1
  72. package/esm/otp/otp.mjs +18 -3
  73. package/esm/otp/otp.mjs.map +1 -1
  74. package/esm/passkeys/passkey-authentication.mjs +2 -2
  75. package/esm/passkeys/passkey-authentication.mjs.map +1 -1
  76. package/esm/passkeys/passkey-challenges.mjs.map +1 -1
  77. package/esm/passkeys/passkey-registration.d.mts.map +1 -1
  78. package/esm/passkeys/passkey-registration.mjs +2 -2
  79. package/esm/passkeys/passkey-registration.mjs.map +1 -1
  80. package/esm/passkeys/simplewebauthn.mjs.map +1 -1
  81. package/esm/providers/apple-provider.d.mts +2 -0
  82. package/esm/providers/apple-provider.d.mts.map +1 -1
  83. package/esm/providers/apple-provider.mjs +4 -1
  84. package/esm/providers/apple-provider.mjs.map +1 -1
  85. package/esm/providers/discord-provider.d.mts +2 -0
  86. package/esm/providers/discord-provider.d.mts.map +1 -1
  87. package/esm/providers/discord-provider.mjs +7 -1
  88. package/esm/providers/discord-provider.mjs.map +1 -1
  89. package/esm/providers/facebook-provider.d.mts +2 -0
  90. package/esm/providers/facebook-provider.d.mts.map +1 -1
  91. package/esm/providers/facebook-provider.mjs +10 -2
  92. package/esm/providers/facebook-provider.mjs.map +1 -1
  93. package/esm/providers/github-provider.d.mts +2 -0
  94. package/esm/providers/github-provider.d.mts.map +1 -1
  95. package/esm/providers/github-provider.mjs +19 -10
  96. package/esm/providers/github-provider.mjs.map +1 -1
  97. package/esm/providers/google-provider.mjs.map +1 -1
  98. package/esm/providers/linkedin-provider.d.mts +2 -0
  99. package/esm/providers/linkedin-provider.d.mts.map +1 -1
  100. package/esm/providers/linkedin-provider.mjs +4 -1
  101. package/esm/providers/linkedin-provider.mjs.map +1 -1
  102. package/esm/providers/provider-login.mjs.map +1 -1
  103. package/esm/providers/provider-state-cookie.mjs.map +1 -1
  104. package/esm/providers/provider-timeout.mjs +17 -0
  105. package/esm/providers/provider-timeout.mjs.map +1 -0
  106. package/esm/providers/resolve-provider-user.mjs +21 -1
  107. package/esm/providers/resolve-provider-user.mjs.map +1 -1
  108. package/esm/providers/x-provider.d.mts +2 -0
  109. package/esm/providers/x-provider.d.mts.map +1 -1
  110. package/esm/providers/x-provider.mjs +7 -1
  111. package/esm/providers/x-provider.mjs.map +1 -1
  112. package/esm/services/auth-config.mjs +65 -6
  113. package/esm/services/auth-config.mjs.map +1 -1
  114. package/esm/services/auth-cookie-names.mjs +49 -0
  115. package/esm/services/auth-cookie-names.mjs.map +1 -0
  116. package/esm/services/auth-events.d.mts +4 -0
  117. package/esm/services/auth-events.d.mts.map +1 -1
  118. package/esm/services/auth-events.mjs.map +1 -1
  119. package/esm/services/auth.service.d.mts +8 -0
  120. package/esm/services/auth.service.d.mts.map +1 -1
  121. package/esm/services/auth.service.mjs +146 -20
  122. package/esm/services/auth.service.mjs.map +1 -1
  123. package/esm/services/current-user.mjs.map +1 -1
  124. package/esm/services/email-verification.mjs.map +1 -1
  125. package/esm/services/generate-jwt-secret.mjs.map +1 -1
  126. package/esm/services/jwt.d.mts.map +1 -1
  127. package/esm/services/jwt.mjs +92 -10
  128. package/esm/services/jwt.mjs.map +1 -1
  129. package/esm/services/one-time-tokens.mjs.map +1 -1
  130. package/esm/services/optional-peer.mjs.map +1 -1
  131. package/esm/services/password-reset.d.mts +5 -0
  132. package/esm/services/password-reset.d.mts.map +1 -1
  133. package/esm/services/password-reset.mjs +15 -1
  134. package/esm/services/password-reset.mjs.map +1 -1
  135. package/esm/services/resolve-request-user.d.mts.map +1 -1
  136. package/esm/services/resolve-request-user.mjs +17 -3
  137. package/esm/services/resolve-request-user.mjs.map +1 -1
  138. package/esm/services/token-family-operation.mjs.map +1 -1
  139. package/esm/services/token-notifications.mjs.map +1 -1
  140. package/esm/session/page-session.d.mts.map +1 -1
  141. package/esm/session/page-session.mjs +5 -3
  142. package/esm/session/page-session.mjs.map +1 -1
  143. package/esm/utils/auth-error-codes.d.mts +8 -1
  144. package/esm/utils/auth-error-codes.d.mts.map +1 -1
  145. package/esm/utils/auth-error-codes.mjs +7 -0
  146. package/esm/utils/auth-error-codes.mjs.map +1 -1
  147. package/esm/utils/normalize-credentials.mjs.map +1 -1
  148. package/esm/utils/safe-equal.mjs.map +1 -1
  149. package/esm/utils/token-digest.mjs +47 -0
  150. package/esm/utils/token-digest.mjs.map +1 -0
  151. package/esm/utils/token-expiry.d.mts +3 -1
  152. package/esm/utils/token-expiry.d.mts.map +1 -1
  153. package/esm/utils/token-expiry.mjs +7 -2
  154. package/esm/utils/token-expiry.mjs.map +1 -1
  155. package/llms-full.txt +70 -30
  156. package/llms.txt +2 -2
  157. package/package.json +9 -9
  158. package/skills/auth-basics/SKILL.md +9 -0
  159. package/skills/customize-token-storage/SKILL.md +10 -10
  160. package/skills/login-with-providers/SKILL.md +14 -3
  161. package/skills/manage-tokens/SKILL.md +6 -0
  162. package/skills/protect-routes/SKILL.md +5 -0
  163. package/skills/throttle-login-attempts/SKILL.md +23 -17
  164. package/skills/verify-email-and-reset-password/SKILL.md +3 -0
@@ -1,13 +1,13 @@
1
1
  ---
2
2
  name: throttle-login-attempts
3
- description: 'Brute-force / credential-stuffing protection via `loginThrottleMiddleware` — a failure-aware route gate that counts only failed logins (resets on success), locks per-account and per-source after a threshold, and rejects pre-controller with 429 so the DB lookup and bcrypt verify are skipped. Cache-backed (shared across replicas), fixed-window, fails open on a cache outage. Triggers: `loginThrottleMiddleware`, `AuthErrorCodes.TooManyAttempts`, `EC004`, "rate limit login", "brute force protection", "lock account after failed logins", "throttle login attempts", "too many login attempts 429"; typical import `import { loginThrottleMiddleware } from "@warlock.js/auth"`. Skip: generic per-route request rate limiting that counts every request (use core `middleware.rateLimit`); gating a route by auth — the `protect-routes` topic; issuing tokens — the `handle-login-and-logout` topic.'
3
+ description: 'Brute-force / credential-stuffing protection via `loginThrottleMiddleware` — a failure-aware route gate that counts each attempt as it arrives and resets on a successful login, locks per-account and per-source after a threshold, and rejects pre-controller with 429 so the DB lookup and bcrypt verify are skipped. Cache-backed (shared across replicas), fixed-window, fails open on a cache outage. Triggers: `loginThrottleMiddleware`, `AuthErrorCodes.TooManyAttempts`, `EC004`, "rate limit login", "brute force protection", "lock account after failed logins", "throttle login attempts", "too many login attempts 429"; typical import `import { loginThrottleMiddleware } from "@warlock.js/auth"`. Skip: generic per-route request rate limiting that counts every request (use core `middleware.rateLimit`); gating a route by auth — the `protect-routes` topic; issuing tokens — the `handle-login-and-logout` topic.'
4
4
  ---
5
5
 
6
6
  # Throttle login attempts with `loginThrottleMiddleware`
7
7
 
8
8
  `loginThrottleMiddleware(options?)` returns a Warlock middleware that defends the login (and refresh / password-reset) routes against brute-force and credential-stuffing — without punishing a user who fat-fingers a password then gets it right.
9
9
 
10
- The trick that makes it different from a plain rate limiter: it's **failure-aware**. It hooks the response _after_ your controller runs (`response.onSent`), counts only the attempts that actually failed, and **clears the counter the moment a login succeeds**. A generic request limiter can't do that — it runs before the controller and counts every hit, success or not.
10
+ The trick that makes it different from a plain rate limiter: it's **failure-aware**. It counts each attempt before your controller runs (so concurrent bursts cannot outrun the limit), hooks the response _after_ the controller (`response.onSent`), locks only when a failure used the last allowed attempt, and **clears the counter the moment a login succeeds**. A generic request limiter can't do that — it counts every hit and never resets on success.
11
11
 
12
12
  ## The shortest version
13
13
 
@@ -54,8 +54,8 @@ router.post("/auth/reset-password", resetPasswordController, {
54
54
 
55
55
  ## How it behaves
56
56
 
57
- 1. **Before the controller** — if the account or the IP is currently locked, it short-circuits with `429` and never touches the database or the bcrypt verify (this is also what neutralises the CPU-DoS angle of brute-forcing). The body carries `AuthErrorCodes.TooManyAttempts` (`EC004`).
58
- 2. **After the controller** — it inspects the outcome. A non-2xx response is a failed login: it bumps a fixed-window counter for each tracked identifier, and once a counter reaches `max` it writes a lock key. A 2xx response is a success: it **clears** the counter and lock for that identifier.
57
+ 1. **Before the controller** — if the account or the IP is currently locked, it short-circuits with `429` and never touches the database or the bcrypt verify (this is also what neutralises the CPU-DoS angle of brute-forcing). The body carries `AuthErrorCodes.TooManyAttempts` (`EC004`). Otherwise it **counts the attempt right away** (fixed-window counter per identifier) and answers `429` once the count passes `max`, so a burst of concurrent requests cannot outrun the limit: at most `max` attempts per window reach the controller.
58
+ 2. **After the controller** — it inspects the outcome. A non-2xx response that used the last allowed attempt writes the lock key (the counter is rewritten with the lock's TTL so both end together), unless a success that finished meanwhile already reset the counter. A 2xx response is a success: it **resets** the counter to 0 and clears the lock for that identifier. If the counter cannot be read back (expired, evicted, a failed read), the failure locks: the check fails closed.
59
59
 
60
60
  Per-account tracking stops a password-spray against one user from many IPs; per-source tracking stops one IP from stuffing many accounts. A lock on **either** identifier rejects the request — defense-in-depth.
61
61
 
@@ -84,16 +84,22 @@ loginThrottleMiddleware({
84
84
  });
85
85
  ```
86
86
 
87
- | Option | Default | Purpose |
88
- | ----------------- | ---------------------------------- | ----------------------------------------- |
89
- | `max` | `5` | Failures within the window before lockout |
90
- | `window` | `"15m"` | Counting window (`ms`-string or seconds) |
91
- | `lockoutDuration` | `"15m"` | Lock TTL once tripped |
92
- | `by` | `["email", "ip"]` | Identifiers tracked independently |
93
- | `identifierKey` | `"email"` | Credential field used as the account key |
94
- | `errorMessage` | i18n `auth.errors.tooManyAttempts` | 429 message override |
95
- | `isFailure` | `(res) => !res.isOk` | What counts as a failed attempt |
96
- | `identify` | built-in email + ip extraction | Custom identifier list |
87
+ | Option | Default | Purpose |
88
+ | ----------------- | ---------------------------------- | --------------------------------------------------------------------- |
89
+ | `max` | `5` | Failures within the window before lockout |
90
+ | `window` | `"15m"` | Counting window (`ms`-string or seconds) |
91
+ | `lockoutDuration` | `"15m"` | Lock TTL once tripped |
92
+ | `by` | `["email", "ip"]` | Identifiers tracked independently |
93
+ | `identifierKey` | `"email"` | Credential field used as the account key |
94
+ | `errorMessage` | i18n `auth.errors.tooManyAttempts` | 429 message override |
95
+ | `isFailure` | `(res) => !res.isOk` | What counts as a failed attempt |
96
+ | `identify` | built-in email + ip extraction | Custom identifier list |
97
+ | `emailLock` | `"global"` | `"pair"` locks the (email, IP) pair instead of the email for everyone |
98
+ | `accountMax` | `max * 10` | Email-wide limit across all IPs when `emailLock: "pair"` |
99
+
100
+ `emailLock: "pair"` stops a stranger from locking a known account out: their `max` failures lock only their own (email, IP) pair, while the email-wide bucket (`accountMax`) still stops guessing spread over many IPs. It applies when `by` tracks both `"email"` and `"ip"` and no `identify` is set.
101
+
102
+ Throttle the anonymous passkey options route the same way, counting every request: `loginThrottleMiddleware({ by: ["ip"], isFailure: () => true, max: 30, window: "1m" })`.
97
103
 
98
104
  ## The 429 response
99
105
 
@@ -123,7 +129,7 @@ router.post("/auth/forgot-password", forgotController, {
123
129
 
124
130
  | | `loginThrottleMiddleware` (auth) | `middleware.rateLimit` (core) |
125
131
  | ---------------------------- | -------------------------------------------- | ----------------------------- |
126
- | Counts | only **failed** logins | **every** request |
132
+ | Counts | every attempt, until a login succeeds | **every** request |
127
133
  | Resets on a successful login | yes | no |
128
134
  | Locks per account / per IP | yes | per key (default IP) |
129
135
  | Storage | `@warlock.js/cache` (shared across replicas) | in-process map |
@@ -133,8 +139,8 @@ They compose. For a hard ceiling on request volume _and_ failure-aware account l
133
139
  ## Gotchas
134
140
 
135
141
  - **Needs an initialised cache driver.** Storage is `@warlock.js/cache` (a peer dep, transitively present via core). If the driver isn't configured at runtime, the middleware **fails open** — it logs and lets the request through, because a throttle outage must never become an auth outage. That also means: no cache, no protection. Verify your cache config in production.
136
- - **Account lockout can be weaponised.** An attacker who knows a victim's email can lock them out by spamming failures. The per-IP counter mitigates this (the attacker's IP locks too), but for purely anonymous endpoints prefer `by: ["ip"]`. For high-value accounts, consider a CAPTCHA step instead of a hard lock.
137
- - **Counts are atomic and exact across servers (redis/pg).** Each failure does a create-only `cache.set(key, 0, { ttl: window })` (only the first failure opens the window and sets the TTL) followed by an atomic `cache.increment(key, 1, { ttl: window })`, so concurrent failures on different servers are never lost. Passing the TTL on the increment also expires a counter recreated after its seed expires, without extending an existing window. The memory cache driver is per-process — counts and locks are then per-process, so use redis/pg when running more than one instance.
142
+ - **Account lockout can be weaponised.** With the default `emailLock: "global"`, an attacker who knows a victim's email can lock them out by spamming failures. Set `emailLock: "pair"` to lock the (email, IP) pair instead; for purely anonymous endpoints prefer `by: ["ip"]`. For high-value accounts, consider a CAPTCHA step instead of a hard lock.
143
+ - **Counts are atomic and exact across servers (redis/pg).** Each attempt does a create-only `cache.set(key, 0, { ttl: window })` (only the first failure opens the window and sets the TTL) followed by an atomic `cache.increment(key, 1, { ttl: window })`, so concurrent failures on different servers are never lost. Passing the TTL on the increment also expires a counter recreated after its seed expires, without extending an existing window. The memory cache driver is per-process — counts and locks are then per-process, so use redis/pg when running more than one instance.
138
144
  - **Account and IP keys are hashed.** The built-in extractor keys both by an HMAC of the trimmed, lower-cased value (`email.<hmac>`, `ip.<hmac>`), keyed from `auth.accessToken.secret`, so cache logs never show the address or IP and a list of known emails cannot reverse the key. Without a configured secret it falls back to SHA-256. A custom `identify` is used as is — hash personal values yourself (`throttleIdentifierHash(kind, value)` is exported).
139
145
  - **Fixed window, not sliding.** The window is anchored at the first failure and does not extend on each subsequent one — predictable lockout timing. The lock key is independent and always lasts `lockoutDuration`: the lock stores its own deadline, so even a sliding-expiry driver (`memoryExtended`) cannot extend it.
140
146
  - **Needs a real cache driver.** The `null` driver stores nothing, so the throttle never locks; it warns once when that happens. On Redis, any version from 6 works.
@@ -86,6 +86,8 @@ router.post("/auth/reset-password", resetPasswordController, {
86
86
  });
87
87
  ```
88
88
 
89
+ `requestPasswordReset` sends the notification **in the background**, so a known account answers as fast as an unknown one (no SMTP round trip on the response path). A failed send is logged and emitted as `password.resetDeliveryFailed` (`(user, error)`); set `auth.passwordReset.awaitDelivery: true` (default `false`) to await it and get its error instead.
90
+
89
91
  `resetPassword` writes the password, then calls `authService.revokeAllTokens(user)`: every refresh token is revoked and every access token deleted, which also kills cookie sessions (a cookie holds an access token). It emits `password.reset`; `requestPasswordReset` emits `password.resetRequested`.
90
92
 
91
93
  **Password hashing.** By default auth saves `hashPassword(plain)` and checks it with `verifyPassword`, the same check login uses. If your model hashes on save (`useHashedPassword()`), that first write gets hashed twice and fails the check, so auth saves the plaintext and lets your transformer hash it once. If neither write passes the check, it throws. Plaintext is never the first thing written. To write it yourself, set `auth.passwordReset.setPassword`.
@@ -118,6 +120,7 @@ const authConfigurations: AuthConfigurations = {
118
120
  setPassword: async (user, plain) => {
119
121
  /* optional */
120
122
  },
123
+ awaitDelivery: false, // default: send in the background (anti-enumeration timing)
121
124
  },
122
125
  oneTimeToken: { model: AppOneTimeToken }, // optional model override
123
126
  };