@aooth/auth 0.1.84 → 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.
@@ -1,3 +1,24 @@
1
+ //#region src/errors.ts
2
+ const defaultMessages = {
3
+ INVALID_TOKEN: "Invalid token",
4
+ TOKEN_EXPIRED: "Token has expired",
5
+ TOKEN_REVOKED: "Token has been revoked",
6
+ REFRESH_REUSE_DETECTED: "Refresh token reuse detected",
7
+ STATELESS_OPERATION_UNSUPPORTED: "Operation is not supported by stateless credential store",
8
+ MAX_CONCURRENT_REACHED: "Maximum concurrent credentials reached for user",
9
+ INVALID_CONFIG: "Invalid auth configuration"
10
+ };
11
+ var AuthError = class extends Error {
12
+ type;
13
+ details;
14
+ name = "AuthError";
15
+ constructor(type, message, details) {
16
+ super(message ?? defaultMessages[type]);
17
+ this.type = type;
18
+ this.details = details;
19
+ }
20
+ };
21
+ //#endregion
1
22
  //#region src/credential/payload.ts
2
23
  const ENVELOPE_KEYS = new Set(Object.keys({
3
24
  userId: true,
@@ -25,4 +46,4 @@ function credentialPayloadOf(state) {
25
46
  return out;
26
47
  }
27
48
  //#endregion
28
- export { credentialPayloadOf as t };
49
+ export { AuthError as n, credentialPayloadOf as t };
package/dist/redis.cjs CHANGED
@@ -29,13 +29,16 @@ var CredentialStoreRedis = class {
29
29
  }
30
30
  async persist(state, ttl) {
31
31
  const token = (0, node_crypto.randomUUID)();
32
+ await this.persistAs(token, state, ttl);
33
+ return token;
34
+ }
35
+ async persistAs(token, state, ttl) {
32
36
  const stored = { ...state };
33
37
  if (typeof ttl === "number") stored.expiresAt = Date.now() + ttl;
34
38
  const ttlMs = Math.max(0, stored.expiresAt - Date.now());
35
39
  if (ttlMs <= 0) throw new Error("CredentialStoreRedis.persist: refusing to persist an already-expired credential");
36
40
  await this.redis.set(this.tokenKey(token), JSON.stringify(stored), "PX", ttlMs);
37
41
  await this.redis.sadd(this.userKey(state.userId), token);
38
- return token;
39
42
  }
40
43
  async retrieve(token) {
41
44
  const raw = await this.redis.get(this.tokenKey(token));
@@ -175,6 +178,13 @@ var RateLimitStoreRedis = class {
175
178
  if (count === 1) await this.redis.pexpire(k, ttlMs);
176
179
  return count;
177
180
  }
181
+ async get(key) {
182
+ const raw = await this.redis.get(this.key(key));
183
+ return raw ? Number(raw) || 0 : 0;
184
+ }
185
+ async expire(key, ttlMs) {
186
+ await this.redis.pexpire(this.key(key), ttlMs);
187
+ }
178
188
  async reset(key) {
179
189
  await this.redis.del(this.key(key));
180
190
  }
package/dist/redis.d.cts CHANGED
@@ -1,5 +1,5 @@
1
- import { i as DenylistStore, r as CredentialStore, s as CredentialState } from "./clock-CyjqzLxc.cjs";
2
- import { t as RateLimitStore } from "./store-ClQnA8yh.cjs";
1
+ import { i as DenylistStore, r as CredentialStore, s as CredentialState } from "./clock-CzAaCoZ5.cjs";
2
+ import { t as RateLimitStore } from "./store-BaFYDHxn.cjs";
3
3
  //#region src/redis/index.d.ts
4
4
  /**
5
5
  * Structural Redis client. Covers the exact set of commands used by
@@ -97,6 +97,7 @@ declare class CredentialStoreRedis<TPayload extends object = object> implements
97
97
  private tokenKey;
98
98
  private userKey;
99
99
  persist(state: CredentialState & TPayload, ttl?: number): Promise<string>;
100
+ persistAs(token: string, state: CredentialState & TPayload, ttl?: number): Promise<void>;
100
101
  retrieve(token: string): Promise<(CredentialState & TPayload) | null>;
101
102
  consume(token: string): Promise<(CredentialState & TPayload) | null>;
102
103
  update(token: string, state: CredentialState & TPayload): Promise<string>;
@@ -146,6 +147,8 @@ declare class RateLimitStoreRedis implements RateLimitStore {
146
147
  constructor(opts: RedisRateLimitStoreOptions);
147
148
  private key;
148
149
  hit(key: string, ttlMs: number): Promise<number>;
150
+ get(key: string): Promise<number>;
151
+ expire(key: string, ttlMs: number): Promise<void>;
149
152
  reset(key: string): Promise<void>;
150
153
  }
151
154
  //#endregion
package/dist/redis.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { i as DenylistStore, r as CredentialStore, s as CredentialState } from "./clock-CyjqzLxc.mjs";
2
- import { t as RateLimitStore } from "./store-CVD1nd2j.mjs";
1
+ import { i as DenylistStore, r as CredentialStore, s as CredentialState } from "./clock-CzAaCoZ5.mjs";
2
+ import { t as RateLimitStore } from "./store-h-bLOAaS.mjs";
3
3
  //#region src/redis/index.d.ts
4
4
  /**
5
5
  * Structural Redis client. Covers the exact set of commands used by
@@ -97,6 +97,7 @@ declare class CredentialStoreRedis<TPayload extends object = object> implements
97
97
  private tokenKey;
98
98
  private userKey;
99
99
  persist(state: CredentialState & TPayload, ttl?: number): Promise<string>;
100
+ persistAs(token: string, state: CredentialState & TPayload, ttl?: number): Promise<void>;
100
101
  retrieve(token: string): Promise<(CredentialState & TPayload) | null>;
101
102
  consume(token: string): Promise<(CredentialState & TPayload) | null>;
102
103
  update(token: string, state: CredentialState & TPayload): Promise<string>;
@@ -146,6 +147,8 @@ declare class RateLimitStoreRedis implements RateLimitStore {
146
147
  constructor(opts: RedisRateLimitStoreOptions);
147
148
  private key;
148
149
  hit(key: string, ttlMs: number): Promise<number>;
150
+ get(key: string): Promise<number>;
151
+ expire(key: string, ttlMs: number): Promise<void>;
149
152
  reset(key: string): Promise<void>;
150
153
  }
151
154
  //#endregion
package/dist/redis.mjs CHANGED
@@ -28,13 +28,16 @@ var CredentialStoreRedis = class {
28
28
  }
29
29
  async persist(state, ttl) {
30
30
  const token = randomUUID();
31
+ await this.persistAs(token, state, ttl);
32
+ return token;
33
+ }
34
+ async persistAs(token, state, ttl) {
31
35
  const stored = { ...state };
32
36
  if (typeof ttl === "number") stored.expiresAt = Date.now() + ttl;
33
37
  const ttlMs = Math.max(0, stored.expiresAt - Date.now());
34
38
  if (ttlMs <= 0) throw new Error("CredentialStoreRedis.persist: refusing to persist an already-expired credential");
35
39
  await this.redis.set(this.tokenKey(token), JSON.stringify(stored), "PX", ttlMs);
36
40
  await this.redis.sadd(this.userKey(state.userId), token);
37
- return token;
38
41
  }
39
42
  async retrieve(token) {
40
43
  const raw = await this.redis.get(this.tokenKey(token));
@@ -174,6 +177,13 @@ var RateLimitStoreRedis = class {
174
177
  if (count === 1) await this.redis.pexpire(k, ttlMs);
175
178
  return count;
176
179
  }
180
+ async get(key) {
181
+ const raw = await this.redis.get(this.key(key));
182
+ return raw ? Number(raw) || 0 : 0;
183
+ }
184
+ async expire(key, ttlMs) {
185
+ await this.redis.pexpire(this.key(key), ttlMs);
186
+ }
177
187
  async reset(key) {
178
188
  await this.redis.del(this.key(key));
179
189
  }
@@ -0,0 +1,69 @@
1
+ import { t as Clock } from "./clock-CzAaCoZ5.cjs";
2
+ //#region src/rate-limit/store.d.ts
3
+ /**
4
+ * Storage contract for the fixed-window limiter (RL.spec.md §4.3).
5
+ *
6
+ * The limiter embeds the window start in the key, so a new window is a new
7
+ * key and correctness never depends on the TTL — the TTL only garbage-collects
8
+ * dead counters.
9
+ */
10
+ interface RateLimitStore {
11
+ /**
12
+ * Atomically increment `key`, setting `ttlMs` expiry when the key is
13
+ * created; returns the new count.
14
+ */
15
+ hit(key: string, ttlMs: number): Promise<number>;
16
+ /**
17
+ * Optional: read `key`'s current count without incrementing it (`0` when
18
+ * missing or expired). Required by `RateLimiter.peek()`.
19
+ */
20
+ get?(key: string): Promise<number>;
21
+ /** Optional: reset `key`'s expiry to `ttlMs` from now (a sliding window); no-op when missing. */
22
+ expire?(key: string, ttlMs: number): Promise<void>;
23
+ /** Optional: drop a counter (tests / admin unblock). */
24
+ reset?(key: string): Promise<void>;
25
+ }
26
+ /**
27
+ * In-memory `RateLimitStore` — the zero-config default (single process only).
28
+ * Lazy-evicts expired counters on access, and sweeps expired counters itself
29
+ * as the map grows (amortized, each time it doubles); `cleanup()` sweeps the
30
+ * whole map on demand (same pattern as `DenylistStoreMemory`).
31
+ */
32
+ declare class RateLimitStoreMemory implements RateLimitStore {
33
+ private readonly entries;
34
+ private readonly clock;
35
+ private readonly maxEntries;
36
+ private readonly evictLast?;
37
+ /** Map size at which `hit` next sweeps expired counters (amortized; doubles). */
38
+ private sweepAt;
39
+ /**
40
+ * Lower bound of every entry's `expiresAt` (exact after a sweep): while
41
+ * `now` is below it nothing can have expired, so a sweep is skipped.
42
+ */
43
+ private minExpiry;
44
+ /**
45
+ * - `maxEntries` caps the live counters kept (default: unbounded). At the
46
+ * cap a new key first sweeps expired counters, then evicts the oldest one.
47
+ * - `evictLast` marks keys to keep while others can go: at the cap the
48
+ * oldest key it does NOT match is evicted first (the oldest matching one
49
+ * only when every key matches) — e.g. active lock markers that a flood of
50
+ * fresh counters must not push out.
51
+ */
52
+ constructor(opts?: {
53
+ clock?: Clock;
54
+ maxEntries?: number;
55
+ evictLast?: (key: string) => boolean;
56
+ });
57
+ hit(key: string, ttlMs: number): Promise<number>;
58
+ /** Drop the oldest key (Map keeps insertion order), sparing `evictLast` keys while it can. */
59
+ private evictOne;
60
+ get(key: string): Promise<number>;
61
+ expire(key: string, ttlMs: number): Promise<void>;
62
+ reset(key: string): Promise<void>;
63
+ /** Drop every counter — test-harness reset between cases. */
64
+ clear(): void;
65
+ cleanup(): Promise<number>;
66
+ private sweep;
67
+ }
68
+ //#endregion
69
+ export { RateLimitStoreMemory as n, RateLimitStore as t };
@@ -0,0 +1,69 @@
1
+ import { t as Clock } from "./clock-CzAaCoZ5.mjs";
2
+ //#region src/rate-limit/store.d.ts
3
+ /**
4
+ * Storage contract for the fixed-window limiter (RL.spec.md §4.3).
5
+ *
6
+ * The limiter embeds the window start in the key, so a new window is a new
7
+ * key and correctness never depends on the TTL — the TTL only garbage-collects
8
+ * dead counters.
9
+ */
10
+ interface RateLimitStore {
11
+ /**
12
+ * Atomically increment `key`, setting `ttlMs` expiry when the key is
13
+ * created; returns the new count.
14
+ */
15
+ hit(key: string, ttlMs: number): Promise<number>;
16
+ /**
17
+ * Optional: read `key`'s current count without incrementing it (`0` when
18
+ * missing or expired). Required by `RateLimiter.peek()`.
19
+ */
20
+ get?(key: string): Promise<number>;
21
+ /** Optional: reset `key`'s expiry to `ttlMs` from now (a sliding window); no-op when missing. */
22
+ expire?(key: string, ttlMs: number): Promise<void>;
23
+ /** Optional: drop a counter (tests / admin unblock). */
24
+ reset?(key: string): Promise<void>;
25
+ }
26
+ /**
27
+ * In-memory `RateLimitStore` — the zero-config default (single process only).
28
+ * Lazy-evicts expired counters on access, and sweeps expired counters itself
29
+ * as the map grows (amortized, each time it doubles); `cleanup()` sweeps the
30
+ * whole map on demand (same pattern as `DenylistStoreMemory`).
31
+ */
32
+ declare class RateLimitStoreMemory implements RateLimitStore {
33
+ private readonly entries;
34
+ private readonly clock;
35
+ private readonly maxEntries;
36
+ private readonly evictLast?;
37
+ /** Map size at which `hit` next sweeps expired counters (amortized; doubles). */
38
+ private sweepAt;
39
+ /**
40
+ * Lower bound of every entry's `expiresAt` (exact after a sweep): while
41
+ * `now` is below it nothing can have expired, so a sweep is skipped.
42
+ */
43
+ private minExpiry;
44
+ /**
45
+ * - `maxEntries` caps the live counters kept (default: unbounded). At the
46
+ * cap a new key first sweeps expired counters, then evicts the oldest one.
47
+ * - `evictLast` marks keys to keep while others can go: at the cap the
48
+ * oldest key it does NOT match is evicted first (the oldest matching one
49
+ * only when every key matches) — e.g. active lock markers that a flood of
50
+ * fresh counters must not push out.
51
+ */
52
+ constructor(opts?: {
53
+ clock?: Clock;
54
+ maxEntries?: number;
55
+ evictLast?: (key: string) => boolean;
56
+ });
57
+ hit(key: string, ttlMs: number): Promise<number>;
58
+ /** Drop the oldest key (Map keeps insertion order), sparing `evictLast` keys while it can. */
59
+ private evictOne;
60
+ get(key: string): Promise<number>;
61
+ expire(key: string, ttlMs: number): Promise<void>;
62
+ reset(key: string): Promise<void>;
63
+ /** Drop every counter — test-harness reset between cases. */
64
+ clear(): void;
65
+ cleanup(): Promise<number>;
66
+ private sweep;
67
+ }
68
+ //#endregion
69
+ export { RateLimitStoreMemory as n, RateLimitStore as t };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aooth/auth",
3
- "version": "0.1.84",
3
+ "version": "0.2.0",
4
4
  "description": "Auth method layer for aoothjs (sessions, tokens, password reset, MFA primitives)",
5
5
  "keywords": [
6
6
  "aoothjs",
@@ -104,13 +104,13 @@
104
104
  },
105
105
  "dependencies": {
106
106
  "jose": "^6.2.3",
107
- "@aooth/user": "0.1.84"
107
+ "@aooth/user": "0.2.0"
108
108
  },
109
109
  "devDependencies": {
110
110
  "@atscript/core": "^0.1.109",
111
- "@atscript/db": "^0.1.163",
112
- "@atscript/db-sql-tools": "^0.1.163",
113
- "@atscript/db-sqlite": "^0.1.163",
111
+ "@atscript/db": "^0.1.164",
112
+ "@atscript/db-sql-tools": "^0.1.164",
113
+ "@atscript/db-sqlite": "^0.1.164",
114
114
  "@atscript/typescript": "^0.1.109",
115
115
  "@types/better-sqlite3": "^7.6.13",
116
116
  "@uniqu/core": "^0.1.15",
@@ -119,7 +119,7 @@
119
119
  "unplugin-atscript": "^0.1.109"
120
120
  },
121
121
  "peerDependencies": {
122
- "@atscript/db": "^0.1.163"
122
+ "@atscript/db": "^0.1.164"
123
123
  },
124
124
  "peerDependenciesMeta": {
125
125
  "@atscript/db": {
@@ -1,41 +0,0 @@
1
- import { t as Clock } from "./clock-CyjqzLxc.mjs";
2
- //#region src/rate-limit/store.d.ts
3
- /**
4
- * Storage contract for the fixed-window limiter (RL.spec.md §4.3).
5
- *
6
- * The limiter embeds the window start in the key, so a new window is a new
7
- * key and correctness never depends on the TTL — the TTL only garbage-collects
8
- * dead counters.
9
- */
10
- interface RateLimitStore {
11
- /**
12
- * Atomically increment `key`, setting `ttlMs` expiry when the key is
13
- * created; returns the new count.
14
- */
15
- hit(key: string, ttlMs: number): Promise<number>;
16
- /** Optional: drop a counter (tests / admin unblock). */
17
- reset?(key: string): Promise<void>;
18
- }
19
- /**
20
- * In-memory `RateLimitStore` — the zero-config default (single process only).
21
- * Lazy-evicts expired counters on access, and sweeps expired counters itself
22
- * as the map grows (amortized, each time it doubles); `cleanup()` sweeps the
23
- * whole map on demand (same pattern as `DenylistStoreMemory`).
24
- */
25
- declare class RateLimitStoreMemory implements RateLimitStore {
26
- private readonly entries;
27
- private readonly clock;
28
- /** Map size at which `hit` next sweeps expired counters (amortized; doubles). */
29
- private sweepAt;
30
- constructor(opts?: {
31
- clock?: Clock;
32
- });
33
- hit(key: string, ttlMs: number): Promise<number>;
34
- reset(key: string): Promise<void>;
35
- /** Drop every counter — test-harness reset between cases. */
36
- clear(): void;
37
- cleanup(): Promise<number>;
38
- private sweep;
39
- }
40
- //#endregion
41
- export { RateLimitStoreMemory as n, RateLimitStore as t };
@@ -1,41 +0,0 @@
1
- import { t as Clock } from "./clock-CyjqzLxc.cjs";
2
- //#region src/rate-limit/store.d.ts
3
- /**
4
- * Storage contract for the fixed-window limiter (RL.spec.md §4.3).
5
- *
6
- * The limiter embeds the window start in the key, so a new window is a new
7
- * key and correctness never depends on the TTL — the TTL only garbage-collects
8
- * dead counters.
9
- */
10
- interface RateLimitStore {
11
- /**
12
- * Atomically increment `key`, setting `ttlMs` expiry when the key is
13
- * created; returns the new count.
14
- */
15
- hit(key: string, ttlMs: number): Promise<number>;
16
- /** Optional: drop a counter (tests / admin unblock). */
17
- reset?(key: string): Promise<void>;
18
- }
19
- /**
20
- * In-memory `RateLimitStore` — the zero-config default (single process only).
21
- * Lazy-evicts expired counters on access, and sweeps expired counters itself
22
- * as the map grows (amortized, each time it doubles); `cleanup()` sweeps the
23
- * whole map on demand (same pattern as `DenylistStoreMemory`).
24
- */
25
- declare class RateLimitStoreMemory implements RateLimitStore {
26
- private readonly entries;
27
- private readonly clock;
28
- /** Map size at which `hit` next sweeps expired counters (amortized; doubles). */
29
- private sweepAt;
30
- constructor(opts?: {
31
- clock?: Clock;
32
- });
33
- hit(key: string, ttlMs: number): Promise<number>;
34
- reset(key: string): Promise<void>;
35
- /** Drop every counter — test-harness reset between cases. */
36
- clear(): void;
37
- cleanup(): Promise<number>;
38
- private sweep;
39
- }
40
- //#endregion
41
- export { RateLimitStoreMemory as n, RateLimitStore as t };