@c9up/warden 0.2.5 → 0.2.7
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 +74 -5
- 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 +38 -12
- package/dist/AttemptStore.js.map +1 -1
- package/dist/AuthManager.d.ts +44 -8
- package/dist/AuthManager.d.ts.map +1 -1
- package/dist/AuthManager.js +42 -11
- package/dist/AuthManager.js.map +1 -1
- package/dist/AuthRateLimiter.d.ts +16 -3
- package/dist/AuthRateLimiter.d.ts.map +1 -1
- package/dist/AuthRateLimiter.js +25 -6
- package/dist/AuthRateLimiter.js.map +1 -1
- package/dist/Authenticator.d.ts +98 -10
- package/dist/Authenticator.d.ts.map +1 -1
- package/dist/Authenticator.js +223 -19
- 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/RedisBlacklistDriver.d.ts.map +1 -1
- package/dist/RedisBlacklistDriver.js +11 -0
- package/dist/RedisBlacklistDriver.js.map +1 -1
- package/dist/RefreshTokenStore.d.ts +37 -6
- package/dist/RefreshTokenStore.d.ts.map +1 -1
- package/dist/RefreshTokenStore.js +51 -10
- package/dist/RefreshTokenStore.js.map +1 -1
- package/dist/ResilientBlacklistDriver.d.ts +23 -6
- package/dist/ResilientBlacklistDriver.d.ts.map +1 -1
- package/dist/ResilientBlacklistDriver.js +26 -9
- package/dist/ResilientBlacklistDriver.js.map +1 -1
- package/dist/TokenBlacklist.d.ts +3 -1
- package/dist/TokenBlacklist.d.ts.map +1 -1
- package/dist/TokenBlacklist.js +27 -1
- package/dist/TokenBlacklist.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/authFinder.d.ts +58 -0
- package/dist/authFinder.d.ts.map +1 -0
- package/dist/authFinder.js +77 -0
- package/dist/authFinder.js.map +1 -0
- package/dist/bearer.d.ts +12 -0
- package/dist/bearer.d.ts.map +1 -0
- package/dist/bearer.js +19 -0
- package/dist/bearer.js.map +1 -0
- package/dist/bouncer/Bouncer.d.ts +2 -2
- package/dist/bouncer/Bouncer.d.ts.map +1 -1
- package/dist/bouncer/Bouncer.js +17 -4
- package/dist/bouncer/Bouncer.js.map +1 -1
- package/dist/bouncer/PolicyAuthorizer.d.ts +1 -1
- package/dist/bouncer/PolicyAuthorizer.d.ts.map +1 -1
- package/dist/bouncer/PolicyAuthorizer.js +1 -1
- package/dist/bouncer/PolicyAuthorizer.js.map +1 -1
- package/dist/bouncer/evaluate.d.ts +5 -1
- package/dist/bouncer/evaluate.d.ts.map +1 -1
- package/dist/bouncer/evaluate.js +7 -6
- package/dist/bouncer/evaluate.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/errors.d.ts +28 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +63 -2
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +10 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -4
- package/dist/index.js.map +1 -1
- package/dist/mfa/MfaManager.d.ts +33 -8
- package/dist/mfa/MfaManager.d.ts.map +1 -1
- package/dist/mfa/MfaManager.js +69 -20
- package/dist/mfa/MfaManager.js.map +1 -1
- package/dist/mfa/TotpProvider.d.ts.map +1 -1
- package/dist/mfa/TotpProvider.js +8 -0
- package/dist/mfa/TotpProvider.js.map +1 -1
- package/dist/mfa/WebauthnProvider.d.ts +22 -0
- package/dist/mfa/WebauthnProvider.d.ts.map +1 -1
- package/dist/mfa/WebauthnProvider.js +57 -12
- package/dist/mfa/WebauthnProvider.js.map +1 -1
- package/dist/middleware.d.ts +13 -3
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +28 -6
- 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/rights/RightsResolver.d.ts +10 -0
- package/dist/rights/RightsResolver.d.ts.map +1 -1
- package/dist/rights/RightsResolver.js +23 -0
- package/dist/rights/RightsResolver.js.map +1 -1
- package/dist/sanitize.d.ts.map +1 -1
- package/dist/sanitize.js +3 -1
- package/dist/sanitize.js.map +1 -1
- package/dist/services/main.d.ts.map +1 -1
- package/dist/services/main.js +12 -2
- package/dist/services/main.js.map +1 -1
- package/dist/standalone.d.ts +24 -3
- package/dist/standalone.d.ts.map +1 -1
- package/dist/standalone.js +22 -32
- package/dist/standalone.js.map +1 -1
- package/dist/strategies/ApiKeyStrategy.d.ts +46 -12
- package/dist/strategies/ApiKeyStrategy.d.ts.map +1 -1
- package/dist/strategies/ApiKeyStrategy.js +76 -37
- package/dist/strategies/ApiKeyStrategy.js.map +1 -1
- package/dist/strategies/BasicAuthStrategy.d.ts.map +1 -1
- package/dist/strategies/BasicAuthStrategy.js +5 -2
- package/dist/strategies/BasicAuthStrategy.js.map +1 -1
- package/dist/strategies/JwtStrategy.d.ts +6 -2
- package/dist/strategies/JwtStrategy.d.ts.map +1 -1
- package/dist/strategies/JwtStrategy.js +58 -11
- package/dist/strategies/JwtStrategy.js.map +1 -1
- package/dist/strategies/SessionStrategy.d.ts.map +1 -1
- package/dist/strategies/SessionStrategy.js +4 -1
- package/dist/strategies/SessionStrategy.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 +44 -3
- package/dist/vendor/quasarConnection.d.ts.map +1 -1
- package/dist/vendor/quasarConnection.js +64 -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.linux-arm64-gnu.node +0 -0
- package/index.linux-x64-gnu.node +0 -0
- package/index.win32-x64-msvc.node +0 -0
- package/package.json +9 -5
- package/src/AccessToken.ts +550 -0
- package/src/AttemptStore.ts +42 -13
- package/src/AuthManager.ts +75 -18
- package/src/AuthRateLimiter.ts +24 -8
- package/src/Authenticator.ts +282 -22
- package/src/Guard.ts +18 -5
- package/src/RedisBlacklistDriver.ts +11 -0
- package/src/RefreshTokenStore.ts +79 -13
- package/src/ResilientBlacklistDriver.ts +30 -9
- package/src/TokenBlacklist.ts +28 -1
- package/src/WardenProvider.ts +6 -83
- package/src/authFinder.ts +118 -0
- package/src/bearer.ts +16 -0
- package/src/bouncer/Bouncer.ts +21 -6
- package/src/bouncer/PolicyAuthorizer.ts +1 -1
- package/src/bouncer/evaluate.ts +7 -10
- package/src/config.ts +8 -4
- package/src/errors.ts +80 -2
- package/src/index.ts +29 -0
- package/src/mfa/MfaManager.ts +98 -22
- package/src/mfa/TotpProvider.ts +15 -2
- package/src/mfa/WebauthnProvider.ts +77 -13
- package/src/middleware.ts +52 -8
- package/src/native.ts +20 -4
- package/src/quasar.ts +13 -2
- package/src/rights/RightsResolver.ts +27 -0
- package/src/sanitize.ts +4 -2
- package/src/services/main.ts +14 -2
- package/src/standalone.ts +75 -37
- package/src/strategies/ApiKeyStrategy.ts +117 -42
- package/src/strategies/BasicAuthStrategy.ts +7 -2
- package/src/strategies/JwtStrategy.ts +61 -10
- package/src/strategies/SessionStrategy.ts +4 -1
- package/src/templateTags.ts +117 -0
- package/src/vendor/nativeBinary.ts +82 -6
- package/src/vendor/quasarConnection.ts +105 -22
- package/stubs/config/auth.stub +3 -0
- package/scripts/build-napi-types.mjs +0 -69
- package/scripts/copy-napi.mjs +0 -75
- package/scripts/generate-napi-types.mjs +0 -156
|
@@ -1,42 +1,59 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* RefreshTokenStore — server-side opaque refresh token management.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* Opaque token pattern:
|
|
5
5
|
* { accessToken, refreshToken, expiresIn }
|
|
6
6
|
* POST /auth/refresh → new pair, old token invalidated
|
|
7
7
|
*
|
|
8
|
+
* A refresh token is a bearer credential, so only `sha256(value)` is
|
|
9
|
+
* persisted, like access tokens, remember-me tokens, OTPs and backup codes: a
|
|
10
|
+
* dump of the table is not a set of working tokens. The driver is keyed by
|
|
11
|
+
* that hash and never sees the value; the application hashes what the client
|
|
12
|
+
* presents with {@link hashRefreshToken} before asking the driver.
|
|
13
|
+
*
|
|
14
|
+
* Cutover from a store that held the value itself: fail closed. Rows written
|
|
15
|
+
* before hashing are keyed by the clear value and no presented token hashes
|
|
16
|
+
* to it, so they simply stop refreshing and those clients sign in again.
|
|
17
|
+
* Nothing re-reads a clear value to upgrade it: that would keep the clear
|
|
18
|
+
* column, the very thing this removes, readable as a credential.
|
|
19
|
+
*
|
|
8
20
|
* @implements MISS-5
|
|
9
21
|
*/
|
|
10
22
|
import { randomBytes } from "node:crypto";
|
|
23
|
+
import { WardenError } from "./errors.js";
|
|
24
|
+
import { hashSecret } from "./RememberMeToken.js";
|
|
11
25
|
/** In-memory driver for development. */
|
|
12
26
|
export class MemoryRefreshTokenDriver {
|
|
13
27
|
#tokens = new Map();
|
|
14
28
|
async store(token) {
|
|
15
|
-
this.#tokens.set(token.
|
|
29
|
+
this.#tokens.set(token.hash, token);
|
|
16
30
|
}
|
|
17
|
-
async find(
|
|
18
|
-
const stored = this.#tokens.get(
|
|
31
|
+
async find(hash) {
|
|
32
|
+
const stored = this.#tokens.get(hash);
|
|
19
33
|
if (!stored)
|
|
20
34
|
return null;
|
|
21
|
-
|
|
22
|
-
|
|
35
|
+
// "Not in the future" rather than "in the past": an expiry that is not
|
|
36
|
+
// a number counts as expired instead of as never.
|
|
37
|
+
if (!(stored.expiresAt > Date.now())) {
|
|
38
|
+
this.#tokens.delete(hash);
|
|
23
39
|
return null;
|
|
24
40
|
}
|
|
25
41
|
return stored;
|
|
26
42
|
}
|
|
27
|
-
async revoke(
|
|
28
|
-
this.#tokens.delete(
|
|
43
|
+
async revoke(hash) {
|
|
44
|
+
this.#tokens.delete(hash);
|
|
29
45
|
}
|
|
30
46
|
async revokeAllForUser(userId) {
|
|
47
|
+
const owner = String(userId);
|
|
31
48
|
for (const [key, val] of this.#tokens) {
|
|
32
|
-
if (val.userId ===
|
|
49
|
+
if (String(val.userId) === owner)
|
|
33
50
|
this.#tokens.delete(key);
|
|
34
51
|
}
|
|
35
52
|
}
|
|
36
53
|
async cleanup() {
|
|
37
54
|
const now = Date.now();
|
|
38
55
|
for (const [key, val] of this.#tokens) {
|
|
39
|
-
if (val.expiresAt
|
|
56
|
+
if (!(val.expiresAt > now))
|
|
40
57
|
this.#tokens.delete(key);
|
|
41
58
|
}
|
|
42
59
|
}
|
|
@@ -45,4 +62,28 @@ export class MemoryRefreshTokenDriver {
|
|
|
45
62
|
export function generateRefreshToken() {
|
|
46
63
|
return randomBytes(48).toString("base64url");
|
|
47
64
|
}
|
|
65
|
+
/** `sha256(value)` as hex: what is stored, and what the driver is asked for. */
|
|
66
|
+
export function hashRefreshToken(value) {
|
|
67
|
+
return hashSecret(value);
|
|
68
|
+
}
|
|
69
|
+
/** Mint a refresh token for `userId`, valid for `ttlSeconds`. */
|
|
70
|
+
export function mintRefreshToken(userId, ttlSeconds) {
|
|
71
|
+
// NaN or Infinity is not a lifetime: the row would never expire.
|
|
72
|
+
if (!Number.isFinite(ttlSeconds) || ttlSeconds <= 0) {
|
|
73
|
+
throw new WardenError("E_WARDEN_TOKEN_TTL_INVALID", `ttlSeconds must be a positive finite number, got ${String(ttlSeconds)}.`, {
|
|
74
|
+
hint: "Give the refresh token a lifetime in seconds, e.g. 2592000 (30 days).",
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
const value = generateRefreshToken();
|
|
78
|
+
const now = Date.now();
|
|
79
|
+
return {
|
|
80
|
+
stored: {
|
|
81
|
+
hash: hashRefreshToken(value),
|
|
82
|
+
userId,
|
|
83
|
+
expiresAt: now + ttlSeconds * 1000,
|
|
84
|
+
createdAt: now,
|
|
85
|
+
},
|
|
86
|
+
value,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
48
89
|
//# sourceMappingURL=RefreshTokenStore.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RefreshTokenStore.js","sourceRoot":"","sources":["../src/RefreshTokenStore.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"RefreshTokenStore.js","sourceRoot":"","sources":["../src/RefreshTokenStore.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AA0BlD,wCAAwC;AACxC,MAAM,OAAO,wBAAwB;IACpC,OAAO,GAAoC,IAAI,GAAG,EAAE,CAAC;IAErD,KAAK,CAAC,KAAK,CAAC,KAAyB;QACpC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IACrC,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,IAAY;QACtB,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACtC,IAAI,CAAC,MAA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C,CAAC;AAC1B,CAAC;AASD,iEAAiE;AACjE,MAAM,UAAU,gBAAgB,CAC/B,MAAuB,EACvB,UAAkB;IAElB,iEAAiE;IACjE,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,UAAU,CAAC,IAAI,UAAU,IAAI,CAAC,EAAE,CAAC;QACrD,MAAM,IAAI,WAAW,CACpB,4BAA4B,EAC5B,oDAAoD,MAAM,CAAC,UAAU,CAAC,GAAG,EACzE;YACC,IAAI,EAAE,uEAAuE;SAC7E,CACD,CAAC;IACH,CAAC;IACD,MAAM,KAAK,GAAG,oBAAoB,EAAE,CAAC;IACrC,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IACvB,OAAO;QACN,MAAM,EAAE;YACP,IAAI,EAAE,gBAAgB,CAAC,KAAK,CAAC;YAC7B,MAAM;YACN,SAAS,EAAE,GAAG,GAAG,UAAU,GAAG,IAAI;YAClC,SAAS,EAAE,GAAG;SACd;QACD,KAAK;KACL,CAAC;AACH,CAAC"}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* `RedisBlacklistDriver`) with an in-memory fallback so a cache outage never
|
|
4
4
|
* takes the whole app down.
|
|
5
5
|
*
|
|
6
|
-
* Policy (fail-
|
|
6
|
+
* Policy (fail-closed on reads, unless `failOpen`):
|
|
7
7
|
* - `add()` — records the revocation in BOTH, always. The fallback is this
|
|
8
8
|
* instance's own record of what it has revoked, not a log of its
|
|
9
9
|
* outages: writing only on failure meant a token revoked while
|
|
@@ -12,11 +12,23 @@
|
|
|
12
12
|
* through. A primary failure still does not fail the logout.
|
|
13
13
|
* - `has()` — asks the fallback FIRST: what this instance revoked is known
|
|
14
14
|
* locally and needs no round trip. Otherwise it asks the
|
|
15
|
-
* primary, and if that throws,
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
15
|
+
* primary, and if that throws, rethrows: the guard asking treats
|
|
16
|
+
* it as a crashed strategy (a 500, or a guest under silent auth),
|
|
17
|
+
* never as "not revoked".
|
|
18
|
+
*
|
|
19
|
+
* Answering "not revoked" (fail-open) assumed every consumer was
|
|
20
|
+
* a short-lived access token. The same blacklist backs a session
|
|
21
|
+
* guard's logout under the cookie store, listed for twice the
|
|
22
|
+
* session lifetime, and JWTs issued for days: during an outage
|
|
23
|
+
* every sign-in revoked by ANOTHER instance, or before this
|
|
24
|
+
* process started, was accepted again. Answering "revoked"
|
|
25
|
+
* instead would not do either: a session guard forgets a revoked
|
|
26
|
+
* sign-in, so an outage would log every user out for good.
|
|
27
|
+
*
|
|
28
|
+
* `failOpen: true` restores the old answer for a deployment whose
|
|
29
|
+
* only consumer is a JWT guard with a short `expiresIn`, where
|
|
30
|
+
* availability through an outage is worth more than a revocation
|
|
31
|
+
* missed for at most one token lifetime.
|
|
20
32
|
*
|
|
21
33
|
* Every degradation invokes `onDegrade`. The default reports the first failure
|
|
22
34
|
* per operation and then a periodic count: an outage that logs on every request
|
|
@@ -30,6 +42,11 @@ export interface BlacklistDegradeEvent {
|
|
|
30
42
|
export interface ResilientBlacklistConfig {
|
|
31
43
|
/** Invoked whenever the primary driver fails and the fallback is used. */
|
|
32
44
|
onDegrade?: (event: BlacklistDegradeEvent) => void;
|
|
45
|
+
/**
|
|
46
|
+
* Let a credential through when the primary cannot answer and this instance
|
|
47
|
+
* never revoked it. Off by default: see the policy above for what it costs.
|
|
48
|
+
*/
|
|
49
|
+
failOpen?: boolean;
|
|
33
50
|
}
|
|
34
51
|
export declare class ResilientBlacklistDriver implements BlacklistDriver {
|
|
35
52
|
#private;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ResilientBlacklistDriver.d.ts","sourceRoot":"","sources":["../src/ResilientBlacklistDriver.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"ResilientBlacklistDriver.d.ts","sourceRoot":"","sources":["../src/ResilientBlacklistDriver.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAEH,OAAO,EACN,KAAK,eAAe,EAEpB,MAAM,qBAAqB,CAAC;AAE7B,MAAM,WAAW,qBAAqB;IACrC,EAAE,EAAE,KAAK,GAAG,KAAK,GAAG,SAAS,CAAC;IAC9B,KAAK,EAAE,OAAO,CAAC;CACf;AAED,MAAM,WAAW,wBAAwB;IACxC,0EAA0E;IAC1E,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,qBAAqB,KAAK,IAAI,CAAC;IACnD;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;CACnB;AAkCD,qBAAa,wBAAyB,YAAW,eAAe;;gBAO9D,OAAO,EAAE,eAAe,EACxB,QAAQ,GAAE,eAA6C,EACvD,MAAM,GAAE,wBAA6B;IAQhC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAgBlD,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAclC,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;CAQ9B"}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* `RedisBlacklistDriver`) with an in-memory fallback so a cache outage never
|
|
4
4
|
* takes the whole app down.
|
|
5
5
|
*
|
|
6
|
-
* Policy (fail-
|
|
6
|
+
* Policy (fail-closed on reads, unless `failOpen`):
|
|
7
7
|
* - `add()` — records the revocation in BOTH, always. The fallback is this
|
|
8
8
|
* instance's own record of what it has revoked, not a log of its
|
|
9
9
|
* outages: writing only on failure meant a token revoked while
|
|
@@ -12,11 +12,23 @@
|
|
|
12
12
|
* through. A primary failure still does not fail the logout.
|
|
13
13
|
* - `has()` — asks the fallback FIRST: what this instance revoked is known
|
|
14
14
|
* locally and needs no round trip. Otherwise it asks the
|
|
15
|
-
* primary, and if that throws,
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
15
|
+
* primary, and if that throws, rethrows: the guard asking treats
|
|
16
|
+
* it as a crashed strategy (a 500, or a guest under silent auth),
|
|
17
|
+
* never as "not revoked".
|
|
18
|
+
*
|
|
19
|
+
* Answering "not revoked" (fail-open) assumed every consumer was
|
|
20
|
+
* a short-lived access token. The same blacklist backs a session
|
|
21
|
+
* guard's logout under the cookie store, listed for twice the
|
|
22
|
+
* session lifetime, and JWTs issued for days: during an outage
|
|
23
|
+
* every sign-in revoked by ANOTHER instance, or before this
|
|
24
|
+
* process started, was accepted again. Answering "revoked"
|
|
25
|
+
* instead would not do either: a session guard forgets a revoked
|
|
26
|
+
* sign-in, so an outage would log every user out for good.
|
|
27
|
+
*
|
|
28
|
+
* `failOpen: true` restores the old answer for a deployment whose
|
|
29
|
+
* only consumer is a JWT guard with a short `expiresIn`, where
|
|
30
|
+
* availability through an outage is worth more than a revocation
|
|
31
|
+
* missed for at most one token lifetime.
|
|
20
32
|
*
|
|
21
33
|
* Every degradation invokes `onDegrade`. The default reports the first failure
|
|
22
34
|
* per operation and then a periodic count: an outage that logs on every request
|
|
@@ -52,16 +64,19 @@ export class ResilientBlacklistDriver {
|
|
|
52
64
|
#primary;
|
|
53
65
|
#fallback;
|
|
54
66
|
#onDegrade;
|
|
67
|
+
#failOpen;
|
|
55
68
|
constructor(primary, fallback = new MemoryBlacklistDriver(), config = {}) {
|
|
56
69
|
this.#primary = primary;
|
|
57
70
|
this.#fallback = fallback;
|
|
58
71
|
this.#onDegrade = config.onDegrade ?? makeDefaultOnDegrade();
|
|
72
|
+
this.#failOpen = config.failOpen === true;
|
|
59
73
|
}
|
|
60
74
|
async add(jti, expiresAt) {
|
|
61
75
|
// The fallback first, and unconditionally: it is what this instance
|
|
62
76
|
// knows it revoked. Recording only on failure left every revocation made
|
|
63
77
|
// while the primary was healthy invisible to the fallback — and those are
|
|
64
|
-
// most of them.
|
|
78
|
+
// most of them. The default fallback sweeps expired entries on write,
|
|
79
|
+
// so recording every revocation keeps only the live ones.
|
|
65
80
|
await this.#fallback.add(jti, expiresAt);
|
|
66
81
|
try {
|
|
67
82
|
await this.#primary.add(jti, expiresAt);
|
|
@@ -82,8 +97,10 @@ export class ResilientBlacklistDriver {
|
|
|
82
97
|
}
|
|
83
98
|
catch (error) {
|
|
84
99
|
this.#onDegrade({ op: "has", error });
|
|
85
|
-
//
|
|
86
|
-
|
|
100
|
+
// Neither store can say the credential was not revoked.
|
|
101
|
+
if (this.#failOpen)
|
|
102
|
+
return false;
|
|
103
|
+
throw error;
|
|
87
104
|
}
|
|
88
105
|
}
|
|
89
106
|
async cleanup() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ResilientBlacklistDriver.js","sourceRoot":"","sources":["../src/ResilientBlacklistDriver.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"ResilientBlacklistDriver.js","sourceRoot":"","sources":["../src/ResilientBlacklistDriver.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAEH,OAAO,EAEN,qBAAqB,GACrB,MAAM,qBAAqB,CAAC;AAiB7B,iFAAiF;AACjF,MAAM,qBAAqB,GAAG,MAAM,CAAC;AAErC;;;;;;GAMG;AACH,SAAS,oBAAoB;IAC5B,MAAM,OAAO,GAAG,IAAI,GAAG,EAAiD,CAAC;IACzE,OAAO,CAAC,EAAE,EAAE,EAAE,KAAK,EAAyB,EAAQ,EAAE;QACrD,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACvB,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAC7B,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,CAAC,KAAK,IAAI,GAAG,EAAE,CAAC;YAC7C,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,CAAC,UAAU,GAAG,CAAC,EAAE,CAAC;gBAC/C,OAAO,CAAC,IAAI,CACX,wCAAwC,EAAE,IAAI,IAAI,CAAC,UAAU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AAG,CAAC,GAAW;QACpB,0EAA0E;QAC1E,iEAAiE;QACjE,IAAI,MAAM,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC;YAAE,OAAO,IAAI,CAAC;QAC/C,IAAI,CAAC;YACJ,OAAO,MAAM,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACrC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,IAAI,CAAC,UAAU,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;YACtC,wDAAwD;YACxD,IAAI,IAAI,CAAC,SAAS;gBAAE,OAAO,KAAK,CAAC;YACjC,MAAM,KAAK,CAAC;QACb,CAAC;IACF,CAAC;IAED,KAAK,CAAC,OAAO;QACZ,IAAI,CAAC;YACJ,MAAM,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC;QACjC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,IAAI,CAAC,UAAU,CAAC,EAAE,EAAE,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC,CAAC;QAC3C,CAAC;QACD,MAAM,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,EAAE,CAAC;IAClC,CAAC;CACD"}
|
package/dist/TokenBlacklist.d.ts
CHANGED
|
@@ -14,9 +14,11 @@ export interface BlacklistDriver {
|
|
|
14
14
|
*/
|
|
15
15
|
cleanup?(): Promise<void>;
|
|
16
16
|
}
|
|
17
|
-
/** In-memory blacklist for development. */
|
|
17
|
+
/** In-memory blacklist for development, and the resilient driver's fallback. */
|
|
18
18
|
export declare class MemoryBlacklistDriver implements BlacklistDriver {
|
|
19
19
|
#private;
|
|
20
|
+
/** Entries currently held, live or not yet swept. */
|
|
21
|
+
get size(): number;
|
|
20
22
|
add(jti: string, expiresAt: number): Promise<void>;
|
|
21
23
|
has(jti: string): Promise<boolean>;
|
|
22
24
|
cleanup(): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TokenBlacklist.d.ts","sourceRoot":"","sources":["../src/TokenBlacklist.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,WAAW,eAAe;IAC/B,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACnD,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACnC;;;OAGG;IACH,OAAO,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1B;AAED,
|
|
1
|
+
{"version":3,"file":"TokenBlacklist.d.ts","sourceRoot":"","sources":["../src/TokenBlacklist.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,WAAW,eAAe;IAC/B,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACnD,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACnC;;;OAGG;IACH,OAAO,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1B;AAED,gFAAgF;AAChF,qBAAa,qBAAsB,YAAW,eAAe;;IAgB5D,qDAAqD;IACrD,IAAI,IAAI,IAAI,MAAM,CAEjB;IAEK,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQlD,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAUlC,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;CAW9B;AAED,qBAAa,cAAc;IACd,OAAO,CAAC,MAAM;gBAAN,MAAM,EAAE,eAAe;IAE3C,uCAAuC;IACjC,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI3D,2CAA2C;IACrC,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAI9C,2EAA2E;IACrE,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;CAG9B"}
|
package/dist/TokenBlacklist.js
CHANGED
|
@@ -5,11 +5,33 @@
|
|
|
5
5
|
*
|
|
6
6
|
* @implements MISS-6
|
|
7
7
|
*/
|
|
8
|
-
/** In-memory blacklist for development. */
|
|
8
|
+
/** In-memory blacklist for development, and the resilient driver's fallback. */
|
|
9
9
|
export class MemoryBlacklistDriver {
|
|
10
10
|
#entries = new Map();
|
|
11
|
+
/**
|
|
12
|
+
* Size at which the next sweep runs, doubling each time.
|
|
13
|
+
*
|
|
14
|
+
* Expired entries used to leave only on a `has()` of that exact jti or an
|
|
15
|
+
* explicit `cleanup()`, which nothing calls. Every logout and every
|
|
16
|
+
* `revoke()` added one, and `ResilientBlacklistDriver` writes every
|
|
17
|
+
* revocation into this store, so a long-lived process grew without bound.
|
|
18
|
+
* Sweeping on write bounds the map by the revocations still live, without
|
|
19
|
+
* a timer that would hold the event loop open. There is no ceiling that
|
|
20
|
+
* evicts: dropping a live entry would un-revoke a token.
|
|
21
|
+
*/
|
|
22
|
+
#sweepAt = 64;
|
|
23
|
+
/** Entries currently held, live or not yet swept. */
|
|
24
|
+
get size() {
|
|
25
|
+
return this.#entries.size;
|
|
26
|
+
}
|
|
11
27
|
async add(jti, expiresAt) {
|
|
28
|
+
// Already past its deadline: `has()` would answer false for it anyway,
|
|
29
|
+
// so storing it only costs memory.
|
|
30
|
+
if (expiresAt < Date.now())
|
|
31
|
+
return;
|
|
12
32
|
this.#entries.set(jti, expiresAt);
|
|
33
|
+
if (this.#entries.size > this.#sweepAt)
|
|
34
|
+
this.#sweep();
|
|
13
35
|
}
|
|
14
36
|
async has(jti) {
|
|
15
37
|
const expiresAt = this.#entries.get(jti);
|
|
@@ -22,11 +44,15 @@ export class MemoryBlacklistDriver {
|
|
|
22
44
|
return true;
|
|
23
45
|
}
|
|
24
46
|
async cleanup() {
|
|
47
|
+
this.#sweep();
|
|
48
|
+
}
|
|
49
|
+
#sweep() {
|
|
25
50
|
const now = Date.now();
|
|
26
51
|
for (const [jti, exp] of this.#entries) {
|
|
27
52
|
if (exp < now)
|
|
28
53
|
this.#entries.delete(jti);
|
|
29
54
|
}
|
|
55
|
+
this.#sweepAt = Math.max(64, this.#entries.size * 2);
|
|
30
56
|
}
|
|
31
57
|
}
|
|
32
58
|
export class TokenBlacklist {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TokenBlacklist.js","sourceRoot":"","sources":["../src/TokenBlacklist.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAYH,
|
|
1
|
+
{"version":3,"file":"TokenBlacklist.js","sourceRoot":"","sources":["../src/TokenBlacklist.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAYH,gFAAgF;AAChF,MAAM,OAAO,qBAAqB;IACjC,QAAQ,GAAwB,IAAI,GAAG,EAAE,CAAC;IAE1C;;;;;;;;;;OAUG;IACH,QAAQ,GAAG,EAAE,CAAC;IAEd,qDAAqD;IACrD,IAAI,IAAI;QACP,OAAO,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC;IAC3B,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,GAAW,EAAE,SAAiB;QACvC,uEAAuE;QACvE,mCAAmC;QACnC,IAAI,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE;YAAE,OAAO;QACnC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;QAClC,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,GAAG,IAAI,CAAC,QAAQ;YAAE,IAAI,CAAC,MAAM,EAAE,CAAC;IACvD,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,GAAW;QACpB,MAAM,SAAS,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GA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ist/WardenProvider.d.ts
CHANGED
|
@@ -9,6 +9,8 @@ interface WardenConfigStore {
|
|
|
9
9
|
export interface WardenAppContext {
|
|
10
10
|
container: WardenContainer;
|
|
11
11
|
config: WardenConfigStore;
|
|
12
|
+
/** Set by the template engine's provider constructor (`InkerProvider`). */
|
|
13
|
+
usingInker?: boolean;
|
|
12
14
|
}
|
|
13
15
|
export default class WardenProvider {
|
|
14
16
|
#private;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WardenProvider.d.ts","sourceRoot":"","sources":["../src/WardenProvider.ts"],"names":[],"mappings":"AAAA,OAAO,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"WardenProvider.d.ts","sourceRoot":"","sources":["../src/WardenProvider.ts"],"names":[],"mappings":"AAAA,OAAO,oBAAoB,CAAC;AAc5B,UAAU,eAAe;IACxB,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,GAAG,IAAI,CAAC;IACxD,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC1C;AAED,UAAU,iBAAiB;IAC1B,GAAG,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS,CAAC;CAC7C;AAED,MAAM,WAAW,gBAAgB;IAChC,SAAS,EAAE,eAAe,CAAC;IAC3B,MAAM,EAAE,iBAAiB,CAAC;IAC1B,2EAA2E;IAC3E,UAAU,CAAC,EAAE,OAAO,CAAC;CACrB;AAED,MAAM,CAAC,OAAO,OAAO,cAAc;;IAItB,SAAS,CAAC,GAAG,EAAE,gBAAgB;gBAArB,GAAG,EAAE,gBAAgB;IAE3C,QAAQ;IAoIF,IAAI;IASJ,QAAQ;CAkBd"}
|
package/dist/WardenProvider.js
CHANGED
|
@@ -8,6 +8,7 @@ import { clearAuth, getAuth, setAuth } from "./services/main.js";
|
|
|
8
8
|
import { ApiKeyStrategy } from "./strategies/ApiKeyStrategy.js";
|
|
9
9
|
import { JwtStrategy } from "./strategies/JwtStrategy.js";
|
|
10
10
|
import { SessionStrategy } from "./strategies/SessionStrategy.js";
|
|
11
|
+
import { installBouncerTemplateTags } from "./templateTags.js";
|
|
11
12
|
export default class WardenProvider {
|
|
12
13
|
app;
|
|
13
14
|
/** The manager this provider installed, so shutdown only clears its own. */
|
|
@@ -157,66 +158,11 @@ export default class WardenProvider {
|
|
|
157
158
|
}
|
|
158
159
|
/**
|
|
159
160
|
* Publish `@can` / `@cannot` to the template engine, as AdonisJS's bouncer
|
|
160
|
-
* does
|
|
161
|
-
*
|
|
162
|
-
*
|
|
163
|
-
* Both are BLOCK tags and both AWAIT — the check returns a promise — so they
|
|
164
|
-
* render on the async path and raise on a synchronous one, matching upstream
|
|
165
|
-
* (which emits `await bouncer.can(...)`). The checks themselves come from
|
|
166
|
-
* `ctx.bouncer.templateHelpers`, shared into the view per request.
|
|
167
|
-
*
|
|
168
|
-
* Resolved through the container: warden must not depend on the engine, and
|
|
169
|
-
* an app with no template layer simply skips this.
|
|
161
|
+
* does — through the engine package's module singleton, guarded by
|
|
162
|
+
* `usingInker` (see `installBouncerTemplateTags`).
|
|
170
163
|
*/
|
|
171
164
|
async #registerTemplateTags() {
|
|
172
|
-
|
|
173
|
-
if (engine === undefined)
|
|
174
|
-
return;
|
|
175
|
-
for (const tagName of ["can", "cannot"]) {
|
|
176
|
-
engine.registerTag({
|
|
177
|
-
tagName,
|
|
178
|
-
block: true,
|
|
179
|
-
seekable: true,
|
|
180
|
-
async compile(_parser, buffer, token) {
|
|
181
|
-
// The shared bag lives in the render scope under `bouncer`.
|
|
182
|
-
const bouncer = token.evaluate("typeof bouncer === 'undefined' ? undefined : bouncer");
|
|
183
|
-
const check = Reflect.get(Object(bouncer), tagName);
|
|
184
|
-
if (typeof check !== "function") {
|
|
185
|
-
// No bouncer on this request: deny rather than render an
|
|
186
|
-
// unguarded body.
|
|
187
|
-
return;
|
|
188
|
-
}
|
|
189
|
-
const args = token.evaluate(`[${token.properties.jsArg}]`);
|
|
190
|
-
const allowed = await check.apply(bouncer, Array.isArray(args) ? args : []);
|
|
191
|
-
if (allowed === true)
|
|
192
|
-
buffer.writeRaw(await token.renderBody());
|
|
193
|
-
},
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
/** The template engine, if the host installed one. */
|
|
198
|
-
async #resolveTemplateEngine() {
|
|
199
|
-
for (const token of ["inker", "view"]) {
|
|
200
|
-
try {
|
|
201
|
-
const binding = await this.app.container.resolve(token);
|
|
202
|
-
for (const candidate of [
|
|
203
|
-
binding,
|
|
204
|
-
Reflect.get(Object(binding), "_templates"),
|
|
205
|
-
]) {
|
|
206
|
-
if (typeof candidate === "object" &&
|
|
207
|
-
candidate !== null &&
|
|
208
|
-
typeof Reflect.get(candidate, "registerTag") === "function") {
|
|
209
|
-
return {
|
|
210
|
-
registerTag: Reflect.get(candidate, "registerTag").bind(candidate),
|
|
211
|
-
};
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
catch {
|
|
216
|
-
// A host without a template layer is not an error.
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
return undefined;
|
|
165
|
+
await installBouncerTemplateTags(this.app.usingInker === true);
|
|
220
166
|
}
|
|
221
167
|
}
|
|
222
168
|
//# sourceMappingURL=WardenProvider.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WardenProvider.js","sourceRoot":"","sources":["../src/WardenProvider.ts"],"names":[],"mappings":"AAAA,OAAO,oBAAoB,CAAC;AAC5B,OAAO,EAAE,WAAW,EAAqB,MAAM,kBAAkB,CAAC;AAElE,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAEjD,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAClE,OAAO,EAAE,cAAc,EAAE,MA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gEAAgE;QAChE,uEAAuE;QACvE,gCAAgC;QAChC,IAAI,IAAI,CAAC,KAAK,KAAK,SAAS,IAAI,OAAO,EAAE,KAAK,IAAI,CAAC,KAAK;YAAE,SAAS,EAAE,CAAC;QACtE,IAAI,CAAC,KAAK,GAAG,SAAS,CAAC;IACxB,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,qBAAqB;QAC1B,MAAM,0BAA0B,CAAC,IAAI,CAAC,GAAG,CAAC,UAAU,KAAK,IAAI,CAAC,CAAC;IAChE,CAAC;CACD"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Timing-safe credential lookup — the counterpart of the upstream
|
|
3
|
+
* `withAuthFinder` mixin's `verifyCredentials`, without the ORM.
|
|
4
|
+
*
|
|
5
|
+
* Why it exists: a login that returns early for an unknown account answers in
|
|
6
|
+
* microseconds, and one that verifies a password hash for a known account
|
|
7
|
+
* answers in ~100 ms. That difference alone lets an anonymous caller list the
|
|
8
|
+
* registered emails, with no rate limit to bypass. The counter-measure is to pay
|
|
9
|
+
* one hash for an unknown uid too, and to fail every case with the same error.
|
|
10
|
+
*
|
|
11
|
+
* Upstream mixes the methods into a Lucid model; warden knows no ORM, so the
|
|
12
|
+
* lookup is passed in (`findForAuth`) and the methods come back as plain
|
|
13
|
+
* functions, bound, ready to hand to a guard:
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* const users = authFinder({ hash, findForAuth: (email) => User.findBy('email', email) })
|
|
17
|
+
* jwtGuard({ secret, findUser, verifyCredentials: users.verifyCredentials })
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
/** What `authFinder` hashes with — `make` and `verify`, AdonisJS argument order. */
|
|
21
|
+
export interface AuthFinderHasher {
|
|
22
|
+
make(value: string): Promise<string>;
|
|
23
|
+
verify(hashedValue: string, plainValue: string): Promise<boolean>;
|
|
24
|
+
}
|
|
25
|
+
export interface AuthFinderOptions<User extends object> {
|
|
26
|
+
/** The hasher the passwords were made with (a sigil `Hash`, for one). */
|
|
27
|
+
hash: AuthFinderHasher;
|
|
28
|
+
/**
|
|
29
|
+
* Find the user a sign-in names — by email, username, whatever the uid is.
|
|
30
|
+
* `null` or `undefined` for none.
|
|
31
|
+
*/
|
|
32
|
+
findForAuth: (uid: string) => Promise<User | null | undefined>;
|
|
33
|
+
/** The property the password hash lives under. Default `password`. */
|
|
34
|
+
passwordColumnName?: string;
|
|
35
|
+
}
|
|
36
|
+
export interface AuthFinder<User extends object> {
|
|
37
|
+
/**
|
|
38
|
+
* Find the user by `uid` and verify `password`. Safe from timing attacks:
|
|
39
|
+
* an unknown uid pays the same one hash a known one does. Every failure
|
|
40
|
+
* throws {@link E_INVALID_CREDENTIALS}.
|
|
41
|
+
*/
|
|
42
|
+
verifyCredentials(uid: string, password: string): Promise<User>;
|
|
43
|
+
/** Whether `password` matches `user`'s stored hash. */
|
|
44
|
+
verifyPassword(user: User, password: string): Promise<boolean>;
|
|
45
|
+
}
|
|
46
|
+
export declare function authFinder<User extends object>(options: AuthFinderOptions<User>): AuthFinder<User>;
|
|
47
|
+
/**
|
|
48
|
+
* A guard's `verifyCredentials` answers a wrong credential with `null`; one
|
|
49
|
+
* built on {@link authFinder} throws {@link E_INVALID_CREDENTIALS} instead, as
|
|
50
|
+
* upstream's does. Both mean the same rejection, so a guard reads them alike.
|
|
51
|
+
*
|
|
52
|
+
* NAMED DEVIATION — upstream's user provider swallows EVERY error into `null`,
|
|
53
|
+
* so a database outage reads as a wrong password. Only E_INVALID_CREDENTIALS
|
|
54
|
+
* is a rejection here; anything else propagates and the auth manager reports it
|
|
55
|
+
* as the strategy crash it is.
|
|
56
|
+
*/
|
|
57
|
+
export declare function rejectedAsNull<User>(verification: Promise<User | null>): Promise<User | null>;
|
|
58
|
+
//# sourceMappingURL=authFinder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authFinder.d.ts","sourceRoot":"","sources":["../src/authFinder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAIH,oFAAoF;AACpF,MAAM,WAAW,gBAAgB;IAChC,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACrC,MAAM,CAAC,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAClE;AAED,MAAM,WAAW,iBAAiB,CAAC,IAAI,SAAS,MAAM;IACrD,yEAAyE;IACzE,IAAI,EAAE,gBAAgB,CAAC;IACvB;;;OAGG;IACH,WAAW,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,GAAG,IAAI,GAAG,SAAS,CAAC,CAAC;IAC/D,sEAAsE;IACtE,kBAAkB,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED,MAAM,WAAW,UAAU,CAAC,IAAI,SAAS,MAAM;IAC9C;;;;OAIG;IACH,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAChE,uDAAuD;IACvD,cAAc,CAAC,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC/D;AAED,wBAAgB,UAAU,CAAC,IAAI,SAAS,MAAM,EAC7C,OAAO,EAAE,iBAAiB,CAAC,IAAI,CAAC,GAC9B,UAAU,CAAC,IAAI,CAAC,CA2ClB;AAED;;;;;;;;;GASG;AACH,wBAAsB,cAAc,CAAC,IAAI,EACxC,YAAY,EAAE,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,GAChC,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,CAOtB"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Timing-safe credential lookup — the counterpart of the upstream
|
|
3
|
+
* `withAuthFinder` mixin's `verifyCredentials`, without the ORM.
|
|
4
|
+
*
|
|
5
|
+
* Why it exists: a login that returns early for an unknown account answers in
|
|
6
|
+
* microseconds, and one that verifies a password hash for a known account
|
|
7
|
+
* answers in ~100 ms. That difference alone lets an anonymous caller list the
|
|
8
|
+
* registered emails, with no rate limit to bypass. The counter-measure is to pay
|
|
9
|
+
* one hash for an unknown uid too, and to fail every case with the same error.
|
|
10
|
+
*
|
|
11
|
+
* Upstream mixes the methods into a Lucid model; warden knows no ORM, so the
|
|
12
|
+
* lookup is passed in (`findForAuth`) and the methods come back as plain
|
|
13
|
+
* functions, bound, ready to hand to a guard:
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* const users = authFinder({ hash, findForAuth: (email) => User.findBy('email', email) })
|
|
17
|
+
* jwtGuard({ secret, findUser, verifyCredentials: users.verifyCredentials })
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
import { E_INVALID_CREDENTIALS } from "./errors.js";
|
|
21
|
+
export function authFinder(options) {
|
|
22
|
+
const { hash, findForAuth } = options;
|
|
23
|
+
const column = options.passwordColumnName ?? "password";
|
|
24
|
+
const storedHash = (user) => {
|
|
25
|
+
const value = Reflect.get(user, column);
|
|
26
|
+
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
27
|
+
};
|
|
28
|
+
const verifyPassword = async (user, password) => {
|
|
29
|
+
const hashed = storedHash(user);
|
|
30
|
+
if (hashed === undefined) {
|
|
31
|
+
// NAMED DEVIATION — upstream throws a RuntimeException (a 500) for
|
|
32
|
+
// a row without a hash. An account that signs in elsewhere (SSO)
|
|
33
|
+
// has none, and a distinct 500 for it would tell a caller the
|
|
34
|
+
// account exists. It is a password that does not match, and it
|
|
35
|
+
// pays the hash a match would have cost.
|
|
36
|
+
await hash.make(password);
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
return hash.verify(hashed, password);
|
|
40
|
+
};
|
|
41
|
+
const verifyCredentials = async (uid, password) => {
|
|
42
|
+
if (!uid || !password)
|
|
43
|
+
throw new E_INVALID_CREDENTIALS();
|
|
44
|
+
const user = await findForAuth(uid);
|
|
45
|
+
if (user === null || user === undefined) {
|
|
46
|
+
// The hash a known uid would cost, paid for nothing: without it the
|
|
47
|
+
// response time says whether the account exists.
|
|
48
|
+
await hash.make(password);
|
|
49
|
+
throw new E_INVALID_CREDENTIALS();
|
|
50
|
+
}
|
|
51
|
+
if (await verifyPassword(user, password))
|
|
52
|
+
return user;
|
|
53
|
+
throw new E_INVALID_CREDENTIALS();
|
|
54
|
+
};
|
|
55
|
+
return { verifyCredentials, verifyPassword };
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* A guard's `verifyCredentials` answers a wrong credential with `null`; one
|
|
59
|
+
* built on {@link authFinder} throws {@link E_INVALID_CREDENTIALS} instead, as
|
|
60
|
+
* upstream's does. Both mean the same rejection, so a guard reads them alike.
|
|
61
|
+
*
|
|
62
|
+
* NAMED DEVIATION — upstream's user provider swallows EVERY error into `null`,
|
|
63
|
+
* so a database outage reads as a wrong password. Only E_INVALID_CREDENTIALS
|
|
64
|
+
* is a rejection here; anything else propagates and the auth manager reports it
|
|
65
|
+
* as the strategy crash it is.
|
|
66
|
+
*/
|
|
67
|
+
export async function rejectedAsNull(verification) {
|
|
68
|
+
try {
|
|
69
|
+
return await verification;
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
if (error instanceof E_INVALID_CREDENTIALS)
|
|
73
|
+
return null;
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=authFinder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authFinder.js","sourceRoot":"","sources":["../src/authFinder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AA+BpD,MAAM,UAAU,UAAU,CACzB,OAAgC;IAEhC,MAAM,EAAE,IAA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AAI,EAAE,QAAQ,CAAC;YAAE,OAAO,IAAI,CAAC;QACtD,MAAM,IAAI,qBAAqB,EAAE,CAAC;IACnC,CAAC,CAAC;IAEF,OAAO,EAAE,iBAAiB,EAAE,cAAc,EAAE,CAAC;AAC9C,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CACnC,YAAkC;IAElC,IAAI,CAAC;QACJ,OAAO,MAAM,YAAY,CAAC;IAC3B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,IAAI,KAAK,YAAY,qBAAqB;YAAE,OAAO,IAAI,CAAC;QACxD,MAAM,KAAK,CAAC;IACb,CAAC;AACF,CAAC"}
|
package/dist/bearer.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read the token out of an `Authorization: Bearer <token>` header.
|
|
3
|
+
*
|
|
4
|
+
* The scheme is matched case-insensitively (RFC 7235: auth-schemes are
|
|
5
|
+
* case-insensitive), so `bearer` and `BEARER` are the same credential, as
|
|
6
|
+
* `Basic` already was. Split on the first spaces like the reference guard:
|
|
7
|
+
* the scheme, then the token; a header with no token is no credential.
|
|
8
|
+
*
|
|
9
|
+
* @returns The token, or `""` when the header carries no bearer credential.
|
|
10
|
+
*/
|
|
11
|
+
export declare function readBearerToken(header: unknown): string;
|
|
12
|
+
//# sourceMappingURL=bearer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bearer.d.ts","sourceRoot":"","sources":["../src/bearer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,CAKvD"}
|