@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.
@@ -30,7 +30,7 @@ export interface PasskeyResult {
30
30
  */
31
31
  export interface PasskeyCheckResult {
32
32
  /** Whether the identifier is valid (email domain or phone format) */
33
- emailValid: boolean;
33
+ identityValid: boolean;
34
34
  /** Whether the resolved SVID has registered passkeys */
35
35
  hasPasskey: boolean;
36
36
  }
@@ -42,10 +42,17 @@ export declare class PasskeyHandler {
42
42
  private readonly state;
43
43
  private readonly baseUrl;
44
44
  private readonly accountId;
45
+ /**
46
+ * Exact origin the passkey popup must postMessage from. Derived from the
47
+ * configured identityBaseUrl so we never accept a result from an
48
+ * unrelated SparkVault subdomain (XSS or subdomain-takeover relay).
49
+ */
50
+ private readonly trustedOrigin;
45
51
  constructor(api: IdentityApi, state: VerificationState);
46
52
  /**
47
- * Check if the current identifier resolves to an SVID with registered passkeys.
48
- * @returns Check result with emailValid and hasPasskey, or null on error
53
+ * Resolve the current identifier (email or phone) to its SVID and report
54
+ * whether that SVID has any registered passkeys.
55
+ * @returns Check result with identityValid and hasPasskey, or null on error
49
56
  */
50
57
  checkPasskey(): Promise<PasskeyCheckResult | null>;
51
58
  /**
@@ -61,7 +68,10 @@ export declare class PasskeyHandler {
61
68
  */
62
69
  private openPasskeyPopup;
63
70
  /**
64
- * Validate that a message origin is from sparkvault.com domain
71
+ * Validate that a postMessage came from the identity service's own
72
+ * origin. We do not accept the broader sparkvault.com family — a popup
73
+ * is only opened against `this.baseUrl`, so any other origin is
74
+ * either an unrelated tab or a forgery attempt.
65
75
  */
66
76
  private isValidMessageOrigin;
67
77
  /**
@@ -71,6 +71,12 @@ export declare class IdentityRenderer {
71
71
  private destroyCurrentView;
72
72
  private createViewForState;
73
73
  private showIdentityInput;
74
+ /**
75
+ * Render the picker. With `autoRoute` left default, a recipient that
76
+ * already has a passkey is sent straight to the WebAuthn view; pass
77
+ * `false` from the passkey view's back arrow so the user lands on the
78
+ * actual list of alternatives instead of bouncing back to passkey.
79
+ */
74
80
  private showMethodSelect;
75
81
  private handleIdentitySubmit;
76
82
  private getAllowedIdentityTypes;
@@ -102,24 +102,6 @@ Object.freeze([
102
102
  ...SPARKVAULT_PLATFORM_SV_HOSTS
103
103
  ]);
104
104
 
105
- function normalizeHostname(hostname) {
106
- if (typeof hostname !== 'string') return '';
107
- return hostname.trim().toLowerCase().replace(/\.$/, '');
108
- }
109
-
110
- function isSparkVaultRootOrSubdomain(hostname) {
111
- const normalized = normalizeHostname(hostname);
112
- return normalized === SPARKVAULT_ROOT_DOMAIN || normalized.endsWith(`.${SPARKVAULT_ROOT_DOMAIN}`);
113
- }
114
-
115
- function isSparkVaultPlatformSvHost(hostname) {
116
- return SPARKVAULT_PLATFORM_SV_HOSTS.includes(normalizeHostname(hostname));
117
- }
118
-
119
- function isSparkVaultOwnedHostname(hostname) {
120
- return isSparkVaultRootOrSubdomain(hostname) || isSparkVaultPlatformSvHost(hostname);
121
- }
122
-
123
105
  /**
124
106
  * SparkVault SDK Configuration
125
107
  */
@@ -1541,17 +1523,19 @@ class PasskeyHandler {
1541
1523
  const config = api.config;
1542
1524
  this.baseUrl = config.identityBaseUrl;
1543
1525
  this.accountId = config.accountId;
1526
+ this.trustedOrigin = new URL(this.baseUrl).origin;
1544
1527
  }
1545
1528
  /**
1546
- * Check if the current identifier resolves to an SVID with registered passkeys.
1547
- * @returns Check result with emailValid and hasPasskey, or null on error
1529
+ * Resolve the current identifier (email or phone) to its SVID and report
1530
+ * whether that SVID has any registered passkeys.
1531
+ * @returns Check result with identityValid and hasPasskey, or null on error
1548
1532
  */
1549
1533
  async checkPasskey() {
1550
1534
  try {
1551
1535
  const response = await this.api.checkPasskey(this.state.recipient, this.state.identityType);
1552
1536
  this.state.setPasskeyStatus(response.has_passkey);
1553
1537
  return {
1554
- emailValid: response.identity_valid,
1538
+ identityValid: response.identity_valid,
1555
1539
  hasPasskey: response.has_passkey,
1556
1540
  };
1557
1541
  }
@@ -1690,16 +1674,13 @@ class PasskeyHandler {
1690
1674
  });
1691
1675
  }
1692
1676
  /**
1693
- * Validate that a message origin is from sparkvault.com domain
1677
+ * Validate that a postMessage came from the identity service's own
1678
+ * origin. We do not accept the broader sparkvault.com family — a popup
1679
+ * is only opened against `this.baseUrl`, so any other origin is
1680
+ * either an unrelated tab or a forgery attempt.
1694
1681
  */
1695
1682
  isValidMessageOrigin(origin) {
1696
- try {
1697
- const url = new URL(origin);
1698
- return url.protocol === 'https:' && url.origin === origin && !url.port && isSparkVaultOwnedHostname(url.hostname);
1699
- }
1700
- catch {
1701
- return false;
1702
- }
1683
+ return origin === this.trustedOrigin;
1703
1684
  }
1704
1685
  /**
1705
1686
  * Handle popup error response
@@ -1828,11 +1809,11 @@ class TotpHandler {
1828
1809
  recipient: this.state.recipient,
1829
1810
  authContext: this.state.authContext,
1830
1811
  });
1831
- if (response.verified && response.token) {
1812
+ if (response.verified && (response.token || response.redirect)) {
1832
1813
  return {
1833
1814
  success: true,
1834
1815
  result: {
1835
- token: response.token,
1816
+ token: response.token ?? '',
1836
1817
  identity: response.identity ?? this.state.recipient,
1837
1818
  identityType: response.identity_type ?? this.state.identityType,
1838
1819
  svid: response.svid,
@@ -5576,9 +5557,12 @@ class IdentityRenderer {
5576
5557
  onBack: isFromPrompt
5577
5558
  ? () => this.handlePasskeyRegistrationSkip()
5578
5559
  : state.mode === 'verify'
5579
- // From passkey verify, enable fallback so showMethodSelect doesn't loop back
5560
+ // Back returns to the picker with passkey still listed.
5561
+ // autoRoute=false prevents the auto-jump from sending the
5562
+ // user straight back to this view. If no fallback methods
5563
+ // exist at all, fall through to identity-input.
5580
5564
  ? this.passkeyFallbackMethodsAvailable()
5581
- ? () => this.handlePasskeyFallback()
5565
+ ? () => this.showMethodSelect({ autoRoute: false })
5582
5566
  : this.recipient
5583
5567
  ? () => this.showIdentityInput()
5584
5568
  : undefined
@@ -5627,7 +5611,14 @@ class IdentityRenderer {
5627
5611
  showIdentityInput(error) {
5628
5612
  this.setState({ view: 'identity-input', error });
5629
5613
  }
5630
- async showMethodSelect() {
5614
+ /**
5615
+ * Render the picker. With `autoRoute` left default, a recipient that
5616
+ * already has a passkey is sent straight to the WebAuthn view; pass
5617
+ * `false` from the passkey view's back arrow so the user lands on the
5618
+ * actual list of alternatives instead of bouncing back to passkey.
5619
+ */
5620
+ async showMethodSelect(options = {}) {
5621
+ const autoRoute = options.autoRoute !== false;
5631
5622
  if (!this.sdkConfig)
5632
5623
  return;
5633
5624
  // Defensive: ensure methods is always an array
@@ -5652,6 +5643,17 @@ class IdentityRenderer {
5652
5643
  // Email users can use email-based methods
5653
5644
  return m.identityType === 'email' || m.identityType === undefined;
5654
5645
  });
5646
+ // Resolve identifier → SVID → has_passkey exactly once per flow. The
5647
+ // result drives both the picker (hide Passkey when there is none) and
5648
+ // the post-verify "add a passkey?" prompt (skip when there already is
5649
+ // one). Cached on verificationState; later reads short-circuit.
5650
+ const { passkey } = this.verificationState;
5651
+ if (passkey.hasPasskey === null && methods.some((m) => m.type === 'passkey')) {
5652
+ await this.passkeyHandler.checkPasskey();
5653
+ }
5654
+ if (this.verificationState.passkey.hasPasskey === false) {
5655
+ methods = methods.filter((m) => m.type !== 'passkey');
5656
+ }
5655
5657
  }
