@broberg/sso 0.20.0 → 0.21.1
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 +23 -0
- package/dist/{chunk-ZDNBHM2G.js → chunk-GK3QCKXB.js} +290 -7
- package/dist/chunk-GK3QCKXB.js.map +1 -0
- package/dist/{chunk-KEBSZBHL.cjs → chunk-XQEHSHLR.cjs} +302 -5
- package/dist/chunk-XQEHSHLR.cjs.map +1 -0
- package/dist/hono.cjs +38 -37
- package/dist/hono.cjs.map +1 -1
- package/dist/hono.d.cts +7 -1
- package/dist/hono.d.ts +7 -1
- package/dist/hono.js +3 -2
- package/dist/hono.js.map +1 -1
- package/dist/index.cjs +112 -325
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +4 -260
- package/dist/index.js.map +1 -1
- package/dist/{session-AgUJ6U_7.d.cts → session-DnotfVNd.d.cts} +17 -1
- package/dist/{session-AgUJ6U_7.d.ts → session-DnotfVNd.d.ts} +17 -1
- package/package.json +1 -1
- package/dist/chunk-KEBSZBHL.cjs.map +0 -1
- package/dist/chunk-ZDNBHM2G.js.map +0 -1
package/README.md
CHANGED
|
@@ -696,3 +696,26 @@ const { token, exp } = await exchangeUserToken({
|
|
|
696
696
|
(the user's BID login has ended — «log ud» means log out), `client_mismatch` (the token belongs to another app),
|
|
697
697
|
`subject_is_ticket`, `session_ended`, `no_rule_for_audience`, `scope_not_allowed`, `client_unauthenticated`.
|
|
698
698
|
- BID issues nothing without a rule for (your app, the receiver). Ask broberg-id for the rule.
|
|
699
|
+
|
|
700
|
+
## Login without a client secret — `clientAuth: "fly"` (0.21.0, F084.181, BID F084.180)
|
|
701
|
+
|
|
702
|
+
An app on Fly can prove itself at BID's token endpoint with its Fly identity instead of a
|
|
703
|
+
`client_secret`. Nothing to store, nothing to rotate, nothing to leak.
|
|
704
|
+
|
|
705
|
+
```ts
|
|
706
|
+
const sso = ssoRoutes({ clientAuth: "fly", tokenStore }); // or SSO_CLIENT_AUTH=fly
|
|
707
|
+
```
|
|
708
|
+
|
|
709
|
+
- At the code exchange (and at refresh) the app asks Fly's machine API for an OIDC token with
|
|
710
|
+
`aud` = BID's **token endpoint** (not the issuer, so a ticket token cannot be replayed as a login
|
|
711
|
+
proof) and sends it as `client_assertion` with
|
|
712
|
+
`client_assertion_type=urn:broberg:params:oauth:client-assertion-type:fly-oidc`. PKCE is unchanged.
|
|
713
|
+
- **No fallback.** With `"fly"`, `client_secret` is never sent to the token endpoint, even if one is
|
|
714
|
+
configured. If Fly cannot give a token, the login fails: the browser gets one generic 500, the log
|
|
715
|
+
gets `fly client assertion unavailable: …`.
|
|
716
|
+
- **BID must be switched first**: the client registered as `token_endpoint_auth_method: fly_oidc`,
|
|
717
|
+
bound to your Fly org and app. Ask broberg-id. Switch the app, measure one login, then BID deletes
|
|
718
|
+
the secret.
|
|
719
|
+
- `clientSecret` is still what introspection and the embedded login use — those are not the token
|
|
720
|
+
endpoint's code exchange.
|
|
721
|
+
- Without the option nothing changes: the 0.20.0 request, byte for byte.
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { importJWK, decodeProtectedHeader, jwtVerify } from 'jose';
|
|
1
|
+
import { importJWK, generateKeyPair, exportJWK, calculateJwkThumbprint, SignJWT, decodeProtectedHeader, jwtVerify } from 'jose';
|
|
2
|
+
import { existsSync } from 'fs';
|
|
3
|
+
import { request } from 'http';
|
|
2
4
|
|
|
3
5
|
// src/config.ts
|
|
4
6
|
var SsoConfigError = class extends Error {
|
|
@@ -14,6 +16,12 @@ function required(env, name, hint) {
|
|
|
14
16
|
}
|
|
15
17
|
return raw.trim();
|
|
16
18
|
}
|
|
19
|
+
function parseClientAuth(raw) {
|
|
20
|
+
const v = raw?.trim();
|
|
21
|
+
if (!v) return void 0;
|
|
22
|
+
if (v === "secret" || v === "fly") return v;
|
|
23
|
+
throw new SsoConfigError(`SSO_CLIENT_AUTH must be "secret" or "fly" (got ${JSON.stringify(raw)}).`);
|
|
24
|
+
}
|
|
17
25
|
var DEFAULT_SESSION_MAX_AGE = 60 * 60 * 24 * 7;
|
|
18
26
|
function loadSsoConfig(env = process.env) {
|
|
19
27
|
const rawIssuer = required(
|
|
@@ -68,7 +76,8 @@ function loadSsoConfig(env = process.env) {
|
|
|
68
76
|
cookieName: env.SSO_COOKIE_NAME?.trim() || "bid_session",
|
|
69
77
|
sessionMaxAge,
|
|
70
78
|
postLogoutRedirectUri: env.SSO_POST_LOGOUT_REDIRECT_URI?.trim() || void 0,
|
|
71
|
-
clientSecret: env.SSO_CLIENT_SECRET?.trim() || void 0
|
|
79
|
+
clientSecret: env.SSO_CLIENT_SECRET?.trim() || void 0,
|
|
80
|
+
clientAuth: parseClientAuth(env.SSO_CLIENT_AUTH)
|
|
72
81
|
};
|
|
73
82
|
}
|
|
74
83
|
|
|
@@ -284,6 +293,184 @@ function createJwksCache(options) {
|
|
|
284
293
|
}
|
|
285
294
|
};
|
|
286
295
|
}
|
|
296
|
+
var DEFAULT_ISSUER = "https://id.broberg.ai";
|
|
297
|
+
var FLY_SOCKET = "/.fly/api";
|
|
298
|
+
var NoWorkloadIdentityError = class extends Error {
|
|
299
|
+
constructor(message) {
|
|
300
|
+
super(message);
|
|
301
|
+
this.name = "NoWorkloadIdentityError";
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
var TicketExchangeError = class extends Error {
|
|
305
|
+
constructor(message, code, status) {
|
|
306
|
+
super(message);
|
|
307
|
+
this.code = code;
|
|
308
|
+
this.status = status;
|
|
309
|
+
this.name = "TicketExchangeError";
|
|
310
|
+
}
|
|
311
|
+
code;
|
|
312
|
+
status;
|
|
313
|
+
};
|
|
314
|
+
var TicketUnavailableError = class extends Error {
|
|
315
|
+
constructor(message) {
|
|
316
|
+
super(message);
|
|
317
|
+
this.name = "TicketUnavailableError";
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
function flyOidcOverSocket(aud) {
|
|
321
|
+
return new Promise((resolve, reject) => {
|
|
322
|
+
const body = JSON.stringify({ aud });
|
|
323
|
+
const req = request(
|
|
324
|
+
{ socketPath: FLY_SOCKET, path: "/v1/tokens/oidc", method: "POST", headers: { "content-type": "application/json", "content-length": Buffer.byteLength(body) } },
|
|
325
|
+
(res) => {
|
|
326
|
+
let data = "";
|
|
327
|
+
res.setEncoding("utf8");
|
|
328
|
+
res.on("data", (c) => data += c);
|
|
329
|
+
res.on("end", () => {
|
|
330
|
+
if ((res.statusCode ?? 0) >= 200 && (res.statusCode ?? 0) < 300) resolve(data.trim());
|
|
331
|
+
else reject(new TicketUnavailableError(`Fly OIDC answered ${res.statusCode}`));
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
);
|
|
335
|
+
req.on("error", (e) => reject(new TicketUnavailableError(`Fly OIDC socket: ${e.message}`)));
|
|
336
|
+
req.end(body);
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
function jwtExp(token) {
|
|
340
|
+
try {
|
|
341
|
+
const payload = JSON.parse(Buffer.from(token.split(".")[1] ?? "", "base64url").toString("utf8"));
|
|
342
|
+
return typeof payload.exp === "number" ? payload.exp : null;
|
|
343
|
+
} catch {
|
|
344
|
+
return null;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
async function readTicketResponse(res, issuer, target, now) {
|
|
348
|
+
let body = {};
|
|
349
|
+
try {
|
|
350
|
+
body = await res.json();
|
|
351
|
+
} catch {
|
|
352
|
+
}
|
|
353
|
+
if (res.status >= 500) throw new TicketUnavailableError(`${issuer} answered ${res.status}`);
|
|
354
|
+
if (!res.ok || !body.access_token) {
|
|
355
|
+
const desc = body.error_description ?? "";
|
|
356
|
+
const code = desc.includes(":") ? desc.slice(0, desc.indexOf(":")).trim() : body.error ?? "exchange_refused";
|
|
357
|
+
throw new TicketExchangeError(`BID refused the ticket for ${target}: ${desc || body.error || res.status}`, code, res.status);
|
|
358
|
+
}
|
|
359
|
+
const nowSec = Math.floor(now() / 1e3);
|
|
360
|
+
const exp = jwtExp(body.access_token) ?? nowSec + (body.expires_in ?? 300);
|
|
361
|
+
return { token: body.access_token, exp };
|
|
362
|
+
}
|
|
363
|
+
function createTicketClient(options = {}) {
|
|
364
|
+
const issuer = (options.issuer ?? DEFAULT_ISSUER).replace(/\/+$/, "");
|
|
365
|
+
const renewBeforeSec = options.renewBeforeSec ?? 30;
|
|
366
|
+
const env = options.env ?? process.env;
|
|
367
|
+
const fetchImpl = options.fetchImpl ?? fetch;
|
|
368
|
+
const now = options.now ?? (() => Date.now());
|
|
369
|
+
const flyOidc = options.flyOidc ?? flyOidcOverSocket;
|
|
370
|
+
const onFly = options.onFly ?? (() => !!env.FLY_APP_NAME && existsSync(FLY_SOCKET));
|
|
371
|
+
const cache = /* @__PURE__ */ new Map();
|
|
372
|
+
const inFlight = /* @__PURE__ */ new Map();
|
|
373
|
+
let dpopKey = null;
|
|
374
|
+
const ownKey = () => dpopKey ??= createDpopKey();
|
|
375
|
+
async function platformToken() {
|
|
376
|
+
if (onFly()) return flyOidc(issuer);
|
|
377
|
+
const url = env.ACTIONS_ID_TOKEN_REQUEST_URL;
|
|
378
|
+
const bearer = env.ACTIONS_ID_TOKEN_REQUEST_TOKEN;
|
|
379
|
+
if (url && bearer) {
|
|
380
|
+
let res;
|
|
381
|
+
try {
|
|
382
|
+
res = await fetchImpl(`${url}&audience=${encodeURIComponent(issuer)}`, { headers: { authorization: `Bearer ${bearer}` } });
|
|
383
|
+
} catch (e) {
|
|
384
|
+
throw new TicketUnavailableError(`GitHub OIDC endpoint unreachable: ${e instanceof Error ? e.message : String(e)}`);
|
|
385
|
+
}
|
|
386
|
+
if (!res.ok) throw new TicketUnavailableError(`GitHub OIDC endpoint answered ${res.status}`);
|
|
387
|
+
const { value } = await res.json();
|
|
388
|
+
if (!value) throw new TicketUnavailableError("GitHub OIDC endpoint returned no token");
|
|
389
|
+
return value;
|
|
390
|
+
}
|
|
391
|
+
throw new NoWorkloadIdentityError(
|
|
392
|
+
"No workload identity to exchange for a ticket: not on a Fly machine (FLY_APP_NAME + /.fly/api) and not in a GitHub Actions job (ACTIONS_ID_TOKEN_REQUEST_URL/_TOKEN \u2014 add `permissions: id-token: write`). A Mac session keeps using its session key."
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
async function exchange(audience, scope) {
|
|
396
|
+
const subject = await platformToken();
|
|
397
|
+
const form = new URLSearchParams({
|
|
398
|
+
grant_type: "urn:ietf:params:oauth:grant-type:token-exchange",
|
|
399
|
+
subject_token: subject,
|
|
400
|
+
subject_token_type: "urn:ietf:params:oauth:token-type:jwt",
|
|
401
|
+
audience
|
|
402
|
+
});
|
|
403
|
+
if (scope) form.set("scope", scope);
|
|
404
|
+
let res;
|
|
405
|
+
try {
|
|
406
|
+
const tokenUrl = `${issuer}/oauth2/token`;
|
|
407
|
+
const headers = { "content-type": "application/x-www-form-urlencoded" };
|
|
408
|
+
if (options.dpop) headers.dpop = await createDpopProof(await ownKey(), { method: "POST", url: tokenUrl, now });
|
|
409
|
+
res = await fetchImpl(tokenUrl, { method: "POST", headers, body: form });
|
|
410
|
+
} catch (e) {
|
|
411
|
+
throw new TicketUnavailableError(`${issuer} unreachable: ${e instanceof Error ? e.message : String(e)}`);
|
|
412
|
+
}
|
|
413
|
+
return readTicketResponse(res, issuer, audience, now);
|
|
414
|
+
}
|
|
415
|
+
const client = {
|
|
416
|
+
async authHeaders({ audience, scope, method, url }) {
|
|
417
|
+
const ticket = await client.get({ audience, scope });
|
|
418
|
+
if (!options.dpop) return { authorization: `Bearer ${ticket}` };
|
|
419
|
+
return { authorization: `DPoP ${ticket}`, dpop: await createDpopProof(await ownKey(), { method, url, ticket, now }) };
|
|
420
|
+
},
|
|
421
|
+
async get({ audience, scope }) {
|
|
422
|
+
const key = `${audience} ${scope ?? ""}`;
|
|
423
|
+
const hit = cache.get(key);
|
|
424
|
+
if (hit && hit.exp - renewBeforeSec > now() / 1e3) return hit.token;
|
|
425
|
+
const pending = inFlight.get(key);
|
|
426
|
+
if (pending) return pending;
|
|
427
|
+
const p = exchange(audience, scope).then((t) => {
|
|
428
|
+
cache.set(key, t);
|
|
429
|
+
return t.token;
|
|
430
|
+
}).finally(() => inFlight.delete(key));
|
|
431
|
+
inFlight.set(key, p);
|
|
432
|
+
return p;
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
return client;
|
|
436
|
+
}
|
|
437
|
+
var shared = null;
|
|
438
|
+
function fetchTicket(opts) {
|
|
439
|
+
shared ??= createTicketClient();
|
|
440
|
+
return shared.get(opts);
|
|
441
|
+
}
|
|
442
|
+
async function exchangeUserToken(opts) {
|
|
443
|
+
const issuer = (opts.issuer ?? DEFAULT_ISSUER).replace(/\/+$/, "");
|
|
444
|
+
const target = opts.resource ?? opts.audience;
|
|
445
|
+
if (!target) throw new TicketExchangeError("exchangeUserToken needs a receiver: resource (its URL) or audience (BID's name for it)", "malformed_request", 0);
|
|
446
|
+
const fetchImpl = opts.fetchImpl ?? fetch;
|
|
447
|
+
const now = opts.now ?? (() => Date.now());
|
|
448
|
+
const form = new URLSearchParams({
|
|
449
|
+
grant_type: "urn:ietf:params:oauth:grant-type:token-exchange",
|
|
450
|
+
subject_token: opts.subjectToken,
|
|
451
|
+
subject_token_type: "urn:ietf:params:oauth:token-type:access_token"
|
|
452
|
+
});
|
|
453
|
+
if (opts.resource) form.set("resource", opts.resource);
|
|
454
|
+
if (opts.audience) form.set("audience", opts.audience);
|
|
455
|
+
if (opts.scope) form.set("scope", opts.scope);
|
|
456
|
+
const tokenUrl = `${issuer}/oauth2/token`;
|
|
457
|
+
const headers = { "content-type": "application/x-www-form-urlencoded" };
|
|
458
|
+
if (opts.clientSecret) {
|
|
459
|
+
headers.authorization = `Basic ${btoa(`${encodeURIComponent(opts.clientId)}:${encodeURIComponent(opts.clientSecret)}`)}`;
|
|
460
|
+
} else {
|
|
461
|
+
form.set("client_id", opts.clientId);
|
|
462
|
+
}
|
|
463
|
+
if (opts.dpopKey) headers.dpop = await createDpopProof(opts.dpopKey, { method: "POST", url: tokenUrl, now });
|
|
464
|
+
let res;
|
|
465
|
+
try {
|
|
466
|
+
res = await fetchImpl(tokenUrl, { method: "POST", headers, body: form });
|
|
467
|
+
} catch (e) {
|
|
468
|
+
throw new TicketUnavailableError(`${issuer} unreachable: ${e instanceof Error ? e.message : String(e)}`);
|
|
469
|
+
}
|
|
470
|
+
return readTicketResponse(res, issuer, target, now);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// src/client.ts
|
|
287
474
|
var SsoError = class extends Error {
|
|
288
475
|
constructor(message) {
|
|
289
476
|
super(message);
|
|
@@ -368,6 +555,7 @@ var INVITATION_OUTCOMES = /* @__PURE__ */ new Set([
|
|
|
368
555
|
var MIGRATION_STATES = /* @__PURE__ */ new Set(["ready", "invited", "expired", "not_invited"]);
|
|
369
556
|
var BACKCHANNEL_EVENT = "http://schemas.openid.net/event/backchannel-logout";
|
|
370
557
|
var MAX_INVITATIONS = 500;
|
|
558
|
+
var FLY_CLIENT_ASSERTION_TYPE = "urn:broberg:params:oauth:client-assertion-type:fly-oidc";
|
|
371
559
|
function excerptForLog(raw, clientSecret) {
|
|
372
560
|
let text = raw.replace(/\s+/g, " ").trim();
|
|
373
561
|
if (clientSecret) text = text.split(clientSecret).join("[redacted client_secret]");
|
|
@@ -403,6 +591,18 @@ var ALLOWED_ALGS = [
|
|
|
403
591
|
function createSsoClient(config, options = {}) {
|
|
404
592
|
const fetchImpl = options.fetchImpl ?? fetch;
|
|
405
593
|
const timeoutMs = options.timeoutMs ?? 1e4;
|
|
594
|
+
const flyOidc = options.flyOidc ?? flyOidcOverSocket;
|
|
595
|
+
async function tokenEndpointAuth(tokenEndpoint) {
|
|
596
|
+
if (config.clientAuth !== "fly") return config.clientSecret ? { client_secret: config.clientSecret } : {};
|
|
597
|
+
let assertion;
|
|
598
|
+
try {
|
|
599
|
+
assertion = await flyOidc(tokenEndpoint);
|
|
600
|
+
} catch (e) {
|
|
601
|
+
throw new SsoError(`fly client assertion unavailable: could not get a Fly OIDC token for ${tokenEndpoint} \u2014 ${e instanceof Error ? e.message : String(e)}`);
|
|
602
|
+
}
|
|
603
|
+
if (!assertion) throw new SsoError(`fly client assertion unavailable: Fly returned an empty token for ${tokenEndpoint}`);
|
|
604
|
+
return { client_assertion_type: FLY_CLIENT_ASSERTION_TYPE, client_assertion: assertion };
|
|
605
|
+
}
|
|
406
606
|
async function appPost(path, bearer, payload, what) {
|
|
407
607
|
const url = new URL(path, config.issuer).toString();
|
|
408
608
|
const ctrl = new AbortController();
|
|
@@ -686,7 +886,7 @@ function createSsoClient(config, options = {}) {
|
|
|
686
886
|
// ALWAYS sent, secret or not. See SsoConfig.clientSecret: the two
|
|
687
887
|
// prove different things, so a confidential client keeps PKCE.
|
|
688
888
|
code_verifier: codeVerifier,
|
|
689
|
-
...
|
|
889
|
+
...await tokenEndpointAuth(token_endpoint)
|
|
690
890
|
})
|
|
691
891
|
});
|
|
692
892
|
const raw = await res.text();
|
|
@@ -780,6 +980,11 @@ function createSsoClient(config, options = {}) {
|
|
|
780
980
|
code_challenge: await challengeFor(codeVerifier),
|
|
781
981
|
code_challenge_method: "S256",
|
|
782
982
|
nonce,
|
|
983
|
+
// F084.183 — the redirect the code is BOUND to, the same value /oauth2/token sends.
|
|
984
|
+
// Without it BID bound the code to the client's FIRST registered redirect, and an
|
|
985
|
+
// app served from another of its registered hosts (Jam on jams.party) got
|
|
986
|
+
// «invalid_grant — redirect_uri mismatch» at the exchange.
|
|
987
|
+
redirect_uri: config.redirectUri,
|
|
783
988
|
...input.intent === "signup" ? { intent: "signup", name: input.name, terms_version: input.termsVersion } : {},
|
|
784
989
|
...input.endUserIp ? { end_user_ip: input.endUserIp } : {}
|
|
785
990
|
});
|
|
@@ -866,7 +1071,7 @@ function createSsoClient(config, options = {}) {
|
|
|
866
1071
|
grant_type: "refresh_token",
|
|
867
1072
|
refresh_token: refreshToken,
|
|
868
1073
|
client_id: config.clientId,
|
|
869
|
-
...
|
|
1074
|
+
...await tokenEndpointAuth(token_endpoint)
|
|
870
1075
|
})
|
|
871
1076
|
});
|
|
872
1077
|
const raw = await res.text();
|
|
@@ -972,6 +1177,84 @@ function createSsoClient(config, options = {}) {
|
|
|
972
1177
|
};
|
|
973
1178
|
}
|
|
974
1179
|
|
|
1180
|
+
// src/dpop.ts
|
|
1181
|
+
async function createDpopKey() {
|
|
1182
|
+
const { privateKey, publicKey } = await generateKeyPair("ES256", { extractable: false });
|
|
1183
|
+
const jwk = await exportJWK(publicKey);
|
|
1184
|
+
const publicJwk = { kty: jwk.kty, crv: jwk.crv, x: jwk.x, y: jwk.y };
|
|
1185
|
+
return { privateKey, publicJwk, jkt: await calculateJwkThumbprint(publicJwk) };
|
|
1186
|
+
}
|
|
1187
|
+
var enc2 = new TextEncoder();
|
|
1188
|
+
var b64url3 = (bytes) => btoa(String.fromCharCode(...new Uint8Array(bytes))).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
1189
|
+
async function accessTokenHash(ticket) {
|
|
1190
|
+
return b64url3(await crypto.subtle.digest("SHA-256", enc2.encode(ticket)));
|
|
1191
|
+
}
|
|
1192
|
+
function normalizeHtu(url) {
|
|
1193
|
+
const u = new URL(url);
|
|
1194
|
+
return `${u.protocol}//${u.host}${u.pathname}`;
|
|
1195
|
+
}
|
|
1196
|
+
async function createDpopProof(key, opts) {
|
|
1197
|
+
const payload = {
|
|
1198
|
+
jti: crypto.randomUUID(),
|
|
1199
|
+
htm: opts.method.toUpperCase(),
|
|
1200
|
+
htu: normalizeHtu(opts.url),
|
|
1201
|
+
iat: Math.floor((opts.now?.() ?? Date.now()) / 1e3)
|
|
1202
|
+
};
|
|
1203
|
+
if (opts.ticket !== void 0) payload.ath = await accessTokenHash(opts.ticket);
|
|
1204
|
+
return new SignJWT(payload).setProtectedHeader({ typ: "dpop+jwt", alg: "ES256", jwk: key.publicJwk }).sign(key.privateKey);
|
|
1205
|
+
}
|
|
1206
|
+
function createMemoryReplayStore(now = () => Date.now()) {
|
|
1207
|
+
const entries = /* @__PURE__ */ new Map();
|
|
1208
|
+
return {
|
|
1209
|
+
async seen(key, ttlMs) {
|
|
1210
|
+
const t = now();
|
|
1211
|
+
for (const [k, until] of entries) if (until <= t) entries.delete(k);
|
|
1212
|
+
if (entries.has(key)) return true;
|
|
1213
|
+
entries.set(key, t + ttlMs);
|
|
1214
|
+
return false;
|
|
1215
|
+
}
|
|
1216
|
+
};
|
|
1217
|
+
}
|
|
1218
|
+
var ASYMMETRIC = /* @__PURE__ */ new Set(["ES256", "ES384", "ES512", "EdDSA", "PS256", "PS384", "PS512", "RS256", "RS384", "RS512"]);
|
|
1219
|
+
async function verifyDpopProof(proof, o) {
|
|
1220
|
+
let header;
|
|
1221
|
+
try {
|
|
1222
|
+
header = decodeProtectedHeader(proof);
|
|
1223
|
+
} catch {
|
|
1224
|
+
throw new SsoError("DPoP proof is not a JWS");
|
|
1225
|
+
}
|
|
1226
|
+
if (header.typ !== "dpop+jwt") throw new SsoError(`DPoP proof has typ ${JSON.stringify(header.typ)}, not "dpop+jwt"`);
|
|
1227
|
+
if (!header.alg || !ASYMMETRIC.has(header.alg)) throw new SsoError(`DPoP proof alg ${JSON.stringify(header.alg)} is not an accepted asymmetric algorithm`);
|
|
1228
|
+
const jwk = header.jwk;
|
|
1229
|
+
if (!jwk || typeof jwk !== "object") throw new SsoError("DPoP proof carries no jwk");
|
|
1230
|
+
if ("d" in jwk) throw new SsoError("DPoP proof jwk contains a private key");
|
|
1231
|
+
let payload;
|
|
1232
|
+
try {
|
|
1233
|
+
const key = await importJWK(jwk, header.alg);
|
|
1234
|
+
({ payload } = await jwtVerify(proof, key, { typ: "dpop+jwt", algorithms: [header.alg] }));
|
|
1235
|
+
} catch (e) {
|
|
1236
|
+
throw new SsoError(`DPoP proof signature rejected: ${e instanceof Error ? e.message : String(e)}`);
|
|
1237
|
+
}
|
|
1238
|
+
if (await calculateJwkThumbprint(jwk) !== o.jkt) throw new SsoError("DPoP proof key does not match the ticket's cnf.jkt");
|
|
1239
|
+
if (payload.htm !== o.method.toUpperCase()) throw new SsoError(`DPoP proof is for ${JSON.stringify(payload.htm)}, the request is ${o.method.toUpperCase()}`);
|
|
1240
|
+
let htu;
|
|
1241
|
+
try {
|
|
1242
|
+
htu = normalizeHtu(String(payload.htu));
|
|
1243
|
+
} catch {
|
|
1244
|
+
throw new SsoError("DPoP proof htu is not a URL");
|
|
1245
|
+
}
|
|
1246
|
+
if (htu !== normalizeHtu(o.url)) throw new SsoError(`DPoP proof is for ${htu}, the request is ${normalizeHtu(o.url)}`);
|
|
1247
|
+
const nowSec = Math.floor(o.now() / 1e3);
|
|
1248
|
+
if (typeof payload.iat !== "number" || Math.abs(nowSec - payload.iat) > o.iatWindowSec) {
|
|
1249
|
+
throw new SsoError(`DPoP proof iat is outside \xB1${o.iatWindowSec}s`);
|
|
1250
|
+
}
|
|
1251
|
+
if (payload.ath !== await accessTokenHash(o.ticket)) throw new SsoError("DPoP proof ath does not match this ticket");
|
|
1252
|
+
if (typeof payload.jti !== "string" || payload.jti === "") throw new SsoError("DPoP proof has no jti");
|
|
1253
|
+
if (await o.replay.seen(`${o.jkt}:${payload.jti}`, o.iatWindowSec * 2 * 1e3)) {
|
|
1254
|
+
throw new SsoError("DPoP proof was already used (jti replay)");
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
|
|
975
1258
|
// src/tokens.ts
|
|
976
1259
|
var warned = false;
|
|
977
1260
|
function memoryTokenStore(options = {}) {
|
|
@@ -1019,6 +1302,6 @@ function newSid() {
|
|
|
1019
1302
|
return btoa(s).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
1020
1303
|
}
|
|
1021
1304
|
|
|
1022
|
-
export { ALLOWED_ALGS, AVATAR_TYPES, DEFAULT_SESSION_MAX_AGE, EMBEDDED_FLOW_MAX_AGE, JwksError, JwksUnavailableError, JwksUnknownKeyError, MAX_AVATAR_BYTES, MAX_INVITATIONS, SsoAppApiError, SsoAvatarRejectedError, SsoConfigError, SsoError, SsoInsufficientScopeError, SsoIntrospectionUnavailableError, SsoReauthError, SsoTokenInactiveError, cookieHeader, createJwksCache, createSsoClient, expiresAtFrom, loadSsoConfig, mediaType, memoryTokenStore, newSid, openEmbeddedFlow, readCookie, sealEmbeddedFlow, signSession, signValue, verifySession, verifyValue };
|
|
1023
|
-
//# sourceMappingURL=chunk-
|
|
1024
|
-
//# sourceMappingURL=chunk-
|
|
1305
|
+
export { ALLOWED_ALGS, AVATAR_TYPES, DEFAULT_ISSUER, DEFAULT_SESSION_MAX_AGE, EMBEDDED_FLOW_MAX_AGE, FLY_CLIENT_ASSERTION_TYPE, JwksError, JwksUnavailableError, JwksUnknownKeyError, MAX_AVATAR_BYTES, MAX_INVITATIONS, NoWorkloadIdentityError, SsoAppApiError, SsoAvatarRejectedError, SsoConfigError, SsoError, SsoInsufficientScopeError, SsoIntrospectionUnavailableError, SsoReauthError, SsoTokenInactiveError, TicketExchangeError, TicketUnavailableError, accessTokenHash, cookieHeader, createDpopKey, createDpopProof, createJwksCache, createMemoryReplayStore, createSsoClient, createTicketClient, exchangeUserToken, expiresAtFrom, fetchTicket, loadSsoConfig, mediaType, memoryTokenStore, newSid, normalizeHtu, openEmbeddedFlow, readCookie, sealEmbeddedFlow, signSession, signValue, verifyDpopProof, verifySession, verifyValue };
|
|
1306
|
+
//# sourceMappingURL=chunk-GK3QCKXB.js.map
|
|
1307
|
+
//# sourceMappingURL=chunk-GK3QCKXB.js.map
|