@sparkvault/sdk-js 6.0.0 → 7.0.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.
package/dist/index.d.ts CHANGED
@@ -342,18 +342,26 @@ declare class IdentityModule {
342
342
  */
343
343
  attach(selector: string, options?: AttachOptions): () => void;
344
344
  /**
345
- * Verify and decode an identity token.
346
- * Validates the token structure, expiry, and issuer.
345
+ * Decode an identity token's claims WITHOUT verifying its signature.
347
346
  *
348
- * Note: For production use, verify the Ed25519 signature server-side
349
- * using the JWKS endpoint.
347
+ * This is an UNVERIFIED, client-side decode: it parses the JWT payload and
348
+ * sanity-checks structure, `exp`, `iss`, `aud`, and the header `alg` (rejecting
349
+ * `none` and any non-EdDSA algorithm so an unsigned or algorithm-confused token
350
+ * cannot masquerade as valid). It does NOT verify the Ed25519 signature, so its
351
+ * result MUST NOT be trusted for any authorization decision.
352
+ *
353
+ * To trust a token (e.g. before keying records by `claims.svid`), verify the
354
+ * Ed25519 signature server-side against the Identity JWKS endpoint
355
+ * (`/v1/products/identity/{account_id}/.well-known/jwks.json`). See docs/SDK.md.
350
356
  */
351
- verifyToken(token: string): Promise<TokenClaims>;
357
+ decodeToken(token: string): TokenClaims;
352
358
  /**
353
359
  * Complete full-page redirect results produced by social-login flows.
354
360
  * The Identity Product redirects back with `#token=...` so the token does
355
- * not hit server logs. Consume it once, validate the issuer/expiry shape,
356
- * then remove it from the URL bar.
361
+ * not hit server logs. Consume it once, run the unverified client-side decode
362
+ * (issuer/audience/expiry/alg sanity-check — NOT signature verification), then
363
+ * remove it from the URL bar. The host must still verify the token server-side
364
+ * via the Identity JWKS before trusting any claim.
357
365
  */
358
366
  private consumeRedirectResult;
359
367
  /**
@@ -22,7 +22,12 @@ export interface PasskeyResult {
22
22
  result?: VerifyResult;
23
23
  error?: string;
24
24
  errorType?: 'cancelled' | 'not_found' | 'not_allowed' | 'popup_blocked' | 'unknown';
25
- /** Machine-readable API error code (e.g. 'identity_locked'). */
25
+ /**
26
+ * Machine-readable API error code (e.g. 'identity_locked',
27
+ * 'identity_trial_exhausted'). Forwarded unchanged from the inline ceremony
28
+ * and the popup so the renderer can route blocked-Identity codes to the
29
+ * dedicated interstitial.
30
+ */
26
31
  errorCode?: string;
27
32
  }
28
33
  /**
@@ -16,6 +16,9 @@ export interface SparkLinkSendResult {
16
16
  /** Login-ceremony handle used to reclaim the result if the WebSocket is lost. */
17
17
  kindling?: string;
18
18
  error?: string;
19
+ /** Machine-readable API error code (e.g. 'identity_trial_exhausted'); routes a
20
+ * blocked Identity to the terminal interstitial instead of a retryable error. */
21
+ errorCode?: string;
19
22
  }
20
23
  /**
21
24
  * Handles SparkLink sending
@@ -15,6 +15,13 @@ export interface TotpSendResult {
15
15
  kindling?: string;
16
16
  expiresAt?: number;
17
17
  error?: string;
18
+ /**
19
+ * Machine-readable API error code (e.g. 'identity_trial_exhausted'). Set when
20
+ * the send-boundary gate refuses to dispatch a code because the owner's
21
+ * Identity access is blocked, so the renderer can show the interstitial
22
+ * instead of a generic "failed to send" error.
23
+ */
24
+ errorCode?: string;
18
25
  }
19
26
  /**
20
27
  * Result of TOTP verify operation
@@ -72,18 +72,26 @@ export declare class IdentityModule {
72
72
  */
73
73
  attach(selector: string, options?: AttachOptions): () => void;
