@sparkvault/sdk-js 1.27.6 → 2.0.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.
@@ -98,24 +98,6 @@ Object.freeze([
98
98
  ...SPARKVAULT_PLATFORM_SV_HOSTS
99
99
  ]);
100
100
 
101
- function normalizeHostname(hostname) {
102
- if (typeof hostname !== 'string') return '';
103
- return hostname.trim().toLowerCase().replace(/\.$/, '');
104
- }
105
-
106
- function isSparkVaultRootOrSubdomain(hostname) {
107
- const normalized = normalizeHostname(hostname);
108
- return normalized === SPARKVAULT_ROOT_DOMAIN || normalized.endsWith(`.${SPARKVAULT_ROOT_DOMAIN}`);
109
- }
110
-
111
- function isSparkVaultPlatformSvHost(hostname) {
112
- return SPARKVAULT_PLATFORM_SV_HOSTS.includes(normalizeHostname(hostname));
113
- }
114
-
115
- function isSparkVaultOwnedHostname(hostname) {
116
- return isSparkVaultRootOrSubdomain(hostname) || isSparkVaultPlatformSvHost(hostname);
117
- }
118
-
119
101
  /**
120
102
  * SparkVault SDK Configuration
121
103
  */
@@ -1537,17 +1519,19 @@ class PasskeyHandler {
1537
1519
  const config = api.config;
1538
1520
  this.baseUrl = config.identityBaseUrl;
1539
1521
  this.accountId = config.accountId;
1522
+ this.trustedOrigin = new URL(this.baseUrl).origin;
1540
1523
  }
1541
1524
  /**
1542
- * Check if the current identifier resolves to an SVID with registered passkeys.
1543
- * @returns Check result with emailValid and hasPasskey, or null on error
1525
+ * Resolve the current identifier (email or phone) to its SVID and report
1526
+ * whether that SVID has any registered passkeys.
1527
+ * @returns Check result with identityValid and hasPasskey, or null on error
1544
1528
  */
1545
1529
  async checkPasskey() {
1546
1530
  try {
1547
1531
  const response = await this.api.checkPasskey(this.state.recipient, this.state.identityType);
1548
1532
  this.state.setPasskeyStatus(response.has_passkey);
1549
1533
  return {
1550
- emailValid: response.identity_valid,
1534
+ identityValid: response.identity_valid,
1551
1535
  hasPasskey: response.has_passkey,
1552
1536
  };
1553
1537
  }
@@ -1686,16 +1670,13 @@ class PasskeyHandler {
1686
1670
  });
1687
1671
  }
1688
1672
  /**
1689
- * Validate that a message origin is from sparkvault.com domain
1673
+ * Validate that a postMessage came from the identity service's own
1674
+ * origin. We do not accept the broader sparkvault.com family — a popup
1675
+ * is only opened against `this.baseUrl`, so any other origin is
1676
+ * either an unrelated tab or a forgery attempt.
1690
1677
  */
1691
1678
  isValidMessageOrigin(origin) {
1692
- try {
1693
- const url = new URL(origin);
1694
- return url.protocol === 'https:' && url.origin === origin && !url.port && isSparkVaultOwnedHostname(url.hostname);
1695
- }
1696
- catch {
1697
- return false;
1698
- }
1679
+ return origin === this.trustedOrigin;
1699
1680
  }
