@passflow/node 0.1.3 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +106 -3
- package/dist/adapter-Du22h3Lk.mjs +57 -0
- package/dist/adapter-DyCsMsEq.cjs +62 -0
- package/dist/evaluate-BAJUQu_Z.d.cts +141 -0
- package/dist/evaluate-BAJUQu_Z.d.mts +141 -0
- package/dist/{evaluate-BTuol-Oa.mjs → evaluate-CHts4VKy.mjs} +77 -18
- package/dist/{evaluate-C20MecTf.cjs → evaluate-Yv4mWPRI.cjs} +88 -17
- package/dist/express.cjs +22 -8
- package/dist/express.d.cts +15 -6
- package/dist/express.d.mts +15 -6
- package/dist/express.mjs +22 -8
- package/dist/fastify.cjs +22 -8
- package/dist/fastify.d.cts +17 -8
- package/dist/fastify.d.mts +17 -8
- package/dist/fastify.mjs +22 -8
- package/dist/hono.cjs +22 -8
- package/dist/hono.d.cts +16 -7
- package/dist/hono.d.mts +16 -7
- package/dist/hono.mjs +22 -8
- package/dist/index.cjs +85 -18
- package/dist/index.d.cts +34 -41
- package/dist/index.d.mts +34 -41
- package/dist/index.mjs +83 -18
- package/dist/loopback-ADMM_GlW.cjs +22 -0
- package/dist/loopback-CDiL_KzV.mjs +17 -0
- package/dist/manager-Bm3vM2Aw.d.cts +93 -0
- package/dist/manager-Bm3vM2Aw.d.mts +93 -0
- package/dist/session-browser.cjs +17 -0
- package/dist/session-browser.d.cts +5 -0
- package/dist/session-browser.d.mts +5 -0
- package/dist/session-browser.mjs +14 -0
- package/dist/session.cjs +923 -0
- package/dist/session.d.cts +7 -0
- package/dist/session.d.mts +7 -0
- package/dist/session.mjs +898 -0
- package/package.json +30 -3
- package/dist/claims-BHJhnzch.d.cts +0 -51
- package/dist/claims-BHJhnzch.d.mts +0 -51
|
@@ -122,6 +122,10 @@ function isStringArray(value) {
|
|
|
122
122
|
function stringsOf(value) {
|
|
123
123
|
return Array.isArray(value) ? value.filter((entry) => typeof entry === "string") : [];
|
|
124
124
|
}
|
|
125
|
+
function scopesOf(payload) {
|
|
126
|
+
if (!Object.hasOwn(payload, "scope")) return stringsOf(payload.scopes);
|
|
127
|
+
return typeof payload.scope === "string" ? payload.scope.split(" ").filter((scope) => scope !== "") : [];
|
|
128
|
+
}
|
|
125
129
|
/** A wrong JSON type means the membership cannot be read. `null` reads as absent, as Go's decoder does. */
|
|
126
130
|
function decodeMembership(value) {
|
|
127
131
|
if (!isPlainObject(value)) return;
|
|
@@ -169,7 +173,7 @@ function decodeTenants(value) {
|
|
|
169
173
|
}
|
|
170
174
|
/** Fail-closed claim extraction from a payload whose signature and registered claims already passed. */
|
|
171
175
|
function extractClaims(payload, verifiedAt) {
|
|
172
|
-
const { sub, iss, aud, exp, iat, auth_time: authTime, acr, amr,
|
|
176
|
+
const { sub, iss, aud, exp, iat, auth_time: authTime, acr, amr, client_id: clientId, sid, type, passflow_tm: tenants } = payload;
|
|
173
177
|
const claims = {
|
|
174
178
|
sub: typeof sub === "string" ? sub : "",
|
|
175
179
|
iss: typeof iss === "string" ? iss : "",
|
|
@@ -177,7 +181,7 @@ function extractClaims(payload, verifiedAt) {
|
|
|
177
181
|
exp,
|
|
178
182
|
acr: typeof acr === "string" ? acr : "0",
|
|
179
183
|
amr: stringsOf(amr),
|
|
180
|
-
scopes:
|
|
184
|
+
scopes: scopesOf(payload),
|
|
181
185
|
type: typeof type === "string" ? type : "",
|
|
182
186
|
tenants: decodeTenants(tenants),
|
|
183
187
|
verifiedAt,
|
|
@@ -185,6 +189,7 @@ function extractClaims(payload, verifiedAt) {
|
|
|
185
189
|
};
|
|
186
190
|
if (isExactSeconds(iat)) claims.iat = iat;
|
|
187
191
|
if (isExactSeconds(authTime)) claims.authTime = Math.trunc(authTime);
|
|
192
|
+
if (typeof clientId === "string") claims.clientId = clientId;
|
|
188
193
|
if (typeof sid === "string") claims.sid = sid;
|
|
189
194
|
return claims;
|
|
190
195
|
}
|
|
@@ -271,17 +276,38 @@ const reject = (status, wwwAuthenticate) => ({
|
|
|
271
276
|
[CACHE_CONTROL]: NO_STORE
|
|
272
277
|
}
|
|
273
278
|
});
|
|
279
|
+
/** Parameter order is fixed: error, scope, resource_metadata. Absent ones are omitted. */
|
|
280
|
+
function formatBearer(error, scope, challenge) {
|
|
281
|
+
const parameters = [];
|
|
282
|
+
if (error !== void 0) parameters.push(`error="${error}"`);
|
|
283
|
+
if (scope !== void 0) parameters.push(`scope="${scope}"`);
|
|
284
|
+
if (challenge !== void 0) parameters.push(`resource_metadata="${challenge.resourceMetadata}"`);
|
|
285
|
+
return parameters.length === 0 ? "Bearer" : `Bearer ${parameters.join(", ")}`;
|
|
286
|
+
}
|
|
287
|
+
function rejectStepUp(stepUp, challenge) {
|
|
288
|
+
return reject(401, challenge === void 0 ? stepUp : `${stepUp}, resource_metadata="${challenge.resourceMetadata}"`);
|
|
289
|
+
}
|
|
274
290
|
/**
|
|
275
291
|
* Authentication failures never carry the step-up error: the browser SDK acts only on the step-up challenge,
|
|
276
292
|
* and every other response, a plain 401 or a 503 included, reaches the caller's code unchanged.
|
|
277
293
|
*/
|
|
278
|
-
function rejectAuthentication(code) {
|
|
294
|
+
function rejectAuthentication(code, challenge) {
|
|
279
295
|
switch (code) {
|
|
280
|
-
case "missing_token": return reject(401,
|
|
281
|
-
case "invalid_token": return reject(401, "
|
|
296
|
+
case "missing_token": return reject(401, formatBearer(void 0, challenge?.scope, challenge));
|
|
297
|
+
case "invalid_token": return reject(401, formatBearer("invalid_token", challenge?.scope, challenge));
|
|
282
298
|
case "jwks_unavailable": return reject(503);
|
|
283
299
|
}
|
|
284
300
|
}
|
|
301
|
+
/** The scope-token of RFC 6749, section 3.3. */
|
|
302
|
+
const SCOPE_TOKEN = /^[\x21\x23-\x5B\x5D-\x7E]+$/;
|
|
303
|
+
/** Internal: one or more scope tokens, or a `TypeError`. The result is a copy. */
|
|
304
|
+
function assertScopes(scopes) {
|
|
305
|
+
if (!Array.isArray(scopes) || scopes.length === 0) throw new TypeError("[passflow] scopes must name one or more scopes");
|
|
306
|
+
return Array.from(scopes, (scope) => {
|
|
307
|
+
if (typeof scope !== "string" || !SCOPE_TOKEN.test(scope)) throw new TypeError("[passflow] scopes must hold RFC 6749 scope tokens");
|
|
308
|
+
return scope;
|
|
309
|
+
});
|
|
310
|
+
}
|
|
285
311
|
/**
|
|
286
312
|
* Validates a requirement and returns it with the effective freshness limit. Throws `TypeError`: this is a
|
|
287
313
|
* programming error that should stop the process at route registration, not become a `Decision`.
|
|
@@ -298,8 +324,12 @@ function assertRequirement(requirement) {
|
|
|
298
324
|
kind: "fresh",
|
|
299
325
|
maxAgeSeconds: assertMaxAge(requirement.maxAgeSeconds)
|
|
300
326
|
};
|
|
327
|
+
if (requirement.kind === "scope") return {
|
|
328
|
+
kind: "scope",
|
|
329
|
+
scopes: assertScopes(requirement.scopes)
|
|
330
|
+
};
|
|
301
331
|
}
|
|
302
|
-
throw new TypeError("[passflow] a requirement is { kind: \"acr\", min }
|
|
332
|
+
throw new TypeError("[passflow] a requirement is { kind: \"acr\", min }, { kind: \"fresh\", maxAgeSeconds } or { kind: \"scope\", scopes }");
|
|
303
333
|
}
|
|
304
334
|
function isFresh(claims, maxAgeSeconds) {
|
|
305
335
|
const { authTime, verifiedAt } = claims;
|
|
@@ -309,40 +339,69 @@ function isFresh(claims, maxAgeSeconds) {
|
|
|
309
339
|
/**
|
|
310
340
|
* Decides one requirement against claims. No claims means the middleware was composed wrongly, so the answer
|
|
311
341
|
* is a 500 with no challenge: never a step-up, which the SDK would try to satisfy, and never a 401, because a
|
|
312
|
-
* wiring bug must not be reported as a bad token. Throws `TypeError` on an invalid requirement.
|
|
342
|
+
* wiring bug must not be reported as a bad token. Throws `TypeError` on an invalid requirement. With `challenge`, a
|
|
343
|
+
* rejection names the resource metadata.
|
|
313
344
|
*/
|
|
314
|
-
function evaluate(claims, requirement) {
|
|
345
|
+
function evaluate(claims, requirement, challenge) {
|
|
315
346
|
const checked = assertRequirement(requirement);
|
|
316
347
|
if (claims === void 0 || claims === null) return reject(500);
|
|
317
|
-
if (checked.kind === "acr") return acrSatisfies(claims.acr, checked.min) ? { kind: "allow" } :
|
|
318
|
-
|
|
348
|
+
if (checked.kind === "acr") return acrSatisfies(claims.acr, checked.min) ? { kind: "allow" } : rejectStepUp(formatChallenge({ acrValues: checked.min }), challenge);
|
|
349
|
+
if (checked.kind === "scope") {
|
|
350
|
+
const { scopes } = checked;
|
|
351
|
+
const granted = claims.scopes;
|
|
352
|
+
return Array.isArray(granted) && scopes.every((scope) => granted.includes(scope)) ? { kind: "allow" } : reject(403, formatBearer("insufficient_scope", scopes.join(" "), challenge));
|
|
353
|
+
}
|
|
354
|
+
return isFresh(claims, checked.maxAgeSeconds) ? { kind: "allow" } : rejectStepUp(formatChallenge({ maxAge: checked.maxAgeSeconds }), challenge);
|
|
319
355
|
}
|
|
320
356
|
/**
|
|
321
357
|
* Authenticates a request. `existing` claims are reused only when `verifier` produced that exact object;
|
|
322
358
|
* otherwise the presented header is verified with `verifier`, so a token outside this verifier's trust policy
|
|
323
|
-
* is rejected as not valid here, never as insufficient. Never rejects: a failure is a `{ reject }` value.
|
|
359
|
+
* is rejected as not valid here, never as insufficient. Never rejects: a failure is a `{ reject }` value. With
|
|
360
|
+
* `challenge`, a 401 carries the resource's scope and metadata URL.
|
|
324
361
|
*/
|
|
325
|
-
async function authenticate(verifier, authorization, existing) {
|
|
362
|
+
async function authenticate(verifier, authorization, existing, challenge) {
|
|
326
363
|
if (existing !== void 0 && existing !== null && verifiedBy(existing, verifier)) return { claims: existing };
|
|
327
364
|
const token = extractBearer(authorization);
|
|
328
|
-
if (token === void 0) return { reject: rejectAuthentication("missing_token") };
|
|
365
|
+
if (token === void 0) return { reject: rejectAuthentication("missing_token", challenge) };
|
|
329
366
|
try {
|
|
330
367
|
return { claims: await verifier.verify(token) };
|
|
331
368
|
} catch (error) {
|
|
332
|
-
return { reject: isPassflowAuthError(error) ? rejectAuthentication(error.code) : reject(500) };
|
|
369
|
+
return { reject: isPassflowAuthError(error) ? rejectAuthentication(error.code, challenge) : reject(500) };
|
|
333
370
|
}
|
|
334
371
|
}
|
|
335
372
|
/**
|
|
336
373
|
* Internal: one adapter step. Authenticates, then applies the requirement if there is one.
|
|
337
374
|
*/
|
|
338
|
-
async function guard(verifier, authorization, existing, requirement) {
|
|
339
|
-
const result = await authenticate(verifier, authorization, existing);
|
|
375
|
+
async function guard(verifier, authorization, existing, requirement, challenge) {
|
|
376
|
+
const result = await authenticate(verifier, authorization, existing, challenge);
|
|
340
377
|
if ("reject" in result || requirement === void 0) return result;
|
|
341
|
-
const decision = evaluate(result.claims, requirement);
|
|
378
|
+
const decision = evaluate(result.claims, requirement, challenge);
|
|
342
379
|
return decision.kind === "reject" ? { reject: decision } : result;
|
|
343
380
|
}
|
|
381
|
+
/**
|
|
382
|
+
* Authenticates a web `Request`, for a fetch-style server or a framework route handler with no adapter here.
|
|
383
|
+
* Verifies the `Authorization` header with `verifier`, then applies `requirement` if there is one. A rejection
|
|
384
|
+
* comes back as a ready `Response`: empty body, the challenge when there is one, and `Cache-Control: no-store`.
|
|
385
|
+
*
|
|
386
|
+
* Throws `TypeError` before it reads a header when the verifier or the request is missing, or the requirement
|
|
387
|
+
* is invalid: those are programming errors, not a bad token.
|
|
388
|
+
*/
|
|
389
|
+
async function authenticateRequest(verifier, request, requirement) {
|
|
390
|
+
if (typeof verifier !== "object" || verifier === null || typeof verifier.verify !== "function") throw new TypeError("[passflow] authenticateRequest needs a verifier from createVerifier");
|
|
391
|
+
if (typeof request !== "object" || request === null || typeof request.headers?.get !== "function") throw new TypeError("[passflow] authenticateRequest needs a Request");
|
|
392
|
+
const checked = requirement === void 0 ? void 0 : assertRequirement(requirement);
|
|
393
|
+
const result = await guard(verifier, request.headers.get("authorization"), void 0, checked);
|
|
394
|
+
if ("reject" in result) {
|
|
395
|
+
const { status, headers } = result.reject;
|
|
396
|
+
return { response: new Response(null, {
|
|
397
|
+
status,
|
|
398
|
+
headers
|
|
399
|
+
}) };
|
|
400
|
+
}
|
|
401
|
+
return { claims: result.claims };
|
|
402
|
+
}
|
|
344
403
|
/** Internal: the adapters fail at construction when handed something that cannot verify. */
|
|
345
404
|
function assertVerifier(verifier) {
|
|
346
405
|
if (typeof verifier !== "object" || verifier === null || typeof verifier.verify !== "function") throw new TypeError("[passflow] an adapter needs a verifier from createVerifier");
|
|
347
406
|
}
|
|
348
|
-
export {
|
|
407
|
+
export { AAL3 as C, acrSatisfies as E, AAL2 as S, acrLevel as T, verifiedBy as _, authenticateRequest as a, extractBearer as b, PassflowAuthError as c, CHALLENGE_HEADER as d, CORS_EXPOSED_HEADERS as f, rolesOf as g, recordProvenance as h, authenticate as i, PassflowUsageError as l, extractClaims as m, assertScopes as n, evaluate as o, withChallengeHeader as p, assertVerifier as r, guard as s, assertRequirement as t, isPassflowAuthError as u, assertMaxAge as v, ACR_NONE as w, AAL1 as x, formatChallenge as y };
|
|
@@ -122,6 +122,10 @@ function isStringArray(value) {
|
|
|
122
122
|
function stringsOf(value) {
|
|
123
123
|
return Array.isArray(value) ? value.filter((entry) => typeof entry === "string") : [];
|
|
124
124
|
}
|
|
125
|
+
function scopesOf(payload) {
|
|
126
|
+
if (!Object.hasOwn(payload, "scope")) return stringsOf(payload.scopes);
|
|
127
|
+
return typeof payload.scope === "string" ? payload.scope.split(" ").filter((scope) => scope !== "") : [];
|
|
128
|
+
}
|
|
125
129
|
/** A wrong JSON type means the membership cannot be read. `null` reads as absent, as Go's decoder does. */
|
|
126
130
|
function decodeMembership(value) {
|
|
127
131
|
if (!isPlainObject(value)) return;
|
|
@@ -169,7 +173,7 @@ function decodeTenants(value) {
|
|
|
169
173
|
}
|
|
170
174
|
/** Fail-closed claim extraction from a payload whose signature and registered claims already passed. */
|
|
171
175
|
function extractClaims(payload, verifiedAt) {
|
|
172
|
-
const { sub, iss, aud, exp, iat, auth_time: authTime, acr, amr,
|
|
176
|
+
const { sub, iss, aud, exp, iat, auth_time: authTime, acr, amr, client_id: clientId, sid, type, passflow_tm: tenants } = payload;
|
|
173
177
|
const claims = {
|
|
174
178
|
sub: typeof sub === "string" ? sub : "",
|
|
175
179
|
iss: typeof iss === "string" ? iss : "",
|
|
@@ -177,7 +181,7 @@ function extractClaims(payload, verifiedAt) {
|
|
|
177
181
|
exp,
|
|
178
182
|
acr: typeof acr === "string" ? acr : "0",
|
|
179
183
|
amr: stringsOf(amr),
|
|
180
|
-
scopes:
|
|
184
|
+
scopes: scopesOf(payload),
|
|
181
185
|
type: typeof type === "string" ? type : "",
|
|
182
186
|
tenants: decodeTenants(tenants),
|
|
183
187
|
verifiedAt,
|
|
@@ -185,6 +189,7 @@ function extractClaims(payload, verifiedAt) {
|
|
|
185
189
|
};
|
|
186
190
|
if (isExactSeconds(iat)) claims.iat = iat;
|
|
187
191
|
if (isExactSeconds(authTime)) claims.authTime = Math.trunc(authTime);
|
|
192
|
+
if (typeof clientId === "string") claims.clientId = clientId;
|
|
188
193
|
if (typeof sid === "string") claims.sid = sid;
|
|
189
194
|
return claims;
|
|
190
195
|
}
|
|
@@ -271,17 +276,38 @@ const reject = (status, wwwAuthenticate) => ({
|
|
|
271
276
|
[CACHE_CONTROL]: NO_STORE
|
|
272
277
|
}
|
|
273
278
|
});
|
|
279
|
+
/** Parameter order is fixed: error, scope, resource_metadata. Absent ones are omitted. */
|
|
280
|
+
function formatBearer(error, scope, challenge) {
|
|
281
|
+
const parameters = [];
|
|
282
|
+
if (error !== void 0) parameters.push(`error="${error}"`);
|
|
283
|
+
if (scope !== void 0) parameters.push(`scope="${scope}"`);
|
|
284
|
+
if (challenge !== void 0) parameters.push(`resource_metadata="${challenge.resourceMetadata}"`);
|
|
285
|
+
return parameters.length === 0 ? "Bearer" : `Bearer ${parameters.join(", ")}`;
|
|
286
|
+
}
|
|
287
|
+
function rejectStepUp(stepUp, challenge) {
|
|
288
|
+
return reject(401, challenge === void 0 ? stepUp : `${stepUp}, resource_metadata="${challenge.resourceMetadata}"`);
|
|
289
|
+
}
|
|
274
290
|
/**
|
|
275
291
|
* Authentication failures never carry the step-up error: the browser SDK acts only on the step-up challenge,
|
|
276
292
|
* and every other response, a plain 401 or a 503 included, reaches the caller's code unchanged.
|
|
277
293
|
*/
|
|
278
|
-
function rejectAuthentication(code) {
|
|
294
|
+
function rejectAuthentication(code, challenge) {
|
|
279
295
|
switch (code) {
|
|
280
|
-
case "missing_token": return reject(401,
|
|
281
|
-
case "invalid_token": return reject(401, "
|
|
296
|
+
case "missing_token": return reject(401, formatBearer(void 0, challenge?.scope, challenge));
|
|
297
|
+
case "invalid_token": return reject(401, formatBearer("invalid_token", challenge?.scope, challenge));
|
|
282
298
|
case "jwks_unavailable": return reject(503);
|
|
283
299
|
}
|
|
284
300
|
}
|
|
301
|
+
/** The scope-token of RFC 6749, section 3.3. */
|
|
302
|
+
const SCOPE_TOKEN = /^[\x21\x23-\x5B\x5D-\x7E]+$/;
|
|
303
|
+
/** Internal: one or more scope tokens, or a `TypeError`. The result is a copy. */
|
|
304
|
+
function assertScopes(scopes) {
|
|
305
|
+
if (!Array.isArray(scopes) || scopes.length === 0) throw new TypeError("[passflow] scopes must name one or more scopes");
|
|
306
|
+
return Array.from(scopes, (scope) => {
|
|
307
|
+
if (typeof scope !== "string" || !SCOPE_TOKEN.test(scope)) throw new TypeError("[passflow] scopes must hold RFC 6749 scope tokens");
|
|
308
|
+
return scope;
|
|
309
|
+
});
|
|
310
|
+
}
|
|
285
311
|
/**
|
|
286
312
|
* Validates a requirement and returns it with the effective freshness limit. Throws `TypeError`: this is a
|
|
287
313
|
* programming error that should stop the process at route registration, not become a `Decision`.
|
|
@@ -298,8 +324,12 @@ function assertRequirement(requirement) {
|
|
|
298
324
|
kind: "fresh",
|
|
299
325
|
maxAgeSeconds: assertMaxAge(requirement.maxAgeSeconds)
|
|
300
326
|
};
|
|
327
|
+
if (requirement.kind === "scope") return {
|
|
328
|
+
kind: "scope",
|
|
329
|
+
scopes: assertScopes(requirement.scopes)
|
|
330
|
+
};
|
|
301
331
|
}
|
|
302
|
-
throw new TypeError("[passflow] a requirement is { kind: \"acr\", min }
|
|
332
|
+
throw new TypeError("[passflow] a requirement is { kind: \"acr\", min }, { kind: \"fresh\", maxAgeSeconds } or { kind: \"scope\", scopes }");
|
|
303
333
|
}
|
|
304
334
|
function isFresh(claims, maxAgeSeconds) {
|
|
305
335
|
const { authTime, verifiedAt } = claims;
|
|
@@ -309,38 +339,67 @@ function isFresh(claims, maxAgeSeconds) {
|
|
|
309
339
|
/**
|
|
310
340
|
* Decides one requirement against claims. No claims means the middleware was composed wrongly, so the answer
|
|
311
341
|
* is a 500 with no challenge: never a step-up, which the SDK would try to satisfy, and never a 401, because a
|
|
312
|
-
* wiring bug must not be reported as a bad token. Throws `TypeError` on an invalid requirement.
|
|
342
|
+
* wiring bug must not be reported as a bad token. Throws `TypeError` on an invalid requirement. With `challenge`, a
|
|
343
|
+
* rejection names the resource metadata.
|
|
313
344
|
*/
|
|
314
|
-
function evaluate(claims, requirement) {
|
|
345
|
+
function evaluate(claims, requirement, challenge) {
|
|
315
346
|
const checked = assertRequirement(requirement);
|
|
316
347
|
if (claims === void 0 || claims === null) return reject(500);
|
|
317
|
-
if (checked.kind === "acr") return acrSatisfies(claims.acr, checked.min) ? { kind: "allow" } :
|
|
318
|
-
|
|
348
|
+
if (checked.kind === "acr") return acrSatisfies(claims.acr, checked.min) ? { kind: "allow" } : rejectStepUp(formatChallenge({ acrValues: checked.min }), challenge);
|
|
349
|
+
if (checked.kind === "scope") {
|
|
350
|
+
const { scopes } = checked;
|
|
351
|
+
const granted = claims.scopes;
|
|
352
|
+
return Array.isArray(granted) && scopes.every((scope) => granted.includes(scope)) ? { kind: "allow" } : reject(403, formatBearer("insufficient_scope", scopes.join(" "), challenge));
|
|
353
|
+
}
|
|
354
|
+
return isFresh(claims, checked.maxAgeSeconds) ? { kind: "allow" } : rejectStepUp(formatChallenge({ maxAge: checked.maxAgeSeconds }), challenge);
|
|
319
355
|
}
|
|
320
356
|
/**
|
|
321
357
|
* Authenticates a request. `existing` claims are reused only when `verifier` produced that exact object;
|
|
322
358
|
* otherwise the presented header is verified with `verifier`, so a token outside this verifier's trust policy
|
|
323
|
-
* is rejected as not valid here, never as insufficient. Never rejects: a failure is a `{ reject }` value.
|
|
359
|
+
* is rejected as not valid here, never as insufficient. Never rejects: a failure is a `{ reject }` value. With
|
|
360
|
+
* `challenge`, a 401 carries the resource's scope and metadata URL.
|
|
324
361
|
*/
|
|
325
|
-
async function authenticate(verifier, authorization, existing) {
|
|
362
|
+
async function authenticate(verifier, authorization, existing, challenge) {
|
|
326
363
|
if (existing !== void 0 && existing !== null && verifiedBy(existing, verifier)) return { claims: existing };
|
|
327
364
|
const token = extractBearer(authorization);
|
|
328
|
-
if (token === void 0) return { reject: rejectAuthentication("missing_token") };
|
|
365
|
+
if (token === void 0) return { reject: rejectAuthentication("missing_token", challenge) };
|
|
329
366
|
try {
|
|
330
367
|
return { claims: await verifier.verify(token) };
|
|
331
368
|
} catch (error) {
|
|
332
|
-
return { reject: isPassflowAuthError(error) ? rejectAuthentication(error.code) : reject(500) };
|
|
369
|
+
return { reject: isPassflowAuthError(error) ? rejectAuthentication(error.code, challenge) : reject(500) };
|
|
333
370
|
}
|
|
334
371
|
}
|
|
335
372
|
/**
|
|
336
373
|
* Internal: one adapter step. Authenticates, then applies the requirement if there is one.
|
|
337
374
|
*/
|
|
338
|
-
async function guard(verifier, authorization, existing, requirement) {
|
|
339
|
-
const result = await authenticate(verifier, authorization, existing);
|
|
375
|
+
async function guard(verifier, authorization, existing, requirement, challenge) {
|
|
376
|
+
const result = await authenticate(verifier, authorization, existing, challenge);
|
|
340
377
|
if ("reject" in result || requirement === void 0) return result;
|
|
341
|
-
const decision = evaluate(result.claims, requirement);
|
|
378
|
+
const decision = evaluate(result.claims, requirement, challenge);
|
|
342
379
|
return decision.kind === "reject" ? { reject: decision } : result;
|
|
343
380
|
}
|
|
381
|
+
/**
|
|
382
|
+
* Authenticates a web `Request`, for a fetch-style server or a framework route handler with no adapter here.
|
|
383
|
+
* Verifies the `Authorization` header with `verifier`, then applies `requirement` if there is one. A rejection
|
|
384
|
+
* comes back as a ready `Response`: empty body, the challenge when there is one, and `Cache-Control: no-store`.
|
|
385
|
+
*
|
|
386
|
+
* Throws `TypeError` before it reads a header when the verifier or the request is missing, or the requirement
|
|
387
|
+
* is invalid: those are programming errors, not a bad token.
|
|
388
|
+
*/
|
|
389
|
+
async function authenticateRequest(verifier, request, requirement) {
|
|
390
|
+
if (typeof verifier !== "object" || verifier === null || typeof verifier.verify !== "function") throw new TypeError("[passflow] authenticateRequest needs a verifier from createVerifier");
|
|
391
|
+
if (typeof request !== "object" || request === null || typeof request.headers?.get !== "function") throw new TypeError("[passflow] authenticateRequest needs a Request");
|
|
392
|
+
const checked = requirement === void 0 ? void 0 : assertRequirement(requirement);
|
|
393
|
+
const result = await guard(verifier, request.headers.get("authorization"), void 0, checked);
|
|
394
|
+
if ("reject" in result) {
|
|
395
|
+
const { status, headers } = result.reject;
|
|
396
|
+
return { response: new Response(null, {
|
|
397
|
+
status,
|
|
398
|
+
headers
|
|
399
|
+
}) };
|
|
400
|
+
}
|
|
401
|
+
return { claims: result.claims };
|
|
402
|
+
}
|
|
344
403
|
/** Internal: the adapters fail at construction when handed something that cannot verify. */
|
|
345
404
|
function assertVerifier(verifier) {
|
|
346
405
|
if (typeof verifier !== "object" || verifier === null || typeof verifier.verify !== "function") throw new TypeError("[passflow] an adapter needs a verifier from createVerifier");
|
|
@@ -417,6 +476,12 @@ Object.defineProperty(exports, "assertRequirement", {
|
|
|
417
476
|
return assertRequirement;
|
|
418
477
|
}
|
|
419
478
|
});
|
|
479
|
+
Object.defineProperty(exports, "assertScopes", {
|
|
480
|
+
enumerable: true,
|
|
481
|
+
get: function() {
|
|
482
|
+
return assertScopes;
|
|
483
|
+
}
|
|
484
|
+
});
|
|
420
485
|
Object.defineProperty(exports, "assertVerifier", {
|
|
421
486
|
enumerable: true,
|
|
422
487
|
get: function() {
|
|
@@ -429,6 +494,12 @@ Object.defineProperty(exports, "authenticate", {
|
|
|
429
494
|
return authenticate;
|
|
430
495
|
}
|
|
431
496
|
});
|
|
497
|
+
Object.defineProperty(exports, "authenticateRequest", {
|
|
498
|
+
enumerable: true,
|
|
499
|
+
get: function() {
|
|
500
|
+
return authenticateRequest;
|
|
501
|
+
}
|
|
502
|
+
});
|
|
432
503
|
Object.defineProperty(exports, "evaluate", {
|
|
433
504
|
enumerable: true,
|
|
434
505
|
get: function() {
|
package/dist/express.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const
|
|
2
|
+
const require_adapter = require("./adapter-DyCsMsEq.cjs");
|
|
3
|
+
const require_evaluate = require("./evaluate-Yv4mWPRI.cjs");
|
|
3
4
|
// src/express.ts
|
|
4
5
|
/** Written directly and never through `next(err)`: a custom error handler would drop `WWW-Authenticate`. */
|
|
5
6
|
function send(res, rejection) {
|
|
@@ -8,13 +9,15 @@ function send(res, rejection) {
|
|
|
8
9
|
/**
|
|
9
10
|
* Express 4 and 5 middleware. `authenticate` always verifies the presented token and replaces any claims on the
|
|
10
11
|
* request. `requireACR` and `requireFresh` work alone: they reuse claims only when this verifier produced that
|
|
11
|
-
* exact object, and otherwise verify the presented token themselves.
|
|
12
|
-
* an invalid level or
|
|
12
|
+
* exact object, and otherwise verify the presented token themselves. `requireScopes`
|
|
13
|
+
* does the same for scopes. All three throw `TypeError` at construction on an invalid level, limit or scope. A
|
|
14
|
+
* protected resource adds its own parameters to the challenges. Mount `metadata` with `app.all`, because a browser
|
|
15
|
+
* client may send a preflight request.
|
|
13
16
|
*/
|
|
14
|
-
function passflowExpress(
|
|
15
|
-
|
|
17
|
+
function passflowExpress(target) {
|
|
18
|
+
const { verifier, challenge, metadata } = require_adapter.adapterTarget(target);
|
|
16
19
|
const middleware = (reuse, requirement) => (req, res, next) => {
|
|
17
|
-
require_evaluate.guard(verifier, req.headers.authorization, reuse ? req.passflow : void 0, requirement).then((result) => {
|
|
20
|
+
require_evaluate.guard(verifier, req.headers.authorization, reuse ? req.passflow : void 0, requirement, challenge).then((result) => {
|
|
18
21
|
if ("reject" in result) {
|
|
19
22
|
send(res, result.reject);
|
|
20
23
|
return;
|
|
@@ -32,7 +35,15 @@ function passflowExpress(verifier) {
|
|
|
32
35
|
requireFresh: (maxAgeSeconds) => middleware(true, require_evaluate.assertRequirement({
|
|
33
36
|
kind: "fresh",
|
|
34
37
|
maxAgeSeconds
|
|
35
|
-
}))
|
|
38
|
+
})),
|
|
39
|
+
requireScopes: (...scopes) => middleware(true, require_evaluate.assertRequirement({
|
|
40
|
+
kind: "scope",
|
|
41
|
+
scopes
|
|
42
|
+
})),
|
|
43
|
+
metadata: (req, res) => {
|
|
44
|
+
const { status, headers, body } = metadata(req.method);
|
|
45
|
+
res.status(status).set(headers).end(body ?? void 0);
|
|
46
|
+
}
|
|
36
47
|
};
|
|
37
48
|
}
|
|
38
49
|
/** The claims a Passflow middleware put on this request. Throws `PassflowUsageError` if none ran. */
|
|
@@ -40,7 +51,10 @@ function getAuth(req) {
|
|
|
40
51
|
if (req.passflow === void 0 || req.passflow === null) throw new require_evaluate.PassflowUsageError("[passflow] getAuth called before a Passflow middleware ran on this request");
|
|
41
52
|
return req.passflow;
|
|
42
53
|
}
|
|
43
|
-
/**
|
|
54
|
+
/**
|
|
55
|
+
* Names `WWW-Authenticate` in the `cors` package's `exposedHeaders`, keeping what is already there. Takes the package's own options type
|
|
56
|
+
* as it is, with `exactOptionalPropertyTypes` on too.
|
|
57
|
+
*/
|
|
44
58
|
function exposeChallengeHeader(cors) {
|
|
45
59
|
return {
|
|
46
60
|
...cors,
|
package/dist/express.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { f as Verifier, n as ProtectedResource, u as PassflowClaims } from "./evaluate-BAJUQu_Z.cjs";
|
|
2
2
|
import { Request, RequestHandler } from "express";
|
|
3
3
|
declare global {
|
|
4
4
|
namespace Express {
|
|
@@ -11,19 +11,28 @@ declare global {
|
|
|
11
11
|
/**
|
|
12
12
|
* Express 4 and 5 middleware. `authenticate` always verifies the presented token and replaces any claims on the
|
|
13
13
|
* request. `requireACR` and `requireFresh` work alone: they reuse claims only when this verifier produced that
|
|
14
|
-
* exact object, and otherwise verify the presented token themselves.
|
|
15
|
-
* an invalid level or
|
|
14
|
+
* exact object, and otherwise verify the presented token themselves. `requireScopes`
|
|
15
|
+
* does the same for scopes. All three throw `TypeError` at construction on an invalid level, limit or scope. A
|
|
16
|
+
* protected resource adds its own parameters to the challenges. Mount `metadata` with `app.all`, because a browser
|
|
17
|
+
* client may send a preflight request.
|
|
16
18
|
*/
|
|
17
|
-
export declare function passflowExpress(
|
|
19
|
+
export declare function passflowExpress(target: Verifier | ProtectedResource): {
|
|
18
20
|
authenticate: RequestHandler;
|
|
19
21
|
requireACR(min: string): RequestHandler;
|
|
20
22
|
requireFresh(maxAgeSeconds: number): RequestHandler;
|
|
23
|
+
/** Answers 403 unless the token carries every scope: what the user delegated to the client, not what the user may do. */
|
|
24
|
+
requireScopes(...scopes: string[]): RequestHandler;
|
|
25
|
+
/** Serves the RFC 9728 metadata document. Throws `PassflowUsageError` when built from a plain verifier. */
|
|
26
|
+
metadata: RequestHandler;
|
|
21
27
|
};
|
|
22
28
|
/** The claims a Passflow middleware put on this request. Throws `PassflowUsageError` if none ran. */
|
|
23
29
|
export declare function getAuth(req: Pick<Request, 'passflow'>): PassflowClaims;
|
|
24
|
-
/**
|
|
30
|
+
/**
|
|
31
|
+
* Names `WWW-Authenticate` in the `cors` package's `exposedHeaders`, keeping what is already there. Takes the package's own options type
|
|
32
|
+
* as it is, with `exactOptionalPropertyTypes` on too.
|
|
33
|
+
*/
|
|
25
34
|
export declare function exposeChallengeHeader<T extends object>(cors: T & {
|
|
26
|
-
exposedHeaders?: string | string[];
|
|
35
|
+
exposedHeaders?: string | readonly string[] | undefined;
|
|
27
36
|
}): Omit<T, 'exposedHeaders'> & {
|
|
28
37
|
exposedHeaders: string[];
|
|
29
38
|
};
|
package/dist/express.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { f as Verifier, n as ProtectedResource, u as PassflowClaims } from "./evaluate-BAJUQu_Z.mjs";
|
|
2
2
|
import { Request, RequestHandler } from "express";
|
|
3
3
|
declare global {
|
|
4
4
|
namespace Express {
|
|
@@ -11,19 +11,28 @@ declare global {
|
|
|
11
11
|
/**
|
|
12
12
|
* Express 4 and 5 middleware. `authenticate` always verifies the presented token and replaces any claims on the
|
|
13
13
|
* request. `requireACR` and `requireFresh` work alone: they reuse claims only when this verifier produced that
|
|
14
|
-
* exact object, and otherwise verify the presented token themselves.
|
|
15
|
-
* an invalid level or
|
|
14
|
+
* exact object, and otherwise verify the presented token themselves. `requireScopes`
|
|
15
|
+
* does the same for scopes. All three throw `TypeError` at construction on an invalid level, limit or scope. A
|
|
16
|
+
* protected resource adds its own parameters to the challenges. Mount `metadata` with `app.all`, because a browser
|
|
17
|
+
* client may send a preflight request.
|
|
16
18
|
*/
|
|
17
|
-
export declare function passflowExpress(
|
|
19
|
+
export declare function passflowExpress(target: Verifier | ProtectedResource): {
|
|
18
20
|
authenticate: RequestHandler;
|
|
19
21
|
requireACR(min: string): RequestHandler;
|
|
20
22
|
requireFresh(maxAgeSeconds: number): RequestHandler;
|
|
23
|
+
/** Answers 403 unless the token carries every scope: what the user delegated to the client, not what the user may do. */
|
|
24
|
+
requireScopes(...scopes: string[]): RequestHandler;
|
|
25
|
+
/** Serves the RFC 9728 metadata document. Throws `PassflowUsageError` when built from a plain verifier. */
|
|
26
|
+
metadata: RequestHandler;
|
|
21
27
|
};
|
|
22
28
|
/** The claims a Passflow middleware put on this request. Throws `PassflowUsageError` if none ran. */
|
|
23
29
|
export declare function getAuth(req: Pick<Request, 'passflow'>): PassflowClaims;
|
|
24
|
-
/**
|
|
30
|
+
/**
|
|
31
|
+
* Names `WWW-Authenticate` in the `cors` package's `exposedHeaders`, keeping what is already there. Takes the package's own options type
|
|
32
|
+
* as it is, with `exactOptionalPropertyTypes` on too.
|
|
33
|
+
*/
|
|
25
34
|
export declare function exposeChallengeHeader<T extends object>(cors: T & {
|
|
26
|
-
exposedHeaders?: string | string[];
|
|
35
|
+
exposedHeaders?: string | readonly string[] | undefined;
|
|
27
36
|
}): Omit<T, 'exposedHeaders'> & {
|
|
28
37
|
exposedHeaders: string[];
|
|
29
38
|
};
|
package/dist/express.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as adapterTarget } from "./adapter-Du22h3Lk.mjs";
|
|
2
|
+
import { l as PassflowUsageError, p as withChallengeHeader, s as guard, t as assertRequirement } from "./evaluate-CHts4VKy.mjs";
|
|
2
3
|
// src/express.ts
|
|
3
4
|
/** Written directly and never through `next(err)`: a custom error handler would drop `WWW-Authenticate`. */
|
|
4
5
|
function send(res, rejection) {
|
|
@@ -7,13 +8,15 @@ function send(res, rejection) {
|
|
|
7
8
|
/**
|
|
8
9
|
* Express 4 and 5 middleware. `authenticate` always verifies the presented token and replaces any claims on the
|
|
9
10
|
* request. `requireACR` and `requireFresh` work alone: they reuse claims only when this verifier produced that
|
|
10
|
-
* exact object, and otherwise verify the presented token themselves.
|
|
11
|
-
* an invalid level or
|
|
11
|
+
* exact object, and otherwise verify the presented token themselves. `requireScopes`
|
|
12
|
+
* does the same for scopes. All three throw `TypeError` at construction on an invalid level, limit or scope. A
|
|
13
|
+
* protected resource adds its own parameters to the challenges. Mount `metadata` with `app.all`, because a browser
|
|
14
|
+
* client may send a preflight request.
|
|
12
15
|
*/
|
|
13
|
-
function passflowExpress(
|
|
14
|
-
|
|
16
|
+
function passflowExpress(target) {
|
|
17
|
+
const { verifier, challenge, metadata } = adapterTarget(target);
|
|
15
18
|
const middleware = (reuse, requirement) => (req, res, next) => {
|
|
16
|
-
guard(verifier, req.headers.authorization, reuse ? req.passflow : void 0, requirement).then((result) => {
|
|
19
|
+
guard(verifier, req.headers.authorization, reuse ? req.passflow : void 0, requirement, challenge).then((result) => {
|
|
17
20
|
if ("reject" in result) {
|
|
18
21
|
send(res, result.reject);
|
|
19
22
|
return;
|
|
@@ -31,7 +34,15 @@ function passflowExpress(verifier) {
|
|
|
31
34
|
requireFresh: (maxAgeSeconds) => middleware(true, assertRequirement({
|
|
32
35
|
kind: "fresh",
|
|
33
36
|
maxAgeSeconds
|
|
34
|
-
}))
|
|
37
|
+
})),
|
|
38
|
+
requireScopes: (...scopes) => middleware(true, assertRequirement({
|
|
39
|
+
kind: "scope",
|
|
40
|
+
scopes
|
|
41
|
+
})),
|
|
42
|
+
metadata: (req, res) => {
|
|
43
|
+
const { status, headers, body } = metadata(req.method);
|
|
44
|
+
res.status(status).set(headers).end(body ?? void 0);
|
|
45
|
+
}
|
|
35
46
|
};
|
|
36
47
|
}
|
|
37
48
|
/** The claims a Passflow middleware put on this request. Throws `PassflowUsageError` if none ran. */
|
|
@@ -39,7 +50,10 @@ function getAuth(req) {
|
|
|
39
50
|
if (req.passflow === void 0 || req.passflow === null) throw new PassflowUsageError("[passflow] getAuth called before a Passflow middleware ran on this request");
|
|
40
51
|
return req.passflow;
|
|
41
52
|
}
|
|
42
|
-
/**
|
|
53
|
+
/**
|
|
54
|
+
* Names `WWW-Authenticate` in the `cors` package's `exposedHeaders`, keeping what is already there. Takes the package's own options type
|
|
55
|
+
* as it is, with `exactOptionalPropertyTypes` on too.
|
|
56
|
+
*/
|
|
43
57
|
function exposeChallengeHeader(cors) {
|
|
44
58
|
return {
|
|
45
59
|
...cors,
|
package/dist/fastify.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const
|
|
2
|
+
const require_adapter = require("./adapter-DyCsMsEq.cjs");
|
|
3
|
+
const require_evaluate = require("./evaluate-Yv4mWPRI.cjs");
|
|
3
4
|
// src/fastify.ts
|
|
4
5
|
function send(reply, rejection) {
|
|
5
6
|
reply.code(rejection.status).headers(rejection.headers).send();
|
|
@@ -9,14 +10,16 @@ function send(reply, rejection) {
|
|
|
9
10
|
* verifier on an instance. Each is a `preHandler`-shaped async function. `authenticate` always verifies the presented
|
|
10
11
|
* token and replaces `request.passflow`. `requireACR` and `requireFresh` reuse claims only when this verifier produced
|
|
11
12
|
* that exact object, and otherwise verify the presented token themselves, so a broad instance-level hook followed by
|
|
12
|
-
* a narrow route-level one enforces the narrow verifier's policy.
|
|
13
|
-
* level or
|
|
13
|
+
* a narrow route-level one enforces the narrow verifier's policy. `requireScopes` does the same for scopes.
|
|
14
|
+
* All three throw `TypeError` at construction on an invalid level, limit or scope. A protected resource adds its own
|
|
15
|
+
* parameters to the challenges. Mount `metadata` on GET, HEAD and OPTIONS, because a browser client may send a preflight
|
|
16
|
+
* request.
|
|
14
17
|
*/
|
|
15
|
-
function createFastifyHooks(
|
|
16
|
-
|
|
18
|
+
function createFastifyHooks(target) {
|
|
19
|
+
const { verifier, challenge, metadata } = require_adapter.adapterTarget(target);
|
|
17
20
|
const hook = (reuse, requirement) => async (request, reply) => {
|
|
18
21
|
const existing = reuse ? request.passflow ?? void 0 : void 0;
|
|
19
|
-
const result = await require_evaluate.guard(verifier, request.headers.authorization, existing, requirement);
|
|
22
|
+
const result = await require_evaluate.guard(verifier, request.headers.authorization, existing, requirement, challenge);
|
|
20
23
|
if ("reject" in result) {
|
|
21
24
|
send(reply, result.reject);
|
|
22
25
|
return reply;
|
|
@@ -32,7 +35,15 @@ function createFastifyHooks(verifier) {
|
|
|
32
35
|
requireFresh: (maxAgeSeconds) => hook(true, require_evaluate.assertRequirement({
|
|
33
36
|
kind: "fresh",
|
|
34
37
|
maxAgeSeconds
|
|
35
|
-
}))
|
|
38
|
+
})),
|
|
39
|
+
requireScopes: (...scopes) => hook(true, require_evaluate.assertRequirement({
|
|
40
|
+
kind: "scope",
|
|
41
|
+
scopes
|
|
42
|
+
})),
|
|
43
|
+
metadata: (request, reply) => {
|
|
44
|
+
const { status, headers, body } = metadata(request.method);
|
|
45
|
+
return reply.code(status).headers(headers).send(body ?? void 0);
|
|
46
|
+
}
|
|
36
47
|
};
|
|
37
48
|
}
|
|
38
49
|
const plugin = async (fastify, opts) => {
|
|
@@ -54,7 +65,10 @@ function getAuth(request) {
|
|
|
54
65
|
if (request.passflow === null || request.passflow === void 0) throw new require_evaluate.PassflowUsageError("[passflow] getAuth called before a Passflow hook ran on this request");
|
|
55
66
|
return request.passflow;
|
|
56
67
|
}
|
|
57
|
-
/**
|
|
68
|
+
/**
|
|
69
|
+
* Names `WWW-Authenticate` in `@fastify/cors`'s `exposedHeaders`, keeping what is already there. Takes the package's own options type
|
|
70
|
+
* as it is, with `exactOptionalPropertyTypes` on too.
|
|
71
|
+
*/
|
|
58
72
|
function exposeChallengeHeader(cors) {
|
|
59
73
|
return {
|
|
60
74
|
...cors,
|