74
74
  /**
75
- * Verify and decode an identity token.
76
- * Validates the token structure, expiry, and issuer.
75
+ * Decode an identity token's claims WITHOUT verifying its signature.
77
76
  *
78
- * Note: For production use, verify the Ed25519 signature server-side
79
- * using the JWKS endpoint.
77
+ * This is an UNVERIFIED, client-side decode: it parses the JWT payload and
78
+ * sanity-checks structure, `exp`, `iss`, `aud`, and the header `alg` (rejecting
79
+ * `none` and any non-EdDSA algorithm so an unsigned or algorithm-confused token
80
+ * cannot masquerade as valid). It does NOT verify the Ed25519 signature, so its
81
+ * result MUST NOT be trusted for any authorization decision.
82
+ *
83
+ * To trust a token (e.g. before keying records by `claims.svid`), verify the
84
+ * Ed25519 signature server-side against the Identity JWKS endpoint
85
+ * (`/v1/products/identity/{account_id}/.well-known/jwks.json`). See docs/SDK.md.
80
86
  */
81
- verifyToken(token: string): Promise<TokenClaims>;
87
+ decodeToken(token: string): TokenClaims;
82
88
  /**
83
89
  * Complete full-page redirect results produced by social-login flows.
84
90
  * The Identity Product redirects back with `#token=...` so the token does
85
- * not hit server logs. Consume it once, validate the issuer/expiry shape,
86
- * then remove it from the URL bar.
91
+ * not hit server logs. Consume it once, run the unverified client-side decode
92
+ * (issuer/audience/expiry/alg sanity-check — NOT signature verification), then
93
+ * remove it from the URL bar. The host must still verify the token server-side
94
+ * via the Identity JWKS before trusting any claim.
87
95
  */
88
96
  private consumeRedirectResult;
89
97
  /**
@@ -113,6 +113,14 @@ export declare class IdentityRenderer {
113
113
  * opening a popup would be blocked.
114
114
  */
115
115
  private handleMethodSelect;
116
+ /**
117
+ * If the error code means the site owner's Identity access is blocked
118
+ * (trial exhausted/expired, or paid capacity wall), render the dedicated
119
+ * end-user interstitial and return true. The message is intentionally
120
+ * generic so no billing internals leak to the end user. Returns false for
121
+ * any other code so the caller falls through to its normal handling.
122
+ */
123
+ private renderBlockedInterstitial;
116
124
  private handleTotpSubmit;
117
125
  private handleSecondFactorSubmit;
118
126
  private handleTotpResend;
@@ -4,6 +4,15 @@
4
4
  * Professional enterprise-quality error display with clear visual hierarchy.
5
5
  */
6
6
  import type { View } from './base';
7
+ /**
8
+ * Error codes that mean the site owner's Identity access is blocked — the
9
+ * free trial was exhausted or expired, or a paid plan hit its capacity wall.
10
+ * The end user can do nothing about these, so the ErrorView renders a friendly,
11
+ * non-leaky interstitial (no "Try Again", no debug code badge) exactly like
12
+ * `identity_locked`. The owner is nudged to subscribe through their own
13
+ * dashboard, never through this end-user surface.
14
+ */
15
+ export declare const IDENTITY_BLOCKED_ERROR_CODES: readonly ["identity_trial_exhausted", "identity_trial_expired", "identity_capacity_blocked"];
7
16
  export interface ErrorViewProps {
8
17
  message: string;
9
18
  code: string;
@@ -1715,9 +1715,11 @@ class TotpHandler {
1715
1715
  }
1716
1716
  catch (error) {
1717
1717
  const message = error instanceof Error ? error.message : 'Failed to send code';
1718
+ const errorCode = error instanceof SparkVaultError ? error.code : undefined;
1718
1719
  return {
1719
1720
  success: false,
1720
1721
  error: message,
1722
+ errorCode,
1721
1723
  };
1722
1724
  }
1723
1725
  }
@@ -1748,9 +1750,11 @@ class TotpHandler {
1748
1750
  }
1749
1751
  catch (error) {
1750
1752
  const message = error instanceof Error ? error.message : 'Failed to resend code';
1753
+ const errorCode = error instanceof SparkVaultError ? error.code : undefined;
1751
1754
  return {
1752
1755
  success: false,
1753
1756
  error: `Failed to resend: ${message}`,
1757
+ errorCode,
1754
1758
  };
1755
1759
  }
1756
1760
  }
@@ -1852,6 +1856,7 @@ class SparkLinkHandler {
1852
1856
  return {
1853
1857
  success: false,
1854
1858
  error: message,
1859
+ errorCode: error instanceof SparkVaultError ? error.code : undefined,
1855
1860
  };
1856
1861
  }
