@sparkvault/sdk-js 7.0.0 → 7.0.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.
@@ -167,6 +167,7 @@ function resolveConfig(config) {
167
167
  accessToken: config.accessToken,
168
168
  getAccessToken: config.getAccessToken,
169
169
  apiKey: config.apiKey,
170
+ clientId: config.clientId,
170
171
  preloadConfig: config.preloadConfig !== false, // Default: true
171
172
  backdropBlur: config.backdropBlur !== false, // Default: true
172
173
  allowedDownloadHostPatterns: config.allowedDownloadHostPatterns ?? DEFAULT_ALLOWED_DOWNLOAD_HOST_PATTERNS,
@@ -1711,9 +1712,11 @@ class TotpHandler {
1711
1712
  }
1712
1713
  catch (error) {
1713
1714
  const message = error instanceof Error ? error.message : 'Failed to send code';
1715
+ const errorCode = error instanceof SparkVaultError ? error.code : undefined;
1714
1716
  return {
1715
1717
  success: false,
1716
1718
  error: message,
1719
+ errorCode,
1717
1720
  };
1718
1721
  }
1719
1722
  }
@@ -1744,9 +1747,11 @@ class TotpHandler {
1744
1747
  }
1745
1748
  catch (error) {
1746
1749
  const message = error instanceof Error ? error.message : 'Failed to resend code';
1750
+ const errorCode = error instanceof SparkVaultError ? error.code : undefined;
1747
1751
  return {
1748
1752
  success: false,
1749
1753
  error: `Failed to resend: ${message}`,
1754
+ errorCode,
1750
1755
  };
1751
1756
  }
1752
1757
  }
@@ -1848,6 +1853,7 @@ class SparkLinkHandler {
1848
1853
  return {
1849
1854
  success: false,
1850
1855
  error: message,
1856
+ errorCode: error instanceof SparkVaultError ? error.code : undefined,
1851
1857
  };
1852
1858
  }
1853
1859
  }
