@basaltkit/auth 2.2.0 → 3.0.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 +25 -11
- package/dist/apikeys-plugin.d.ts +46 -0
- package/dist/apikeys-plugin.js +72 -7
- package/dist/apikeys.d.ts +5 -0
- package/dist/apikeys.js +10 -0
- package/dist/auth.d.ts +59 -7
- package/dist/auth.js +174 -32
- package/dist/index.d.ts +4 -4
- package/dist/index.js +4 -4
- package/dist/oauth-plugin.d.ts +9 -0
- package/dist/oauth-plugin.js +28 -4
- package/dist/oauth.d.ts +41 -4
- package/dist/oauth.js +99 -11
- package/dist/plugin.d.ts +49 -2
- package/dist/plugin.js +60 -4
- package/dist/routes.d.ts +25 -1
- package/dist/routes.js +40 -21
- package/dist/stores.d.ts +14 -0
- package/dist/stores.js +24 -1
- package/dist/throttle.d.ts +29 -0
- package/dist/throttle.js +79 -8
- package/dist/webauthn-plugin.js +9 -1
- package/package.json +6 -6
package/dist/throttle.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
1
2
|
import { BasaltError } from '@basaltkit/core';
|
|
2
3
|
/** Raised when an identifier has exceeded the failed-login budget. */
|
|
3
4
|
export class AccountLockedError extends BasaltError {
|
|
@@ -13,33 +14,103 @@ export class AccountLockedError extends BasaltError {
|
|
|
13
14
|
* rolling window and locks the account once the budget is spent. A successful
|
|
14
15
|
* login clears the counter. In-memory per process; back it with Redis for a
|
|
15
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.
|
|
16
24
|
*/
|
|
17
25
|
export class LoginThrottle {
|
|
18
26
|
failures = new Map();
|
|
19
27
|
maxAttempts;
|
|
20
28
|
windowMs;
|
|
29
|
+
maxEntries;
|
|
21
30
|
clock;
|
|
22
31
|
constructor(options = {}) {
|
|
23
32
|
this.maxAttempts = options.maxAttempts ?? 5;
|
|
24
33
|
this.windowMs = options.windowMs ?? 15 * 60_000;
|
|
34
|
+
this.maxEntries = Math.max(1, options.maxEntries ?? 100_000);
|
|
25
35
|
this.clock = options.clock ?? (() => Date.now());
|
|
26
36
|
}
|
|
37
|
+
/** Number of identifiers currently tracked. */
|
|
38
|
+
get size() {
|
|
39
|
+
return this.failures.size;
|
|
40
|
+
}
|
|
41
|
+
/** Total characters retained in keys — diagnostics for memory bounds. */
|
|
42
|
+
retainedKeyChars() {
|
|
43
|
+
let n = 0;
|
|
44
|
+
for (const key of this.failures.keys())
|
|
45
|
+
n += key.length;
|
|
46
|
+
return n;
|
|
47
|
+
}
|
|
48
|
+
digest(key) {
|
|
49
|
+
return createHash('sha256').update(key).digest('base64url');
|
|
50
|
+
}
|
|
51
|
+
live(digest, now) {
|
|
52
|
+
const entry = this.failures.get(digest);
|
|
53
|
+
if (entry && now >= entry.resetAt) {
|
|
54
|
+
this.failures.delete(digest);
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
return entry;
|
|
58
|
+
}
|
|
59
|
+
store(digest, entry, now) {
|
|
60
|
+
if (!this.failures.has(digest) && this.failures.size >= this.maxEntries) {
|
|
61
|
+
for (const [k, e] of this.failures)
|
|
62
|
+
if (now >= e.resetAt)
|
|
63
|
+
this.failures.delete(k);
|
|
64
|
+
// Still full: evict the oldest entries (Map iterates in insertion order).
|
|
65
|
+
for (const k of this.failures.keys()) {
|
|
66
|
+
if (this.failures.size < this.maxEntries)
|
|
67
|
+
break;
|
|
68
|
+
this.failures.delete(k);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
this.failures.set(digest, entry);
|
|
72
|
+
}
|
|
27
73
|
/** Throws {@link AccountLockedError} when the key is currently locked. */
|
|
28
74
|
assertAllowed(key) {
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
75
|
+
const now = this.clock();
|
|
76
|
+
const entry = this.live(this.digest(key), now);
|
|
77
|
+
if (entry && entry.count >= this.maxAttempts) {
|
|
78
|
+
throw new AccountLockedError(entry.resetAt - now);
|
|
32
79
|
}
|
|
33
80
|
}
|
|
34
81
|
recordFailure(key) {
|
|
35
82
|
const now = this.clock();
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
entry = { count: 0, resetAt: now + this.windowMs };
|
|
83
|
+
const digest = this.digest(key);
|
|
84
|
+
const entry = this.live(digest, now) ?? { count: 0, resetAt: now + this.windowMs };
|
|
39
85
|
entry.count += 1;
|
|
40
|
-
this.
|
|
86
|
+
this.store(digest, entry, now);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Atomically checks the budget and counts this attempt (synchronously, before
|
|
90
|
+
* any await), throwing {@link AccountLockedError} when it is spent. Pair with
|
|
91
|
+
* {@link release} on success; a failure simply keeps the reservation.
|
|
92
|
+
*/
|
|
93
|
+
reserve(key) {
|
|
94
|
+
const now = this.clock();
|
|
95
|
+
const digest = this.digest(key);
|
|
96
|
+
const entry = this.live(digest, now);
|
|
97
|
+
if (entry && entry.count >= this.maxAttempts)
|
|
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);
|
|
102
|
+
}
|
|
103
|
+
/** Returns one reservation (a successful attempt must not consume budget). */
|
|
104
|
+
release(key) {
|
|
105
|
+
const digest = this.digest(key);
|
|
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);
|
|
41
112
|
}
|
|
42
113
|
reset(key) {
|
|
43
|
-
this.failures.delete(key);
|
|
114
|
+
this.failures.delete(this.digest(key));
|
|
44
115
|
}
|
|
45
116
|
}
|
package/dist/webauthn-plugin.js
CHANGED
|
@@ -5,7 +5,15 @@ export const WEBAUTHN = createToken('auth:webauthn');
|
|
|
5
5
|
export function webauthnPlugin(options) {
|
|
6
6
|
return definePlugin({
|
|
7
7
|
name: 'basalt:auth-webauthn',
|
|
8
|
-
register({ container }) {
|
|
8
|
+
register({ container, hooks }) {
|
|
9
|
+
// Passkeys enrolled on a never-verified account by whoever registered it
|
|
10
|
+
// are login credentials too: a verified social login adopting the account
|
|
11
|
+
// removes them with the rest.
|
|
12
|
+
hooks.on('auth:social_account_adopted', async ({ user }) => {
|
|
13
|
+
const service = container.get(WEBAUTHN);
|
|
14
|
+
for (const credential of await service.list(user.id))
|
|
15
|
+
await service.remove(credential.id);
|
|
16
|
+
});
|
|
9
17
|
container.singleton(WEBAUTHN, () => new WebAuthnService({
|
|
10
18
|
config: options.config,
|
|
11
19
|
verifier: options.verifier,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@basaltkit/auth",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.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/
|
|
22
|
-
"@basaltkit/
|
|
21
|
+
"@basaltkit/core": "^1.3.1",
|
|
22
|
+
"@basaltkit/http": "^2.1.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.5.1",
|
|
29
29
|
"typescript": "^7.0.2",
|
|
30
|
-
"vitest": "^
|
|
30
|
+
"vitest": "^5.0.0",
|
|
31
31
|
"zod": "^4.5.4",
|
|
32
|
-
"@basaltkit/fastify": "^2.
|
|
32
|
+
"@basaltkit/fastify": "^2.1.0",
|
|
33
33
|
"@basaltkit/tsconfig": "^0.24.0"
|
|
34
34
|
},
|
|
35
35
|
"publishConfig": {
|