1700
1681
  /**
1701
1682
  * Handle popup error response
@@ -1824,11 +1805,11 @@ class TotpHandler {
1824
1805
  recipient: this.state.recipient,
1825
1806
  authContext: this.state.authContext,
1826
1807
  });
1827
- if (response.verified && response.token) {
1808
+ if (response.verified && (response.token || response.redirect)) {
1828
1809
  return {
1829
1810
  success: true,
1830
1811
  result: {
1831
- token: response.token,
1812
+ token: response.token ?? '',
1832
1813
  identity: response.identity ?? this.state.recipient,
1833
1814
  identityType: response.identity_type ?? this.state.identityType,
1834
1815
  svid: response.svid,
@@ -5572,9 +5553,12 @@ class IdentityRenderer {
5572
5553
  onBack: isFromPrompt
5573
5554
  ? () => this.handlePasskeyRegistrationSkip()
5574
5555
  : state.mode === 'verify'
5575
- // From passkey verify, enable fallback so showMethodSelect doesn't loop back
5556
+ // Back returns to the picker with passkey still listed.
5557
+ // autoRoute=false prevents the auto-jump from sending the
5558
+ // user straight back to this view. If no fallback methods
5559
+ // exist at all, fall through to identity-input.
5576
5560
  ? this.passkeyFallbackMethodsAvailable()
5577
- ? () => this.handlePasskeyFallback()
5561
+ ? () => this.showMethodSelect({ autoRoute: false })
5578
5562
  : this.recipient
5579
5563
  ? () => this.showIdentityInput()
5580
5564
  : undefined
@@ -5623,7 +5607,14 @@ class IdentityRenderer {
5623
5607
  showIdentityInput(error) {
5624
5608
  this.setState({ view: 'identity-input', error });
5625
5609
  }
5626
- async showMethodSelect() {
5610
+ /**
5611
+ * Render the picker. With `autoRoute` left default, a recipient that
5612
+ * already has a passkey is sent straight to the WebAuthn view; pass
5613
+ * `false` from the passkey view's back arrow so the user lands on the
5614
+ * actual list of alternatives instead of bouncing back to passkey.
5615
+ */
5616
+ async showMethodSelect(options = {}) {
5617
+ const autoRoute = options.autoRoute !== false;
5627
5618
  if (!this.sdkConfig)
5628
5619
  return;
5629
5620
  // Defensive: ensure methods is always an array
@@ -5648,6 +5639,17 @@ class IdentityRenderer {
5648
5639
  // Email users can use email-based methods
5649
5640
  return m.identityType === 'email' || m.identityType === undefined;
5650
5641
  });
5642
+ // Resolve identifier → SVID → has_passkey exactly once per flow. The
5643
+ // result drives both the picker (hide Passkey when there is none) and
5644
+ // the post-verify "add a passkey?" prompt (skip when there already is
5645
+ // one). Cached on verificationState; later reads short-circuit.
5646
+ const { passkey } = this.verificationState;
5647
+ if (passkey.hasPasskey === null && methods.some((m) => m.type === 'passkey')) {
5648
+ await this.passkeyHandler.checkPasskey();
5649
+ }
5650
+ if (this.verificationState.passkey.hasPasskey === false) {
5651
+ methods = methods.filter((m) => m.type !== 'passkey');
5652
+ }
5651
5653
  }
5652
5654
  else {
5653
5655
  // No PII prompt is needed when only SVID-routed methods are enabled.
@@ -5664,6 +5666,19 @@ class IdentityRenderer {
5664
5666
  });
5665
5667
  return;
5666
5668
  }
5669
+ // When a passkey is already registered for this identifier, jump
5670
+ // straight to the WebAuthn ceremony instead of asking the user to
5671
+ // pick a method — it is the safest, fastest option. The back arrow on
5672
+ // the passkey view re-enters this method with autoRoute=false so the
5673
+ // user lands on the full picker (passkey still listed) and can pick
5674
+ // a code or SparkLink instead.
5675
+ if (autoRoute
5676
+ && this.verificationState.passkey.hasPasskey === true
5677
+ && !this.verificationState.passkey.isFallbackMode
5678
+ && methods.some((m) => m.type === 'passkey')) {
5679
+ this.setState({ view: 'passkey', recipient: this.recipient, mode: 'verify' });
5680
+ return;
5681
+ }
5667
5682
  if (methods.length === 1) {
5668
5683
  this.handleMethodSelect(methods[0]);
5669
5684
  return;
@@ -6002,7 +6017,7 @@ class IdentityRenderer {
6002
6017
  if (passkey.hasPasskey === null) {
6003
6018
  const checkResult = await this.passkeyHandler.checkPasskey();
6004
6019
  // Only prompt if the verified identifier resolves to an SVID without a passkey.
6005
- return checkResult !== null && checkResult.emailValid && !checkResult.hasPasskey;
6020
+ return checkResult !== null && checkResult.identityValid && !checkResult.hasPasskey;
6006
6021
  }
6007
6022
  return !passkey.hasPasskey;
6008
6023
  }