@warlock.js/auth 5.51.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.
- package/CHANGELOG.md +50 -0
- package/esm/commands/auth-cleanup-command.mjs.map +1 -1
- package/esm/commands/auth-purge-never-expiring-command.mjs.map +1 -1
- package/esm/commands/jwt-secret-generator-command.mjs.map +1 -1
- package/esm/contracts/index.d.mts +3 -3
- package/esm/contracts/providers.d.mts +53 -0
- package/esm/contracts/providers.d.mts.map +1 -1
- package/esm/contracts/types.d.mts +71 -0
- package/esm/contracts/types.d.mts.map +1 -1
- package/esm/contracts/types.mjs.map +1 -1
- package/esm/errors/auth-provider-sdk-missing.error.mjs.map +1 -1
- package/esm/errors/email-not-verified.error.mjs.map +1 -1
- package/esm/errors/index.d.mts +2 -1
- package/esm/errors/index.mjs +2 -1
- package/esm/errors/invalid-one-time-token.error.mjs.map +1 -1
- package/esm/errors/invalid-passkey.error.mjs.map +1 -1
- package/esm/errors/invalid-provider-callback.error.mjs.map +1 -1
- package/esm/errors/notifications-unavailable.error.mjs.map +1 -1
- package/esm/errors/provider-account-link-blocked.error.d.mts +16 -0
- package/esm/errors/provider-account-link-blocked.error.d.mts.map +1 -0
- package/esm/errors/provider-account-link-blocked.error.mjs +22 -0
- package/esm/errors/provider-account-link-blocked.error.mjs.map +1 -0
- package/esm/errors/provider-email-not-verified.error.mjs.map +1 -1
- package/esm/index.d.mts +4 -3
- package/esm/index.mjs +2 -1
- package/esm/localization/ar.translations.mjs.map +1 -1
- package/esm/localization/auth-default-translations.mjs.map +1 -1
- package/esm/localization/en.translations.mjs.map +1 -1
- package/esm/localization/init.mjs.map +1 -1
- package/esm/localization/register-auth-translations.mjs.map +1 -1
- package/esm/middleware/auth.middleware.mjs +1 -1
- package/esm/middleware/auth.middleware.mjs.map +1 -1
- package/esm/middleware/csrf-origin-check.mjs.map +1 -1
- package/esm/middleware/localized-login-path.mjs.map +1 -1
- package/esm/middleware/login-throttle.middleware.d.mts +38 -8
- package/esm/middleware/login-throttle.middleware.d.mts.map +1 -1
- package/esm/middleware/login-throttle.middleware.mjs +95 -30
- package/esm/middleware/login-throttle.middleware.mjs.map +1 -1
- package/esm/middleware/one-time-token-throttle.middleware.mjs.map +1 -1
- package/esm/middleware/otp-throttle.middleware.mjs.map +1 -1
- package/esm/middleware/require-verified-email.middleware.mjs.map +1 -1
- package/esm/models/access-token/access-token.model.d.mts +14 -3
- package/esm/models/access-token/access-token.model.d.mts.map +1 -1
- package/esm/models/access-token/access-token.model.mjs +15 -8
- package/esm/models/access-token/access-token.model.mjs.map +1 -1
- package/esm/models/access-token/family-association.migration.mjs.map +1 -1
- package/esm/models/access-token/migration.mjs.map +1 -1
- package/esm/models/auth-token-family/auth-token-family.model.mjs.map +1 -1
- package/esm/models/auth-token-family/migration.mjs.map +1 -1
- package/esm/models/auth.model.d.mts +8 -0
- package/esm/models/auth.model.d.mts.map +1 -1
- package/esm/models/auth.model.mjs +3 -0
- package/esm/models/auth.model.mjs.map +1 -1
- package/esm/models/index.mjs.map +1 -1
- package/esm/models/one-time-token/migration.mjs.map +1 -1
- package/esm/models/one-time-token/one-time-token.model.d.mts +7 -2
- package/esm/models/one-time-token/one-time-token.model.d.mts.map +1 -1
- package/esm/models/one-time-token/one-time-token.model.mjs +8 -7
- package/esm/models/one-time-token/one-time-token.model.mjs.map +1 -1
- package/esm/models/passkey-credential/migration.mjs.map +1 -1
- package/esm/models/passkey-credential/passkey-credential.model.mjs.map +1 -1
- package/esm/models/provider-account/migration.mjs.map +1 -1
- package/esm/models/provider-account/provider-account.model.mjs.map +1 -1
- package/esm/models/refresh-token/migration.mjs.map +1 -1
- package/esm/models/refresh-token/refresh-token.model.d.mts +15 -3
- package/esm/models/refresh-token/refresh-token.model.d.mts.map +1 -1
- package/esm/models/refresh-token/refresh-token.model.mjs +17 -12
- package/esm/models/refresh-token/refresh-token.model.mjs.map +1 -1
- package/esm/models/refresh-token/successor-association.migration.mjs.map +1 -1
- package/esm/otp/otp.d.mts +5 -1
- package/esm/otp/otp.d.mts.map +1 -1
- package/esm/otp/otp.mjs +18 -3
- package/esm/otp/otp.mjs.map +1 -1
- package/esm/passkeys/passkey-authentication.mjs +2 -2
- package/esm/passkeys/passkey-authentication.mjs.map +1 -1
- package/esm/passkeys/passkey-challenges.mjs.map +1 -1
- package/esm/passkeys/passkey-registration.d.mts.map +1 -1
- package/esm/passkeys/passkey-registration.mjs +2 -2
- package/esm/passkeys/passkey-registration.mjs.map +1 -1
- package/esm/passkeys/simplewebauthn.mjs.map +1 -1
- package/esm/providers/apple-provider.d.mts +2 -0
- package/esm/providers/apple-provider.d.mts.map +1 -1
- package/esm/providers/apple-provider.mjs +4 -1
- package/esm/providers/apple-provider.mjs.map +1 -1
- package/esm/providers/discord-provider.d.mts +2 -0
- package/esm/providers/discord-provider.d.mts.map +1 -1
- package/esm/providers/discord-provider.mjs +7 -1
- package/esm/providers/discord-provider.mjs.map +1 -1
- package/esm/providers/facebook-provider.d.mts +2 -0
- package/esm/providers/facebook-provider.d.mts.map +1 -1
- package/esm/providers/facebook-provider.mjs +10 -2
- package/esm/providers/facebook-provider.mjs.map +1 -1
- package/esm/providers/github-provider.d.mts +2 -0
- package/esm/providers/github-provider.d.mts.map +1 -1
- package/esm/providers/github-provider.mjs +19 -10
- package/esm/providers/github-provider.mjs.map +1 -1
- package/esm/providers/google-provider.mjs.map +1 -1
- package/esm/providers/linkedin-provider.d.mts +2 -0
- package/esm/providers/linkedin-provider.d.mts.map +1 -1
- package/esm/providers/linkedin-provider.mjs +4 -1
- package/esm/providers/linkedin-provider.mjs.map +1 -1
- package/esm/providers/provider-login.mjs.map +1 -1
- package/esm/providers/provider-state-cookie.mjs.map +1 -1
- package/esm/providers/provider-timeout.mjs +17 -0
- package/esm/providers/provider-timeout.mjs.map +1 -0
- package/esm/providers/resolve-provider-user.mjs +21 -1
- package/esm/providers/resolve-provider-user.mjs.map +1 -1
- package/esm/providers/x-provider.d.mts +2 -0
- package/esm/providers/x-provider.d.mts.map +1 -1
- package/esm/providers/x-provider.mjs +7 -1
- package/esm/providers/x-provider.mjs.map +1 -1
- package/esm/services/auth-config.mjs +68 -6
- package/esm/services/auth-config.mjs.map +1 -1
- package/esm/services/auth-cookie-names.mjs +49 -0
- package/esm/services/auth-cookie-names.mjs.map +1 -0
- package/esm/services/auth-events.d.mts +4 -0
- package/esm/services/auth-events.d.mts.map +1 -1
- package/esm/services/auth-events.mjs.map +1 -1
- package/esm/services/auth.service.d.mts +14 -0
- package/esm/services/auth.service.d.mts.map +1 -1
- package/esm/services/auth.service.mjs +163 -21
- package/esm/services/auth.service.mjs.map +1 -1
- package/esm/services/current-user.mjs.map +1 -1
- package/esm/services/email-verification.mjs.map +1 -1
- package/esm/services/generate-jwt-secret.mjs.map +1 -1
- package/esm/services/jwt.d.mts.map +1 -1
- package/esm/services/jwt.mjs +92 -10
- package/esm/services/jwt.mjs.map +1 -1
- package/esm/services/one-time-tokens.mjs.map +1 -1
- package/esm/services/optional-peer.mjs.map +1 -1
- package/esm/services/password-reset.d.mts +5 -0
- package/esm/services/password-reset.d.mts.map +1 -1
- package/esm/services/password-reset.mjs +15 -1
- package/esm/services/password-reset.mjs.map +1 -1
- package/esm/services/resolve-request-user.d.mts.map +1 -1
- package/esm/services/resolve-request-user.mjs +17 -3
- package/esm/services/resolve-request-user.mjs.map +1 -1
- package/esm/services/token-family-operation.mjs.map +1 -1
- package/esm/services/token-notifications.mjs.map +1 -1
- package/esm/session/page-session.d.mts.map +1 -1
- package/esm/session/page-session.mjs +5 -3
- package/esm/session/page-session.mjs.map +1 -1
- package/esm/utils/auth-error-codes.d.mts +8 -1
- package/esm/utils/auth-error-codes.d.mts.map +1 -1
- package/esm/utils/auth-error-codes.mjs +7 -0
- package/esm/utils/auth-error-codes.mjs.map +1 -1
- package/esm/utils/normalize-credentials.mjs +32 -0
- package/esm/utils/normalize-credentials.mjs.map +1 -0
- package/esm/utils/safe-equal.mjs.map +1 -1
- package/esm/utils/token-digest.mjs +47 -0
- package/esm/utils/token-digest.mjs.map +1 -0
- package/esm/utils/token-expiry.d.mts +3 -1
- package/esm/utils/token-expiry.d.mts.map +1 -1
- package/esm/utils/token-expiry.mjs +7 -2
- package/esm/utils/token-expiry.mjs.map +1 -1
- package/llms-full.txt +72 -30
- package/llms.txt +2 -2
- package/package.json +9 -9
- package/skills/auth-basics/SKILL.md +9 -0
- package/skills/customize-token-storage/SKILL.md +10 -10
- package/skills/handle-login-and-logout/SKILL.md +2 -0
- package/skills/login-with-providers/SKILL.md +14 -3
- package/skills/manage-tokens/SKILL.md +6 -0
- package/skills/protect-routes/SKILL.md +5 -0
- package/skills/throttle-login-attempts/SKILL.md +23 -17
- package/skills/verify-email-and-reset-password/SKILL.md +3 -0
|
@@ -67,6 +67,9 @@ A token passes three separate checks, in order — a token can fail any one of t
|
|
|
67
67
|
1. **The JWT verifies** — signature, algorithm, `tokenType`, and (since 4.12.0) the presence of an `exp` claim that has not passed. A token with _no_ `exp` is rejected: there is no deadline to check, so it would otherwise verify indefinitely.
|
|
68
68
|
2. **The access-token row still exists** — deleting it (logout) invalidates the token immediately, before its JWT expiry.
|
|
69
69
|
3. **The row's own `expires_at` has not passed** (since 4.12.0). The database is the authority on the session: a row whose expiry has elapsed is rejected and deleted, even if the JWT itself is still within its lifetime. A row with a missing or unparseable `expires_at` is treated as expired, not as never-expiring.
|
|
70
|
+
4. **The claims name the row's user** (since 5.53): the token's `id` (and `userType`, when present) must equal the row's `user_id` / `user_type`, or it is `InvalidAccessToken`.
|
|
71
|
+
|
|
72
|
+
Expired or malformed tokens and CSRF Origin mismatches are logged at `debug`, not `error` — any anonymous client can produce them.
|
|
70
73
|
|
|
71
74
|
Missing or invalid credentials return 401; an authenticated disallowed user type returns 403:
|
|
72
75
|
|
|
@@ -269,6 +272,8 @@ export default {
|
|
|
269
272
|
|
|
270
273
|
The cookie name `setAuthCookie`/`clearAuthCookie` write **must match** the name a `cookie:<name>` token source reads — they are not linked automatically.
|
|
271
274
|
|
|
275
|
+
`auth.cookie.hostPrefix: true` (default `false`) writes the auth cookies as `__Host-<name>` with `Secure`, so a subdomain or plain-HTTP page cannot plant or overwrite them. It requires `path: "/"` and no Domain (`cookie.cookieDomain`, `options.domain`, `http.cookies.options.domain`); a write that breaks this throws naming the setting. Reads (`cookie:<name>` sources, `pageSession`) try `__Host-<name>` first and fall back to `<name>`, and the clear helpers remove both, so switching it on signs no one out.
|
|
276
|
+
|
|
272
277
|
### CSRF: an automatic Origin check on cookie-authenticated writes
|
|
273
278
|
|
|
274
279
|
Cookie auth changes the CSRF picture that bearer-token auth doesn't have: a browser attaches cookies to same-site requests it didn't originate. `SameSite=Lax` already blocks the classic cross-site `<form method="post">` case, but not a same-site GET redirect chain or a client that ignores `SameSite`.
|
|
@@ -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
|
|
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_
|
|
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
|
|
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 |
|
|
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.**
|
|
137
|
-
- **Counts are atomic and exact across servers (redis/pg).** Each
|
|
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
|
};
|