@basaltkit/auth 3.0.0 → 3.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 +27 -4
- package/dist/auth.d.ts +58 -2
- package/dist/auth.js +130 -32
- package/dist/index.d.ts +15 -3
- package/dist/index.js +4 -3
- package/dist/jwt.d.ts +5 -0
- package/dist/oauth-plugin.js +3 -0
- package/dist/plugin.d.ts +28 -1
- package/dist/plugin.js +53 -18
- package/dist/redis-throttle.d.ts +34 -0
- package/dist/redis-throttle.js +69 -0
- package/dist/routes.d.ts +10 -0
- package/dist/routes.js +47 -18
- package/dist/throttle.d.ts +95 -27
- package/dist/throttle.js +104 -65
- package/package.json +7 -7
package/dist/throttle.js
CHANGED
|
@@ -9,108 +9,147 @@ export class AccountLockedError extends BasaltError {
|
|
|
9
9
|
this.retryAfterMs = retryAfterMs;
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
|
-
/**
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
* login clears the counter. In-memory per process; back it with Redis for a
|
|
16
|
-
* cluster by supplying the same behavior.
|
|
17
|
-
*
|
|
18
|
-
* Identifiers are stored as SHA-256 digests (fixed size, never the raw email),
|
|
19
|
-
* and the table is bounded by {@link LoginThrottleOptions.maxEntries}.
|
|
20
|
-
*
|
|
21
|
-
* Use {@link reserve} before verifying credentials: it counts the attempt
|
|
22
|
-
* synchronously, so a parallel burst cannot run more verifications than the
|
|
23
|
-
* budget allows. {@link release} gives the reservation back on success.
|
|
24
|
-
*/
|
|
25
|
-
export class LoginThrottle {
|
|
26
|
-
failures = new Map();
|
|
27
|
-
maxAttempts;
|
|
28
|
-
windowMs;
|
|
12
|
+
/** In-process {@link ThrottleStore} (the default): synchronous and bounded. */
|
|
13
|
+
export class MemoryThrottleStore {
|
|
14
|
+
entries = new Map();
|
|
29
15
|
maxEntries;
|
|
30
16
|
clock;
|
|
31
17
|
constructor(options = {}) {
|
|
32
|
-
this.maxAttempts = options.maxAttempts ?? 5;
|
|
33
|
-
this.windowMs = options.windowMs ?? 15 * 60_000;
|
|
34
18
|
this.maxEntries = Math.max(1, options.maxEntries ?? 100_000);
|
|
35
19
|
this.clock = options.clock ?? (() => Date.now());
|
|
36
20
|
}
|
|
37
|
-
/** Number of
|
|
21
|
+
/** Number of keys currently tracked. */
|
|
38
22
|
get size() {
|
|
39
|
-
return this.
|
|
23
|
+
return this.entries.size;
|
|
40
24
|
}
|
|
41
25
|
/** Total characters retained in keys — diagnostics for memory bounds. */
|
|
42
26
|
retainedKeyChars() {
|
|
43
27
|
let n = 0;
|
|
44
|
-
for (const key of this.
|
|
28
|
+
for (const key of this.entries.keys())
|
|
45
29
|
n += key.length;
|
|
46
30
|
return n;
|
|
47
31
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
live(digest, now) {
|
|
52
|
-
const entry = this.failures.get(digest);
|
|
32
|
+
live(key, now) {
|
|
33
|
+
const entry = this.entries.get(key);
|
|
53
34
|
if (entry && now >= entry.resetAt) {
|
|
54
|
-
this.
|
|
35
|
+
this.entries.delete(key);
|
|
55
36
|
return undefined;
|
|
56
37
|
}
|
|
57
38
|
return entry;
|
|
58
39
|
}
|
|
59
|
-
store(
|
|
60
|
-
if (!this.
|
|
61
|
-
for (const [k, e] of this.
|
|
40
|
+
store(key, entry, now) {
|
|
41
|
+
if (!this.entries.has(key) && this.entries.size >= this.maxEntries) {
|
|
42
|
+
for (const [k, e] of this.entries)
|
|
62
43
|
if (now >= e.resetAt)
|
|
63
|
-
this.
|
|
44
|
+
this.entries.delete(k);
|
|
64
45
|
// Still full: evict the oldest entries (Map iterates in insertion order).
|
|
65
|
-
for (const k of this.
|
|
66
|
-
if (this.
|
|
46
|
+
for (const k of this.entries.keys()) {
|
|
47
|
+
if (this.entries.size < this.maxEntries)
|
|
67
48
|
break;
|
|
68
|
-
this.
|
|
49
|
+
this.entries.delete(k);
|
|
69
50
|
}
|
|
70
51
|
}
|
|
71
|
-
this.
|
|
52
|
+
this.entries.set(key, entry);
|
|
53
|
+
}
|
|
54
|
+
hit(key, windowMs) {
|
|
55
|
+
const now = this.clock();
|
|
56
|
+
const entry = this.live(key, now) ?? { count: 0, resetAt: now + windowMs };
|
|
57
|
+
entry.count += 1;
|
|
58
|
+
this.store(key, entry, now);
|
|
59
|
+
return { count: entry.count, retryAfterMs: entry.resetAt - now };
|
|
60
|
+
}
|
|
61
|
+
peek(key) {
|
|
62
|
+
const now = this.clock();
|
|
63
|
+
const entry = this.live(key, now);
|
|
64
|
+
return entry ? { count: entry.count, retryAfterMs: entry.resetAt - now } : null;
|
|
65
|
+
}
|
|
66
|
+
release(key) {
|
|
67
|
+
const entry = this.live(key, this.clock());
|
|
68
|
+
if (!entry)
|
|
69
|
+
return;
|
|
70
|
+
entry.count -= 1;
|
|
71
|
+
if (entry.count <= 0)
|
|
72
|
+
this.entries.delete(key);
|
|
73
|
+
}
|
|
74
|
+
reset(key) {
|
|
75
|
+
this.entries.delete(key);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const isPromise = (value) => typeof value?.then === 'function';
|
|
79
|
+
/** Runs `next` on a value that may or may not be a promise — sync in, sync out. */
|
|
80
|
+
function chain(value, next) {
|
|
81
|
+
return isPromise(value) ? value.then(next) : next(value);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Brute-force guard: counts failed logins per identifier (email) within a
|
|
85
|
+
* fixed window and locks the account once the budget is spent. A successful
|
|
86
|
+
* login clears the counter. Counters live in a {@link ThrottleStore}: in memory
|
|
87
|
+
* per process by default, or shared across replicas (Redis).
|
|
88
|
+
*
|
|
89
|
+
* Identifiers are handed to the store as SHA-256 digests (fixed size, never
|
|
90
|
+
* the raw email).
|
|
91
|
+
*
|
|
92
|
+
* Use {@link reserve} before verifying credentials: it counts the attempt
|
|
93
|
+
* atomically (synchronously with the memory store, in one Redis script with a
|
|
94
|
+
* shared one), so a parallel burst cannot run more verifications than the
|
|
95
|
+
* budget allows. {@link release} gives the reservation back on success.
|
|
96
|
+
*
|
|
97
|
+
* Every method returns synchronously with a synchronous store (the default)
|
|
98
|
+
* and a promise otherwise; `await` the result when the store may be async.
|
|
99
|
+
*/
|
|
100
|
+
export class LoginThrottle {
|
|
101
|
+
store;
|
|
102
|
+
maxAttempts;
|
|
103
|
+
windowMs;
|
|
104
|
+
prefix;
|
|
105
|
+
constructor(options = {}) {
|
|
106
|
+
this.maxAttempts = options.maxAttempts ?? 5;
|
|
107
|
+
this.windowMs = options.windowMs ?? 15 * 60_000;
|
|
108
|
+
this.prefix = options.namespace ? `${options.namespace}:` : '';
|
|
109
|
+
this.store =
|
|
110
|
+
options.store ??
|
|
111
|
+
new MemoryThrottleStore({
|
|
112
|
+
...(options.maxEntries !== undefined ? { maxEntries: options.maxEntries } : {}),
|
|
113
|
+
...(options.clock ? { clock: options.clock } : {}),
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
/** Number of identifiers currently tracked (in-memory store only; 0 otherwise). */
|
|
117
|
+
get size() {
|
|
118
|
+
return this.store instanceof MemoryThrottleStore ? this.store.size : 0;
|
|
119
|
+
}
|
|
120
|
+
/** Total characters retained in keys — diagnostics for memory bounds (in-memory store only). */
|
|
121
|
+
retainedKeyChars() {
|
|
122
|
+
return this.store instanceof MemoryThrottleStore ? this.store.retainedKeyChars() : 0;
|
|
123
|
+
}
|
|
124
|
+
key(key) {
|
|
125
|
+
return `${this.prefix}${createHash('sha256').update(key).digest('base64url')}`;
|
|
72
126
|
}
|
|
73
127
|
/** Throws {@link AccountLockedError} when the key is currently locked. */
|
|
74
128
|
assertAllowed(key) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
129
|
+
return chain(this.store.peek(this.key(key)), (window) => {
|
|
130
|
+
if (window && window.count >= this.maxAttempts)
|
|
131
|
+
throw new AccountLockedError(window.retryAfterMs);
|
|
132
|
+
});
|
|
80
133
|
}
|
|
81
134
|
recordFailure(key) {
|
|
82
|
-
|
|
83
|
-
const digest = this.digest(key);
|
|
84
|
-
const entry = this.live(digest, now) ?? { count: 0, resetAt: now + this.windowMs };
|
|
85
|
-
entry.count += 1;
|
|
86
|
-
this.store(digest, entry, now);
|
|
135
|
+
return chain(this.store.hit(this.key(key), this.windowMs), () => undefined);
|
|
87
136
|
}
|
|
88
137
|
/**
|
|
89
|
-
* Atomically
|
|
90
|
-
*
|
|
91
|
-
*
|
|
138
|
+
* Atomically counts this attempt and throws {@link AccountLockedError} when
|
|
139
|
+
* it exceeds the budget. Pair with {@link release} on success; a failure
|
|
140
|
+
* simply keeps the reservation.
|
|
92
141
|
*/
|
|
93
142
|
reserve(key) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
throw new AccountLockedError(entry.resetAt - now);
|
|
99
|
-
const next = entry ?? { count: 0, resetAt: now + this.windowMs };
|
|
100
|
-
next.count += 1;
|
|
101
|
-
this.store(digest, next, now);
|
|
143
|
+
return chain(this.store.hit(this.key(key), this.windowMs), (window) => {
|
|
144
|
+
if (window.count > this.maxAttempts)
|
|
145
|
+
throw new AccountLockedError(window.retryAfterMs);
|
|
146
|
+
});
|
|
102
147
|
}
|
|
103
148
|
/** Returns one reservation (a successful attempt must not consume budget). */
|
|
104
149
|
release(key) {
|
|
105
|
-
|
|
106
|
-
const entry = this.live(digest, this.clock());
|
|
107
|
-
if (!entry)
|
|
108
|
-
return;
|
|
109
|
-
entry.count -= 1;
|
|
110
|
-
if (entry.count <= 0)
|
|
111
|
-
this.failures.delete(digest);
|
|
150
|
+
return this.store.release(this.key(key));
|
|
112
151
|
}
|
|
113
152
|
reset(key) {
|
|
114
|
-
this.
|
|
153
|
+
return this.store.reset(this.key(key));
|
|
115
154
|
}
|
|
116
155
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@basaltkit/auth",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"engines": {
|
|
5
5
|
"node": ">=22.5.0"
|
|
6
6
|
},
|
|
@@ -18,18 +18,18 @@
|
|
|
18
18
|
"dist"
|
|
19
19
|
],
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@basaltkit/core": "^1.3.
|
|
22
|
-
"@basaltkit/http": "^2.
|
|
21
|
+
"@basaltkit/core": "^1.3.2",
|
|
22
|
+
"@basaltkit/http": "^2.2.0"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
|
25
25
|
"zod": "^4.0.0"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@types/node": "^26.
|
|
28
|
+
"@types/node": "^26.6.1",
|
|
29
29
|
"typescript": "^7.0.2",
|
|
30
|
-
"vitest": "^5.0.
|
|
31
|
-
"zod": "^4.5
|
|
32
|
-
"@basaltkit/fastify": "^2.
|
|
30
|
+
"vitest": "^5.0.1",
|
|
31
|
+
"zod": "^4.6.5",
|
|
32
|
+
"@basaltkit/fastify": "^2.2.0",
|
|
33
33
|
"@basaltkit/tsconfig": "^0.24.0"
|
|
34
34
|
},
|
|
35
35
|
"publishConfig": {
|