1857
1862
  }
@@ -5181,6 +5186,22 @@ class SparkLinkWaitingView {
5181
5186
  *
5182
5187
  * Professional enterprise-quality error display with clear visual hierarchy.
5183
5188
  */
5189
+ /**
5190
+ * Error codes that mean the site owner's Identity access is blocked — the
5191
+ * free trial was exhausted or expired, or a paid plan hit its capacity wall.
5192
+ * The end user can do nothing about these, so the ErrorView renders a friendly,
5193
+ * non-leaky interstitial (no "Try Again", no debug code badge) exactly like
5194
+ * `identity_locked`. The owner is nudged to subscribe through their own
5195
+ * dashboard, never through this end-user surface.
5196
+ */
5197
+ const IDENTITY_BLOCKED_ERROR_CODES = [
5198
+ 'identity_trial_exhausted',
5199
+ 'identity_trial_expired',
5200
+ 'identity_capacity_blocked',
5201
+ ];
5202
+ function isBlockedCode(code) {
5203
+ return IDENTITY_BLOCKED_ERROR_CODES.includes(code);
5204
+ }
5184
5205
  class ErrorView {
5185
5206
  constructor(props) {
5186
5207
  this.retryButton = null;
@@ -5204,15 +5225,26 @@ class ErrorView {
5204
5225
  // title, no debug error-code badge, and no "Try Again" (retrying the
5205
5226
  // login cannot clear a lockdown — only managing it at auth.sv can).
5206
5227
  const isLocked = this.props.code === 'identity_locked';
5228
+ // A blocked Identity (trial exhausted/expired, or paid capacity wall) is
5229
+ // likewise terminal for the end user: retrying cannot restore access, only
5230
+ // the site owner subscribing can. Show a friendly, non-leaky interstitial.
5231
+ const isBlocked = isBlockedCode(this.props.code);
5232
+ // Both states are terminal — suppress the debug code badge and the retry
5233
+ // button, leaving only Close.
5234
+ const isTerminal = isLocked || isBlocked;
5207
5235
  const title = document.createElement('h3');
5208
5236
  title.className = 'sv-error-title';
5209
- title.textContent = isLocked ? 'This SparkVault Identity is locked' : 'Something went wrong';
5237
+ title.textContent = isLocked
5238
+ ? 'This SparkVault Identity is locked'
5239
+ : isBlocked
5240
+ ? 'Sign-in unavailable'
5241
+ : 'Something went wrong';
5210
5242
  const message = document.createElement('p');
5211
5243
  message.className = 'sv-error-message';
5212
5244
  message.textContent = this.props.message;
5213
5245
  contentSection.appendChild(title);
5214
5246
  contentSection.appendChild(message);
5215
- if (!isLocked) {
5247
+ if (!isTerminal) {
5216
5248
  // Error code badge — useful for debugging genuine errors only.
5217
5249
  const codeBadge = document.createElement('div');
5218
5250
  codeBadge.className = 'sv-error-code-badge';
@@ -5229,7 +5261,7 @@ class ErrorView {
5229
5261
  // Action buttons with proper styling
5230
5262
  const buttonContainer = div();
5231
5263
  buttonContainer.className = 'sv-error-actions';
5232
- if (!isLocked) {
5264
+ if (!isTerminal) {
5233
5265
  this.retryButton = document.createElement('button');
5234
5266
  this.retryButton.className = 'sv-btn sv-btn-error-primary';
5235
5267
  this.retryButton.textContent = 'Try Again';
@@ -5696,6 +5728,13 @@ class OriginNotAllowedView {
5696
5728
  * Orchestrates view state and rendering for identity verification flows.
5697
5729
  * Uses extracted handlers for TOTP and Passkey operations.
5698
5730
  */
5731
+ /**
5732
+ * End-user copy for a blocked Identity. Intentionally generic — it never names
5733
+ * the trial, billing, usage, or any owner-facing internal. The site owner is
5734
+ * nudged to subscribe through their own dashboard and emails, not here.
5735
+ */
5736
+ const IDENTITY_BLOCKED_END_USER_MESSAGE = "This app's sign-in is temporarily unavailable. Please contact the site owner.";
5737
+ const BLOCKED_ERROR_CODES = new Set(IDENTITY_BLOCKED_ERROR_CODES);
5699
5738
  const SPARKLINK_CSP_BLOCKED_ERROR = {
5700
5739
  message: [
5701
5740
  'Connection blocked by security policy.',
@@ -6229,6 +6268,8 @@ class IdentityRenderer {
6229
6268
  this.setState({ view: 'loading' });
6230
6269
  const result = await this.totpHandler.send(totpMethod);
6231
6270
  if (!result.success) {
6271
+ if (this.renderBlockedInterstitial(result.errorCode))
6272
+ return;
6232
6273
  this.setState({
6233
6274
  view: 'error',
6234
6275
  message: result.error ?? 'Failed to send code',
@@ -6287,6 +6328,12 @@ class IdentityRenderer {
6287
6328
  const result = await this.sparkLinkHandler.send(wsResult.connectionId);
6288
6329
  if (!result.success) {
6289
6330
  this.cleanupSparkLink();
6331
+ // A blocked Identity (trial exhausted/expired, paid capacity) is
6332
+ // terminal — show the "sign-in unavailable" interstitial, not a
6333
+ // retryable "Try Again" that invites retrying a permanently-blocked
6334
+ // login. Mirrors the totp send path.
6335
+ if (this.renderBlockedInterstitial(result.errorCode))
6336
+ return;
6290
6337
  this.setState({
6291
6338
  view: 'error',
6292
6339
  message: result.error ?? 'Failed to send SparkLink',
@@ -6324,6 +6371,24 @@ class IdentityRenderer {
6324
6371
  this.handleErrorWithRecovery(error, 'unknown');
6325
6372
  }
6326
6373
  }
6374
+ /**
6375
+ * If the error code means the site owner's Identity access is blocked
6376
+ * (trial exhausted/expired, or paid capacity wall), render the dedicated
6377
+ * end-user interstitial and return true. The message is intentionally
6378
+ * generic so no billing internals leak to the end user. Returns false for
6379
+ * any other code so the caller falls through to its normal handling.
6380
+ */
6381
+ renderBlockedInterstitial(code) {
6382
+ if (!code || !BLOCKED_ERROR_CODES.has(code)) {
6383
+ return false;
6384
+ }
6385
+ this.setState({
6386
+ view: 'error',
6387
+ code,
6388
+ message: IDENTITY_BLOCKED_END_USER_MESSAGE,
6389
+ });
6390
+ return true;
6391
+ }
6327
6392
  async handleTotpSubmit(code) {
6328
6393
  if (this.viewState.view !== 'totp-verify')
6329
6394
  return;
@@ -6340,6 +6405,7 @@ class IdentityRenderer {
6340
6405
  if (result.success && result.result) {
6341
6406
  this.runPostLogin(result.result);
6342
6407
  }
6408
+ else if (this.renderBlockedInterstitial(result.errorCode)) ;
6343
6409
  else if (result.errorCode === 'identity_locked') {
6344
6410
  // The identity is locked down — a dedicated, actionable state, not a
6345
6411
  // generic "invalid code" retry. The person must manage it at auth.sv.
@@ -6420,6 +6486,8 @@ class IdentityRenderer {
6420
6486
  const currentMethod = this.verificationState.totp.method ?? 'email';
6421
6487
  const result = await this.totpHandler.resend();
6422
6488
  if (!result.success) {
6489
+ if (this.renderBlockedInterstitial(result.errorCode))
6490
+ return;
6423
6491
  this.setState({
6424
6492
  view: 'totp-verify',
6425
6493
  email: this.recipient,
@@ -6462,6 +6530,7 @@ class IdentityRenderer {
6462
6530
  navigateWith(this.options, result.result.redirect);
6463
6531
  }
6464
6532
  }
6533
+ else if (this.renderBlockedInterstitial(result.errorCode)) ;
6465
6534
  else if (result.errorCode === 'identity_locked') {
6466
6535
  // Dedicated state — retrying cannot unlock the identity.
6467
6536
  this.setState({
@@ -7101,6 +7170,8 @@ class IdentityRenderer {
7101
7170
  this.setState({ view: 'loading' });
7102
7171
  const result = await this.totpHandler.send('email');
7103
7172
  if (!result.success) {
7173
+ if (this.renderBlockedInterstitial(result.errorCode))
7174
+ return;
7104
7175
  this.setState({
7105
7176
  view: 'error',
7106
7177
  message: result.error ?? 'Failed to send code',
@@ -7934,13 +8005,19 @@ class IdentityModule {
7934
8005
  return cleanup;
7935
8006
  }
7936
8007
  /**
7937
- * Verify and decode an identity token.
7938
- * Validates the token structure, expiry, and issuer.
8008
+ * Decode an identity token's claims WITHOUT verifying its signature.
8009
+ *
8010
+ * This is an UNVERIFIED, client-side decode: it parses the JWT payload and
8011
+ * sanity-checks structure, `exp`, `iss`, `aud`, and the header `alg` (rejecting
8012
+ * `none` and any non-EdDSA algorithm so an unsigned or algorithm-confused token
8013
+ * cannot masquerade as valid). It does NOT verify the Ed25519 signature, so its
8014
+ * result MUST NOT be trusted for any authorization decision.
7939
8015
  *
7940
- * Note: For production use, verify the Ed25519 signature server-side
7941
- * using the JWKS endpoint.
8016
+ * To trust a token (e.g. before keying records by `claims.svid`), verify the
8017
+ * Ed25519 signature server-side against the Identity JWKS endpoint
8018
+ * (`/v1/products/identity/{account_id}/.well-known/jwks.json`). See docs/SDK.md.
7942
8019
  */
7943
- async verifyToken(token) {
8020
+ decodeToken(token) {
7944
8021
  if (!token || typeof token !== 'string') {
7945
8022
  throw new ValidationError('Token is required');
7946
8023
  }
@@ -7948,7 +8025,17 @@ class IdentityModule {
7948
8025
  if (parts.length !== 3) {
7949
8026
  throw new ValidationError('Invalid token format');
7950
8027
  }
7951
- const [, payloadB64] = parts;
8028
+ const [headerB64, payloadB64] = parts;
8029
+ const header = JSON.parse(base64urlToString(headerB64));
8030
+ if (typeof header !== 'object' || header === null) {
8031
+ throw new ValidationError('Invalid token header');
8032
+ }
8033
+ // Reject alg:none (unsigned) and any algorithm other than EdDSA. Identity
8034
+ // tokens are Ed25519-signed; anything else is an unsigned or algorithm-
8035
+ // confused token that could never pass server-side JWKS verification.
8036
+ if (header.alg !== 'EdDSA') {
8037
+ throw new ValidationError('Unsupported token algorithm');
8038
+ }
7952
8039
  const payload = JSON.parse(base64urlToString(payloadB64));
7953
8040
  if (!isTokenClaims(payload)) {
7954
8041
  throw new ValidationError('Invalid token payload structure');
@@ -7961,13 +8048,18 @@ class IdentityModule {
7961
8048
  if (payload.iss !== expectedIssuer) {
7962
8049
  throw new ValidationError('Invalid token issuer');
7963
8050
  }
8051
+ if (payload.aud !== this.config.accountId) {
8052
+ throw new ValidationError('Invalid token audience');
8053
+ }
7964
8054
  return payload;
7965
8055
  }
7966
8056
  /**
7967
8057
  * Complete full-page redirect results produced by social-login flows.
7968
8058
  * The Identity Product redirects back with `#token=...` so the token does
7969
- * not hit server logs. Consume it once, validate the issuer/expiry shape,
7970
- * then remove it from the URL bar.
8059
+ * not hit server logs. Consume it once, run the unverified client-side decode
8060
+ * (issuer/audience/expiry/alg sanity-check — NOT signature verification), then
8061
+ * remove it from the URL bar. The host must still verify the token server-side
8062
+ * via the Identity JWKS before trusting any claim.
7971
8063
  */
7972
8064
  async consumeRedirectResult() {
7973
8065
  if (typeof window === 'undefined' || !window.location.hash) {
@@ -7994,7 +8086,11 @@ class IdentityModule {
7994
8086
  if (expectedState && params.get('state') !== expectedState) {
7995
8087
  throw new ValidationError('Invalid OAuth state');
7996
8088
  }
7997
- const claims = await this.verifyToken(token);
8089
+ // Unverified client-side decode — surfaces the claims for the result
8090
+ // shape and rejects an obviously-bad token (expiry/issuer/audience/alg).
8091
+ // The host MUST still verify result.token server-side via the Identity
8092
+ // JWKS before trusting any claim for an authorization decision.
8093
+ const claims = this.decodeToken(token);
7998
8094
  return {
7999
8095
  token,
8000
8096
  identity: claims.identity,