5656
5658
  else {
5657
5659
  // No PII prompt is needed when only SVID-routed methods are enabled.
@@ -5668,6 +5670,19 @@ class IdentityRenderer {
5668
5670
  });
5669
5671
  return;
5670
5672
  }
5673
+ // When a passkey is already registered for this identifier, jump
5674
+ // straight to the WebAuthn ceremony instead of asking the user to
5675
+ // pick a method — it is the safest, fastest option. The back arrow on
5676
+ // the passkey view re-enters this method with autoRoute=false so the
5677
+ // user lands on the full picker (passkey still listed) and can pick
5678
+ // a code or SparkLink instead.
5679
+ if (autoRoute
5680
+ && this.verificationState.passkey.hasPasskey === true
5681
+ && !this.verificationState.passkey.isFallbackMode
5682
+ && methods.some((m) => m.type === 'passkey')) {
5683
+ this.setState({ view: 'passkey', recipient: this.recipient, mode: 'verify' });
5684
+ return;
5685
+ }
5671
5686
  if (methods.length === 1) {
5672
5687
  this.handleMethodSelect(methods[0]);
5673
5688
  return;
@@ -6006,7 +6021,7 @@ class IdentityRenderer {
6006
6021
  if (passkey.hasPasskey === null) {
6007
6022
  const checkResult = await this.passkeyHandler.checkPasskey();
6008
6023
  // Only prompt if the verified identifier resolves to an SVID without a passkey.
6009
- return checkResult !== null && checkResult.emailValid && !checkResult.hasPasskey;
6024
+ return checkResult !== null && checkResult.identityValid && !checkResult.hasPasskey;
6010
6025
  }
6011
6026
  return !passkey.hasPasskey;
6012
6027
  }