@blamejs/core 0.7.18 → 0.7.19
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/CHANGELOG.md +425 -423
- package/README.md +150 -150
- package/bin/blamejs.js +0 -0
- package/index.js +310 -308
- package/lib/api-key.js +660 -660
- package/lib/api-snapshot.js +338 -338
- package/lib/app-shutdown.js +385 -385
- package/lib/app.js +365 -365
- package/lib/archive.js +250 -250
- package/lib/atomic-file.js +544 -544
- package/lib/audit-chain.js +177 -177
- package/lib/audit-sign.js +344 -344
- package/lib/audit-tools.js +677 -677
- package/lib/audit.js +766 -766
- package/lib/auth/jwt-external.js +365 -0
- package/lib/auth/jwt.js +337 -311
- package/lib/auth/lockout.js +436 -436
- package/lib/auth/oauth.js +721 -721
- package/lib/auth/passkey.js +181 -181
- package/lib/auth/password.js +628 -594
- package/lib/backup/bundle.js +217 -217
- package/lib/backup/crypto.js +176 -176
- package/lib/backup/index.js +515 -515
- package/lib/backup/manifest.js +282 -282
- package/lib/break-glass.js +1338 -1338
- package/lib/bundler.js +441 -441
- package/lib/cache-redis.js +256 -256
- package/lib/cache.js +1206 -1206
- package/lib/canonical-json.js +115 -115
- package/lib/chain-writer.js +234 -234
- package/lib/cli-helpers.js +206 -206
- package/lib/cli.js +2334 -2334
- package/lib/cluster-provider-db.js +317 -317
- package/lib/cluster-storage.js +226 -226
- package/lib/cluster.js +703 -703
- package/lib/config-drift.js +301 -301
- package/lib/consent.js +222 -222
- package/lib/constants.js +191 -191
- package/lib/cookies.js +315 -315
- package/lib/credential-hash.js +322 -322
- package/lib/crypto.js +266 -266
- package/lib/csv.js +275 -275
- package/lib/db-declare-row-policy.js +267 -267
- package/lib/db-declare-view.js +420 -420
- package/lib/db-query.js +406 -406
- package/lib/db-schema.js +319 -319
- package/lib/db.js +1288 -1288
- package/lib/deprecate.js +222 -222
- package/lib/dev.js +335 -335
- package/lib/dual-control.js +473 -473
- package/lib/error-page.js +420 -420
- package/lib/external-db-migrate.js +441 -441
- package/lib/external-db.js +1061 -1061
- package/lib/file-type.js +273 -273
- package/lib/forms.js +422 -422
- package/lib/framework-error.js +293 -293
- package/lib/framework-schema.js +717 -717
- package/lib/handlers.js +350 -350
- package/lib/http-client-cookie-jar.js +508 -508
- package/lib/http-client.js +1195 -1195
- package/lib/i18n.js +878 -878
- package/lib/jobs.js +185 -185
- package/lib/log-stream-cloudwatch.js +369 -369
- package/lib/log-stream-local.js +146 -146
- package/lib/log-stream-otlp-grpc.js +410 -410
- package/lib/log-stream-otlp.js +286 -286
- package/lib/log-stream-syslog.js +302 -302
- package/lib/log-stream-webhook.js +199 -199
- package/lib/log-stream.js +330 -330
- package/lib/log.js +500 -500
- package/lib/mail-bounce.js +528 -528
- package/lib/mail-dkim.js +369 -369
- package/lib/mail.js +981 -981
- package/lib/metrics.js +683 -683
- package/lib/middleware/api-encrypt.js +936 -936
- package/lib/middleware/attach-user.js +157 -157
- package/lib/middleware/bearer-auth.js +152 -0
- package/lib/middleware/body-parser.js +1170 -1170
- package/lib/middleware/bot-guard.js +178 -178
- package/lib/middleware/compression.js +452 -452
- package/lib/middleware/cors.js +314 -314
- package/lib/middleware/csp-nonce.js +348 -348
- package/lib/middleware/csrf-protect.js +316 -316
- package/lib/middleware/db-role-for.js +264 -264
- package/lib/middleware/health.js +392 -392
- package/lib/middleware/index.js +82 -79
- package/lib/middleware/rate-limit.js +358 -358
- package/lib/middleware/request-id.js +61 -61
- package/lib/middleware/request-log.js +168 -168
- package/lib/middleware/require-auth.js +104 -104
- package/lib/middleware/security-headers.js +116 -116
- package/lib/middleware/sse.js +166 -166
- package/lib/migrations.js +383 -383
- package/lib/mtls-ca.js +518 -518
- package/lib/mtls-engine-default.js +481 -481
- package/lib/network-dns.js +632 -632
- package/lib/network-heartbeat.js +290 -290
- package/lib/network-nts.js +574 -574
- package/lib/network-proxy.js +265 -265
- package/lib/network-tls.js +328 -328
- package/lib/network.js +233 -233
- package/lib/notify.js +612 -612
- package/lib/ntp-check.js +229 -229
- package/lib/numeric-bounds.js +111 -111
- package/lib/object-store/azure-blob-bucket-ops.js +349 -349
- package/lib/object-store/azure-blob.js +488 -488
- package/lib/object-store/gcs-bucket-ops.js +351 -351
- package/lib/object-store/gcs.js +519 -519
- package/lib/object-store/http-put.js +153 -153
- package/lib/object-store/index.js +197 -197
- package/lib/object-store/sigv4-bucket-ops.js +1092 -1092
- package/lib/object-store/sigv4.js +903 -903
- package/lib/observability.js +151 -151
- package/lib/otel-export.js +269 -269
- package/lib/pagination.js +464 -464
- package/lib/parsers/index.js +80 -80
- package/lib/parsers/safe-env.js +642 -642
- package/lib/parsers/safe-ini.js +292 -292
- package/lib/parsers/safe-toml.js +784 -784
- package/lib/parsers/safe-xml.js +390 -390
- package/lib/parsers/safe-yaml.js +1015 -1015
- package/lib/permissions.js +708 -708
- package/lib/pqc-agent.js +87 -87
- package/lib/pqc-gate.js +279 -279
- package/lib/protobuf-encoder.js +190 -190
- package/lib/protocol-dispatcher.js +161 -161
- package/lib/pubsub-redis.js +167 -167
- package/lib/pubsub.js +429 -429
- package/lib/queue-local.js +476 -476
- package/lib/queue-redis.js +745 -745
- package/lib/queue-sqs.js +319 -319
- package/lib/queue.js +695 -695
- package/lib/redis-client.js +519 -519
- package/lib/request-helpers.js +340 -340
- package/lib/restore-bundle.js +237 -237
- package/lib/restore-rollback.js +259 -259
- package/lib/restore.js +409 -409
- package/lib/retry.js +376 -376
- package/lib/router.js +748 -748
- package/lib/safe-async.js +735 -735
- package/lib/safe-buffer.js +237 -237
- package/lib/safe-json.js +541 -541
- package/lib/safe-schema.js +1266 -1266
- package/lib/safe-url.js +159 -159
- package/lib/scheduler.js +706 -706
- package/lib/security-assert.js +373 -373
- package/lib/seeders.js +618 -618
- package/lib/session.js +535 -478
- package/lib/slug.js +269 -269
- package/lib/ssrf-guard.js +401 -401
- package/lib/storage.js +471 -471
- package/lib/subject.js +281 -281
- package/lib/template.js +791 -791
- package/lib/testing.js +798 -798
- package/lib/time.js +310 -310
- package/lib/totp.js +302 -302
- package/lib/tracing.js +494 -494
- package/lib/uuid.js +132 -132
- package/lib/validate-opts.js +340 -340
- package/lib/vault/index.js +308 -308
- package/lib/vault/rotate.js +784 -784
- package/lib/vault/wrap.js +296 -296
- package/lib/vendor/noble-ciphers.cjs +9 -9
- package/lib/webhook.js +595 -595
- package/lib/websocket.js +1048 -1048
- package/package.json +77 -77
- package/sbom.cyclonedx.json +7 -7
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* jwt-external — verify JWTs signed by an external IdP using classical
|
|
4
|
+
* algorithms (RS256 / RS384 / RS512 / PS256 / PS384 / PS512 / ES256 /
|
|
5
|
+
* ES384 / ES512 / EdDSA).
|
|
6
|
+
*
|
|
7
|
+
* Distinct from `b.auth.jwt.verify` which is PQC-only (ML-DSA-65 etc.).
|
|
8
|
+
* Operators integrating with Auth0 / Okta / Keycloak / AWS Cognito /
|
|
9
|
+
* Azure AD / Google IdP / Apple-sign-in use this primitive — those IdPs
|
|
10
|
+
* sign with classical algorithms and the framework's PQC verifier
|
|
11
|
+
* cannot accept their tokens.
|
|
12
|
+
*
|
|
13
|
+
* var rv = await b.auth.jwt.verifyExternal(token, {
|
|
14
|
+
* algorithms: ["RS256", "ES256"], // REQUIRED — no defaults
|
|
15
|
+
* jwks: jwksKeysArray, // pre-fetched RFC 7517 keys
|
|
16
|
+
* // OR
|
|
17
|
+
* jwksUri: "https://example.auth0.com/.well-known/jwks.json",
|
|
18
|
+
* jwksCacheMs: C.TIME.minutes(10), // default 10m
|
|
19
|
+
* // OR
|
|
20
|
+
* keyResolver: async function (header) { return jwkOrKeyObject; },
|
|
21
|
+
*
|
|
22
|
+
* audience: "api://my-api", // optional but recommended
|
|
23
|
+
* issuer: "https://example.auth0.com/", // optional but recommended
|
|
24
|
+
* subject: "user@example.com", // optional sub-equality check
|
|
25
|
+
* clockSkewMs: 30 * 1000, // default 30s tolerance
|
|
26
|
+
* });
|
|
27
|
+
* // → { header, claims } (throws AuthError on any failure)
|
|
28
|
+
*
|
|
29
|
+
* Defenses against the well-known JWT pitfalls:
|
|
30
|
+
*
|
|
31
|
+
* - alg confusion (CVE-2024-54150 / CVE-2025-30144 / CVE-2026-22817
|
|
32
|
+
* Hono class) — `algorithms` is REQUIRED with no default; `none`,
|
|
33
|
+
* `HS256` cannot be accepted unless the operator explicitly listed
|
|
34
|
+
* them, and even then the verifier refuses HS* algs in
|
|
35
|
+
* verifyExternal because HMAC + a public-key JWKS is the canonical
|
|
36
|
+
* alg-confusion shape. Operators with HMAC need a different path.
|
|
37
|
+
* - kid spoofing — the resolved key MUST come from the operator's
|
|
38
|
+
* trust source (the JWKS array or operator's keyResolver). The
|
|
39
|
+
* header's `kid` only selects WHICH key from that source.
|
|
40
|
+
* - exp/nbf/iat — checked against now (with clockSkewMs tolerance).
|
|
41
|
+
* - aud / iss / sub — checked when the operator passes the expected
|
|
42
|
+
* value. iss MUST match exactly (not substring).
|
|
43
|
+
* - JWKS endpoint trust — `jwksUri` resolves through the framework's
|
|
44
|
+
* `b.httpClient` (SSRF gate, TLS-required by default, response-size
|
|
45
|
+
* cap). The jwksCache is per-process and TTL-bounded.
|
|
46
|
+
*
|
|
47
|
+
* Returns { header, claims } on success. Every failure surfaces as
|
|
48
|
+
* AuthError with a code in `auth-jwt-external/<reason>` so operators
|
|
49
|
+
* can route alerts on a single class.
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
var nodeCrypto = require("crypto");
|
|
53
|
+
var safeJson = require("../safe-json");
|
|
54
|
+
var safeUrl = require("../safe-url");
|
|
55
|
+
var lazyRequire = require("../lazy-require");
|
|
56
|
+
var validateOpts = require("../validate-opts");
|
|
57
|
+
var C = require("../constants");
|
|
58
|
+
var { AuthError } = require("../framework-error");
|
|
59
|
+
|
|
60
|
+
var httpClient = lazyRequire(function () { return require("../http-client"); });
|
|
61
|
+
var cache = lazyRequire(function () { return require("../cache"); });
|
|
62
|
+
|
|
63
|
+
// ---- constants ----
|
|
64
|
+
|
|
65
|
+
var DEFAULT_CLOCK_SKEW_MS = C.TIME.seconds(30);
|
|
66
|
+
var DEFAULT_JWKS_CACHE_MS = C.TIME.minutes(10);
|
|
67
|
+
var MAX_JWKS_BYTES = C.BYTES.kib(64);
|
|
68
|
+
var MAX_TOKEN_BYTES = C.BYTES.kib(16);
|
|
69
|
+
|
|
70
|
+
// HMAC-shaped algs (HS256/384/512) and "none" are NEVER accepted by
|
|
71
|
+
// this primitive. HMAC + a JWKS-shaped public-key trust source is the
|
|
72
|
+
// canonical alg-confusion vector; "none" is the canonical alg-bypass.
|
|
73
|
+
var REFUSED_ALGS = ["HS256", "HS384", "HS512", "none"];
|
|
74
|
+
|
|
75
|
+
// PSS salt lengths per RFC 7518 §3.5.
|
|
76
|
+
var PSS_SALT_SHA256 = 32; // allow:raw-byte-literal — RFC 7518 SHA-256 salt length
|
|
77
|
+
var PSS_SALT_SHA384 = 48; // allow:raw-byte-literal — RFC 7518 SHA-384 salt length
|
|
78
|
+
var PSS_SALT_SHA512 = 64; // allow:raw-byte-literal — RFC 7518 SHA-512 salt length
|
|
79
|
+
|
|
80
|
+
var SUPPORTED_CLASSICAL_ALGS = [
|
|
81
|
+
"RS256", "RS384", "RS512",
|
|
82
|
+
"PS256", "PS384", "PS512",
|
|
83
|
+
"ES256", "ES384", "ES512",
|
|
84
|
+
"EdDSA",
|
|
85
|
+
];
|
|
86
|
+
|
|
87
|
+
// ---- per-instance JWKS cache shared across calls ----
|
|
88
|
+
|
|
89
|
+
var _sharedJwksCache = null;
|
|
90
|
+
function _getJwksCache() {
|
|
91
|
+
if (_sharedJwksCache) return _sharedJwksCache;
|
|
92
|
+
_sharedJwksCache = cache().create({
|
|
93
|
+
namespace: "auth-jwt-external.jwks",
|
|
94
|
+
ttlMs: DEFAULT_JWKS_CACHE_MS,
|
|
95
|
+
});
|
|
96
|
+
return _sharedJwksCache;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// ---- helpers ----
|
|
100
|
+
|
|
101
|
+
function _b64urlDecode(s) {
|
|
102
|
+
if (typeof s !== "string") {
|
|
103
|
+
throw new AuthError("auth-jwt-external/bad-base64", "expected base64url string");
|
|
104
|
+
}
|
|
105
|
+
var padded = s.replace(/-/g, "+").replace(/_/g, "/");
|
|
106
|
+
while (padded.length % 4) padded += "="; // allow:raw-byte-literal — base64 quartet padding
|
|
107
|
+
return Buffer.from(padded, "base64");
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function _verifyParamsForAlg(alg) {
|
|
111
|
+
if (alg === "RS256") return { hash: "sha256", padding: nodeCrypto.constants.RSA_PKCS1_PADDING };
|
|
112
|
+
if (alg === "RS384") return { hash: "sha384", padding: nodeCrypto.constants.RSA_PKCS1_PADDING };
|
|
113
|
+
if (alg === "RS512") return { hash: "sha512", padding: nodeCrypto.constants.RSA_PKCS1_PADDING };
|
|
114
|
+
if (alg === "PS256") return { hash: "sha256", padding: nodeCrypto.constants.RSA_PKCS1_PSS_PADDING, saltLength: PSS_SALT_SHA256 };
|
|
115
|
+
if (alg === "PS384") return { hash: "sha384", padding: nodeCrypto.constants.RSA_PKCS1_PSS_PADDING, saltLength: PSS_SALT_SHA384 };
|
|
116
|
+
if (alg === "PS512") return { hash: "sha512", padding: nodeCrypto.constants.RSA_PKCS1_PSS_PADDING, saltLength: PSS_SALT_SHA512 };
|
|
117
|
+
if (alg === "ES256") return { hash: "sha256", dsaEncoding: "ieee-p1363" };
|
|
118
|
+
if (alg === "ES384") return { hash: "sha384", dsaEncoding: "ieee-p1363" };
|
|
119
|
+
if (alg === "ES512") return { hash: "sha512", dsaEncoding: "ieee-p1363" };
|
|
120
|
+
if (alg === "EdDSA") return { hash: null };
|
|
121
|
+
throw new AuthError("auth-jwt-external/unsupported-alg",
|
|
122
|
+
"alg '" + alg + "' is not supported by verifyExternal");
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function _jwkToKey(jwk) {
|
|
126
|
+
try { return nodeCrypto.createPublicKey({ key: jwk, format: "jwk" }); }
|
|
127
|
+
catch (e) {
|
|
128
|
+
throw new AuthError("auth-jwt-external/bad-jwk",
|
|
129
|
+
"could not import JWK (kid=" + (jwk && jwk.kid) + "): " + ((e && e.message) || String(e)));
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function _toKey(value) {
|
|
134
|
+
if (!value) {
|
|
135
|
+
throw new AuthError("auth-jwt-external/no-key",
|
|
136
|
+
"key resolution returned no value");
|
|
137
|
+
}
|
|
138
|
+
if (value instanceof nodeCrypto.KeyObject) return value;
|
|
139
|
+
if (typeof value === "object" && value.kty) return _jwkToKey(value);
|
|
140
|
+
if (typeof value === "string") {
|
|
141
|
+
try { return nodeCrypto.createPublicKey({ key: value, format: "pem" }); }
|
|
142
|
+
catch (e) {
|
|
143
|
+
throw new AuthError("auth-jwt-external/bad-pem",
|
|
144
|
+
"PEM parse failed: " + ((e && e.message) || String(e)));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (Buffer.isBuffer(value)) {
|
|
148
|
+
try { return nodeCrypto.createPublicKey({ key: value, format: "pem" }); }
|
|
149
|
+
catch (e) {
|
|
150
|
+
throw new AuthError("auth-jwt-external/bad-pem",
|
|
151
|
+
"PEM parse failed: " + ((e && e.message) || String(e)));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
throw new AuthError("auth-jwt-external/bad-key-shape",
|
|
155
|
+
"key must be a JWK object, PEM string/Buffer, or KeyObject");
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async function _fetchJwks(uri, cacheMs) {
|
|
159
|
+
// Validate the URI and fetch via http-client (SSRF gate, response-
|
|
160
|
+
// size cap, TLS-required by default).
|
|
161
|
+
safeUrl.parse(uri, { allowedProtocols: safeUrl.ALLOW_HTTP_TLS });
|
|
162
|
+
var jc = _getJwksCache();
|
|
163
|
+
var key = "jwks:" + uri;
|
|
164
|
+
return await jc.wrap(key, async function () {
|
|
165
|
+
var res = await httpClient().request({
|
|
166
|
+
method: "GET",
|
|
167
|
+
url: uri,
|
|
168
|
+
maxBytes: MAX_JWKS_BYTES,
|
|
169
|
+
timeoutMs: C.TIME.seconds(10),
|
|
170
|
+
});
|
|
171
|
+
if (res.statusCode < 200 || res.statusCode >= 300) { // allow:raw-byte-literal — HTTP 2xx range
|
|
172
|
+
throw new AuthError("auth-jwt-external/jwks-fetch-failed",
|
|
173
|
+
"JWKS endpoint " + uri + " returned " + res.statusCode);
|
|
174
|
+
}
|
|
175
|
+
var jwks;
|
|
176
|
+
try { jwks = safeJson.parse(res.body.toString("utf8"), { maxBytes: MAX_JWKS_BYTES }); }
|
|
177
|
+
catch (e) {
|
|
178
|
+
throw new AuthError("auth-jwt-external/jwks-parse-failed",
|
|
179
|
+
"JWKS parse failed: " + ((e && e.message) || String(e)));
|
|
180
|
+
}
|
|
181
|
+
if (!jwks || !Array.isArray(jwks.keys)) {
|
|
182
|
+
throw new AuthError("auth-jwt-external/bad-jwks",
|
|
183
|
+
"JWKS response missing 'keys' array");
|
|
184
|
+
}
|
|
185
|
+
return jwks.keys;
|
|
186
|
+
}, cacheMs || DEFAULT_JWKS_CACHE_MS);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function _selectKey(keys, header) {
|
|
190
|
+
if (!Array.isArray(keys) || keys.length === 0) {
|
|
191
|
+
throw new AuthError("auth-jwt-external/no-jwks-keys",
|
|
192
|
+
"JWKS source has no keys");
|
|
193
|
+
}
|
|
194
|
+
if (header.kid) {
|
|
195
|
+
for (var i = 0; i < keys.length; i += 1) {
|
|
196
|
+
if (keys[i].kid === header.kid) return keys[i];
|
|
197
|
+
}
|
|
198
|
+
throw new AuthError("auth-jwt-external/no-matching-kid",
|
|
199
|
+
"no JWKS key matches header.kid='" + header.kid + "'");
|
|
200
|
+
}
|
|
201
|
+
if (keys.length === 1) return keys[0];
|
|
202
|
+
throw new AuthError("auth-jwt-external/kid-required",
|
|
203
|
+
"JWKS has " + keys.length + " keys but token header has no kid");
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// ---- public surface ----
|
|
207
|
+
|
|
208
|
+
async function verifyExternal(token, opts) {
|
|
209
|
+
if (typeof token !== "string" || token.length === 0) {
|
|
210
|
+
throw new AuthError("auth-jwt-external/no-token", "token must be a non-empty string");
|
|
211
|
+
}
|
|
212
|
+
if (token.length > MAX_TOKEN_BYTES) {
|
|
213
|
+
throw new AuthError("auth-jwt-external/token-too-large",
|
|
214
|
+
"token exceeds " + MAX_TOKEN_BYTES + " bytes");
|
|
215
|
+
}
|
|
216
|
+
opts = opts || {};
|
|
217
|
+
validateOpts(opts, [
|
|
218
|
+
"algorithms", "jwks", "jwksUri", "jwksCacheMs", "keyResolver",
|
|
219
|
+
"audience", "issuer", "subject", "clockSkewMs",
|
|
220
|
+
], "auth.jwt.verifyExternal");
|
|
221
|
+
|
|
222
|
+
if (!Array.isArray(opts.algorithms) || opts.algorithms.length === 0) {
|
|
223
|
+
throw new AuthError("auth-jwt-external/algorithms-required",
|
|
224
|
+
"verifyExternal: opts.algorithms is required (no defaults — operator MUST " +
|
|
225
|
+
"name accepted algorithms to defend against alg-confusion)");
|
|
226
|
+
}
|
|
227
|
+
for (var ai = 0; ai < opts.algorithms.length; ai += 1) {
|
|
228
|
+
var listed = opts.algorithms[ai];
|
|
229
|
+
if (REFUSED_ALGS.indexOf(listed) !== -1) {
|
|
230
|
+
throw new AuthError("auth-jwt-external/refused-alg",
|
|
231
|
+
"verifyExternal refuses '" + listed + "' (HMAC/none is the alg-confusion vector " +
|
|
232
|
+
"against JWKS-shaped public-key trust sources)");
|
|
233
|
+
}
|
|
234
|
+
if (SUPPORTED_CLASSICAL_ALGS.indexOf(listed) === -1) {
|
|
235
|
+
throw new AuthError("auth-jwt-external/unsupported-alg",
|
|
236
|
+
"alg '" + listed + "' is not supported (supported: " +
|
|
237
|
+
SUPPORTED_CLASSICAL_ALGS.join(", ") + ")");
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
var sourcesGiven = (opts.jwks ? 1 : 0) + (opts.jwksUri ? 1 : 0) +
|
|
241
|
+
(typeof opts.keyResolver === "function" ? 1 : 0);
|
|
242
|
+
if (sourcesGiven === 0) {
|
|
243
|
+
throw new AuthError("auth-jwt-external/no-key-source",
|
|
244
|
+
"verifyExternal: pass exactly one of jwks, jwksUri, keyResolver");
|
|
245
|
+
}
|
|
246
|
+
if (sourcesGiven > 1) {
|
|
247
|
+
throw new AuthError("auth-jwt-external/conflicting-key-source",
|
|
248
|
+
"verifyExternal: pass exactly one of jwks, jwksUri, keyResolver");
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Decode header + payload.
|
|
252
|
+
var parts = token.split(".");
|
|
253
|
+
if (parts.length !== 3) {
|
|
254
|
+
throw new AuthError("auth-jwt-external/malformed-jwt",
|
|
255
|
+
"token does not have 3 parts");
|
|
256
|
+
}
|
|
257
|
+
var header, payload;
|
|
258
|
+
try {
|
|
259
|
+
header = safeJson.parse(_b64urlDecode(parts[0]).toString("utf8"), { maxBytes: MAX_JWKS_BYTES });
|
|
260
|
+
payload = safeJson.parse(_b64urlDecode(parts[1]).toString("utf8"), { maxBytes: MAX_JWKS_BYTES });
|
|
261
|
+
} catch (e) {
|
|
262
|
+
throw new AuthError("auth-jwt-external/malformed-jwt",
|
|
263
|
+
"header/payload decode failed: " + ((e && e.message) || String(e)));
|
|
264
|
+
}
|
|
265
|
+
if (!header || typeof header.alg !== "string") {
|
|
266
|
+
throw new AuthError("auth-jwt-external/malformed-jwt", "header missing 'alg'");
|
|
267
|
+
}
|
|
268
|
+
if (header.crit !== undefined) {
|
|
269
|
+
throw new AuthError("auth-jwt-external/unknown-crit",
|
|
270
|
+
"token declares 'crit' header — verifyExternal does not support critical extensions");
|
|
271
|
+
}
|
|
272
|
+
if (opts.algorithms.indexOf(header.alg) === -1) {
|
|
273
|
+
throw new AuthError("auth-jwt-external/alg-not-allowed",
|
|
274
|
+
"token alg='" + header.alg + "' not in allowed list [" + opts.algorithms.join(", ") + "]");
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Resolve key.
|
|
278
|
+
var key;
|
|
279
|
+
if (typeof opts.keyResolver === "function") {
|
|
280
|
+
var resolved;
|
|
281
|
+
try { resolved = await opts.keyResolver(header); }
|
|
282
|
+
catch (e) {
|
|
283
|
+
throw new AuthError("auth-jwt-external/key-resolver-failed",
|
|
284
|
+
"keyResolver threw: " + ((e && e.message) || String(e)));
|
|
285
|
+
}
|
|
286
|
+
key = _toKey(resolved);
|
|
287
|
+
} else {
|
|
288
|
+
var keys = opts.jwks ? opts.jwks
|
|
289
|
+
: await _fetchJwks(opts.jwksUri, opts.jwksCacheMs);
|
|
290
|
+
var jwk = _selectKey(keys, header);
|
|
291
|
+
key = _jwkToKey(jwk);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// Verify signature.
|
|
295
|
+
var params = _verifyParamsForAlg(header.alg);
|
|
296
|
+
var signingInput = parts[0] + "." + parts[1];
|
|
297
|
+
var sig = _b64urlDecode(parts[2]);
|
|
298
|
+
var verifyOpts = { key: key };
|
|
299
|
+
if (params.padding !== undefined) verifyOpts.padding = params.padding;
|
|
300
|
+
if (params.saltLength !== undefined) verifyOpts.saltLength = params.saltLength;
|
|
301
|
+
if (params.dsaEncoding !== undefined) verifyOpts.dsaEncoding = params.dsaEncoding;
|
|
302
|
+
var verified;
|
|
303
|
+
try {
|
|
304
|
+
verified = nodeCrypto.verify(params.hash, Buffer.from(signingInput, "ascii"), verifyOpts, sig);
|
|
305
|
+
} catch (e) {
|
|
306
|
+
throw new AuthError("auth-jwt-external/invalid-signature",
|
|
307
|
+
"signature verification failed: " + ((e && e.message) || String(e)));
|
|
308
|
+
}
|
|
309
|
+
if (!verified) {
|
|
310
|
+
throw new AuthError("auth-jwt-external/invalid-signature",
|
|
311
|
+
"signature verification failed");
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Claim validation.
|
|
315
|
+
var clockSkewMs = typeof opts.clockSkewMs === "number" ? opts.clockSkewMs : DEFAULT_CLOCK_SKEW_MS;
|
|
316
|
+
var nowSec = Math.floor(Date.now() / C.TIME.seconds(1));
|
|
317
|
+
var skewSec = Math.floor(clockSkewMs / C.TIME.seconds(1));
|
|
318
|
+
|
|
319
|
+
if (typeof payload.exp !== "number") {
|
|
320
|
+
throw new AuthError("auth-jwt-external/missing-exp", "claim 'exp' missing");
|
|
321
|
+
}
|
|
322
|
+
if (payload.exp + skewSec < nowSec) {
|
|
323
|
+
throw new AuthError("auth-jwt-external/expired",
|
|
324
|
+
"token expired (exp=" + payload.exp + ", now=" + nowSec + ")");
|
|
325
|
+
}
|
|
326
|
+
if (typeof payload.nbf === "number" && payload.nbf - skewSec > nowSec) {
|
|
327
|
+
throw new AuthError("auth-jwt-external/nbf-future",
|
|
328
|
+
"token not-yet-valid (nbf=" + payload.nbf + ", now=" + nowSec + ")");
|
|
329
|
+
}
|
|
330
|
+
if (typeof payload.iat === "number" && payload.iat - skewSec > nowSec) {
|
|
331
|
+
throw new AuthError("auth-jwt-external/iat-future",
|
|
332
|
+
"token iat is in the future");
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if (opts.audience) {
|
|
336
|
+
var aud = payload.aud;
|
|
337
|
+
var expectedAud = Array.isArray(opts.audience) ? opts.audience : [opts.audience];
|
|
338
|
+
var actualAud = Array.isArray(aud) ? aud : (typeof aud === "string" ? [aud] : []);
|
|
339
|
+
var audMatch = false;
|
|
340
|
+
for (var ej = 0; ej < expectedAud.length; ej += 1) {
|
|
341
|
+
if (actualAud.indexOf(expectedAud[ej]) !== -1) { audMatch = true; break; }
|
|
342
|
+
}
|
|
343
|
+
if (!audMatch) {
|
|
344
|
+
throw new AuthError("auth-jwt-external/aud-mismatch",
|
|
345
|
+
"token aud '" + JSON.stringify(aud) + "' does not match expected '" +
|
|
346
|
+
JSON.stringify(opts.audience) + "'");
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
if (opts.issuer && payload.iss !== opts.issuer) {
|
|
350
|
+
throw new AuthError("auth-jwt-external/iss-mismatch",
|
|
351
|
+
"token iss '" + payload.iss + "' does not match expected '" + opts.issuer + "'");
|
|
352
|
+
}
|
|
353
|
+
if (opts.subject && payload.sub !== opts.subject) {
|
|
354
|
+
throw new AuthError("auth-jwt-external/sub-mismatch",
|
|
355
|
+
"token sub '" + payload.sub + "' does not match expected '" + opts.subject + "'");
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
return { header: header, claims: payload };
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
module.exports = {
|
|
362
|
+
verifyExternal: verifyExternal,
|
|
363
|
+
SUPPORTED_CLASSICAL_ALGS: SUPPORTED_CLASSICAL_ALGS,
|
|
364
|
+
REFUSED_ALGS: REFUSED_ALGS,
|
|
365
|
+
};
|