@sparkvault/sdk-js 8.5.0 → 8.6.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.
@@ -1347,14 +1347,25 @@ class VerificationState {
1347
1347
  *
1348
1348
  * The single transfer path: synchronous `window.open` inside the user gesture
1349
1349
  * (so the browser never blocks it), strict `event.origin` + `data.type`
1350
- * validation against the caller-supplied trusted origin, a 60s timeout, and a
1351
- * `window.closed` poll that surfaces a manual close as a cancellation.
1350
+ * validation against the caller-supplied trusted origin, a 30-minute
1351
+ * abandoned-window backstop, and a `window.closed` poll that surfaces a manual
1352
+ * close as a cancellation.
1352
1353
  */
1353
1354
  /** Default popup dimensions — a centered login/ceremony window. */
1354
1355
  const DEFAULT_WIDTH = 480;
1355
1356
  const DEFAULT_HEIGHT = 640;
1356
- /** Default timeout for popup operations (60 seconds). */
1357
- const DEFAULT_TIMEOUT_MS = 60000;
1357
+ /**
1358
+ * Default timeout for popup operations: 30 minutes. This is an
1359
+ * abandoned-window backstop — never a liveness gate. A person mid-login is
1360
+ * often slow for legitimate reasons (waiting out an OTP resend backoff,
1361
+ * fetching their phone for a cross-device passkey, hunting for an alternative
1362
+ * method), and yanking the window away from them mid-flow is far worse than
1363
+ * letting an orphaned promise linger. Active cancellation is handled
1364
+ * elsewhere: the `window.closed` poll catches a manual close within 500ms and
1365
+ * an explicit in-popup cancel posts back immediately; server-side challenge
1366
+ * TTLs (OTP expiry, auth-request lifetime) bound how stale a completion can be.
1367
+ */
1368
+ const DEFAULT_TIMEOUT_MS = 30 * 60 * 1000;
1358
1369
  /** How often to check whether the user closed the popup manually. */
1359
1370
  const CLOSED_POLL_INTERVAL_MS = 500;
1360
1371
  /**
@@ -1443,6 +1454,15 @@ class PopupLauncher {
1443
1454
  /** Passkey popup window dimensions. */
1444
1455
  const POPUP_WIDTH = 450;
1445
1456
  const POPUP_HEIGHT = 500;
1457
+ /**
1458
+ * Device-level hint the Identity API sets on every completed passkey login
1459
+ * (products/product-identity/src/utils/passkey-hint-cookie.js — the cookie
1460
+ * NAME is the wire contract between the two). Host-locked to the Identity
1461
+ * origin and deliberately JS-readable, it means "a passkey login has completed
1462
+ * in this browser", which is what licenses auto-starting the discoverable
1463
+ * ceremony for returning people without ever prompting first-time visitors.
1464
+ */
1465
+ const PASSKEY_HINT_COOKIE = '__Host-authsv_passkey';
1446
1466
  /** `data.type` the passkey popup posts back. */
1447
1467
  const PASSKEY_RESULT_TYPE = 'sparkvault-passkey-result';
1448
1468
  /**
@@ -1503,9 +1523,9 @@ class PasskeyHandler {
1503
1523
  * supported in cross-origin iframes, so the embedded widget uses the inline
1504
1524
  * path.)
1505
1525
  */
1506
- async verify() {
1526
+ async verify(signal) {
1507
1527
  if (this.canRunCeremonyInline()) {
1508
- return this.verifyInline();
1528
+ return this.verifyInline(signal);
1509
1529
  }
1510
1530
  return this.openPasskeyPopup('verify');
1511
1531
  }
@@ -1519,11 +1539,27 @@ class PasskeyHandler {
1519
1539
  && !!navigator.credentials
1520
1540
  && window.location.origin === this.trustedOrigin;
1521
1541
  }
1542
+ /**
1543
+ * True when the discoverable ceremony may be fired WITHOUT a user gesture:
1544
+ * the flow is running on the Identity origin (the inline ceremony needs no
1545
+ * popup, so no gesture) AND this browser carries the passkey-hint cookie —
1546
+ * a passkey login has completed here before. First visits never see an
1547
+ * unprompted system sheet.
1548
+ */
1549
+ canAutoVerify() {
1550
+ if (!this.canRunCeremonyInline())
1551
+ return false;
1552
+ return document.cookie
1553
+ .split(';')
1554
+ .some((part) => part.trim().startsWith(`${PASSKEY_HINT_COOKIE}=1`));
1555
+ }
1522
1556
  /**
1523
1557
  * Run the discoverable verification ceremony directly in this document and
1524
1558
  * map the outcome onto the same PasskeyResult shape the popup produces.
1559
+ * An optional AbortSignal cancels the pending system sheet (the auto-started
1560
+ * ceremony's "Use another method" escape hatch).
1525
1561
  */
1526
- async verifyInline() {
1562
+ async verifyInline(signal) {
1527
1563
  try {
1528
1564
  // When the widget has an identifier, scope the ceremony to that person's
1529
1565
  // passkeys so the authenticator offers only the matching credential;
@@ -1531,6 +1567,7 @@ class PasskeyHandler {
1531
1567
  const identityType = this.state.identityType === 'phone' ? 'phone' : 'email';
1532
1568
  const challenge = await this.api.startPasskeyVerify(this.state.recipient, identityType, this.state.authContext);
1533
1569
  const credential = await navigator.credentials.get({
1570
+ ...(signal ? { signal } : {}),
1534
1571
  publicKey: {
1535
1572
  challenge: base64urlToArrayBuffer(challenge.challenge),
1536
1573
  rpId: challenge.rpId,
@@ -1576,8 +1613,10 @@ class PasskeyHandler {
1576
1613
  * (cancelled, identity locked, no passkey → registration).
1577
1614
  */
1578
1615
  mapInlineCeremonyError(error) {
1579
- // User dismissed the system sheet, or no credential was available.
1580
- if (error instanceof Error && error.name === 'NotAllowedError') {
1616
+ // User dismissed the system sheet or no credential was available
1617
+ // (NotAllowedError), or the caller aborted the pending ceremony via its
1618
+ // AbortSignal (AbortError). Both end the ceremony without a credential.
1619
+ if (error instanceof Error && (error.name === 'NotAllowedError' || error.name === 'AbortError')) {
1581
1620
  return {
1582
1621
  success: false,
1583
1622
  error: 'Authentication was cancelled. Please try again.',
@@ -4184,6 +4223,7 @@ class IdentityInputView {
4184
4223
  this.inputElement = null;
4185
4224
  this.labelElement = null;
4186
4225
  this.submitButton = null;
4226
+ this.passkeyLink = null;
4187
4227
  this.errorContainer = null;
4188
4228
  this.focusTimeoutId = null;
4189
4229
  this.detectedType = 'email';
@@ -4196,6 +4236,7 @@ class IdentityInputView {
4196
4236
  }
4197
4237
  };
4198
4238
  this.boundHandleSubmit = () => this.handleSubmit();
4239
+ this.boundHandlePasskey = props.onPasskey ? () => props.onPasskey() : null;
4199
4240
  }
4200
4241
  get allowsBoth() {
4201
4242
  return this.props.allowedTypes.includes('email') && this.props.allowedTypes.includes('phone');
@@ -4251,6 +4292,13 @@ class IdentityInputView {
4251
4292
  this.submitButton.addEventListener('click', this.boundHandleSubmit);
4252
4293
  formSection.appendChild(this.submitButton);
4253
4294
  container.appendChild(formSection);
4295
+ if (this.boundHandlePasskey) {
4296
+ this.passkeyLink = document.createElement('button');
4297
+ this.passkeyLink.className = 'sv-fallback-link';
4298
+ this.passkeyLink.textContent = 'Sign in with a passkey';
4299
+ this.passkeyLink.addEventListener('click', this.boundHandlePasskey);
4300
+ container.appendChild(this.passkeyLink);
4301
+ }
4254
4302
  this.focusTimeoutId = setTimeout(() => this.inputElement?.focus(), 50);
4255
4303
  return container;
4256
4304
  }
@@ -4394,6 +4442,9 @@ class IdentityInputView {
4394
4442
  if (this.submitButton) {
4395
4443
  this.submitButton.removeEventListener('click', this.boundHandleSubmit);
4396
4444
  }
4445
+ if (this.passkeyLink && this.boundHandlePasskey) {
4446
+ this.passkeyLink.removeEventListener('click', this.boundHandlePasskey);
4447
+ }
4397
4448
  }
4398
4449
  }
4399
4450
 
@@ -5061,6 +5112,55 @@ class PasskeyView {
5061
5112
  }
5062
5113
  }
5063
5114
 
5115
+ /**
5116
+ * Passkey Auto-Verify View
5117
+ *
5118
+ * Shown behind the browser's passkey sheet while an auto-started discoverable
5119
+ * ceremony runs (returning browsers carrying the passkey-hint cookie). The
5120
+ * ceremony was fired without a click, so this screen has no action button —
5121
+ * only a live status and an escape hatch to the identifier form for anyone
5122
+ * who wants a different method (or whose sheet hangs).
5123
+ */
5124
+ class PasskeyAutoView {
5125
+ constructor(props) {
5126
+ this.fallbackLink = null;
5127
+ this.props = props;
5128
+ this.boundHandleFallback = () => this.props.onUseAnotherMethod();
5129
+ }
5130
+ render() {
5131
+ const container = div();
5132
+ const iconContainer = div();
5133
+ iconContainer.appendChild(createPasskeyIcon());
5134
+ const title = document.createElement('h3');
5135
+ title.className = 'sv-title';
5136
+ title.textContent = 'Signing you in';
5137
+ const subtitle = document.createElement('p');
5138
+ subtitle.className = 'sv-subtitle';
5139
+ subtitle.textContent = 'Use your passkey to continue';
5140
+ const spinner = document.createElement('div');
5141
+ spinner.className = 'sv-spinner';
5142
+ spinner.setAttribute('role', 'status');
5143
+ spinner.setAttribute('aria-label', 'Waiting for your passkey');
5144
+ const spinnerRow = div('sv-loading');
5145
+ spinnerRow.appendChild(spinner);
5146
+ this.fallbackLink = document.createElement('button');
5147
+ this.fallbackLink.className = 'sv-fallback-link';
5148
+ this.fallbackLink.textContent = 'Use another method';
5149
+ this.fallbackLink.addEventListener('click', this.boundHandleFallback);
5150
+ container.appendChild(iconContainer);
5151
+ container.appendChild(title);
5152
+ container.appendChild(subtitle);
5153
+ container.appendChild(spinnerRow);
5154
+ container.appendChild(this.fallbackLink);
5155
+ return container;
5156
+ }
5157
+ destroy() {
5158
+ if (this.fallbackLink) {
5159
+ this.fallbackLink.removeEventListener('click', this.boundHandleFallback);
5160
+ }
5161
+ }
5162
+ }
5163
+
5064
5164
  /**
5065
5165
  * Passkey Registration Prompt View
5066
5166
  *
@@ -6036,6 +6136,9 @@ class IdentityRenderer {
6036
6136
  this.currentView = null;
6037
6137
  this.focusTimeoutId = null;
6038
6138
  this.morphTimer = null;
6139
+ // Live discoverable (no-identifier) ceremony. The controller cancels the
6140
+ // pending browser sheet when the person escapes to the identifier form.
6141
+ this.discoverableVerifyAbort = null;
6039
6142
  // Post-login recommendation pipeline: the verified result to finalize with,
6040
6143
  // and the remaining server-recommended steps to walk before finalizing.
6041
6144
  this.postLoginResult = null;
@@ -6247,6 +6350,10 @@ class IdentityRenderer {
6247
6350
  close() {
6248
6351
  // Cancel all pending API requests first
6249
6352
  this.api.abort();
6353
+ // A pending discoverable ceremony would otherwise hold the browser sheet
6354
+ // open against a dialog that no longer exists.
6355
+ this.discoverableVerifyAbort?.abort();
6356
+ this.discoverableVerifyAbort = null;
6250
6357
  if (this.focusTimeoutId !== null) {
6251
6358
  clearTimeout(this.focusTimeoutId);
6252
6359
  this.focusTimeoutId = null;
@@ -6275,6 +6382,58 @@ class IdentityRenderer {
6275
6382
  await this.showMethodSelect();
6276
6383
  return;
6277
6384
  }
6385
+ // Returning browser on the Identity origin (passkey-hint cookie present):
6386
+ // auto-start the discoverable ceremony instead of asking for an identifier
6387
+ // — the Google-style one-touch return. A cancel lands on the identifier
6388
+ // form, which keeps a "Sign in with a passkey" way back in.
6389
+ if (this.canDiscoverableVerify()) {
6390
+ void this.launchDiscoverableVerify();
6391
+ return;
6392
+ }
6393
+ this.showIdentityInput();
6394
+ }
6395
+ /**
6396
+ * True when the discoverable ceremony may be started without a gesture and
6397
+ * this account actually offers passkey login: the flow runs on the Identity
6398
+ * origin, the browser carries the passkey hint, and `passkey` is among the
6399
+ * account's enabled methods.
6400
+ */
6401
+ canDiscoverableVerify() {
6402
+ if (!this.passkeyHandler.canAutoVerify())
6403
+ return false;
6404
+ const methods = enrichMethods(this.sdkConfig?.methods ?? []);
6405
+ return methods.some((m) => m.type === 'passkey');
6406
+ }
6407
+ /**
6408
+ * Run the no-identifier discoverable ceremony behind the passkey-auto view.
6409
+ * Fired on open for returning browsers and from the identifier form's
6410
+ * "Sign in with a passkey" shortcut. Anything short of a terminal outcome
6411
+ * (dismissed sheet, no credential on the device, transient failure) falls
6412
+ * back to the identifier form with no error banner — the person simply
6413
+ * lands where a first-time visitor starts.
6414
+ */
6415
+ async launchDiscoverableVerify() {
6416
+ this.discoverableVerifyAbort = new AbortController();
6417
+ const { signal } = this.discoverableVerifyAbort;
6418
+ this.setState({ view: 'passkey-auto' });
6419
+ const result = await this.passkeyHandler.verify(signal);
6420
+ if (signal.aborted) {
6421
+ // The person already escaped to the identifier form; the aborted
6422
+ // ceremony's result is stale.
6423
+ return;
6424
+ }
6425
+ this.discoverableVerifyAbort = null;
6426
+ if (this.applyTerminalPasskeyResult(result))
6427
+ return;
6428
+ this.showIdentityInput();
6429
+ }
6430
+ /**
6431
+ * Abort the pending discoverable ceremony (dismisses the browser sheet where
6432
+ * the platform allows) and land on the identifier form.
6433
+ */
6434
+ cancelDiscoverableVerify() {
6435
+ this.discoverableVerifyAbort?.abort();
6436
+ this.discoverableVerifyAbort = null;
6278
6437
  this.showIdentityInput();
6279
6438
  }
6280
6439
  render() {
@@ -6385,6 +6544,11 @@ class IdentityRenderer {
6385
6544
  initialValue: this.recipient,
6386
6545
  error: state.error,
6387
6546
  onSubmit: (result) => this.handleIdentitySubmit(result),
6547
+ // Returning browsers keep a one-tap way back into the discoverable
6548
+ // ceremony (e.g. after dismissing the auto-started sheet).
6549
+ onPasskey: this.canDiscoverableVerify()
6550
+ ? () => { void this.launchDiscoverableVerify(); }
6551
+ : undefined,
6388
6552
  });
6389
6553
  case 'method-select':
6390
6554
  return new MethodSelectView({
@@ -6436,6 +6600,10 @@ class IdentityRenderer {
6436
6600
  : undefined,
6437
6601
  });
6438
6602
  }
6603
+ case 'passkey-auto':
6604
+ return new PasskeyAutoView({
6605
+ onUseAnotherMethod: () => this.cancelDiscoverableVerify(),
6606
+ });
6439
6607
  case 'passkey-prompt':
6440
6608
  return new PasskeyPromptView({
6441
6609
  identityLabel: state.recipient,
@@ -6868,27 +7036,42 @@ class IdentityRenderer {
6868
7036
  this.applyPasskeyResult(result, 'verify', fallbackToList);
6869
7037
  }
6870
7038
  /**
6871
- * Apply a passkey ceremony result: complete on success, or route to the
6872
- * matching recovery state. Shared by the passkey view and the direct
6873
- * picker launch so both behave identically.
7039
+ * Handle the terminal passkey outcomes every entry point shares: a verified
7040
+ * login (close + onSuccess + redirect), a blocked-Identity interstitial, or
7041
+ * a locked identity. Returns false for the retryable failures each caller
7042
+ * routes according to its own flow.
6874
7043
  */
6875
- applyPasskeyResult(result, currentMode, fallbackToList = false) {
7044
+ applyTerminalPasskeyResult(result) {
6876
7045
  if (result.success && result.result) {
6877
7046
  this.close();
6878
7047
  this.safeOnSuccess(result.result);
6879
7048
  if (typeof result.result.redirect === 'string' && result.result.redirect) {
6880
7049
  window.location.href = result.result.redirect;
6881
7050
  }
7051
+ return true;
6882
7052
  }
6883
- else if (this.renderBlockedInterstitial(result.errorCode)) ;
6884
- else if (result.errorCode === 'identity_locked') {
7053
+ if (this.renderBlockedInterstitial(result.errorCode)) {
7054
+ // Terminal interstitial already rendered — the owner must subscribe.
7055
+ return true;
7056
+ }
7057
+ if (result.errorCode === 'identity_locked') {
6885
7058
  // Dedicated state — retrying cannot unlock the identity.
6886
7059
  this.setState({
6887
7060
  view: 'error',
6888
7061
  code: 'identity_locked',
6889
7062
  message: 'This SparkVault Identity is locked. Visit auth.sv to manage it.',
6890
7063
  });
7064
+ return true;
6891
7065
  }
7066
+ return false;
7067
+ }
7068
+ /**
7069
+ * Apply a passkey ceremony result: complete on success, or route to the
7070
+ * matching recovery state. Shared by the passkey view and the direct
7071
+ * picker launch so both behave identically.
7072
+ */
7073
+ applyPasskeyResult(result, currentMode, fallbackToList = false) {
7074
+ if (this.applyTerminalPasskeyResult(result)) ;
6892
7075
  else if (fallbackToList && this.passkeyFallbackMethodsAvailable()) {
6893
7076
  // Auto-launched passkey that the person cancelled or that failed: drop to
6894
7077
  // the full method list (passkey still listed) rather than a passkey-only