@atol-sh/js 0.4.1 → 0.4.2

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/dist/index.cjs CHANGED
@@ -4860,6 +4860,9 @@ var ResponseVaultContext = class {
4860
4860
  // src/oidc/response-vault-transport.ts
4861
4861
  var ResponseVaultTransport = class extends ResponseVaultContext {
4862
4862
  async bootstrapClient(authorization, mode = "create") {
4863
+ if (mode === "par_terminal_only" && !authorization.bootstrapAttemptCapability) {
4864
+ throw new NetworkError("response vault: PAR recovery attempt capability is unavailable");
4865
+ }
4863
4866
  const endpoints = await this.endpointRegistry();
4864
4867
  const requestStartedAt = Date.now();
4865
4868
  const response = await this.postJSON(
@@ -4887,7 +4890,7 @@ var ResponseVaultTransport = class extends ResponseVaultContext {
4887
4890
  if (isAuthenticatedTerminalResponse(response, envelope)) {
4888
4891
  throw new ResponseVaultAuthenticatedTerminalError();
4889
4892
  }
4890
- if (mode === "terminal_only" && isAuthenticatedNonterminalResponse(response, envelope)) {
4893
+ if (mode !== "create" && isAuthenticatedNonterminalResponse(response, envelope)) {
4891
4894
  throw new ResponseVaultAuthorizationBusyError();
4892
4895
  }
4893
4896
  if (response.status === 410 || envelope.error === "authorization_terminal") {
@@ -4907,6 +4910,23 @@ var ResponseVaultTransport = class extends ResponseVaultContext {
4907
4910
  }
4908
4911
  throw protocolError(response.status, envelope, "client bootstrap");
4909
4912
  }
4913
+ async probeBootstrapTerminalStatus(authority, possibleCommittedPAR) {
4914
+ if (!possibleCommittedPAR && !authority.bootstrapAttemptCapability) {
4915
+ const challenge = await this.bootstrapClient(authority);
4916
+ if (!("attemptCapability" in challenge)) {
4917
+ throw new NetworkError("response vault: expected a bootstrap attempt challenge");
4918
+ }
4919
+ authority = { ...authority, bootstrapAttemptCapability: challenge.attemptCapability };
4920
+ }
4921
+ try {
4922
+ await this.bootstrapClient(authority, possibleCommittedPAR ? "par_terminal_only" : "terminal_only");
4923
+ } catch (error) {
4924
+ if (error instanceof ResponseVaultAuthenticatedTerminalError) return "terminal";
4925
+ if (error instanceof ResponseVaultAuthorizationBusyError) return "nonterminal";
4926
+ throw error;
4927
+ }
4928
+ throw new NetworkError("response vault: bootstrap terminal status is unavailable");
4929
+ }
4910
4930
  async pushAuthorization(authorization, csrf) {
4911
4931
  if (!authorization.bootstrapTerminalProof) {
4912
4932
  throw new Error(
@@ -5080,6 +5100,26 @@ var ResponseVaultRejection = class extends ResponseVaultTransport {
5080
5100
  throw new ResponseVaultAuthorizationBusyError();
5081
5101
  }
5082
5102
  }
5103
+ async probeTerminalStatus(cancellation) {
5104
+ const response = await this.postJSON(
5105
+ `${this.clientOrigin}${REJECT_PATH}`,
5106
+ {
5107
+ version: RESPONSE_VAULT_PROTOCOL_VERSION,
5108
+ mode: "terminal_only",
5109
+ transaction_id: cancellation.transactionId,
5110
+ client_id: cancellation.binding.clientId,
5111
+ tab_secret: cancellation.tabSecret,
5112
+ cancellation_secret: cancellation.cancellationSecret,
5113
+ terminal_proof: cancellation.terminalProof
5114
+ },
5115
+ "authorization terminal probe",
5116
+ "same-origin"
5117
+ );
5118
+ const envelope = await responseEnvelope(response);
5119
+ if (isAuthenticatedTerminalResponse(response, envelope)) return "terminal";
5120
+ if (isAuthenticatedNonterminalResponse(response, envelope)) return "nonterminal";
5121
+ throw protocolError(response.status, envelope, "authorization terminal probe");
5122
+ }
5083
5123
  async probeTerminal(initial, claim, onExecutionClaimed) {
5084
5124
  if (!initial.terminalProof) {
5085
5125
  throw new Error(
@@ -5092,39 +5132,9 @@ var ResponseVaultRejection = class extends ResponseVaultTransport {
5092
5132
  onExecutionClaimed?.();
5093
5133
  let lastError;
5094
5134
  for (let attempt = 0; attempt < 3; attempt++) {
5095
- let authenticatedTerminal = false;
5096
- let authenticatedNonterminal = false;
5135
+ let outcome;
5097
5136
  try {
5098
- const response = await this.postJSON(
5099
- `${this.clientOrigin}${REJECT_PATH}`,
5100
- {
5101
- version: RESPONSE_VAULT_PROTOCOL_VERSION,
5102
- mode: "terminal_only",
5103
- transaction_id: cancellation.transactionId,
5104
- client_id: cancellation.binding.clientId,
5105
- tab_secret: cancellation.tabSecret,
5106
- cancellation_secret: cancellation.cancellationSecret,
5107
- terminal_proof: cancellation.terminalProof
5108
- },
5109
- "authorization terminal probe",
5110
- "same-origin"
5111
- );
5112
- const envelope = await responseEnvelope(response);
5113
- authenticatedTerminal = isAuthenticatedTerminalResponse(
5114
- response,
5115
- envelope
5116
- );
5117
- authenticatedNonterminal = isAuthenticatedNonterminalResponse(
5118
- response,
5119
- envelope
5120
- );
5121
- if (!authenticatedTerminal && !authenticatedNonterminal) {
5122
- throw protocolError(
5123
- response.status,
5124
- envelope,
5125
- "authorization terminal probe"
5126
- );
5127
- }
5137
+ outcome = await this.probeTerminalStatus(cancellation);
5128
5138
  } catch (error) {
5129
5139
  lastError = error;
5130
5140
  if (attempt < 2) {
@@ -5132,7 +5142,7 @@ var ResponseVaultRejection = class extends ResponseVaultTransport {
5132
5142
  }
5133
5143
  continue;
5134
5144
  }
5135
- if (authenticatedTerminal) {
5145
+ if (outcome === "terminal") {
5136
5146
  try {
5137
5147
  await discardResponseVaultCancellation(cancellation);
5138
5148
  } catch (error) {
@@ -5338,6 +5348,84 @@ var ResponseVaultRejection = class extends ResponseVaultTransport {
5338
5348
  }
5339
5349
  };
5340
5350
 
5351
+ // src/oidc/response-vault-storage-peer-recovery.ts
5352
+ function isRejectionCustody(status) {
5353
+ return status === "rejecting" || status === "reject_retry" || status === "reject_blocked";
5354
+ }
5355
+ function isExpiredPeerRecoveryCandidate(custody) {
5356
+ const mode = "dpopKey" in custody ? custody.mode : custody.sourceMode;
5357
+ return custody.expiresAt <= Date.now() && (mode === "interactive" || isRejectionCustody(custody.status));
5358
+ }
5359
+ async function checkExpiredPeerCustody(transaction, binding, expected, requester) {
5360
+ const key = slotKey(binding);
5361
+ const slot = await requestResult(
5362
+ transaction.objectStore(RESPONSE_VAULT_SLOT_STORE).get(key),
5363
+ "expired peer slot read"
5364
+ );
5365
+ const stored = await requestResult(
5366
+ transaction.objectStore(RESPONSE_VAULT_RECORD_STORE).get(expected.reference.id),
5367
+ "expired peer custody read"
5368
+ );
5369
+ const now = Date.now();
5370
+ assertCredentialGenerationSnapshot(binding, requester);
5371
+ const authority = currentCredentialAuthority(binding);
5372
+ if (authority.signedOut || authority.generation !== requester.logout || expected.credentialGeneration.logout !== requester.logout || expected.credentialGeneration.tabOwner === requester.tabOwner || expected.expiresAt > now || !validSlotRecord(slot, key) || slot.referenceId !== expected.reference.id || slot.expiresAt !== expected.expiresAt) {
5373
+ throw new ResponseVaultExecutionBusyError();
5374
+ }
5375
+ const validatePeer = (record) => {
5376
+ assertCustodyRevision(expected, record.revision);
5377
+ if (record.status !== expected.status || record.expiresAt !== expected.expiresAt || record.credentialGeneration.logout !== expected.credentialGeneration.logout || record.credentialGeneration.tabOwner !== expected.credentialGeneration.tabOwner) {
5378
+ throw new ResponseVaultExecutionBusyError();
5379
+ }
5380
+ };
5381
+ if (expected.status === "prepared" || expected.status === "par_pending") {
5382
+ const record = stored;
5383
+ assertRecordBinding(record, expected.reference, binding, now, false, true);
5384
+ validatePeer(record);
5385
+ if (record.mode !== "interactive") throw new ResponseVaultExecutionBusyError();
5386
+ return {
5387
+ kind: record.status === "par_pending" ? "par" : "bootstrap",
5388
+ authority: {
5389
+ tabSecret: record.tabSecret,
5390
+ cancellationSecret: record.cancellationSecret,
5391
+ parIdempotencyKey: record.parIdempotencyKey,
5392
+ bootstrapAttemptCapability: record.bootstrapAttemptCapability
5393
+ }
5394
+ };
5395
+ }
5396
+ assertCancellationBinding(stored, expected.reference, binding, now, false, true);
5397
+ validatePeer(stored);
5398
+ if (stored.custody === "cancellation" ? !isRejectionCustody(stored.status) : stored.mode !== "interactive") {
5399
+ throw new ResponseVaultExecutionBusyError();
5400
+ }
5401
+ return { kind: "transaction", authority: publicCancellation(stored, expected.lifecycleEpoch) };
5402
+ }
5403
+ async function recoverExpiredPeerAuthorization(binding, expected, requester, probe) {
5404
+ const owner = randomOpaque();
5405
+ await acquireExecutionLock(expected.reference, owner, false);
5406
+ try {
5407
+ const database = await openDB();
5408
+ try {
5409
+ const read = database.transaction([RESPONSE_VAULT_RECORD_STORE, RESPONSE_VAULT_SLOT_STORE], "readonly");
5410
+ const candidate = await checkExpiredPeerCustody(read, binding, expected, requester);
5411
+ await transactionDone(read, "expired peer custody read");
5412
+ if (await probe(candidate) !== "terminal") {
5413
+ return false;
5414
+ }
5415
+ const discard = database.transaction([RESPONSE_VAULT_RECORD_STORE, RESPONSE_VAULT_SLOT_STORE], "readwrite");
5416
+ await checkExpiredPeerCustody(discard, binding, expected, requester);
5417
+ discard.objectStore(RESPONSE_VAULT_SLOT_STORE).delete(slotKey(binding));
5418
+ discard.objectStore(RESPONSE_VAULT_RECORD_STORE).delete(expected.reference.id);
5419
+ await transactionDone(discard, "terminal peer custody discard");
5420
+ return true;
5421
+ } finally {
5422
+ database.close();
5423
+ }
5424
+ } finally {
5425
+ await releaseOwnedExecutionLockIfHeld(expected.reference, owner);
5426
+ }
5427
+ }
5428
+
5341
5429
  // src/oidc/response-vault-custody.ts
5342
5430
  var ResponseVaultCustodyLayer = class extends ResponseVaultRejection {
5343
5431
  invalidateLifecycle(rotateLogout = false) {
@@ -5664,6 +5752,18 @@ var ResponseVaultCustodyLayer = class extends ResponseVaultRejection {
5664
5752
  const currentGeneration = credentialGenerationSnapshot(this.config);
5665
5753
  const rejectionOwner = authorization.executionClaim?.owner ?? active.rejectionOwner ?? randomOpaque2();
5666
5754
  if (!bindingMatches || authorization.credentialGeneration.tabOwner !== currentGeneration.tabOwner) {
5755
+ if (bindingMatches && isExpiredPeerRecoveryCandidate(authorization) && authorization.credentialGeneration.logout === currentGeneration.logout && abandonedCleanupError === void 0) {
5756
+ const recovered = await recoverExpiredPeerAuthorization(
5757
+ this.config,
5758
+ authorization,
5759
+ currentGeneration,
5760
+ (candidate) => candidate.kind === "transaction" ? this.probeTerminalStatus(candidate.authority) : this.probeBootstrapTerminalStatus(candidate.authority, candidate.kind === "par")
5761
+ );
5762
+ if (recovered) {
5763
+ this.clearActive(authorization.reference);
5764
+ return null;
5765
+ }
5766
+ }
5667
5767
  failAfterCleanup(new ResponseVaultAuthorizationBusyError());
5668
5768
  }
5669
5769
  try {
@@ -5762,7 +5862,10 @@ var ResponseVaultCustodyLayer = class extends ResponseVaultRejection {
5762
5862
  );
5763
5863
  onExecutionClaimed?.();
5764
5864
  try {
5765
- await this.bootstrapClient(claimed, "terminal_only");
5865
+ await this.bootstrapClient(
5866
+ claimed,
5867
+ claimed.status === "par_pending" ? "par_terminal_only" : "terminal_only"
5868
+ );
5766
5869
  throw new Error(
5767
5870
  "response vault: terminal bootstrap classification returned"
5768
5871
  );
@@ -6471,6 +6574,15 @@ var OrdinaryAuthorizationPendingError = class extends Error {
6471
6574
  this.name = "OrdinaryAuthorizationPendingError";
6472
6575
  }
6473
6576
  };
6577
+ var OrdinaryAuthorizationIntentConflictError = class extends Error {
6578
+ constructor(pendingMode) {
6579
+ super(
6580
+ "An earlier sign-in is unfinished. Retry that sign-in, or sign in from a new tab."
6581
+ );
6582
+ this.pendingMode = pendingMode;
6583
+ this.name = "OrdinaryAuthorizationIntentConflictError";
6584
+ }
6585
+ };
6474
6586
  function validCanonicalSHA256(value) {
6475
6587
  if (!CANONICAL_SHA256.test(value)) return false;
6476
6588
  try {
@@ -7505,7 +7617,7 @@ function createBrowserAuthActions(runtime) {
7505
7617
  });
7506
7618
  }
7507
7619
  async function selectOrganization(organizationId) {
7508
- if (!CANONICAL_ULID.test(organizationId)) {
7620
+ if (organizationId !== null && (typeof organizationId !== "string" || !CANONICAL_ULID.test(organizationId))) {
7509
7621
  throw new Error(
7510
7622
  "organizationId must be a canonical ULID"
7511
7623
  );
@@ -7524,16 +7636,17 @@ function createBrowserAuthActions(runtime) {
7524
7636
  throw new StaleCredentialTransactionError();
7525
7637
  }
7526
7638
  const selectionEpoch = ++runtime.credentialEpoch;
7639
+ const expectedOrganizationId = organizationId ?? void 0;
7527
7640
  runtime.sessionOps.clearRenewTimer();
7528
7641
  try {
7529
7642
  const grant = await runtime.renewalOps.renewViaIframe(
7530
- organizationId,
7643
+ expectedOrganizationId,
7531
7644
  selectionGeneration,
7532
7645
  true
7533
7646
  );
7534
7647
  await runtime.credentialOps.applyAuthorizationGrant(grant, {
7535
7648
  epoch: selectionEpoch,
7536
- expectedOrganizationId: organizationId,
7649
+ expectedOrganizationId,
7537
7650
  enforceExpectedOrganization: true,
7538
7651
  preserveExistingTokens: false,
7539
7652
  expectedNonce: grant.nonce,
@@ -7659,10 +7772,8 @@ function createBrowserAuthActions(runtime) {
7659
7772
  return operation;
7660
7773
  }
7661
7774
  async function cancelLoginOnce() {
7662
- if (!runtime.responseVault) {
7663
- throw new Error("cancelLogin requires response-vault authorization");
7664
- }
7665
- const recoverableFailedInitialization = runtime.initPromise !== null && runtime.initRejected && runtime.failedInitCancellationAuthority !== null && !runtime.lifecycleOps.isCallbackUrl() && !runtime.lifecycleOps.isPostLogoutCallbackUrl();
7775
+ const responseVault = runtime.responseVault;
7776
+ const recoverableFailedInitialization = responseVault !== null && runtime.initPromise !== null && runtime.initRejected && runtime.failedInitCancellationAuthority !== null && !runtime.lifecycleOps.isCallbackUrl() && !runtime.lifecycleOps.isPostLogoutCallbackUrl();
7666
7777
  if (runtime.initPromise !== null && !recoverableFailedInitialization || runtime.completeLoginPromise !== null || runtime.currentUser !== null || runtime.accessToken !== void 0 || runtime.currentIdToken !== void 0 || runtime.refreshToken !== void 0) {
7667
7778
  throw new Error(
7668
7779
  "cancelLogin requires an uninitialized signed-out client"
@@ -7671,17 +7782,25 @@ function createBrowserAuthActions(runtime) {
7671
7782
  if (runtime.destroyed) {
7672
7783
  throw new Error("auth client has been destroyed");
7673
7784
  }
7785
+ if (!responseVault) {
7786
+ const generation = credentialGenerationSnapshot(
7787
+ runtime.credentialGenerationBinding
7788
+ );
7789
+ await runtime.ordinaryAuthorization.invalidateCurrent(generation);
7790
+ runtime.sessionOps.assertCredentialAuthority(generation);
7791
+ return;
7792
+ }
7674
7793
  const markCleanupOwned = () => {
7675
7794
  runtime.destroyAuthorizationCleanupStarted = true;
7676
7795
  };
7677
7796
  if (recoverableFailedInitialization) {
7678
7797
  await cancelResponseVaultFailedInitialization(
7679
- runtime.responseVault,
7798
+ responseVault,
7680
7799
  runtime.failedInitCancellationAuthority,
7681
7800
  markCleanupOwned
7682
7801
  );
7683
7802
  } else {
7684
- await runtime.responseVault.cancelLogin(markCleanupOwned);
7803
+ await responseVault.cancelLogin(markCleanupOwned);
7685
7804
  }
7686
7805
  if (recoverableFailedInitialization) {
7687
7806
  runtime.initPromise = null;
@@ -8432,51 +8551,6 @@ async function settleOrdinaryAuthorization(authority, mode) {
8432
8551
  }
8433
8552
  }
8434
8553
 
8435
- // src/oidc/ordinary-authorization-pre-navigation.ts
8436
- function assertSubmittedCancellation(record) {
8437
- if (record.preNavigationCancellationEndpoint !== `${record.issuer}/auth/par/pre-navigation-cancellation` || record.parPhase !== "submitted" || record.requestUri !== null || record.terminalProof !== null || record.rootExpiresAt !== null || record.rootExpiresAtMs !== null || record.custodyExpiresAtMs !== null || record.rejectionRequired || record.cleanupStarted) {
8438
- throw new Error(
8439
- "ordinary browser authorization: pre-navigation cancellation authority is invalid"
8440
- );
8441
- }
8442
- }
8443
- async function cancelSubmittedOrdinaryAuthorization(record) {
8444
- assertSubmittedCancellation(record);
8445
- const controller = new AbortController();
8446
- const timeout = setTimeout(
8447
- () => controller.abort(),
8448
- SETTLEMENT_TIMEOUT_MS
8449
- );
8450
- try {
8451
- let response;
8452
- try {
8453
- response = await fetch(
8454
- record.preNavigationCancellationEndpoint,
8455
- {
8456
- method: "POST",
8457
- headers: {
8458
- Accept: "application/json",
8459
- "Content-Type": "application/x-www-form-urlencoded"
8460
- },
8461
- body: ordinaryAuthorizationPARBody(record),
8462
- cache: "no-store",
8463
- credentials: "omit",
8464
- redirect: "error",
8465
- referrerPolicy: "no-referrer",
8466
- signal: controller.signal
8467
- }
8468
- );
8469
- } catch {
8470
- throw new OrdinaryAuthorizationPendingError();
8471
- }
8472
- if (response.status !== 204 || response.headers.get("cache-control") !== "no-store" || !hasAuthenticatedOrdinaryAuthorizationSignal(response) || response.headers.get("content-type") !== null || response.body !== null) {
8473
- throw new OrdinaryAuthorizationPendingError();
8474
- }
8475
- } finally {
8476
- clearTimeout(timeout);
8477
- }
8478
- }
8479
-
8480
8554
  // src/oidc/ordinary-authorization-database.ts
8481
8555
  function openOrdinaryAuthorizationDB() {
8482
8556
  return new Promise((resolve, reject) => {
@@ -9290,6 +9364,117 @@ async function deleteOrdinaryAuthorizationRecordRevision(expected) {
9290
9364
  }
9291
9365
  }
9292
9366
 
9367
+ // src/oidc/ordinary-authorization-transport.ts
9368
+ function validOrdinaryPARResponseHeaders(response) {
9369
+ return response.headers.get("cache-control") === "no-store" && response.headers.get("content-type") === "application/json" && response.headers.get("atol-browser-authorization") === "v2";
9370
+ }
9371
+ async function postOrdinaryPAR(record, signal) {
9372
+ const startedAt = Date.now();
9373
+ try {
9374
+ const response = await fetch(record.pushedAuthorizationRequestEndpoint, {
9375
+ method: "POST",
9376
+ headers: {
9377
+ Accept: "application/json",
9378
+ "Content-Type": "application/x-www-form-urlencoded"
9379
+ },
9380
+ body: ordinaryAuthorizationPARBody(record),
9381
+ cache: "no-store",
9382
+ credentials: "omit",
9383
+ redirect: "error",
9384
+ referrerPolicy: "no-referrer",
9385
+ signal
9386
+ });
9387
+ return { response, startedAt };
9388
+ } catch {
9389
+ throw new OrdinaryAuthorizationPendingError();
9390
+ }
9391
+ }
9392
+ async function bindOrdinaryPARChallenge(record, response, startedAt, signal) {
9393
+ if (response.status !== 428 || !validOrdinaryPARResponseHeaders(response)) {
9394
+ throw new OrdinaryAuthorizationPendingError();
9395
+ }
9396
+ let challengeText;
9397
+ try {
9398
+ challengeText = await readBoundedParResponse(response, signal);
9399
+ } catch {
9400
+ throw new OrdinaryAuthorizationPendingError();
9401
+ }
9402
+ const challenge = ordinaryAuthorizationAttemptChallenge(
9403
+ record,
9404
+ challengeText,
9405
+ startedAt
9406
+ );
9407
+ const bound = await bindOrdinaryAuthorizationAttemptCapability(
9408
+ record,
9409
+ challenge.attemptCapability,
9410
+ challenge.attemptExpiresAt
9411
+ );
9412
+ assertOrdinaryAuthorizationRecord(
9413
+ bound,
9414
+ record,
9415
+ record.credentialGeneration,
9416
+ record.intent
9417
+ );
9418
+ await assertOrdinaryAuthorizationRecordState(bound);
9419
+ return bound;
9420
+ }
9421
+
9422
+ // src/oidc/ordinary-authorization-pre-navigation.ts
9423
+ function assertSubmittedCancellation(record) {
9424
+ if (record.preNavigationCancellationEndpoint !== `${record.issuer}/auth/par/pre-navigation-cancellation` || record.parPhase !== "submitted" || record.requestUri !== null || record.terminalProof !== null || record.rootExpiresAt !== null || record.rootExpiresAtMs !== null || record.custodyExpiresAtMs !== null || record.rejectionRequired || record.cleanupStarted) {
9425
+ throw new Error(
9426
+ "ordinary browser authorization: pre-navigation cancellation authority is invalid"
9427
+ );
9428
+ }
9429
+ }
9430
+ async function cancelSubmittedOrdinaryAuthorization(record) {
9431
+ assertSubmittedCancellation(record);
9432
+ const controller = new AbortController();
9433
+ const timeout = setTimeout(
9434
+ () => controller.abort(),
9435
+ SETTLEMENT_TIMEOUT_MS
9436
+ );
9437
+ try {
9438
+ let submitted = record;
9439
+ if (record.attemptExpiresAtMs === null) {
9440
+ const challenge = await postOrdinaryPAR(record, controller.signal);
9441
+ submitted = await bindOrdinaryPARChallenge(
9442
+ record,
9443
+ challenge.response,
9444
+ challenge.startedAt,
9445
+ controller.signal
9446
+ );
9447
+ }
9448
+ let response;
9449
+ try {
9450
+ response = await fetch(
9451
+ submitted.preNavigationCancellationEndpoint,
9452
+ {
9453
+ method: "POST",
9454
+ headers: {
9455
+ Accept: "application/json",
9456
+ "Content-Type": "application/x-www-form-urlencoded"
9457
+ },
9458
+ body: ordinaryAuthorizationPARBody(submitted),
9459
+ cache: "no-store",
9460
+ credentials: "omit",
9461
+ redirect: "error",
9462
+ referrerPolicy: "no-referrer",
9463
+ signal: controller.signal
9464
+ }
9465
+ );
9466
+ } catch {
9467
+ throw new OrdinaryAuthorizationPendingError();
9468
+ }
9469
+ if (response.status !== 204 || response.headers.get("cache-control") !== "no-store" || !hasAuthenticatedOrdinaryAuthorizationSignal(response) || response.headers.get("content-type") !== null || response.body !== null) {
9470
+ throw new OrdinaryAuthorizationPendingError();
9471
+ }
9472
+ return submitted;
9473
+ } finally {
9474
+ clearTimeout(timeout);
9475
+ }
9476
+ }
9477
+
9293
9478
  // src/oidc/ordinary-authorization-cleanup.ts
9294
9479
  var OrdinaryAuthorizationCleanupController = class {
9295
9480
  constructor(manager, binding, storageKey) {
@@ -9363,8 +9548,8 @@ var OrdinaryAuthorizationCleanupController = class {
9363
9548
  await this.startAndFinishCleanup(rejecting);
9364
9549
  }
9365
9550
  async rejectSubmittedAndCleanup(record) {
9366
- await cancelSubmittedOrdinaryAuthorization(record);
9367
- await this.startAndFinishCleanup(record);
9551
+ const settled = await cancelSubmittedOrdinaryAuthorization(record);
9552
+ await this.startAndFinishCleanup(settled);
9368
9553
  }
9369
9554
  async terminalOnlyAndCleanup(record) {
9370
9555
  this.assertExactPointer(record);
@@ -9395,6 +9580,7 @@ var OrdinaryAuthorizationCleanupController = class {
9395
9580
  }
9396
9581
  return;
9397
9582
  }
9583
+ if (pointer) this.assertExactPointer(record);
9398
9584
  assertOrdinaryAuthorizationRecord(
9399
9585
  record,
9400
9586
  this.binding,
@@ -9850,12 +10036,13 @@ var OrdinaryAuthorizationAdmissionTerminalError = class extends Error {
9850
10036
  }
9851
10037
  };
9852
10038
  async function rejectUncommittedAdmission(submitted) {
10039
+ let settled;
9853
10040
  try {
9854
- await cancelSubmittedOrdinaryAuthorization(submitted);
10041
+ settled = await cancelSubmittedOrdinaryAuthorization(submitted);
9855
10042
  } catch {
9856
10043
  return null;
9857
10044
  }
9858
- return startOrdinaryAuthorizationCleanup(submitted);
10045
+ return startOrdinaryAuthorizationCleanup(settled);
9859
10046
  }
9860
10047
  async function rejectOrRetainInvalidAdmission(submitted) {
9861
10048
  const cleaning = await rejectUncommittedAdmission(submitted);
@@ -9864,9 +10051,6 @@ async function rejectOrRetainInvalidAdmission(submitted) {
9864
10051
  }
9865
10052
  throw new OrdinaryAuthorizationPendingError();
9866
10053
  }
9867
- function validPARResponseHeaders(response) {
9868
- return response.headers.get("cache-control") === "no-store" && response.headers.get("content-type") === "application/json" && response.headers.get("atol-browser-authorization") === "v2";
9869
- }
9870
10054
  async function admitOrdinaryAuthorization(record) {
9871
10055
  if (ordinaryAuthorizationRecordExpired(record)) {
9872
10056
  if (record.parPhase === "submitted") {
@@ -9894,89 +10078,21 @@ async function admitOrdinaryAuthorization(record) {
9894
10078
  PAR_TIMEOUT_MS
9895
10079
  );
9896
10080
  try {
9897
- let response;
9898
- let requestStartedAt = Date.now();
9899
- try {
9900
- response = await fetch(
9901
- submitted.pushedAuthorizationRequestEndpoint,
9902
- {
9903
- method: "POST",
9904
- headers: {
9905
- Accept: "application/json",
9906
- "Content-Type": "application/x-www-form-urlencoded"
9907
- },
9908
- body: ordinaryAuthorizationPARBody(submitted),
9909
- cache: "no-store",
9910
- credentials: "omit",
9911
- redirect: "error",
9912
- referrerPolicy: "no-referrer",
9913
- signal: controller.signal
9914
- }
9915
- );
9916
- } catch {
9917
- throw new OrdinaryAuthorizationPendingError();
9918
- }
10081
+ const first = await postOrdinaryPAR(submitted, controller.signal);
10082
+ let response = first.response;
9919
10083
  if (response.status === 428) {
9920
- if (!validPARResponseHeaders(response)) {
9921
- throw new OrdinaryAuthorizationPendingError();
9922
- }
9923
- let challengeText;
9924
- try {
9925
- challengeText = await readBoundedParResponse(
9926
- response,
9927
- controller.signal
9928
- );
9929
- } catch {
9930
- throw new OrdinaryAuthorizationPendingError();
9931
- }
9932
- const challenge = ordinaryAuthorizationAttemptChallenge(
9933
- submitted,
9934
- challengeText,
9935
- requestStartedAt
9936
- );
9937
- submitted = await bindOrdinaryAuthorizationAttemptCapability(
10084
+ submitted = await bindOrdinaryPARChallenge(
9938
10085
  submitted,
9939
- challenge.attemptCapability,
9940
- challenge.attemptExpiresAt
9941
- );
9942
- assertOrdinaryAuthorizationRecord(
9943
- submitted,
9944
- {
9945
- issuer: record.issuer,
9946
- clientId: record.clientId,
9947
- redirectUri: record.redirectUri,
9948
- scope: record.scope
9949
- },
9950
- record.credentialGeneration,
9951
- record.intent
10086
+ response,
10087
+ first.startedAt,
10088
+ controller.signal
9952
10089
  );
9953
- await assertOrdinaryAuthorizationRecordState(submitted);
9954
- requestStartedAt = Date.now();
9955
- try {
9956
- response = await fetch(
9957
- submitted.pushedAuthorizationRequestEndpoint,
9958
- {
9959
- method: "POST",
9960
- headers: {
9961
- Accept: "application/json",
9962
- "Content-Type": "application/x-www-form-urlencoded"
9963
- },
9964
- body: ordinaryAuthorizationPARBody(submitted),
9965
- cache: "no-store",
9966
- credentials: "omit",
9967
- redirect: "error",
9968
- referrerPolicy: "no-referrer",
9969
- signal: controller.signal
9970
- }
9971
- );
9972
- } catch {
9973
- throw new OrdinaryAuthorizationPendingError();
9974
- }
10090
+ response = (await postOrdinaryPAR(submitted, controller.signal)).response;
9975
10091
  }
9976
10092
  if (response.status !== 201) {
9977
10093
  throw new OrdinaryAuthorizationPendingError();
9978
10094
  }
9979
- if (!validPARResponseHeaders(response)) {
10095
+ if (!validOrdinaryPARResponseHeaders(response)) {
9980
10096
  return rejectOrRetainInvalidAdmission(submitted);
9981
10097
  }
9982
10098
  let responseText;
@@ -10257,9 +10373,15 @@ var OrdinaryBrowserAuthorization = class {
10257
10373
  record,
10258
10374
  this.binding,
10259
10375
  generation,
10260
- intent
10376
+ record.intent
10261
10377
  );
10262
10378
  await assertOrdinaryAuthorizationRecordState(record);
10379
+ if (record.intent !== intent) {
10380
+ const pending = parseOrdinaryAuthorizationIntent(record.intent);
10381
+ throw new OrdinaryAuthorizationIntentConflictError(
10382
+ pending.prompt === "none" ? "silent" : "interactive"
10383
+ );
10384
+ }
10263
10385
  if (record.cleanupStarted) {
10264
10386
  await this.callbacks.finishCleanup(record);
10265
10387
  return null;
@@ -13623,6 +13745,8 @@ exports.KEKVault = KEKVault;
13623
13745
  exports.LoginRequiredError = LoginRequiredError;
13624
13746
  exports.NetworkError = NetworkError;
13625
13747
  exports.OAuthProtocolError = OAuthProtocolError;
13748
+ exports.OrdinaryAuthorizationIntentConflictError = OrdinaryAuthorizationIntentConflictError;
13749
+ exports.OrdinaryAuthorizationPendingError = OrdinaryAuthorizationPendingError;
13626
13750
  exports.RESPONSE_VAULT_PROTOCOL_VERSION = RESPONSE_VAULT_PROTOCOL_VERSION;
13627
13751
  exports.RecoveryAction = RecoveryAction;
13628
13752
  exports._resetDiscoveryCache = _resetDiscoveryCache;