@softistx/alxia-react-router 0.87.0 → 0.89.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 +72 -3
- package/dist/chunks/{index-t2ph23wk.js → index-5hjhzztk.js} +6 -17
- package/dist/chunks/{index-t2ph23wk.js.map → index-5hjhzztk.js.map} +3 -4
- package/dist/chunks/{index-v2z5k640.js → index-6zx9dfvs.js} +3 -18
- package/dist/chunks/{index-v2z5k640.js.map → index-6zx9dfvs.js.map} +3 -4
- package/dist/chunks/index-njfh26ez.js +19 -0
- package/dist/chunks/index-njfh26ez.js.map +10 -0
- package/dist/chunks/index-t68hwhz1.js +37 -0
- package/dist/chunks/{index-0786n4qe.js.map → index-t68hwhz1.js.map} +5 -4
- package/dist/errors/index.js +5 -3
- package/dist/errors/index.js.map +2 -2
- package/dist/oidc/callback.d.ts +4 -0
- package/dist/oidc/callback.d.ts.map +1 -0
- package/dist/oidc/client.d.ts +57 -0
- package/dist/oidc/client.d.ts.map +1 -0
- package/dist/oidc/context.d.ts +42 -0
- package/dist/oidc/context.d.ts.map +1 -0
- package/dist/oidc/dpop-fetch.d.ts +40 -0
- package/dist/oidc/dpop-fetch.d.ts.map +1 -0
- package/dist/oidc/env.d.ts +52 -0
- package/dist/oidc/env.d.ts.map +1 -0
- package/dist/oidc/index.d.ts +5 -1
- package/dist/oidc/index.d.ts.map +1 -1
- package/dist/oidc/index.js +614 -326
- package/dist/oidc/index.js.map +19 -7
- package/dist/oidc/logout.d.ts +4 -0
- package/dist/oidc/logout.d.ts.map +1 -0
- package/dist/oidc/refresh.d.ts +10 -0
- package/dist/oidc/refresh.d.ts.map +1 -0
- package/dist/oidc/routes.d.ts +76 -0
- package/dist/oidc/routes.d.ts.map +1 -0
- package/dist/oidc/session.d.ts +7 -0
- package/dist/oidc/session.d.ts.map +1 -0
- package/dist/oidc/signin.d.ts +4 -0
- package/dist/oidc/signin.d.ts.map +1 -0
- package/dist/oidc/stored.d.ts +21 -0
- package/dist/oidc/stored.d.ts.map +1 -0
- package/dist/oidc/tokens.d.ts +13 -0
- package/dist/oidc/tokens.d.ts.map +1 -0
- package/dist/oidc/{oidc.d.ts → types.d.ts} +8 -45
- package/dist/oidc/{oidc.d.ts.map → types.d.ts.map} +1 -1
- package/dist/oidc/user.d.ts +6 -0
- package/dist/oidc/user.d.ts.map +1 -0
- package/dist/pwa/react/index.d.ts +2 -0
- package/dist/pwa/react/index.d.ts.map +1 -1
- package/dist/pwa/react/index.js +264 -3
- package/dist/pwa/react/index.js.map +6 -3
- package/dist/pwa/react/push-status.d.ts +31 -0
- package/dist/pwa/react/push-status.d.ts.map +1 -0
- package/dist/pwa/react/push-store.d.ts +55 -0
- package/dist/pwa/react/push-store.d.ts.map +1 -0
- package/dist/pwa/react/use-push-subscription.d.ts +15 -0
- package/dist/pwa/react/use-push-subscription.d.ts.map +1 -0
- package/dist/pwa/worker/index.d.ts +2 -0
- package/dist/pwa/worker/index.d.ts.map +1 -1
- package/dist/pwa/worker/index.js +94 -4
- package/dist/pwa/worker/index.js.map +7 -6
- package/dist/pwa/worker/plan.d.ts +7 -0
- package/dist/pwa/worker/plan.d.ts.map +1 -1
- package/dist/pwa/worker/push.d.ts +48 -0
- package/dist/pwa/worker/push.d.ts.map +1 -0
- package/dist/pwa/worker/scope.d.ts +23 -0
- package/dist/pwa/worker/scope.d.ts.map +1 -1
- package/dist/pwa/worker/start-worker.d.ts +2 -0
- package/dist/pwa/worker/start-worker.d.ts.map +1 -1
- package/dist/session/index.js +9 -7
- package/dist/session/index.js.map +2 -2
- package/dist/session/model/index.js +2 -2
- package/docs/README.md +1 -1
- package/docs/guide/fetcher.md +13 -0
- package/docs/guide/oidc.md +172 -6
- package/docs/guide/pwa.md +101 -1
- package/docs/roadmap.md +8 -0
- package/docs/troubleshooting.md +127 -0
- package/package.json +7 -7
- package/dist/chunks/index-0786n4qe.js +0 -25
package/dist/oidc/index.js
CHANGED
|
@@ -1,9 +1,33 @@
|
|
|
1
|
+
import {
|
|
2
|
+
siteCheck
|
|
3
|
+
} from "../chunks/index-6zx9dfvs.js";
|
|
1
4
|
import {
|
|
2
5
|
sessionCookies2
|
|
3
6
|
} from "../chunks/index-1nw5yeek.js";
|
|
4
7
|
import {
|
|
5
|
-
safeNext2
|
|
6
|
-
|
|
8
|
+
safeNext2,
|
|
9
|
+
sitePath
|
|
10
|
+
} from "../chunks/index-t68hwhz1.js";
|
|
11
|
+
|
|
12
|
+
// src/oidc/errors.ts
|
|
13
|
+
class OidcError extends Error {
|
|
14
|
+
code;
|
|
15
|
+
headers;
|
|
16
|
+
name = "OidcError";
|
|
17
|
+
constructor(code, message, headers = new Headers, options) {
|
|
18
|
+
super(message, options);
|
|
19
|
+
this.code = code;
|
|
20
|
+
this.headers = headers;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// src/oidc/flow.ts
|
|
25
|
+
import {
|
|
26
|
+
createLocalJWKSet,
|
|
27
|
+
decodeJwt,
|
|
28
|
+
errors as joseErrors,
|
|
29
|
+
jwtVerify
|
|
30
|
+
} from "jose";
|
|
7
31
|
|
|
8
32
|
// src/oidc/dpop.ts
|
|
9
33
|
import {
|
|
@@ -161,27 +185,8 @@ function dpopBinding(config) {
|
|
|
161
185
|
}
|
|
162
186
|
};
|
|
163
187
|
}
|
|
164
|
-
// src/oidc/errors.ts
|
|
165
|
-
class OidcError extends Error {
|
|
166
|
-
code;
|
|
167
|
-
headers;
|
|
168
|
-
name = "OidcError";
|
|
169
|
-
constructor(code, message, headers = new Headers, options) {
|
|
170
|
-
super(message, options);
|
|
171
|
-
this.code = code;
|
|
172
|
-
this.headers = headers;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
// src/oidc/oidc.ts
|
|
176
|
-
import { redirect } from "react-router";
|
|
177
188
|
|
|
178
189
|
// src/oidc/flow.ts
|
|
179
|
-
import {
|
|
180
|
-
createLocalJWKSet,
|
|
181
|
-
decodeJwt,
|
|
182
|
-
errors as joseErrors,
|
|
183
|
-
jwtVerify
|
|
184
|
-
} from "jose";
|
|
185
190
|
var DISCOVERY_TTL = 60 * 60 * 1000;
|
|
186
191
|
var JWKS_TTL = 10 * 60 * 1000;
|
|
187
192
|
var isRecord = (value) => typeof value === "object" && value !== null;
|
|
@@ -364,44 +369,154 @@ async function pushRequest(fetcher, endpoint, client, params, dpop) {
|
|
|
364
369
|
return answer["request_uri"];
|
|
365
370
|
}
|
|
366
371
|
|
|
367
|
-
// src/oidc/
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
372
|
+
// src/oidc/stored.ts
|
|
373
|
+
var isKey = (value) => typeof value === "object" && value !== null && ["ES256", "EdDSA"].includes(value["alg"]) && typeof value["jwk"] === "object";
|
|
374
|
+
var isFlow = (value) => typeof value === "object" && value !== null && ["state", "nonce", "verifier", "next"].every((key) => typeof value[key] === "string") && typeof value["exp"] === "number" && (value["dpop"] === undefined || isKey(value["dpop"]));
|
|
375
|
+
var isStored = (value) => typeof value === "object" && value !== null && typeof value["idToken"] === "string" && typeof value["accessToken"] === "string" && typeof value["expiresAt"] === "number" && (value["dpop"] === undefined || isKey(value["dpop"]));
|
|
376
|
+
|
|
377
|
+
// src/oidc/tokens.ts
|
|
378
|
+
async function toStored(ctx, response, nonce, previous, key) {
|
|
379
|
+
const { now } = ctx;
|
|
380
|
+
const dpop = key && (String(response.token_type).toLowerCase() === "dpop" || previous?.dpop !== undefined && response.token_type === undefined) ? key : undefined;
|
|
381
|
+
if (!response.id_token && previous) {
|
|
382
|
+
const kept = decodeJwt(previous.idToken);
|
|
383
|
+
return {
|
|
384
|
+
claims: kept,
|
|
385
|
+
stored: {
|
|
386
|
+
idToken: previous.idToken,
|
|
387
|
+
dpop,
|
|
388
|
+
accessToken: response.access_token,
|
|
389
|
+
refreshToken: response.refresh_token ?? previous.refreshToken,
|
|
390
|
+
expiresAt: typeof response.expires_in === "number" ? now() + response.expires_in * 1000 : now() + 300000
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
const idToken = response.id_token;
|
|
395
|
+
if (!idToken) {
|
|
396
|
+
throw new OidcError("invalid-id-token", "The token endpoint sent no id token.");
|
|
397
|
+
}
|
|
398
|
+
const claims = await ctx.issuerApi.verifyIdToken(idToken, ctx.clientId, nonce);
|
|
399
|
+
if (previous && decodeJwt(previous.idToken).sub !== claims.sub) {
|
|
400
|
+
throw new OidcError("invalid-id-token", "A refresh changed the user.");
|
|
401
|
+
}
|
|
402
|
+
const expiresAt = typeof response.expires_in === "number" ? now() + response.expires_in * 1000 : (claims["exp"] ?? 0) * 1000;
|
|
403
|
+
return {
|
|
404
|
+
claims,
|
|
405
|
+
stored: {
|
|
406
|
+
idToken,
|
|
407
|
+
dpop,
|
|
408
|
+
accessToken: response.access_token,
|
|
409
|
+
refreshToken: response.refresh_token ?? previous?.refreshToken,
|
|
410
|
+
expiresAt
|
|
393
411
|
}
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
async function commit(ctx, headers, stored) {
|
|
415
|
+
const { now } = ctx;
|
|
416
|
+
const lifetime = stored.refreshToken ? ctx.sessionSeconds : Math.max(0, (stored.expiresAt - now()) / 1000);
|
|
417
|
+
const cookie = ctx.cookies.commit(await ctx.tokenSeal.seal(stored), new Date(now() + lifetime * 1000), now());
|
|
418
|
+
if (cookie.length > 4096) {
|
|
419
|
+
throw new OidcError("token-cookie-too-large", `The token cookie is ${cookie.length} bytes, over the 4096 a browser keeps. Ask the issuer for fewer claims or a shorter access token.`);
|
|
394
420
|
}
|
|
421
|
+
headers.append("Set-Cookie", cookie);
|
|
395
422
|
}
|
|
396
423
|
|
|
397
|
-
// src/oidc/
|
|
424
|
+
// src/oidc/callback.ts
|
|
425
|
+
async function checkedFlow(ctx, request, fail) {
|
|
426
|
+
const url = new URL(request.url);
|
|
427
|
+
const sealed = ctx.cookies.challengeOf(request);
|
|
428
|
+
const flow = sealed ? await ctx.flowSeal.open(sealed) : null;
|
|
429
|
+
if (!isFlow(flow)) {
|
|
430
|
+
throw fail("missing-challenge", "The sign-in challenge cookie is missing or unreadable (a second tab, an expired page or a blocked cookie).");
|
|
431
|
+
}
|
|
432
|
+
if (flow.exp <= ctx.now()) {
|
|
433
|
+
throw fail("challenge-expired", "The sign-in took too long; start again.");
|
|
434
|
+
}
|
|
435
|
+
const returned = url.searchParams.get("state");
|
|
436
|
+
if (returned === null || !sameText(returned, flow.state)) {
|
|
437
|
+
throw fail("state-mismatch", "The returned state is not the one this sign-in sent.");
|
|
438
|
+
}
|
|
439
|
+
const failure = url.searchParams.get("error");
|
|
440
|
+
if (failure) {
|
|
441
|
+
const description = url.searchParams.get("error_description");
|
|
442
|
+
throw fail("provider-error", `The issuer refused: ${failure}${description ? ` (${description})` : ""}.`);
|
|
443
|
+
}
|
|
444
|
+
const claimedIssuer = url.searchParams.get("iss");
|
|
445
|
+
if (claimedIssuer !== null && claimedIssuer !== ctx.issuer) {
|
|
446
|
+
throw fail("issuer-mismatch", "The callback names another issuer.");
|
|
447
|
+
}
|
|
448
|
+
const code = url.searchParams.get("code");
|
|
449
|
+
if (!code)
|
|
450
|
+
throw fail("missing-code", "The callback carries no code.");
|
|
451
|
+
return { flow, code };
|
|
452
|
+
}
|
|
453
|
+
async function callback(ctx, request) {
|
|
454
|
+
const cleared = () => {
|
|
455
|
+
const headers = new Headers;
|
|
456
|
+
headers.append("Set-Cookie", ctx.cookies.destroyChallenge());
|
|
457
|
+
return headers;
|
|
458
|
+
};
|
|
459
|
+
const fail = (code, message) => new OidcError(code, message, cleared());
|
|
460
|
+
const { flow, code } = await checkedFlow(ctx, request, fail);
|
|
461
|
+
try {
|
|
462
|
+
const document = await ctx.issuerApi.discover();
|
|
463
|
+
const answer = await tokenRequest(ctx.fetcher, document.token_endpoint, ctx.client, {
|
|
464
|
+
...ctx.extraTokenParams,
|
|
465
|
+
grant_type: "authorization_code",
|
|
466
|
+
code,
|
|
467
|
+
redirect_uri: ctx.redirectUri,
|
|
468
|
+
code_verifier: flow.verifier
|
|
469
|
+
}, "token-exchange-failed", ctx.signing(flow.dpop));
|
|
470
|
+
const { stored } = await toStored(ctx, answer, flow.nonce, undefined, flow.dpop);
|
|
471
|
+
const headers = cleared();
|
|
472
|
+
await commit(ctx, headers, stored);
|
|
473
|
+
headers.set("Location", safeNext2(flow.next));
|
|
474
|
+
headers.set("Cache-Control", "no-store");
|
|
475
|
+
return new Response(null, { status: 302, headers });
|
|
476
|
+
} catch (error) {
|
|
477
|
+
if (error instanceof OidcError) {
|
|
478
|
+
for (const value of cleared().getSetCookie()) {
|
|
479
|
+
error.headers.append("Set-Cookie", value);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
throw error;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
// src/oidc/context.ts
|
|
398
487
|
var DEFAULT_ALGORITHMS = ["RS256", "PS256", "ES256", "EdDSA"];
|
|
399
488
|
var CHALLENGE_SECONDS = 600;
|
|
400
|
-
var
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
489
|
+
var MAX_TIMER_MS = 2147483647;
|
|
490
|
+
function checkRequired(options) {
|
|
491
|
+
for (const [label, value] of [
|
|
492
|
+
["issuer", options.issuer],
|
|
493
|
+
["clientId", options.clientId],
|
|
494
|
+
["redirectUri", options.redirectUri]
|
|
495
|
+
]) {
|
|
496
|
+
if (!value)
|
|
497
|
+
throw new TypeError(`oidcClient(): ${label} is required.`);
|
|
498
|
+
}
|
|
499
|
+
const { secret } = options;
|
|
500
|
+
if (typeof secret !== "string" || secret.length < 32) {
|
|
501
|
+
throw new TypeError("oidcClient(): secret must be a string of at least 32 characters.");
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
function checkTuning(options, par, dpopAlg, refreshFlightMs) {
|
|
505
|
+
if (!["auto", "always", "never"].includes(par)) {
|
|
506
|
+
throw new TypeError("oidcClient(): par must be 'auto', 'always' or 'never'.");
|
|
507
|
+
}
|
|
508
|
+
if (dpopAlg !== undefined && !["ES256", "EdDSA"].includes(dpopAlg)) {
|
|
509
|
+
throw new TypeError("oidcClient(): dpop.alg must be 'ES256' or 'EdDSA'.");
|
|
510
|
+
}
|
|
511
|
+
if (options.revokeTimeoutMs !== undefined && !(Number.isFinite(options.revokeTimeoutMs) && options.revokeTimeoutMs > 0)) {
|
|
512
|
+
throw new TypeError("oidcClient(): revokeTimeoutMs must be a positive finite number of milliseconds.");
|
|
513
|
+
}
|
|
514
|
+
if (!(Number.isFinite(refreshFlightMs) && refreshFlightMs >= 0 && refreshFlightMs <= MAX_TIMER_MS)) {
|
|
515
|
+
throw new TypeError("oidcClient(): refreshFlightMs must be a finite number of milliseconds, from 0 to 2147483647.");
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
function createContext(options) {
|
|
519
|
+
checkRequired(options);
|
|
405
520
|
const {
|
|
406
521
|
issuer,
|
|
407
522
|
clientId,
|
|
@@ -411,19 +526,9 @@ function oidcClient(options) {
|
|
|
411
526
|
signInPath = "/sign-in",
|
|
412
527
|
sessionSeconds = 30 * 24 * 3600,
|
|
413
528
|
refreshSkewSeconds = 60,
|
|
529
|
+
refreshFlightMs = 1e4,
|
|
414
530
|
clockToleranceSeconds = 30
|
|
415
531
|
} = options;
|
|
416
|
-
for (const [label, value] of [
|
|
417
|
-
["issuer", issuer],
|
|
418
|
-
["clientId", clientId],
|
|
419
|
-
["redirectUri", redirectUri]
|
|
420
|
-
]) {
|
|
421
|
-
if (!value)
|
|
422
|
-
throw new TypeError(`oidcClient(): ${label} is required.`);
|
|
423
|
-
}
|
|
424
|
-
if (typeof secret !== "string" || secret.length < 32) {
|
|
425
|
-
throw new TypeError("oidcClient(): secret must be a string of at least 32 characters.");
|
|
426
|
-
}
|
|
427
532
|
const {
|
|
428
533
|
client_id: _id,
|
|
429
534
|
client_secret: _secret,
|
|
@@ -444,299 +549,482 @@ function oidcClient(options) {
|
|
|
444
549
|
});
|
|
445
550
|
const flowSeal = sealer(secret, "flow");
|
|
446
551
|
const tokenSeal = sealer(secret, "tokens");
|
|
552
|
+
const fetcher = options.fetch ?? ((input, init) => fetch(input, init));
|
|
447
553
|
const issuerApi = issuerClient({
|
|
448
554
|
issuer,
|
|
449
|
-
fetch:
|
|
555
|
+
fetch: fetcher,
|
|
450
556
|
now,
|
|
451
557
|
algorithms: [...options.algorithms ?? DEFAULT_ALGORITHMS],
|
|
452
558
|
clockToleranceSeconds
|
|
453
559
|
});
|
|
454
|
-
const fetcher = options.fetch ?? ((input, init) => fetch(input, init));
|
|
455
|
-
const client = { id: clientId, secret: clientSecret };
|
|
456
560
|
const par = options.par ?? "auto";
|
|
457
|
-
if (!["auto", "always", "never"].includes(par)) {
|
|
458
|
-
throw new TypeError("oidcClient(): par must be 'auto', 'always' or 'never'.");
|
|
459
|
-
}
|
|
460
561
|
const dpopAlg = options.dpop ? options.dpop.alg ?? "ES256" : undefined;
|
|
461
|
-
|
|
462
|
-
|
|
562
|
+
checkTuning(options, par, dpopAlg, refreshFlightMs);
|
|
563
|
+
const nonces = nonceMemory();
|
|
564
|
+
return {
|
|
565
|
+
options,
|
|
566
|
+
issuer,
|
|
567
|
+
clientId,
|
|
568
|
+
redirectUri,
|
|
569
|
+
signInPath,
|
|
570
|
+
sessionSeconds,
|
|
571
|
+
refreshSkewSeconds,
|
|
572
|
+
refreshFlightMs,
|
|
573
|
+
scopes,
|
|
574
|
+
extraTokenParams,
|
|
575
|
+
now,
|
|
576
|
+
cookies,
|
|
577
|
+
flowSeal,
|
|
578
|
+
tokenSeal,
|
|
579
|
+
issuerApi,
|
|
580
|
+
fetcher,
|
|
581
|
+
client: { id: clientId, secret: clientSecret },
|
|
582
|
+
par,
|
|
583
|
+
dpopAlg,
|
|
584
|
+
nonces,
|
|
585
|
+
signing: (key) => key ? { key, nonces, now } : undefined,
|
|
586
|
+
flights: new Map,
|
|
587
|
+
stores: new WeakMap
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
// src/oidc/revoke.ts
|
|
592
|
+
async function revokeTokens(input) {
|
|
593
|
+
const { stored } = input;
|
|
594
|
+
const signal = AbortSignal.timeout(input.timeoutMs);
|
|
595
|
+
const timedOut = new Promise((_, reject) => {
|
|
596
|
+
signal.addEventListener("abort", () => reject(new OidcError("revoke-failed", `The revocation endpoint did not answer within ${input.timeoutMs} ms.`)), { once: true });
|
|
597
|
+
});
|
|
598
|
+
timedOut.catch(() => {
|
|
599
|
+
return;
|
|
600
|
+
});
|
|
601
|
+
const tokens = [
|
|
602
|
+
...stored.refreshToken ? [[stored.refreshToken, "refresh_token"]] : [],
|
|
603
|
+
[stored.accessToken, "access_token"]
|
|
604
|
+
];
|
|
605
|
+
for (const [token, hint] of tokens) {
|
|
606
|
+
try {
|
|
607
|
+
await Promise.race([
|
|
608
|
+
revokeRequest(input.fetcher, input.endpoint, input.client, { token, token_type_hint: hint }, input.signing(stored.dpop), signal),
|
|
609
|
+
timedOut
|
|
610
|
+
]);
|
|
611
|
+
} catch (error) {
|
|
612
|
+
input.report(error instanceof OidcError ? error : new OidcError("revoke-failed", "Revocation failed.", undefined, {
|
|
613
|
+
cause: error
|
|
614
|
+
}));
|
|
615
|
+
if (signal.aborted)
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
463
618
|
}
|
|
464
|
-
|
|
465
|
-
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
// src/oidc/logout.ts
|
|
622
|
+
async function logout(ctx, request) {
|
|
623
|
+
const { options } = ctx;
|
|
624
|
+
const report = (error) => {
|
|
625
|
+
try {
|
|
626
|
+
options.onRevokeError?.(error);
|
|
627
|
+
} catch {}
|
|
628
|
+
};
|
|
629
|
+
const headers = new Headers({ "Cache-Control": "no-store" });
|
|
630
|
+
headers.append("Set-Cookie", ctx.cookies.destroy());
|
|
631
|
+
const sealed = ctx.cookies.token(request);
|
|
632
|
+
const opened = sealed ? await ctx.tokenSeal.open(sealed) : null;
|
|
633
|
+
const document = await ctx.issuerApi.discover().catch((cause) => {
|
|
634
|
+
if (options.revokeOnLogout) {
|
|
635
|
+
report(new OidcError("discovery-failed", "Discovery failed at logout, so the tokens were not revoked.", undefined, { cause }));
|
|
636
|
+
}
|
|
637
|
+
return null;
|
|
638
|
+
});
|
|
639
|
+
if (options.revokeOnLogout && document?.revocation_endpoint && isStored(opened)) {
|
|
640
|
+
await revokeTokens({
|
|
641
|
+
fetcher: ctx.fetcher,
|
|
642
|
+
client: ctx.client,
|
|
643
|
+
endpoint: document.revocation_endpoint,
|
|
644
|
+
stored: opened,
|
|
645
|
+
signing: ctx.signing,
|
|
646
|
+
timeoutMs: options.revokeTimeoutMs ?? 3000,
|
|
647
|
+
report
|
|
648
|
+
});
|
|
466
649
|
}
|
|
467
|
-
const
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
650
|
+
const endpoint = document?.end_session_endpoint;
|
|
651
|
+
if (!endpoint) {
|
|
652
|
+
headers.set("Location", options.postLogoutRedirectUri ?? "/");
|
|
653
|
+
return new Response(null, { status: 302, headers });
|
|
654
|
+
}
|
|
655
|
+
const target = new URL(endpoint);
|
|
656
|
+
if (isStored(opened))
|
|
657
|
+
target.searchParams.set("id_token_hint", opened.idToken);
|
|
658
|
+
target.searchParams.set("client_id", ctx.clientId);
|
|
659
|
+
if (options.postLogoutRedirectUri) {
|
|
660
|
+
target.searchParams.set("post_logout_redirect_uri", options.postLogoutRedirectUri);
|
|
661
|
+
}
|
|
662
|
+
headers.set("Location", target.toString());
|
|
663
|
+
return new Response(null, { status: 302, headers });
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
// src/oidc/user.ts
|
|
667
|
+
async function toUser(ctx, stored, headers) {
|
|
668
|
+
const user = {
|
|
669
|
+
claims: decodeJwt(stored.idToken),
|
|
670
|
+
accessToken: stored.accessToken,
|
|
671
|
+
idToken: stored.idToken,
|
|
672
|
+
expiresAt: stored.expiresAt,
|
|
673
|
+
headers,
|
|
674
|
+
dpop: stored.dpop ? dpopBinding({
|
|
675
|
+
key: stored.dpop,
|
|
676
|
+
thumbprint: await dpopThumbprint(stored.dpop),
|
|
677
|
+
accessToken: stored.accessToken,
|
|
678
|
+
nonces: ctx.nonces,
|
|
679
|
+
fetch: ctx.fetcher,
|
|
680
|
+
now: ctx.now
|
|
681
|
+
}) : null,
|
|
682
|
+
fetch: ctx.fetcher
|
|
683
|
+
};
|
|
684
|
+
ctx.stores.set(user, stored);
|
|
685
|
+
return user;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
// src/oidc/refresh.ts
|
|
689
|
+
function refreshing(ctx, stored, key) {
|
|
690
|
+
const { flights } = ctx;
|
|
691
|
+
const flying = flights.get(key);
|
|
692
|
+
if (flying)
|
|
693
|
+
return flying;
|
|
694
|
+
const flight = (async () => {
|
|
695
|
+
const document = await ctx.issuerApi.discover();
|
|
696
|
+
const answer = await tokenRequest(ctx.fetcher, document.token_endpoint, ctx.client, {
|
|
697
|
+
...ctx.extraTokenParams,
|
|
698
|
+
grant_type: "refresh_token",
|
|
699
|
+
refresh_token: key,
|
|
700
|
+
scope: ctx.scopes.join(" ")
|
|
701
|
+
}, "refresh-failed", ctx.signing(stored.dpop));
|
|
702
|
+
return (await toStored(ctx, answer, undefined, stored, stored.dpop)).stored;
|
|
703
|
+
})();
|
|
704
|
+
flights.set(key, flight);
|
|
705
|
+
const forget = () => {
|
|
706
|
+
const timer = setTimeout(() => flights.delete(key), ctx.refreshFlightMs);
|
|
707
|
+
if (typeof timer === "object")
|
|
708
|
+
timer.unref();
|
|
709
|
+
};
|
|
710
|
+
flight.then(forget, () => flights.delete(key));
|
|
711
|
+
return flight;
|
|
712
|
+
}
|
|
713
|
+
async function refresh(ctx, user, refreshOptions = {}) {
|
|
714
|
+
const stored = ctx.stores.get(user);
|
|
715
|
+
if (!stored) {
|
|
716
|
+
throw new TypeError("oidc.refresh(): the user was not returned by this client.");
|
|
717
|
+
}
|
|
718
|
+
if (!refreshOptions.force && stored.expiresAt - ctx.refreshSkewSeconds * 1000 > ctx.now()) {
|
|
719
|
+
return user;
|
|
720
|
+
}
|
|
721
|
+
const headers = new Headers;
|
|
722
|
+
try {
|
|
723
|
+
if (!stored.refreshToken)
|
|
724
|
+
throw new Error("no refresh token");
|
|
725
|
+
const next = await refreshing(ctx, stored, stored.refreshToken);
|
|
726
|
+
await commit(ctx, headers, next);
|
|
727
|
+
return toUser(ctx, next, headers);
|
|
728
|
+
} catch (cause) {
|
|
729
|
+
if (stored.expiresAt > ctx.now())
|
|
730
|
+
return user;
|
|
731
|
+
throw cause instanceof OidcError ? cause : new OidcError("refresh-failed", "The refresh failed.", undefined, {
|
|
732
|
+
cause
|
|
733
|
+
});
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
// src/oidc/session.ts
|
|
738
|
+
import { redirect } from "react-router";
|
|
739
|
+
async function oidcUser(ctx, request) {
|
|
740
|
+
const headers = new Headers;
|
|
741
|
+
const sealed = ctx.cookies.token(request);
|
|
742
|
+
if (!sealed)
|
|
743
|
+
return null;
|
|
744
|
+
const opened = await ctx.tokenSeal.open(sealed);
|
|
745
|
+
if (!isStored(opened)) {
|
|
746
|
+
headers.append("Set-Cookie", ctx.cookies.destroy());
|
|
747
|
+
return null;
|
|
748
|
+
}
|
|
749
|
+
let stored = opened;
|
|
750
|
+
if (stored.expiresAt - ctx.refreshSkewSeconds * 1000 <= ctx.now()) {
|
|
751
|
+
try {
|
|
752
|
+
if (!stored.refreshToken)
|
|
753
|
+
throw new Error("no refresh token");
|
|
754
|
+
stored = await refreshing(ctx, stored, stored.refreshToken);
|
|
755
|
+
await commit(ctx, headers, stored);
|
|
756
|
+
} catch {
|
|
757
|
+
if (stored.expiresAt <= ctx.now())
|
|
758
|
+
return null;
|
|
485
759
|
}
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
760
|
+
}
|
|
761
|
+
return toUser(ctx, stored, headers);
|
|
762
|
+
}
|
|
763
|
+
var toSignIn = (ctx, request, headers) => {
|
|
764
|
+
const url = new URL(request.url);
|
|
765
|
+
const next = safeNext2(`${url.pathname}${url.search}`);
|
|
766
|
+
const target = new URL(ctx.signInPath, url);
|
|
767
|
+
target.searchParams.set("next", next);
|
|
768
|
+
throw redirect(`${target.pathname}${target.search}`, { headers });
|
|
769
|
+
};
|
|
770
|
+
async function requireOidcUser(ctx, request) {
|
|
771
|
+
const user = await oidcUser(ctx, request);
|
|
772
|
+
if (user)
|
|
773
|
+
return user;
|
|
774
|
+
const headers = new Headers;
|
|
775
|
+
if (ctx.cookies.token(request)) {
|
|
776
|
+
headers.append("Set-Cookie", ctx.cookies.destroy());
|
|
777
|
+
}
|
|
778
|
+
return toSignIn(ctx, request, headers);
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
// src/oidc/signin.ts
|
|
782
|
+
async function signIn(ctx, request) {
|
|
783
|
+
const { clientId, now } = ctx;
|
|
784
|
+
const document = await ctx.issuerApi.discover();
|
|
785
|
+
const url = new URL(request.url);
|
|
786
|
+
const flow = {
|
|
787
|
+
state: randomToken(),
|
|
788
|
+
nonce: randomToken(),
|
|
789
|
+
verifier: randomToken(48),
|
|
790
|
+
next: safeNext2(url.searchParams.get("next")),
|
|
791
|
+
exp: now() + CHALLENGE_SECONDS * 1000,
|
|
792
|
+
dpop: ctx.dpopAlg ? await generateDpopKey(ctx.dpopAlg) : undefined
|
|
793
|
+
};
|
|
794
|
+
const authRequest = {
|
|
795
|
+
...ctx.options.authorizationParams,
|
|
796
|
+
response_type: "code",
|
|
797
|
+
client_id: clientId,
|
|
798
|
+
redirect_uri: ctx.redirectUri,
|
|
799
|
+
scope: ctx.scopes.join(" "),
|
|
800
|
+
state: flow.state,
|
|
801
|
+
nonce: flow.nonce,
|
|
802
|
+
code_challenge: await s256(flow.verifier),
|
|
803
|
+
code_challenge_method: "S256",
|
|
804
|
+
...flow.dpop && { dpop_jkt: await dpopThumbprint(flow.dpop) }
|
|
805
|
+
};
|
|
806
|
+
const endpoint = document.pushed_authorization_request_endpoint;
|
|
807
|
+
const push = ctx.par === "always" || ctx.par === "auto" && document.require_pushed_authorization_requests === true;
|
|
808
|
+
const target = new URL(document.authorization_endpoint);
|
|
809
|
+
if (push) {
|
|
810
|
+
if (!endpoint) {
|
|
811
|
+
throw new OidcError("par-failed", "Pushed authorization requests are required but the issuer publishes no pushed_authorization_request_endpoint.");
|
|
489
812
|
}
|
|
490
|
-
const
|
|
491
|
-
|
|
492
|
-
|
|
813
|
+
const requestUri = await pushRequest(ctx.fetcher, endpoint, ctx.client, authRequest, ctx.signing(flow.dpop));
|
|
814
|
+
target.searchParams.set("client_id", clientId);
|
|
815
|
+
target.searchParams.set("request_uri", requestUri);
|
|
816
|
+
} else {
|
|
817
|
+
for (const [key, value] of Object.entries(authRequest)) {
|
|
818
|
+
target.searchParams.set(key, value);
|
|
493
819
|
}
|
|
494
|
-
const expiresAt = typeof response.expires_in === "number" ? now() + response.expires_in * 1000 : (claims["exp"] ?? 0) * 1000;
|
|
495
|
-
return {
|
|
496
|
-
claims,
|
|
497
|
-
stored: {
|
|
498
|
-
idToken,
|
|
499
|
-
dpop,
|
|
500
|
-
accessToken: response.access_token,
|
|
501
|
-
refreshToken: response.refresh_token ?? previous?.refreshToken,
|
|
502
|
-
expiresAt
|
|
503
|
-
}
|
|
504
|
-
};
|
|
505
820
|
}
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
821
|
+
const headers = new Headers({ "Cache-Control": "no-store" });
|
|
822
|
+
headers.append("Set-Cookie", ctx.cookies.commitChallenge(await ctx.flowSeal.seal(flow), null, now()));
|
|
823
|
+
headers.set("Location", target.toString());
|
|
824
|
+
return new Response(null, { status: 302, headers });
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
// src/oidc/client.ts
|
|
828
|
+
function oidcClient(options) {
|
|
829
|
+
const ctx = createContext(options);
|
|
830
|
+
return {
|
|
831
|
+
names: ctx.cookies.names,
|
|
832
|
+
oidcUser: (request) => oidcUser(ctx, request),
|
|
833
|
+
refresh: (user, refreshOptions) => refresh(ctx, user, refreshOptions),
|
|
834
|
+
requireOidcUser: (request) => requireOidcUser(ctx, request),
|
|
835
|
+
signIn: (request) => signIn(ctx, request),
|
|
836
|
+
callback: (request) => callback(ctx, request),
|
|
837
|
+
logout: (request) => logout(ctx, request)
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
// src/oidc/dpop-fetch.ts
|
|
841
|
+
function dpopFetch(oidc, user, options = {}) {
|
|
842
|
+
let last = user;
|
|
843
|
+
return async (input, init) => {
|
|
844
|
+
const next = await oidc.refresh(last);
|
|
845
|
+
if (next.accessToken !== last.accessToken || next.expiresAt !== last.expiresAt) {
|
|
846
|
+
last = next;
|
|
847
|
+
options.onUser?.(next);
|
|
511
848
|
}
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
const target = new URL(signInPath, url);
|
|
518
|
-
target.searchParams.set("next", next);
|
|
519
|
-
throw redirect(`${target.pathname}${target.search}`, { headers });
|
|
849
|
+
if (last.dpop)
|
|
850
|
+
return last.dpop.fetch(input, init);
|
|
851
|
+
const headers = new Headers(init?.headers);
|
|
852
|
+
headers.set("authorization", `Bearer ${last.accessToken}`);
|
|
853
|
+
return last.fetch(String(input), { ...init, headers });
|
|
520
854
|
};
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
855
|
+
}
|
|
856
|
+
// src/oidc/env.ts
|
|
857
|
+
class OidcEnvError extends Error {
|
|
858
|
+
problems;
|
|
859
|
+
name = "OidcEnvError";
|
|
860
|
+
constructor(problems) {
|
|
861
|
+
super(`The OIDC environment is not usable:
|
|
862
|
+
${problems.map((p) => `- ${p}`).join(`
|
|
863
|
+
`)}`);
|
|
864
|
+
this.problems = problems;
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
var LOOPBACK = new Set(["localhost", "127.0.0.1", "[::1]"]);
|
|
868
|
+
var named = (context, suffix) => `${context.prefix}_${suffix}`;
|
|
869
|
+
function plain(context, suffix) {
|
|
870
|
+
const value = context.env[named(context, suffix)]?.trim();
|
|
871
|
+
return value ? value : undefined;
|
|
872
|
+
}
|
|
873
|
+
function required(context, suffix) {
|
|
874
|
+
const value = plain(context, suffix);
|
|
875
|
+
if (value === undefined) {
|
|
876
|
+
context.problems.push(`${named(context, suffix)} is not set.`);
|
|
877
|
+
}
|
|
878
|
+
return value;
|
|
879
|
+
}
|
|
880
|
+
function url(context, suffix, mandatory) {
|
|
881
|
+
const value = mandatory ? required(context, suffix) : plain(context, suffix);
|
|
882
|
+
if (value === undefined)
|
|
883
|
+
return;
|
|
884
|
+
const variable = named(context, suffix);
|
|
885
|
+
let parsed;
|
|
886
|
+
try {
|
|
887
|
+
parsed = new URL(value);
|
|
888
|
+
} catch {
|
|
889
|
+
context.problems.push(`${variable} is not an absolute URL.`);
|
|
890
|
+
return;
|
|
891
|
+
}
|
|
892
|
+
if (parsed.protocol !== "https:" && !(parsed.protocol === "http:" && LOOPBACK.has(parsed.hostname))) {
|
|
893
|
+
context.problems.push(`${variable} must be an https: URL.`);
|
|
894
|
+
return;
|
|
895
|
+
}
|
|
896
|
+
return value;
|
|
897
|
+
}
|
|
898
|
+
async function secret(context, suffix, mandatory) {
|
|
899
|
+
const direct = plain(context, suffix);
|
|
900
|
+
const fileSuffix = `${suffix}_FILE`;
|
|
901
|
+
const path = plain(context, fileSuffix);
|
|
902
|
+
if (direct !== undefined && path !== undefined) {
|
|
903
|
+
context.problems.push(`Set ${named(context, suffix)} or ${named(context, fileSuffix)}, not both.`);
|
|
904
|
+
return;
|
|
905
|
+
}
|
|
906
|
+
if (path === undefined) {
|
|
907
|
+
if (direct !== undefined) {
|
|
908
|
+
return { value: direct, variable: named(context, suffix) };
|
|
550
909
|
}
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
try {
|
|
554
|
-
if (!stored.refreshToken)
|
|
555
|
-
throw new Error("no refresh token");
|
|
556
|
-
stored = await refreshing(stored);
|
|
557
|
-
await commit(headers, stored);
|
|
558
|
-
} catch {
|
|
559
|
-
if (stored.expiresAt <= now())
|
|
560
|
-
return null;
|
|
561
|
-
}
|
|
910
|
+
if (mandatory) {
|
|
911
|
+
context.problems.push(`${named(context, fileSuffix)} (or ${named(context, suffix)}) is not set.`);
|
|
562
912
|
}
|
|
563
|
-
return
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
913
|
+
return;
|
|
914
|
+
}
|
|
915
|
+
const variable = named(context, fileSuffix);
|
|
916
|
+
let text;
|
|
917
|
+
try {
|
|
918
|
+
const { mode } = await context.statFile(path);
|
|
919
|
+
if ((mode & 63) !== 0) {
|
|
920
|
+
context.problems.push(`${variable} names a file readable by group or others (mode ${(mode & 511).toString(8)}); restrict it to its owner, such as chmod 600.`);
|
|
921
|
+
return;
|
|
922
|
+
}
|
|
923
|
+
text = await context.readFile(path);
|
|
924
|
+
} catch {
|
|
925
|
+
context.problems.push(`${variable} names a file that cannot be read: check the path and its permissions.`);
|
|
926
|
+
return;
|
|
927
|
+
}
|
|
928
|
+
const value = text.trim();
|
|
929
|
+
if (value === "") {
|
|
930
|
+
context.problems.push(`${variable} names an empty file.`);
|
|
931
|
+
return;
|
|
932
|
+
}
|
|
933
|
+
if (/\s/.test(value)) {
|
|
934
|
+
context.problems.push(`${variable} names a file with whitespace inside: it must hold one value.`);
|
|
935
|
+
return;
|
|
936
|
+
}
|
|
937
|
+
return { value, variable };
|
|
938
|
+
}
|
|
939
|
+
async function oidcFromEnv(source = {}) {
|
|
940
|
+
const context = {
|
|
941
|
+
env: source.env ?? process.env,
|
|
942
|
+
prefix: source.prefix ?? "OIDC",
|
|
943
|
+
problems: [],
|
|
944
|
+
readFile: source.readFile ?? ((path) => Bun.file(path).text()),
|
|
945
|
+
statFile: source.statFile ?? ((path) => Bun.file(path).stat())
|
|
946
|
+
};
|
|
947
|
+
const issuer = url(context, "ISSUER", true);
|
|
948
|
+
const clientId = required(context, "CLIENT_ID");
|
|
949
|
+
const clientSecret = await secret(context, "CLIENT_SECRET", false);
|
|
950
|
+
const redirectUri = url(context, "REDIRECT_URI", true);
|
|
951
|
+
const postLogoutRedirectUri = url(context, "POST_LOGOUT_REDIRECT_URI", false);
|
|
952
|
+
const session = await secret(context, "SESSION_SECRET", true);
|
|
953
|
+
if (session !== undefined && session.value.length < 32) {
|
|
954
|
+
context.problems.push(`The session secret must be 32 characters or more (${session.variable}).`);
|
|
955
|
+
}
|
|
956
|
+
if (context.problems.length > 0 || issuer === undefined || clientId === undefined || redirectUri === undefined || session === undefined) {
|
|
957
|
+
throw new OidcEnvError(context.problems);
|
|
578
958
|
}
|
|
579
959
|
return {
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
redirect_uri: redirectUri,
|
|
607
|
-
scope: scopes.join(" "),
|
|
608
|
-
state: flow.state,
|
|
609
|
-
nonce: flow.nonce,
|
|
610
|
-
code_challenge: await s256(flow.verifier),
|
|
611
|
-
code_challenge_method: "S256",
|
|
612
|
-
...flow.dpop && { dpop_jkt: await dpopThumbprint(flow.dpop) }
|
|
613
|
-
};
|
|
614
|
-
const endpoint = document.pushed_authorization_request_endpoint;
|
|
615
|
-
const push = par === "always" || par === "auto" && document.require_pushed_authorization_requests === true;
|
|
616
|
-
const target = new URL(document.authorization_endpoint);
|
|
617
|
-
if (push) {
|
|
618
|
-
if (!endpoint) {
|
|
619
|
-
throw new OidcError("par-failed", "Pushed authorization requests are required but the issuer publishes no pushed_authorization_request_endpoint.");
|
|
620
|
-
}
|
|
621
|
-
const requestUri = await pushRequest(fetcher, endpoint, client, authRequest, signing(flow.dpop));
|
|
622
|
-
target.searchParams.set("client_id", clientId);
|
|
623
|
-
target.searchParams.set("request_uri", requestUri);
|
|
624
|
-
} else {
|
|
625
|
-
for (const [key, value] of Object.entries(authRequest)) {
|
|
626
|
-
target.searchParams.set(key, value);
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
const headers = new Headers({ "Cache-Control": "no-store" });
|
|
630
|
-
setCookie(headers, cookies.commitChallenge(await flowSeal.seal(flow), null, now()));
|
|
631
|
-
headers.set("Location", target.toString());
|
|
632
|
-
return new Response(null, { status: 302, headers });
|
|
633
|
-
},
|
|
634
|
-
async callback(request) {
|
|
635
|
-
const cleared = () => {
|
|
636
|
-
const headers = new Headers;
|
|
637
|
-
setCookie(headers, cookies.destroyChallenge());
|
|
638
|
-
return headers;
|
|
639
|
-
};
|
|
640
|
-
const fail = (code, message) => new OidcError(code, message, cleared());
|
|
641
|
-
const url = new URL(request.url);
|
|
642
|
-
const sealed = cookies.challengeOf(request);
|
|
643
|
-
const flow = sealed ? await flowSeal.open(sealed) : null;
|
|
644
|
-
if (!isFlow(flow)) {
|
|
645
|
-
throw fail("missing-challenge", "The sign-in challenge cookie is missing or unreadable (a second tab, an expired page or a blocked cookie).");
|
|
646
|
-
}
|
|
647
|
-
if (flow.exp <= now()) {
|
|
648
|
-
throw fail("challenge-expired", "The sign-in took too long; start again.");
|
|
649
|
-
}
|
|
650
|
-
const returned = url.searchParams.get("state");
|
|
651
|
-
if (returned === null || !sameText(returned, flow.state)) {
|
|
652
|
-
throw fail("state-mismatch", "The returned state is not the one this sign-in sent.");
|
|
653
|
-
}
|
|
654
|
-
const failure = url.searchParams.get("error");
|
|
655
|
-
if (failure) {
|
|
656
|
-
const description = url.searchParams.get("error_description");
|
|
657
|
-
throw fail("provider-error", `The issuer refused: ${failure}${description ? ` (${description})` : ""}.`);
|
|
658
|
-
}
|
|
659
|
-
const claimedIssuer = url.searchParams.get("iss");
|
|
660
|
-
if (claimedIssuer !== null && claimedIssuer !== issuer) {
|
|
661
|
-
throw fail("issuer-mismatch", "The callback names another issuer.");
|
|
662
|
-
}
|
|
663
|
-
const code = url.searchParams.get("code");
|
|
664
|
-
if (!code)
|
|
665
|
-
throw fail("missing-code", "The callback carries no code.");
|
|
960
|
+
issuer,
|
|
961
|
+
clientId,
|
|
962
|
+
clientSecret: clientSecret?.value,
|
|
963
|
+
redirectUri,
|
|
964
|
+
secret: session.value,
|
|
965
|
+
name: plain(context, "COOKIE_NAME") ?? "oidc",
|
|
966
|
+
postLogoutRedirectUri
|
|
967
|
+
};
|
|
968
|
+
}
|
|
969
|
+
// src/oidc/routes.ts
|
|
970
|
+
import { redirect as redirect2 } from "react-router";
|
|
971
|
+
function signInRoute(oidc, options = {}) {
|
|
972
|
+
return {
|
|
973
|
+
async loader({ request }) {
|
|
974
|
+
await options.guard?.(request);
|
|
975
|
+
return oidc.signIn(request);
|
|
976
|
+
}
|
|
977
|
+
};
|
|
978
|
+
}
|
|
979
|
+
function callbackRoute(oidc, options = {}) {
|
|
980
|
+
const errorPath = sitePath(options.errorPath ?? "/", {
|
|
981
|
+
label: "callbackRoute(): errorPath"
|
|
982
|
+
});
|
|
983
|
+
return {
|
|
984
|
+
async loader({ request }) {
|
|
985
|
+
await options.guard?.(request);
|
|
666
986
|
try {
|
|
667
|
-
|
|
668
|
-
const answer = await tokenRequest(fetcher, document.token_endpoint, client, {
|
|
669
|
-
...extraTokenParams,
|
|
670
|
-
grant_type: "authorization_code",
|
|
671
|
-
code,
|
|
672
|
-
redirect_uri: redirectUri,
|
|
673
|
-
code_verifier: flow.verifier
|
|
674
|
-
}, "token-exchange-failed", signing(flow.dpop));
|
|
675
|
-
const { stored } = await toStored(answer, flow.nonce, undefined, flow.dpop);
|
|
676
|
-
const headers = cleared();
|
|
677
|
-
await commit(headers, stored);
|
|
678
|
-
headers.set("Location", safeNext2(flow.next));
|
|
679
|
-
headers.set("Cache-Control", "no-store");
|
|
680
|
-
return new Response(null, { status: 302, headers });
|
|
987
|
+
return await oidc.callback(request);
|
|
681
988
|
} catch (error) {
|
|
682
|
-
if (error instanceof OidcError)
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
}
|
|
687
|
-
throw error;
|
|
989
|
+
if (!(error instanceof OidcError))
|
|
990
|
+
throw error;
|
|
991
|
+
if (options.onError)
|
|
992
|
+
return options.onError(error);
|
|
993
|
+
return redirect2(`${errorPath}?${new URLSearchParams({ "sign-in-error": error.code })}`, { headers: error.headers });
|
|
688
994
|
}
|
|
995
|
+
}
|
|
996
|
+
};
|
|
997
|
+
}
|
|
998
|
+
function signOutRoute(oidc, options = {}) {
|
|
999
|
+
const sameSite = siteCheck(options.origin);
|
|
1000
|
+
const redirectTo = sitePath(options.redirectTo ?? "/", {
|
|
1001
|
+
label: "signOutRoute(): redirectTo",
|
|
1002
|
+
query: true
|
|
1003
|
+
});
|
|
1004
|
+
return {
|
|
1005
|
+
async action({ request }) {
|
|
1006
|
+
await options.guard?.(request);
|
|
1007
|
+
sameSite(request);
|
|
1008
|
+
return oidc.logout(request);
|
|
689
1009
|
},
|
|
690
|
-
async
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
options.onRevokeError?.(error);
|
|
694
|
-
} catch {}
|
|
695
|
-
};
|
|
696
|
-
const headers = new Headers({ "Cache-Control": "no-store" });
|
|
697
|
-
setCookie(headers, cookies.destroy());
|
|
698
|
-
const sealed = cookies.token(request);
|
|
699
|
-
const opened = sealed ? await tokenSeal.open(sealed) : null;
|
|
700
|
-
const document = await issuerApi.discover().catch((cause) => {
|
|
701
|
-
if (options.revokeOnLogout) {
|
|
702
|
-
report(new OidcError("discovery-failed", "Discovery failed at logout, so the tokens were not revoked.", undefined, { cause }));
|
|
703
|
-
}
|
|
704
|
-
return null;
|
|
705
|
-
});
|
|
706
|
-
if (options.revokeOnLogout && document?.revocation_endpoint && isStored(opened)) {
|
|
707
|
-
await revokeTokens({
|
|
708
|
-
fetcher,
|
|
709
|
-
client,
|
|
710
|
-
endpoint: document.revocation_endpoint,
|
|
711
|
-
stored: opened,
|
|
712
|
-
signing,
|
|
713
|
-
timeoutMs: options.revokeTimeoutMs ?? 3000,
|
|
714
|
-
report
|
|
715
|
-
});
|
|
716
|
-
}
|
|
717
|
-
const endpoint = document?.end_session_endpoint;
|
|
718
|
-
if (!endpoint) {
|
|
719
|
-
headers.set("Location", options.postLogoutRedirectUri ?? "/");
|
|
720
|
-
return new Response(null, { status: 302, headers });
|
|
721
|
-
}
|
|
722
|
-
const target = new URL(endpoint);
|
|
723
|
-
if (isStored(opened))
|
|
724
|
-
target.searchParams.set("id_token_hint", opened.idToken);
|
|
725
|
-
target.searchParams.set("client_id", clientId);
|
|
726
|
-
if (options.postLogoutRedirectUri) {
|
|
727
|
-
target.searchParams.set("post_logout_redirect_uri", options.postLogoutRedirectUri);
|
|
728
|
-
}
|
|
729
|
-
headers.set("Location", target.toString());
|
|
730
|
-
return new Response(null, { status: 302, headers });
|
|
1010
|
+
async loader({ request }) {
|
|
1011
|
+
await options.guard?.(request);
|
|
1012
|
+
return redirect2(redirectTo);
|
|
731
1013
|
}
|
|
732
1014
|
};
|
|
733
1015
|
}
|
|
734
1016
|
export {
|
|
1017
|
+
OidcEnvError,
|
|
735
1018
|
OidcError,
|
|
1019
|
+
callbackRoute,
|
|
1020
|
+
dpopFetch,
|
|
736
1021
|
generateDpopKey,
|
|
737
1022
|
oidcClient,
|
|
738
|
-
|
|
1023
|
+
oidcFromEnv,
|
|
1024
|
+
signDpopProof,
|
|
1025
|
+
signInRoute,
|
|
1026
|
+
signOutRoute
|
|
739
1027
|
};
|
|
740
1028
|
|
|
741
|
-
//# debugId=
|
|
1029
|
+
//# debugId=C1E1692B5010A35F64756E2164756E21
|
|
742
1030
|
//# sourceMappingURL=index.js.map
|