@@ -5177,6 +5183,22 @@ class SparkLinkWaitingView {
5177
5183
  *
5178
5184
  * Professional enterprise-quality error display with clear visual hierarchy.
5179
5185
  */
5186
+ /**
5187
+ * Error codes that mean the site owner's Identity access is blocked — the
5188
+ * free trial was exhausted or expired, or a paid plan hit its capacity wall.
5189
+ * The end user can do nothing about these, so the ErrorView renders a friendly,
5190
+ * non-leaky interstitial (no "Try Again", no debug code badge) exactly like
5191
+ * `identity_locked`. The owner is nudged to subscribe through their own
5192
+ * dashboard, never through this end-user surface.
5193
+ */
5194
+ const IDENTITY_BLOCKED_ERROR_CODES = [
5195
+ 'identity_trial_exhausted',
5196
+ 'identity_trial_expired',
5197
+ 'identity_capacity_blocked',
5198
+ ];
5199
+ function isBlockedCode(code) {
5200
+ return IDENTITY_BLOCKED_ERROR_CODES.includes(code);
5201
+ }
5180
5202
  class ErrorView {
5181
5203
  constructor(props) {
5182
5204
  this.retryButton = null;
@@ -5200,15 +5222,26 @@ class ErrorView {
5200
5222
  // title, no debug error-code badge, and no "Try Again" (retrying the
5201
5223
  // login cannot clear a lockdown — only managing it at auth.sv can).
5202
5224
  const isLocked = this.props.code === 'identity_locked';
5225
+ // A blocked Identity (trial exhausted/expired, or paid capacity wall) is
5226
+ // likewise terminal for the end user: retrying cannot restore access, only
5227
+ // the site owner subscribing can. Show a friendly, non-leaky interstitial.
5228
+ const isBlocked = isBlockedCode(this.props.code);
5229
+ // Both states are terminal — suppress the debug code badge and the retry
5230
+ // button, leaving only Close.
5231
+ const isTerminal = isLocked || isBlocked;
5203
5232
  const title = document.createElement('h3');
5204
5233
  title.className = 'sv-error-title';
5205
- title.textContent = isLocked ? 'This SparkVault Identity is locked' : 'Something went wrong';
5234
+ title.textContent = isLocked
5235
+ ? 'This SparkVault Identity is locked'
5236
+ : isBlocked
5237
+ ? 'Sign-in unavailable'
5238
+ : 'Something went wrong';
5206
5239
  const message = document.createElement('p');
5207
5240
  message.className = 'sv-error-message';
5208
5241
  message.textContent = this.props.message;
5209
5242
  contentSection.appendChild(title);
5210
5243
  contentSection.appendChild(message);
5211
- if (!isLocked) {
5244
+ if (!isTerminal) {
5212
5245
  // Error code badge — useful for debugging genuine errors only.
5213
5246
  const codeBadge = document.createElement('div');
5214
5247
  codeBadge.className = 'sv-error-code-badge';
@@ -5225,7 +5258,7 @@ class ErrorView {
5225
5258
  // Action buttons with proper styling
5226
5259
  const buttonContainer = div();
5227
5260
  buttonContainer.className = 'sv-error-actions';
5228
- if (!isLocked) {
5261
+ if (!isTerminal) {
5229
5262
  this.retryButton = document.createElement('button');
5230
5263
  this.retryButton.className = 'sv-btn sv-btn-error-primary';
5231
5264
  this.retryButton.textContent = 'Try Again';
@@ -5692,6 +5725,13 @@ class OriginNotAllowedView {
5692
5725
  * Orchestrates view state and rendering for identity verification flows.
5693
5726
  * Uses extracted handlers for TOTP and Passkey operations.
5694
5727
  */
5728
+ /**
5729
+ * End-user copy for a blocked Identity. Intentionally generic — it never names
5730
+ * the trial, billing, usage, or any owner-facing internal. The site owner is
5731
+ * nudged to subscribe through their own dashboard and emails, not here.
5732
+ */
5733
+ const IDENTITY_BLOCKED_END_USER_MESSAGE = "This app's sign-in is temporarily unavailable. Please contact the site owner.";
5734
+ const BLOCKED_ERROR_CODES = new Set(IDENTITY_BLOCKED_ERROR_CODES);
5695
5735
  const SPARKLINK_CSP_BLOCKED_ERROR = {
5696
5736
  message: [
5697
5737
  'Connection blocked by security policy.',
@@ -6225,6 +6265,8 @@ class IdentityRenderer {
6225
6265
  this.setState({ view: 'loading' });
6226
6266
  const result = await this.totpHandler.send(totpMethod);
6227
6267
  if (!result.success) {
6268
+ if (this.renderBlockedInterstitial(result.errorCode))
6269
+ return;
6228
6270
  this.setState({
6229
6271
  view: 'error',
6230
6272
  message: result.error ?? 'Failed to send code',
@@ -6283,6 +6325,12 @@ class IdentityRenderer {
6283
6325
  const result = await this.sparkLinkHandler.send(wsResult.connectionId);
6284
6326
  if (!result.success) {
6285
6327
  this.cleanupSparkLink();
6328
+ // A blocked Identity (trial exhausted/expired, paid capacity) is
6329
+ // terminal — show the "sign-in unavailable" interstitial, not a
6330
+ // retryable "Try Again" that invites retrying a permanently-blocked
6331
+ // login. Mirrors the totp send path.
6332
+ if (this.renderBlockedInterstitial(result.errorCode))
6333
+ return;
6286
6334
  this.setState({
6287
6335
  view: 'error',
6288
6336
  message: result.error ?? 'Failed to send SparkLink',
@@ -6320,6 +6368,24 @@ class IdentityRenderer {
6320
6368
  this.handleErrorWithRecovery(error, 'unknown');
6321
6369
  }
6322
6370
  }
6371
+ /**
6372
+ * If the error code means the site owner's Identity access is blocked
6373
+ * (trial exhausted/expired, or paid capacity wall), render the dedicated
6374
+ * end-user interstitial and return true. The message is intentionally
6375
+ * generic so no billing internals leak to the end user. Returns false for
6376
+ * any other code so the caller falls through to its normal handling.
6377
+ */
6378
+ renderBlockedInterstitial(code) {
6379
+ if (!code || !BLOCKED_ERROR_CODES.has(code)) {
6380
+ return false;
6381
+ }
6382
+ this.setState({
6383
+ view: 'error',
6384
+ code,
6385
+ message: IDENTITY_BLOCKED_END_USER_MESSAGE,
6386
+ });
6387
+ return true;
6388
+ }
6323
6389
  async handleTotpSubmit(code) {
6324
6390
  if (this.viewState.view !== 'totp-verify')
6325
6391
  return;
@@ -6336,6 +6402,7 @@ class IdentityRenderer {
6336
6402
  if (result.success && result.result) {
6337
6403
  this.runPostLogin(result.result);
6338
6404
  }
6405
+ else if (this.renderBlockedInterstitial(result.errorCode)) ;
6339
6406
  else if (result.errorCode === 'identity_locked') {
6340
6407
  // The identity is locked down — a dedicated, actionable state, not a
6341
6408
  // generic "invalid code" retry. The person must manage it at auth.sv.
@@ -6416,6 +6483,8 @@ class IdentityRenderer {
6416
6483
  const currentMethod = this.verificationState.totp.method ?? 'email';
6417
6484
  const result = await this.totpHandler.resend();
6418
6485
  if (!result.success) {
6486
+ if (this.renderBlockedInterstitial(result.errorCode))
6487
+ return;
6419
6488
  this.setState({
6420
6489
  view: 'totp-verify',
6421
6490
  email: this.recipient,
@@ -6458,6 +6527,7 @@ class IdentityRenderer {
6458
6527
  navigateWith(this.options, result.result.redirect);
6459
6528
  }
6460
6529
  }
6530
+ else if (this.renderBlockedInterstitial(result.errorCode)) ;
6461
6531
  else if (result.errorCode === 'identity_locked') {
6462
6532
  // Dedicated state — retrying cannot unlock the identity.
6463
6533
  this.setState({
@@ -7097,6 +7167,8 @@ class IdentityRenderer {
7097
7167
  this.setState({ view: 'loading' });
7098
7168
  const result = await this.totpHandler.send('email');
7099
7169
  if (!result.success) {
7170
+ if (this.renderBlockedInterstitial(result.errorCode))
7171
+ return;
7100
7172
  this.setState({
7101
7173
  view: 'error',
7102
7174
  message: result.error ?? 'Failed to send code',
@@ -7774,6 +7846,74 @@ class IdentityModule {
7774
7846
  }
7775
7847
  return new URL(path, this.config.portalBaseUrl).toString();
7776
7848
  }
7849
+ /**
7850
+ * One-click sign-in through the browser's native FedCM account chooser.
7851
+ *
7852
+ * When the person is already signed into auth.sv (carrying the SSO session),
7853
+ * the browser surfaces a "Continue as …" prompt with no popup, redirect, or
7854
+ * typing. Requires a registered `clientId` in the SDK config and a browser
7855
+ * with FedCM support — callers should feature-detect with
7856
+ * `IdentityModule.isFedcmAvailable()` and fall back to `verify()` otherwise.
7857
+ *
7858
+ * @returns the verified result, or `null` when the browser has no FedCM
7859
+ * support (so the caller can fall back). Throws `UserCancelledError` when the
7860
+ * person dismisses the chooser.
7861
+ *
7862
+ * @example
7863
+ * const result = (await sv.products.identity.isFedcmAvailable())
7864
+ * ? await sv.products.identity.signInWithFedcm().catch(() => sv.products.identity.verify())
7865
+ * : await sv.products.identity.verify();
7866
+ */
7867
+ async signInWithFedcm(options = {}) {
7868
+ if (!IdentityModule.isFedcmAvailable()) {
7869
+ return null;
7870
+ }
7871
+ if (!this.config.clientId) {
7872
+ throw new ValidationError('clientId must be set in the SDK config to use FedCM sign-in', {
7873
+ field: 'clientId',
7874
+ });
7875
+ }
7876
+ const nonce = options.nonce ?? generateState();
7877
+ const configURL = `${this.config.portalBaseUrl}/${this.config.accountId}/fedcm/config.json`;
7878
+ const request = {
7879
+ identity: { providers: [{ configURL, clientId: this.config.clientId, nonce }] },
7880
+ ...(options.mediation ? { mediation: options.mediation } : {}),
7881
+ };
7882
+ let credential;
7883
+ try {
7884
+ // FedCM's `identity` provider request isn't in every TS DOM lib version, so
7885
+ // adapt navigator.credentials.get through our own request/credential types
7886
+ // (avoids naming the type-only `CredentialRequestOptions` DOM global).
7887
+ const getCredential = navigator.credentials.get.bind(navigator.credentials);
7888
+ credential = await getCredential(request);
7889
+ }
7890
+ catch (error) {
7891
+ // The browser rejects with an AbortError/NotAllowedError when the person
7892
+ // dismisses the chooser — normalize that to the SDK's cancel signal.
7893
+ throw new UserCancelledError(error instanceof Error ? error.message : undefined);
7894
+ }
7895
+ const token = credential?.token;
7896
+ if (!token) {
7897
+ throw new ValidationError('FedCM did not return a token');
7898
+ }
7899
+ // Same decode + issuer/expiry/alg validation every returned token goes through.
7900
+ const claims = this.decodeToken(token);
7901
+ return {
7902
+ token,
7903
+ identity: claims.identity,
7904
+ identityType: claims.identity_type,
7905
+ svid: claims.svid,
7906
+ sessionId: claims.session_id,
7907
+ };
7908
+ }
7909
+ /** Whether this browser exposes the FedCM API (`navigator.credentials` + IdentityCredential). */
7910
+ static isFedcmAvailable() {
7911
+ return (typeof window !== 'undefined' &&
7912
+ 'IdentityCredential' in window &&
7913
+ typeof navigator !== 'undefined' &&
7914
+ !!navigator.credentials &&
7915
+ typeof navigator.credentials.get === 'function');
7916
+ }
7777
7917
  /**
7778
7918
  * Verify user identity.
7779
7919
  *