@okeav/idp-pg 0.1.0 → 0.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/CHANGELOG.md CHANGED
@@ -3,6 +3,11 @@
3
3
  Each release names the `@okeav/idp-core` commit its identity-provider code is in step with. A
4
4
  security fix in either package must be applied to the other.
5
5
 
6
+ ## Unreleased
7
+
8
+ - `config.rateLimiting.limiter`: pass your own rate limiter instead of an adapter. Validated at startup; refused together with
9
+ `adapter: 'redis'`. Keys carry `keyPrefix` as with the adapters, and its errors fail closed as theirs do.
10
+
6
11
  ## 0.1.0 — 2026-11-03
7
12
 
8
13
  In step with `@okeav/idp-core` 0.4.0 at `f30273c`.
package/README.md CHANGED
@@ -162,7 +162,7 @@ The MFA challenge limit covers both `/mfa/verify` and `/webauthn/mfa/verify`. Se
162
162
 
163
163
  Override any rule under `config.rateLimiting.<rule>` — each takes `{ max, windowSeconds }` (positive integers). Set `config.rateLimiting.enabled = false` to disable entirely if you already rate-limit at a gateway/CDN layer (e.g. Cloudflare, an API gateway) covering every one of these endpoints — running it twice is redundant, not harmful, but the extra storage round-trip on every request is pure overhead if a layer in front of this service already enforces it.
164
164
 
165
- Per-IP keys collapse IPv6 addresses to their /64; per-address keys use the email's keyed hash, so Redis and logs never hold email addresses. Uses the same adapter pattern as the cache layer: `config.rateLimiting.adapter` is `'memory'` (default, single-process, capped at 500k keys — when full, the oldest counters that haven't reached their limit are evicted (never an exhausted one), and a new key is refused — failing closed — only if every counter is exhausted) or `'redis'` (shares the cache adapter's Redis connection automatically if `config.cache.adapter` is also `'redis'`, otherwise set `config.rateLimiting.redis` separately — startup fails rather than connecting to an unconfigured localhost).
165
+ Per-IP keys collapse IPv6 addresses to their /64; per-address keys use the email's keyed hash, so Redis and logs never hold email addresses. Uses the same adapter pattern as the cache layer: `config.rateLimiting.adapter` is `'memory'` (default, single-process, capped at 500k keys — when full, the oldest counters that haven't reached their limit are evicted (never an exhausted one), and a new key is refused — failing closed — only if every counter is exhausted) or `'redis'` (shares the cache adapter's Redis connection automatically if `config.cache.adapter` is also `'redis'`, otherwise set `config.rateLimiting.redis` separately — startup fails rather than connecting to an unconfigured localhost). Or pass a limiter of your own as `config.rateLimiting.limiter` (`check`, `increment` and `reset`, as the exported `RateLimiter` type; `increment` must be atomic), for instance one counting in your own database (it replaces the memory adapter, and is refused with `adapter: 'redis'`).
166
166
 
167
167
  **Backend errors fail closed everywhere except refresh.** If the rate limiter's backend errors (Redis down, timeout), every limited request is rejected with 503 `RATE_LIMITER_UNAVAILABLE` — an attacker who can knock the limiter over must not get unlimited guesses. Refresh fails open: the error is logged and the request proceeds.
168
168
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@okeav/idp-pg",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Standalone identity provider for Node.js on PostgreSQL — OAuth2/OIDC authorization server, password + MFA + passkeys + social login, session management and service-to-service JWKS trust. Built from @okeav/idp-core with a native PostgreSQL store.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -11,16 +11,19 @@ export { NoopRateLimiter } from './noop.adapter.js';
11
11
  * @param {object} [config]
12
12
  * @param {boolean} [config.enabled=true] - set false to disable entirely (e.g. you already rate-limit at a gateway/CDN)
13
13
  * @param {'memory'|'redis'} [config.adapter='memory']
14
+ * @param {import('./rate-limiter.js').RateLimiter} [config.limiter] - the app's own limiter (e.g. counting in its database), instead of an adapter
14
15
  * @param {string} [config.keyPrefix='ratelimit:']
15
16
  * @param {object} [config.redis] - { host, port, password, db } — only read if adapter === 'redis' and no shared client is available
16
17
  * @param {import('ioredis').Redis} [sharedRedisClient] - reused automatically when config.cache.adapter === 'redis' and rateLimiting.adapter is also 'redis' with no separate connection details of its own
17
18
  * @returns {Promise<import('./rate-limiter.js').RateLimiter>}
18
19
  */
19
20
  export async function createRateLimiter(config = {}, sharedRedisClient = null) {
21
+ assertLimiter(config);
20
22
  if (config.enabled === false) return prefixed(new NoopRateLimiter(), '');
21
23
 
22
- const adapter = config.adapter || 'memory';
23
24
  const prefix = config.keyPrefix ?? 'ratelimit:';
25
+ if (config.limiter !== undefined) return prefixed(config.limiter, prefix);
26
+ const adapter = config.adapter || 'memory';
24
27
 
25
28
  let raw;
26
29
  if (adapter === 'redis') {
@@ -38,6 +41,16 @@ export async function createRateLimiter(config = {}, sharedRedisClient = null) {
38
41
  return prefixed(raw, prefix);
39
42
  }
40
43
 
44
+ /** A limiter that can't count would fail every sign-in, or none: refused at startup, whether or not limiting is enabled. */
45
+ function assertLimiter({ limiter, adapter }) {
46
+ if (limiter === undefined) return;
47
+ // 'memory' is what an unset adapter resolves to, so only a chosen Redis contradicts a limiter.
48
+ if (adapter === 'redis') throw new Error("config.rateLimiting takes a limiter or adapter 'redis', not both");
49
+ for (const method of ['check', 'increment', 'reset']) {
50
+ if (typeof limiter?.[method] !== 'function') throw new Error(`config.rateLimiting.limiter needs a ${method}() method`);
51
+ }
52
+ }
53
+
41
54
  function prefixed(limiter, prefix) {
42
55
  const k = (key) => `${prefix}${key}`;
43
56
  return {
package/types/index.d.ts CHANGED
@@ -302,6 +302,12 @@ export interface IdpConfig {
302
302
  rateLimiting?: {
303
303
  /** Set false to disable entirely — e.g. you already rate-limit at a gateway/CDN layer. Default true. */
304
304
  enabled?: boolean;
305
+ /**
306
+ * Your own limiter, for instance one counting in your database, so every instance shares the counts. It replaces the
307
+ * memory adapter; refused with `adapter: 'redis'`. `increment` must be atomic. An error it throws fails closed (503)
308
+ * everywhere but refresh.
309
+ */
310
+ limiter?: RateLimiter;
305
311
  adapter?: 'memory' | 'redis';
306
312
  keyPrefix?: string;
307
313
  /** Only read if adapter === 'redis'; omit to reuse cache.adapter's Redis connection when that's also 'redis'. */