@c9up/warden 0.2.5 → 0.2.6
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 +70 -3
- package/dist/AccessToken.d.ts +257 -0
- package/dist/AccessToken.d.ts.map +1 -0
- package/dist/AccessToken.js +370 -0
- package/dist/AccessToken.js.map +1 -0
- package/dist/AttemptStore.d.ts +16 -2
- package/dist/AttemptStore.d.ts.map +1 -1
- package/dist/AttemptStore.js +26 -12
- package/dist/AttemptStore.js.map +1 -1
- package/dist/AuthManager.d.ts +33 -2
- package/dist/AuthManager.d.ts.map +1 -1
- package/dist/AuthManager.js +23 -2
- package/dist/AuthManager.js.map +1 -1
- package/dist/Authenticator.d.ts +19 -1
- package/dist/Authenticator.d.ts.map +1 -1
- package/dist/Authenticator.js +37 -3
- package/dist/Authenticator.js.map +1 -1
- package/dist/Guard.d.ts.map +1 -1
- package/dist/Guard.js +17 -5
- package/dist/Guard.js.map +1 -1
- package/dist/ResilientBlacklistDriver.d.ts +23 -6
- package/dist/ResilientBlacklistDriver.d.ts.map +1 -1
- package/dist/ResilientBlacklistDriver.js +24 -8
- package/dist/ResilientBlacklistDriver.js.map +1 -1
- package/dist/WardenProvider.d.ts +2 -0
- package/dist/WardenProvider.d.ts.map +1 -1
- package/dist/WardenProvider.js +4 -58
- package/dist/WardenProvider.js.map +1 -1
- package/dist/config.d.ts +8 -4
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +4 -1
- package/dist/config.js.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/mfa/MfaManager.d.ts +16 -4
- package/dist/mfa/MfaManager.d.ts.map +1 -1
- package/dist/mfa/MfaManager.js +32 -6
- package/dist/mfa/MfaManager.js.map +1 -1
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +27 -5
- package/dist/middleware.js.map +1 -1
- package/dist/native.d.ts +6 -2
- package/dist/native.d.ts.map +1 -1
- package/dist/native.js +19 -2
- package/dist/native.js.map +1 -1
- package/dist/quasar.d.ts.map +1 -1
- package/dist/quasar.js +8 -1
- package/dist/quasar.js.map +1 -1
- package/dist/standalone.d.ts +24 -3
- package/dist/standalone.d.ts.map +1 -1
- package/dist/standalone.js +21 -29
- package/dist/standalone.js.map +1 -1
- package/dist/strategies/ApiKeyStrategy.d.ts +45 -12
- package/dist/strategies/ApiKeyStrategy.d.ts.map +1 -1
- package/dist/strategies/ApiKeyStrategy.js +71 -37
- package/dist/strategies/ApiKeyStrategy.js.map +1 -1
- package/dist/strategies/JwtStrategy.d.ts.map +1 -1
- package/dist/strategies/JwtStrategy.js +3 -3
- package/dist/strategies/JwtStrategy.js.map +1 -1
- package/dist/templateTags.d.ts +42 -0
- package/dist/templateTags.d.ts.map +1 -0
- package/dist/templateTags.js +82 -0
- package/dist/templateTags.js.map +1 -0
- package/dist/vendor/nativeBinary.d.ts +26 -2
- package/dist/vendor/nativeBinary.d.ts.map +1 -1
- package/dist/vendor/nativeBinary.js +63 -6
- package/dist/vendor/nativeBinary.js.map +1 -1
- package/dist/vendor/quasarConnection.d.ts +26 -3
- package/dist/vendor/quasarConnection.d.ts.map +1 -1
- package/dist/vendor/quasarConnection.js +44 -23
- package/dist/vendor/quasarConnection.js.map +1 -1
- package/index.darwin-arm64.node +0 -0
- package/index.darwin-x64.node +0 -0
- package/index.win32-x64-msvc.node +0 -0
- package/package.json +7 -2
- package/src/AccessToken.ts +550 -0
- package/src/AttemptStore.ts +29 -13
- package/src/AuthManager.ts +40 -2
- package/src/Authenticator.ts +39 -3
- package/src/Guard.ts +18 -5
- package/src/ResilientBlacklistDriver.ts +28 -8
- package/src/WardenProvider.ts +6 -83
- package/src/config.ts +8 -4
- package/src/index.ts +18 -0
- package/src/mfa/MfaManager.ts +39 -6
- package/src/middleware.ts +36 -5
- package/src/native.ts +20 -4
- package/src/quasar.ts +13 -2
- package/src/standalone.ts +74 -33
- package/src/strategies/ApiKeyStrategy.ts +112 -42
- package/src/strategies/JwtStrategy.ts +3 -1
- package/src/templateTags.ts +117 -0
- package/src/vendor/nativeBinary.ts +82 -6
- package/src/vendor/quasarConnection.ts +84 -22
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Access tokens — opaque, revocable bearer tokens an application issues to its
|
|
3
|
+
* own users (API keys, personal access tokens, mobile app tokens).
|
|
4
|
+
*
|
|
5
|
+
* Modelled on `@adonisjs/auth`'s access-tokens guard (`AccessToken` +
|
|
6
|
+
* `DbAccessTokensProvider`), so an app moving over keeps its token format:
|
|
7
|
+
*
|
|
8
|
+
* value = prefix + base64url(identifier) + "." + base64url(secret)
|
|
9
|
+
* secret = random(tokenSecretLength) + crc32(random) in decimal
|
|
10
|
+
* stored = sha256(secret), never the secret itself
|
|
11
|
+
*
|
|
12
|
+
* What makes it safe, all taken from upstream:
|
|
13
|
+
*
|
|
14
|
+
* 1. **Only a hash is stored.** A dump of the tokens table hands out no
|
|
15
|
+
* working token: the secret exists only in the hands of whoever received
|
|
16
|
+
* the value when it was minted.
|
|
17
|
+
* 2. **The comparison is constant-time**, so a timing side channel cannot
|
|
18
|
+
* forge a secret one byte at a time.
|
|
19
|
+
* 3. **The value is recognisable.** The `oat_` prefix and the CRC32 suffix let
|
|
20
|
+
* secret scanners (GitHub's, a pre-commit hook) flag a leaked token.
|
|
21
|
+
* 4. **Abilities, expiry and last use travel with the token**, and the guard
|
|
22
|
+
* hands the token to the request as `user.currentAccessToken`, where a
|
|
23
|
+
* handler checks `allows()` / `authorize()`.
|
|
24
|
+
*
|
|
25
|
+
* Two places go further than upstream, both named where they happen: the row
|
|
26
|
+
* identifier is random rather than a database counter, and neither verifying
|
|
27
|
+
* nor invalidating touches a row before its secret has been checked.
|
|
28
|
+
*
|
|
29
|
+
* Warden owns no database: rows go through an {@link AccessTokenStore} the
|
|
30
|
+
* application implements over its own table. {@link MemoryAccessTokenStore}
|
|
31
|
+
* serves tests and single-process tools.
|
|
32
|
+
*/
|
|
33
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
34
|
+
import { resolveExpiresInSeconds } from "./duration.js";
|
|
35
|
+
import { E_UNAUTHORIZED_ACCESS, WardenError } from "./errors.js";
|
|
36
|
+
import { decodeTokenValue, encodeTokenValue, safeCompareHashes, } from "./RememberMeToken.js";
|
|
37
|
+
import { Secret } from "./Secret.js";
|
|
38
|
+
/** The polynomial of CRC-32 (IEEE 802.3), reflected. */
|
|
39
|
+
const CRC32_POLYNOMIAL = 0xedb88320;
|
|
40
|
+
const CRC32_TABLE = Array.from({ length: 256 }, (_, n) => {
|
|
41
|
+
let c = n;
|
|
42
|
+
for (let k = 0; k < 8; k++) {
|
|
43
|
+
c = c & 1 ? CRC32_POLYNOMIAL ^ (c >>> 1) : c >>> 1;
|
|
44
|
+
}
|
|
45
|
+
return c >>> 0;
|
|
46
|
+
});
|
|
47
|
+
/**
|
|
48
|
+
* CRC-32 of a string's UTF-8 bytes, as an unsigned integer.
|
|
49
|
+
*
|
|
50
|
+
* Not a security measure — a checksum anyone can compute. It is there so a
|
|
51
|
+
* scanner can tell a real token from random text that happens to carry the
|
|
52
|
+
* prefix, which is what keeps leaked-token alerts precise.
|
|
53
|
+
*/
|
|
54
|
+
export function crc32(input) {
|
|
55
|
+
let crc = 0xffffffff;
|
|
56
|
+
for (const byte of new TextEncoder().encode(input)) {
|
|
57
|
+
crc = (CRC32_TABLE[(crc ^ byte) & 0xff] ?? 0) ^ (crc >>> 8);
|
|
58
|
+
}
|
|
59
|
+
return (crc ^ 0xffffffff) >>> 0;
|
|
60
|
+
}
|
|
61
|
+
/** `sha256(secret)` as hex — what gets stored. */
|
|
62
|
+
function hashOf(secret) {
|
|
63
|
+
return createHash("sha256").update(secret).digest("hex");
|
|
64
|
+
}
|
|
65
|
+
/** A random URL-safe string of exactly `size` characters. */
|
|
66
|
+
function randomString(size) {
|
|
67
|
+
return randomBytes(size).toString("base64url").slice(0, size);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* One access token. Hands out `value` only right after it was minted, because
|
|
71
|
+
* nothing stored can rebuild it.
|
|
72
|
+
*/
|
|
73
|
+
export class AccessToken {
|
|
74
|
+
/**
|
|
75
|
+
* Split a token value into its identifier and secret, or `null` for
|
|
76
|
+
* anything that is not one of ours — a missing prefix, a malformed body.
|
|
77
|
+
* A bad token is an ordinary event, not an exception.
|
|
78
|
+
*/
|
|
79
|
+
static decode(prefix, value) {
|
|
80
|
+
if (typeof value !== "string" || !value.startsWith(prefix))
|
|
81
|
+
return null;
|
|
82
|
+
const decoded = decodeTokenValue(value.slice(prefix.length));
|
|
83
|
+
if (!decoded)
|
|
84
|
+
return null;
|
|
85
|
+
return {
|
|
86
|
+
identifier: decoded.identifier,
|
|
87
|
+
secret: new Secret(decoded.secret),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/** A fresh secret, checksummed, and the hash to store for it. */
|
|
91
|
+
static seed(size) {
|
|
92
|
+
const seed = randomString(size);
|
|
93
|
+
const secret = `${seed}${crc32(seed)}`;
|
|
94
|
+
return { secret: new Secret(secret), hash: hashOf(secret) };
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Everything the persistence layer needs for a new token, without deciding
|
|
98
|
+
* how it is stored. `expiresIn` is in SECONDS; absent, the token never
|
|
99
|
+
* expires.
|
|
100
|
+
*/
|
|
101
|
+
static createTransientToken(userId, size, expiresIn) {
|
|
102
|
+
const expiresAt = expiresIn === undefined
|
|
103
|
+
? undefined
|
|
104
|
+
: new Date(Date.now() + expiresIn * 1000);
|
|
105
|
+
return { userId, expiresAt, ...AccessToken.seed(size) };
|
|
106
|
+
}
|
|
107
|
+
identifier;
|
|
108
|
+
tokenableId;
|
|
109
|
+
type;
|
|
110
|
+
name;
|
|
111
|
+
hash;
|
|
112
|
+
abilities;
|
|
113
|
+
createdAt;
|
|
114
|
+
updatedAt;
|
|
115
|
+
lastUsedAt;
|
|
116
|
+
expiresAt;
|
|
117
|
+
/** The public token value — only on a token just minted. */
|
|
118
|
+
value;
|
|
119
|
+
constructor(attributes) {
|
|
120
|
+
this.identifier = attributes.identifier;
|
|
121
|
+
this.tokenableId = attributes.tokenableId;
|
|
122
|
+
this.type = attributes.type;
|
|
123
|
+
this.name = attributes.name;
|
|
124
|
+
this.hash = attributes.hash;
|
|
125
|
+
this.abilities = [...(attributes.abilities ?? ["*"])];
|
|
126
|
+
this.createdAt = attributes.createdAt;
|
|
127
|
+
this.updatedAt = attributes.updatedAt;
|
|
128
|
+
this.lastUsedAt = attributes.lastUsedAt;
|
|
129
|
+
this.expiresAt = attributes.expiresAt;
|
|
130
|
+
if (attributes.secret) {
|
|
131
|
+
if (attributes.prefix === undefined) {
|
|
132
|
+
throw new WardenError("INVALID_CONFIG", "Cannot compute the token value without its prefix.");
|
|
133
|
+
}
|
|
134
|
+
this.value = new Secret(`${attributes.prefix}${encodeTokenValue(attributes.identifier, attributes.secret.release())}`);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/** Whether the token may do `ability`. `*` means every ability. */
|
|
138
|
+
allows(ability) {
|
|
139
|
+
return this.abilities.includes(ability) || this.abilities.includes("*");
|
|
140
|
+
}
|
|
141
|
+
/** The negation of {@link allows}. */
|
|
142
|
+
denies(ability) {
|
|
143
|
+
return !this.allows(ability);
|
|
144
|
+
}
|
|
145
|
+
/** Throw `E_UNAUTHORIZED_ACCESS` unless the token may do `ability`. */
|
|
146
|
+
authorize(ability) {
|
|
147
|
+
if (this.denies(ability)) {
|
|
148
|
+
throw new E_UNAUTHORIZED_ACCESS("Unauthorized access", {
|
|
149
|
+
guardDriverName: "access_tokens",
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Whether the token is past its expiry. A token with none never expires; an
|
|
155
|
+
* expiry that is not a valid date counts as past, never as "no expiry".
|
|
156
|
+
*/
|
|
157
|
+
isExpired() {
|
|
158
|
+
if (this.expiresAt === null)
|
|
159
|
+
return false;
|
|
160
|
+
return !(this.expiresAt.getTime() > Date.now());
|
|
161
|
+
}
|
|
162
|
+
/** Constant-time check of a presented secret against the stored hash. */
|
|
163
|
+
verify(secret) {
|
|
164
|
+
return safeCompareHashes(this.hash, hashOf(secret.release()));
|
|
165
|
+
}
|
|
166
|
+
/** What a client receives. `token` is present only on a token just minted. */
|
|
167
|
+
toJSON() {
|
|
168
|
+
return {
|
|
169
|
+
type: "bearer",
|
|
170
|
+
name: this.name,
|
|
171
|
+
token: this.value?.release(),
|
|
172
|
+
abilities: this.abilities,
|
|
173
|
+
lastUsedAt: this.lastUsedAt,
|
|
174
|
+
expiresAt: this.expiresAt,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/** In-memory store — for tests and single-process tools. Not for a cluster. */
|
|
179
|
+
export class MemoryAccessTokenStore {
|
|
180
|
+
#rows = new Map();
|
|
181
|
+
async create(token) {
|
|
182
|
+
this.#rows.set(token.identifier, {
|
|
183
|
+
...token,
|
|
184
|
+
abilities: [...token.abilities],
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
async find(identifier) {
|
|
188
|
+
const row = this.#rows.get(identifier);
|
|
189
|
+
return row ? { ...row, abilities: [...row.abilities] } : null;
|
|
190
|
+
}
|
|
191
|
+
async touch(identifier, lastUsedAt) {
|
|
192
|
+
const row = this.#rows.get(identifier);
|
|
193
|
+
if (row)
|
|
194
|
+
this.#rows.set(identifier, { ...row, lastUsedAt });
|
|
195
|
+
}
|
|
196
|
+
async delete(identifier) {
|
|
197
|
+
return this.#rows.delete(identifier);
|
|
198
|
+
}
|
|
199
|
+
async listForUser(userId) {
|
|
200
|
+
return [...this.#rows.values()]
|
|
201
|
+
.filter((row) => String(row.tokenableId) === String(userId))
|
|
202
|
+
.map((row) => ({ ...row, abilities: [...row.abilities] }));
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
/** Fewer random characters than this and a secret becomes guessable. */
|
|
206
|
+
const MIN_SECRET_LENGTH = 32;
|
|
207
|
+
/**
|
|
208
|
+
* Mints, verifies, lists and revokes access tokens over an
|
|
209
|
+
* {@link AccessTokenStore}.
|
|
210
|
+
*/
|
|
211
|
+
export class AccessTokensProvider {
|
|
212
|
+
prefix;
|
|
213
|
+
type;
|
|
214
|
+
tokenSecretLength;
|
|
215
|
+
#store;
|
|
216
|
+
#expiresIn;
|
|
217
|
+
constructor(options) {
|
|
218
|
+
this.#store = options.store;
|
|
219
|
+
this.prefix = options.prefix ?? "oat_";
|
|
220
|
+
this.type = options.type ?? "auth_token";
|
|
221
|
+
this.tokenSecretLength = options.tokenSecretLength ?? 40;
|
|
222
|
+
this.#expiresIn = options.expiresIn;
|
|
223
|
+
if (this.prefix.length === 0 || this.prefix.includes(".")) {
|
|
224
|
+
// The value is split on its first dot after the prefix.
|
|
225
|
+
throw new WardenError("INVALID_CONFIG", `The access-token prefix must be non-empty and carry no ".", got ${JSON.stringify(this.prefix)}`);
|
|
226
|
+
}
|
|
227
|
+
if (!Number.isSafeInteger(this.tokenSecretLength) ||
|
|
228
|
+
this.tokenSecretLength < MIN_SECRET_LENGTH) {
|
|
229
|
+
throw new WardenError("INVALID_CONFIG", `tokenSecretLength must be a whole number of at least ${MIN_SECRET_LENGTH}, got ${String(this.tokenSecretLength)}`);
|
|
230
|
+
}
|
|
231
|
+
// A lifetime that does not parse fails at boot, not at the first mint.
|
|
232
|
+
if (this.#expiresIn !== undefined)
|
|
233
|
+
this.#lifetimeSeconds(this.#expiresIn);
|
|
234
|
+
}
|
|
235
|
+
#lifetimeSeconds(value) {
|
|
236
|
+
return resolveExpiresInSeconds(value, undefined, 0);
|
|
237
|
+
}
|
|
238
|
+
#toToken(row) {
|
|
239
|
+
return new AccessToken(this.#attributes(row));
|
|
240
|
+
}
|
|
241
|
+
#attributes(row) {
|
|
242
|
+
return {
|
|
243
|
+
identifier: row.identifier,
|
|
244
|
+
tokenableId: row.tokenableId,
|
|
245
|
+
type: row.type,
|
|
246
|
+
name: row.name,
|
|
247
|
+
hash: row.hash,
|
|
248
|
+
abilities: row.abilities,
|
|
249
|
+
createdAt: new Date(row.createdAt),
|
|
250
|
+
updatedAt: new Date(row.updatedAt),
|
|
251
|
+
lastUsedAt: row.lastUsedAt === null ? null : new Date(row.lastUsedAt),
|
|
252
|
+
expiresAt: row.expiresAt === null ? null : new Date(row.expiresAt),
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
/** The row, only when it is of this provider's type and belongs to `user`. */
|
|
256
|
+
async #ownedRow(user, identifier) {
|
|
257
|
+
const row = await this.#store.find(identifier);
|
|
258
|
+
if (row === null || row.type !== this.type)
|
|
259
|
+
return null;
|
|
260
|
+
return String(row.tokenableId) === String(user.id) ? row : null;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Mint a token for `user`. The returned token is the only one that carries
|
|
264
|
+
* `value`: hand it to the client now, it cannot be read back later.
|
|
265
|
+
*
|
|
266
|
+
* NAMED DEVIATION — the identifier is 128 random bits, not the row's
|
|
267
|
+
* auto-increment id: a guessable identifier lets anyone enumerate rows and
|
|
268
|
+
* aim at a given user's token.
|
|
269
|
+
*/
|
|
270
|
+
async create(user, abilities = ["*"], options = {}) {
|
|
271
|
+
const expiresIn = options.expiresIn ?? this.#expiresIn;
|
|
272
|
+
const transient = AccessToken.createTransientToken(user.id, this.tokenSecretLength, expiresIn === undefined ? undefined : this.#lifetimeSeconds(expiresIn));
|
|
273
|
+
const now = Date.now();
|
|
274
|
+
const row = {
|
|
275
|
+
identifier: randomBytes(16).toString("hex"),
|
|
276
|
+
tokenableId: transient.userId,
|
|
277
|
+
type: this.type,
|
|
278
|
+
name: options.name ?? null,
|
|
279
|
+
hash: transient.hash,
|
|
280
|
+
abilities: [...abilities],
|
|
281
|
+
createdAt: now,
|
|
282
|
+
updatedAt: now,
|
|
283
|
+
lastUsedAt: null,
|
|
284
|
+
expiresAt: transient.expiresAt?.getTime() ?? null,
|
|
285
|
+
};
|
|
286
|
+
await this.#store.create(row);
|
|
287
|
+
return new AccessToken({
|
|
288
|
+
...this.#attributes(row),
|
|
289
|
+
prefix: this.prefix,
|
|
290
|
+
secret: transient.secret,
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
/** One of `user`'s tokens by identifier, or `null`. */
|
|
294
|
+
async find(user, identifier) {
|
|
295
|
+
const row = await this.#ownedRow(user, identifier);
|
|
296
|
+
return row === null ? null : this.#toToken(row);
|
|
297
|
+
}
|
|
298
|
+
/** Delete one of `user`'s tokens; answers how many rows went (0 or 1). */
|
|
299
|
+
async delete(user, identifier) {
|
|
300
|
+
const row = await this.#ownedRow(user, identifier);
|
|
301
|
+
if (row === null)
|
|
302
|
+
return 0;
|
|
303
|
+
return (await this.#store.delete(row.identifier)) ? 1 : 0;
|
|
304
|
+
}
|
|
305
|
+
/** Delete every token of `user` of this provider's type. */
|
|
306
|
+
async deleteAll(user) {
|
|
307
|
+
let deleted = 0;
|
|
308
|
+
for (const row of await this.#store.listForUser(user.id)) {
|
|
309
|
+
if (row.type !== this.type)
|
|
310
|
+
continue;
|
|
311
|
+
if (await this.#store.delete(row.identifier))
|
|
312
|
+
deleted++;
|
|
313
|
+
}
|
|
314
|
+
return deleted;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Every token of `user` of this provider's type: most recently used first,
|
|
318
|
+
* never-used ones last, newest first among equals.
|
|
319
|
+
*/
|
|
320
|
+
async all(user) {
|
|
321
|
+
const rows = (await this.#store.listForUser(user.id)).filter((row) => row.type === this.type);
|
|
322
|
+
rows.sort((a, b) => (b.lastUsedAt ?? Number.NEGATIVE_INFINITY) -
|
|
323
|
+
(a.lastUsedAt ?? Number.NEGATIVE_INFINITY) ||
|
|
324
|
+
b.createdAt - a.createdAt);
|
|
325
|
+
return rows.map((row) => this.#toToken(row));
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* The token behind a presented value, or `null` for anything wrong: not one
|
|
329
|
+
* of ours, unknown, of another type, a secret that does not match, expired.
|
|
330
|
+
* The caller cannot tell these apart, which is deliberate.
|
|
331
|
+
*
|
|
332
|
+
* NAMED DEVIATION — upstream records the use before it compares the secret,
|
|
333
|
+
* so anyone who knows an identifier can move a token's "last used" date.
|
|
334
|
+
* Here only a verified token is touched.
|
|
335
|
+
*/
|
|
336
|
+
async verify(tokenValue) {
|
|
337
|
+
const decoded = AccessToken.decode(this.prefix, tokenValue.release());
|
|
338
|
+
if (!decoded)
|
|
339
|
+
return null;
|
|
340
|
+
const row = await this.#store.find(decoded.identifier);
|
|
341
|
+
if (row === null || row.type !== this.type)
|
|
342
|
+
return null;
|
|
343
|
+
const token = this.#toToken(row);
|
|
344
|
+
if (!token.verify(decoded.secret) || token.isExpired())
|
|
345
|
+
return null;
|
|
346
|
+
const now = Date.now();
|
|
347
|
+
await this.#store.touch(row.identifier, now);
|
|
348
|
+
return this.#toToken({ ...row, lastUsedAt: now });
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Revoke the token behind a presented value. Answers whether one went.
|
|
352
|
+
*
|
|
353
|
+
* NAMED DEVIATION — upstream deletes from the identifier alone. Here the
|
|
354
|
+
* secret must match first: an identifier is not a credential, and without
|
|
355
|
+
* the check anyone who learned one (a log line, a token listing) could
|
|
356
|
+
* revoke someone else's token.
|
|
357
|
+
*/
|
|
358
|
+
async invalidate(tokenValue) {
|
|
359
|
+
const decoded = AccessToken.decode(this.prefix, tokenValue.release());
|
|
360
|
+
if (!decoded)
|
|
361
|
+
return false;
|
|
362
|
+
const row = await this.#store.find(decoded.identifier);
|
|
363
|
+
if (row === null || row.type !== this.type)
|
|
364
|
+
return false;
|
|
365
|
+
if (!this.#toToken(row).verify(decoded.secret))
|
|
366
|
+
return false;
|
|
367
|
+
return this.#store.delete(row.identifier);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
//# sourceMappingURL=AccessToken.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":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AAO,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,SAAS,EAAE;YAAE,OAAO,IAAI,CAAC;QACpE,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACvB,MAAM,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC;QAC7C,OAAO,IAAI,CAAC,QAAQ,CAAC,EAAE,GAAG,GAAG,EAAE,UAAU,EAAE,GAAG,EAAE,CAAC,CAAC;IACnD,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,UAAU,CAAC,UAA0B;QAC1C,MAAM,OAAO,GAAG,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,UAAU,CAAC,OAAO,EAAE,CAAC,CAAC;QACtE,IAAI,CAAC,OAAO;YAAE,OAAO,KAAK,CAAC;QAC3B,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;QACvD,IAAI,GAAG,KAAK,IAAI,IAAI,GAAG,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI;YAAE,OAAO,KAAK,CAAC;QACzD,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC;YAAE,OAAO,KAAK,CAAC;QAC7D,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAC3C,CAAC;CACD"}
|
package/dist/AttemptStore.d.ts
CHANGED
|
@@ -12,7 +12,10 @@
|
|
|
12
12
|
*/
|
|
13
13
|
/** A window of attempts counted under one key. */
|
|
14
14
|
export interface AttemptCount {
|
|
15
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* Attempts in the current window, this one included. `Infinity` when the
|
|
17
|
+
* store could not count the attempt at all, which refuses it.
|
|
18
|
+
*/
|
|
16
19
|
count: number;
|
|
17
20
|
/** Seconds until the window ends and the count starts over. */
|
|
18
21
|
resetSeconds: number;
|
|
@@ -28,7 +31,18 @@ export interface AttemptStore {
|
|
|
28
31
|
/** Forget `key`'s attempts — after a success. */
|
|
29
32
|
reset(key: string): Promise<void>;
|
|
30
33
|
}
|
|
31
|
-
/**
|
|
34
|
+
/**
|
|
35
|
+
* In this process's memory, bounded. Not shared across instances.
|
|
36
|
+
*
|
|
37
|
+
* When the bound is reached and every window is still live, a NEW key is
|
|
38
|
+
* refused — its attempt reported as over any limit — rather than a live window
|
|
39
|
+
* evicted to make room. Evicting forgave the oldest counters, and the keys are
|
|
40
|
+
* attacker-chosen (`id:<identifier>`): a flood of made-up identifiers erased a
|
|
41
|
+
* victim's lockout, and the attacker's own, and the guessing resumed. Refusing
|
|
42
|
+
* keeps every lockout in place; what it costs during such a flood is that
|
|
43
|
+
* first attempts under new keys wait until windows expire. A deployment that
|
|
44
|
+
* cannot accept that uses {@link RedisAttemptStore}, which has no such bound.
|
|
45
|
+
*/
|
|
32
46
|
export declare class MemoryAttemptStore implements AttemptStore {
|
|
33
47
|
#private;
|
|
34
48
|
constructor(options?: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AttemptStore.d.ts","sourceRoot":"","sources":["../src/AttemptStore.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,kDAAkD;AAClD,MAAM,WAAW,YAAY;IAC5B
|
|
1
|
+
{"version":3,"file":"AttemptStore.d.ts","sourceRoot":"","sources":["../src/AttemptStore.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,kDAAkD;AAClD,MAAM,WAAW,YAAY;IAC5B;;;OAGG;IACH,KAAK,EAAE,MAAM,CAAC;IACd,+DAA+D;IAC/D,YAAY,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,YAAY;IAC5B;;;OAGG;IACH,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG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AAC,EAAE,MAAM,CAAA;KAAO;IAYnE,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IAkBpE,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAOnC,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAIvC"}
|
package/dist/AttemptStore.js
CHANGED
|
@@ -12,7 +12,18 @@
|
|
|
12
12
|
*/
|
|
13
13
|
/** Most keys the memory store holds before it makes room. */
|
|
14
14
|
const DEFAULT_MAX_KEYS = 100_000;
|
|
15
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* In this process's memory, bounded. Not shared across instances.
|
|
17
|
+
*
|
|
18
|
+
* When the bound is reached and every window is still live, a NEW key is
|
|
19
|
+
* refused — its attempt reported as over any limit — rather than a live window
|
|
20
|
+
* evicted to make room. Evicting forgave the oldest counters, and the keys are
|
|
21
|
+
* attacker-chosen (`id:<identifier>`): a flood of made-up identifiers erased a
|
|
22
|
+
* victim's lockout, and the attacker's own, and the guessing resumed. Refusing
|
|
23
|
+
* keeps every lockout in place; what it costs during such a flood is that
|
|
24
|
+
* first attempts under new keys wait until windows expire. A deployment that
|
|
25
|
+
* cannot accept that uses {@link RedisAttemptStore}, which has no such bound.
|
|
26
|
+
*/
|
|
16
27
|
export class MemoryAttemptStore {
|
|
17
28
|
#entries = new Map();
|
|
18
29
|
#maxKeys;
|
|
@@ -24,7 +35,10 @@ export class MemoryAttemptStore {
|
|
|
24
35
|
let entry = this.#entries.get(key);
|
|
25
36
|
if (entry === undefined || entry.resetAt <= now) {
|
|
26
37
|
this.#entries.delete(key);
|
|
27
|
-
this.#makeRoom(now);
|
|
38
|
+
const wait = this.#makeRoom(now);
|
|
39
|
+
if (wait !== undefined) {
|
|
40
|
+
return { count: Number.POSITIVE_INFINITY, resetSeconds: wait };
|
|
41
|
+
}
|
|
28
42
|
entry = { count: 0, resetAt: now + windowSeconds * 1000 };
|
|
29
43
|
this.#entries.set(key, entry);
|
|
30
44
|
}
|
|
@@ -48,23 +62,23 @@ export class MemoryAttemptStore {
|
|
|
48
62
|
this.#entries.delete(key);
|
|
49
63
|
}
|
|
50
64
|
/**
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
* bound would take the process down instead.
|
|
65
|
+
* Make room for one more key by dropping the expired windows. A live window
|
|
66
|
+
* is never dropped: that would forgive its attempts. Returns `undefined`
|
|
67
|
+
* when there is room, else the seconds until the earliest live window ends.
|
|
55
68
|
*/
|
|
56
69
|
#makeRoom(now) {
|
|
57
70
|
if (this.#entries.size < this.#maxKeys)
|
|
58
|
-
return;
|
|
71
|
+
return undefined;
|
|
72
|
+
let earliest = Number.POSITIVE_INFINITY;
|
|
59
73
|
for (const [key, entry] of this.#entries) {
|
|
60
74
|
if (entry.resetAt <= now)
|
|
61
75
|
this.#entries.delete(key);
|
|
76
|
+
else
|
|
77
|
+
earliest = Math.min(earliest, entry.resetAt);
|
|
62
78
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
this.#entries.delete(key);
|
|
67
|
-
}
|
|
79
|
+
if (this.#entries.size < this.#maxKeys)
|
|
80
|
+
return undefined;
|
|
81
|
+
return Math.max(1, Math.ceil((earliest - now) / 1000));
|
|
68
82
|
}
|
|
69
83
|
}
|
|
70
84
|
/**
|
package/dist/AttemptStore.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AttemptStore.js","sourceRoot":"","sources":["../src/AttemptStore.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;
|
|
1
|
+
{"version":3,"file":"AttemptStore.js","sourceRoot":"","sources":["../src/AttemptStore.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAyBH,6DAA6D;AAC7D,MAAM,gBAAgB,GAAG,OAAO,CAAC;AAEjC;;;;;;;;;;;GA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AAC,KAAK,CAAC,GAAW;QACtB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,EAAE,CAAC;QACpC,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,OAAO,IAAI,GAAG,EAAE,CAAC,CAAC;QACzD,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC;QACjC,OAAO,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IAChD,CAAC;IAED,KAAK,CAAC,KAAK,CAAC,GAAW;QACtB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,EAAE,CAAC;QACpC,MAAM,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,OAAO,IAAI,GAAG,EAAE,CAAC,CAAC;IAC5C,CAAC;CACD"}
|
package/dist/AuthManager.d.ts
CHANGED
|
@@ -8,10 +8,38 @@ import type { WardenContext } from "./middleware.js";
|
|
|
8
8
|
import { RightsResolver } from "./rights/RightsResolver.js";
|
|
9
9
|
import type { EffectivePermissions, Scope } from "./rights/types.js";
|
|
10
10
|
import type { SessionGuardState, SessionStore } from "./strategies/SessionStrategy.js";
|
|
11
|
+
/**
|
|
12
|
+
* The token a request authenticated with, when it authenticated with one: an
|
|
13
|
+
* access token, or an OAuth token from a guard such as visa's. Its abilities
|
|
14
|
+
* are what the TOKEN may do, a separate axis from `UserPayload.permissions`
|
|
15
|
+
* (what the USER may do): a request may act only where both agree.
|
|
16
|
+
*/
|
|
17
|
+
export interface CurrentAccessToken {
|
|
18
|
+
readonly abilities: readonly string[];
|
|
19
|
+
/** Whether the token may do `ability` (`*` grants every ability). */
|
|
20
|
+
allows(ability: string): boolean;
|
|
21
|
+
denies(ability: string): boolean;
|
|
22
|
+
/** Throw unless the token may do `ability`. */
|
|
23
|
+
authorize(ability: string): void;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Whether the token the user authenticated with, if any, allows `permission`.
|
|
27
|
+
*
|
|
28
|
+
* A permission check on a request made with a token is an INTERSECTION: the
|
|
29
|
+
* user must hold the permission AND the token must allow it. The token never
|
|
30
|
+
* widens what the user may do (a same-named ability grants nothing the user
|
|
31
|
+
* lacks), and it narrows it: a token limited to a few abilities does not carry
|
|
32
|
+
* every permission of its user. A user with no `currentAccessToken` (a session,
|
|
33
|
+
* a JWT) is not restricted here.
|
|
34
|
+
*/
|
|
35
|
+
export declare function tokenAllows(user: UserPayload, permission: string): boolean;
|
|
11
36
|
export interface UserPayload {
|
|
12
37
|
id: string;
|
|
13
38
|
roles?: string[];
|
|
39
|
+
/** The user's own permissions. Never widened by a token's abilities. */
|
|
14
40
|
permissions?: string[];
|
|
41
|
+
/** Set by a token guard: the token this request authenticated with. */
|
|
42
|
+
currentAccessToken?: CurrentAccessToken;
|
|
15
43
|
[key: string]: unknown;
|
|
16
44
|
}
|
|
17
45
|
export interface AuthResult {
|
|
@@ -160,12 +188,15 @@ export declare class AuthManager {
|
|
|
160
188
|
/**
|
|
161
189
|
* Check if a user has a specific permission within a scope (default
|
|
162
190
|
* `"global"`). Derived from roles + store grants ONLY — token
|
|
163
|
-
* `user.permissions` is NOT read (D1, cerebrum 459).
|
|
191
|
+
* `user.permissions` is NOT read (D1, cerebrum 459). When the user came in
|
|
192
|
+
* with a token (`currentAccessToken`), the token must allow it too: see
|
|
193
|
+
* {@link tokenAllows}.
|
|
164
194
|
*/
|
|
165
195
|
hasPermission(user: UserPayload, permission: string, scope?: Scope): Promise<boolean>;
|
|
166
196
|
/**
|
|
167
197
|
* Check if a user has ALL required permissions within a scope (default
|
|
168
|
-
* `"global"`). Empty list ⇒ vacuously true.
|
|
198
|
+
* `"global"`). Empty list ⇒ vacuously true. Intersected with the token's
|
|
199
|
+
* abilities like {@link hasPermission}.
|
|
169
200
|
*/
|
|
170
201
|
hasAllPermissions(user: UserPayload, permissions: string[], scope?: Scope): Promise<boolean>;
|
|
171
202
|
/** Get a registered strategy by name. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AuthManager.d.ts","sourceRoot":"","sources":["../src/AuthManager.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAErD,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAC5D,OAAO,KAAK,EAAE,oBAAoB,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAErE,OAAO,KAAK,EACX,iBAAiB,EACjB,YAAY,EACZ,MAAM,iCAAiC,CAAC;AAEzC,MAAM,WAAW,WAAW;IAC3B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;IACvB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACvB;AAED,MAAM,WAAW,UAAU;IAC1B,aAAa,EAAE,OAAO,CAAC;IACvB,IAAI,CAAC,EAAE,WAAW,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;;;OAMG;IACH,aAAa,CAAC,EAAE,IAAI,CAAC;CACrB;AAED;;;;;;GAMG;AACH,MAAM,WAAW,kBAAkB;IAClC,qCAAqC;IACrC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,iDAAiD;IACjD,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,uBAAuB;IACvB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACjC;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,eAAe,CAAC,UAAU,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAGtE;AAED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I,MAAM,CAEhC;IAED,0CAA0C;IAC1C,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,YAAY,GAAG,IAAI;IAI5D,yCAAyC;IACzC,gBAAgB,IAAI,MAAM,EAAE;IAI5B;;;;;;;;;OASG;IACG,KAAK,CACV,IAAI,EAAE,WAAW,EACjB,OAAO,EAAE,YAAY,EACrB,YAAY,CAAC,EAAE,MAAM,EACrB,KAAK,CAAC,EAAE,iBAAiB,GACvB,OAAO,CAAC,IAAI,CAAC;IAwBhB;;;;;;OAMG;IACG,8BAA8B,CACnC,WAAW,EAAE,OAAO,EACpB,YAAY,CAAC,EAAE,MAAM,EACrB,KAAK,CAAC,EAAE,iBAAiB,GACvB,OAAO,CAAC;QAAE,IAAI,EAAE,WAAW,CAAC;QAAC,WAAW,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC;IAc7D;;;;OAIG;IACG,MAAM,CACX,OAAO,EAAE,YAAY,EACrB,YAAY,CAAC,EAAE,MAAM,EACrB,KAAK,CAAC,EAAE,iBAAiB,GACvB,OAAO,CAAC,IAAI,CAAC;CAehB;AAqDD,kEAAkE;AAClE;;;;;;GAMG"}
|
package/dist/AuthManager.js
CHANGED
|
@@ -8,6 +8,20 @@ import { WardenError } from "./errors.js";
|
|
|
8
8
|
import { MemoryRightsStore } from "./rights/MemoryRightsStore.js";
|
|
9
9
|
import { RightsResolver } from "./rights/RightsResolver.js";
|
|
10
10
|
import { sanitizePayload } from "./sanitize.js";
|
|
11
|
+
/**
|
|
12
|
+
* Whether the token the user authenticated with, if any, allows `permission`.
|
|
13
|
+
*
|
|
14
|
+
* A permission check on a request made with a token is an INTERSECTION: the
|
|
15
|
+
* user must hold the permission AND the token must allow it. The token never
|
|
16
|
+
* widens what the user may do (a same-named ability grants nothing the user
|
|
17
|
+
* lacks), and it narrows it: a token limited to a few abilities does not carry
|
|
18
|
+
* every permission of its user. A user with no `currentAccessToken` (a session,
|
|
19
|
+
* a JWT) is not restricted here.
|
|
20
|
+
*/
|
|
21
|
+
export function tokenAllows(user, permission) {
|
|
22
|
+
const token = user.currentAccessToken;
|
|
23
|
+
return token === undefined || token.allows(permission);
|
|
24
|
+
}
|
|
11
25
|
/**
|
|
12
26
|
* The event namespace for a guard, taken from its DRIVER.
|
|
13
27
|
*
|
|
@@ -190,16 +204,23 @@ export class AuthManager {
|
|
|
190
204
|
/**
|
|
191
205
|
* Check if a user has a specific permission within a scope (default
|
|
192
206
|
* `"global"`). Derived from roles + store grants ONLY — token
|
|
193
|
-
* `user.permissions` is NOT read (D1, cerebrum 459).
|
|
207
|
+
* `user.permissions` is NOT read (D1, cerebrum 459). When the user came in
|
|
208
|
+
* with a token (`currentAccessToken`), the token must allow it too: see
|
|
209
|
+
* {@link tokenAllows}.
|
|
194
210
|
*/
|
|
195
211
|
async hasPermission(user, permission, scope = "global") {
|
|
212
|
+
if (!tokenAllows(user, permission))
|
|
213
|
+
return false;
|
|
196
214
|
return (await this.resolvePermissions(user, scope)).has(permission);
|
|
197
215
|
}
|
|
198
216
|
/**
|
|
199
217
|
* Check if a user has ALL required permissions within a scope (default
|
|
200
|
-
* `"global"`). Empty list ⇒ vacuously true.
|
|
218
|
+
* `"global"`). Empty list ⇒ vacuously true. Intersected with the token's
|
|
219
|
+
* abilities like {@link hasPermission}.
|
|
201
220
|
*/
|
|
202
221
|
async hasAllPermissions(user, permissions, scope = "global") {
|
|
222
|
+
if (!permissions.every((p) => tokenAllows(user, p)))
|
|
223
|
+
return false;
|
|
203
224
|
return (await this.resolvePermissions(user, scope)).hasAll(permissions);
|
|
204
225
|
}
|
|
205
226
|
/** Get a registered strategy by name. */
|