@sparkvault/sdk-js 9.0.0 → 9.1.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.
package/dist/index.cjs CHANGED
@@ -8573,23 +8573,16 @@ function attachClickHandler(selector, onClick) {
8573
8573
  * sv.products.identity.attach('.login-btn');
8574
8574
  */
8575
8575
  const PORTAL_SECTIONS = ['home', 'identity', 'security', 'sessions', 'sites'];
8576
+ // The exact algorithms the Identity server signs identity tokens with: EdDSA by
8577
+ // default, RS256 when a client opts in via `id_token_signed_response_alg`. The
8578
+ // unverified client-side decode gates on this set so an unsigned (`none`) or
8579
+ // algorithm-confused token cannot masquerade as valid.
8580
+ const ACCEPTED_TOKEN_ALGS = ['EdDSA', 'RS256'];
8576
8581
  // A sign-in is "pending" from popup open until its result arrives; a marker
8577
8582
  // that outlives a lost result (a mobile opener-tab discard clears the message
8578
8583
  // listener but not sessionStorage) is what lets the next load self-heal.
8579
8584
  const PENDING_KEY = 'sv_signin_pending';
8580
8585
  const PENDING_TTL_MS = 10 * 60 * 1000;
8581
- // One-shot: the identifier the recovery bounce is re-minting for. Written only
8582
- // by the recovery bounce; the return leg consumes it to verify the redeemed
8583
- // result is for that same account (see consumeHandoffFragment).
8584
- const RECOVERY_HINT_KEY = 'sv_recovery_hint';
8585
- /**
8586
- * Normalize an identifier for equality, mirroring the server: trim, and
8587
- * lowercase emails (a value containing '@'). Phones compare trimmed-exact.
8588
- */
8589
- function normalizeIdentity(value) {
8590
- const trimmed = value.trim();
8591
- return trimmed.includes('@') ? trimmed.toLowerCase() : trimmed;
8592
- }
8593
8586
  /**
8594
8587
  * Read and parse the pending marker — JSON `{ at, hint }`, where `hint` is the
8595
8588
  * email or phone the sign-in was started with (null when the flow collects the
@@ -8985,14 +8978,16 @@ class IdentityModule {
8985
8978
  * Decode an identity token's claims WITHOUT verifying its signature.
8986
8979
  *
8987
8980
  * This is an UNVERIFIED, client-side decode: it parses the JWT payload and
8988
- * sanity-checks structure, `exp`, `iss`, `aud`, and the header `alg` (rejecting
8989
- * `none` and any non-EdDSA algorithm so an unsigned or algorithm-confused token
8990
- * cannot masquerade as valid). It does NOT verify the Ed25519 signature, so its
8991
- * result MUST NOT be trusted for any authorization decision.
8981
+ * sanity-checks structure, `exp`, `iss`, `aud`, and the header `alg` (accepting
8982
+ * only the algorithms the Identity server signs with, so an unsigned or
8983
+ * algorithm-confused token cannot masquerade as valid). It does NOT verify the
8984
+ * signature, so its result MUST NOT be trusted for any authorization decision.
8992
8985
  *
8993
8986
  * To trust a token (e.g. before keying records by `claims.svid`), verify the
8994
- * Ed25519 signature server-side against the Identity JWKS endpoint
8995
- * (`https://auth.sparkvault.com/{account_id}/.well-known/jwks.json`). See docs/SDK.md.
8987
+ * signature server-side against the Identity JWKS endpoint
8988
+ * (`https://auth.sparkvault.com/{account_id}/.well-known/jwks.json`), which
8989
+ * publishes the EdDSA and RS256 keys and selects the verifying key by `kid`.
8990
+ * See docs/SDK.md.
8996
8991
  */
8997
8992
  decodeToken(token) {
8998
8993
  if (!token || typeof token !== 'string') {
@@ -9007,10 +9002,12 @@ class IdentityModule {
9007
9002
  if (typeof header !== 'object' || header === null) {
9008
9003
  throw new ValidationError('Invalid token header');
9009
9004
  }
9010
- // Reject alg:none (unsigned) and any algorithm other than EdDSA. Identity
9011
- // tokens are Ed25519-signed; anything else is an unsigned or algorithm-
9012
- // confused token that could never pass server-side JWKS verification.
9013
- if (header.alg !== 'EdDSA') {
9005
+ // Accept exactly the algorithms the Identity server signs with; reject
9006
+ // everything else — `none` (unsigned), a symmetric or algorithm-confused
9007
+ // token, or an absent alg — none of which could pass the server-side JWKS
9008
+ // verification the host performs before trusting the token.
9009
+ const alg = header.alg;
9010
+ if (typeof alg !== 'string' || !ACCEPTED_TOKEN_ALGS.includes(alg)) {
9014
9011
  throw new ValidationError('Unsupported token algorithm');
9015
9012
  }
9016
9013
  const payload = JSON.parse(base64urlToString(payloadB64));
@@ -9091,11 +9088,9 @@ class IdentityModule {
9091
9088
  // (and a StrictMode double-invocation finds an empty hash and no-ops).
9092
9089
  let expectedState = null;
9093
9090
  let codeVerifier = null;
9094
- let expectedRecoveryIdentity = null;
9095
9091
  try {
9096
9092
  expectedState = sessionStorage.getItem('sv_oauth_state');
9097
9093
  codeVerifier = sessionStorage.getItem('sv_pkce_verifier');
9098
- expectedRecoveryIdentity = sessionStorage.getItem(RECOVERY_HINT_KEY);
9099
9094
  this.clearOneShotAuthState();
9100
9095
  }
9101
9096
  catch {
@@ -9116,6 +9111,12 @@ class IdentityModule {
9116
9111
  if (!codeVerifier) {
9117
9112
  throw new ValidationError('Sign-in session was lost — please sign in again.');
9118
9113
  }
9114
+ // WHICH account the code carries is settled where it is minted, never
9115
+ // here: the code is PKCE- and origin-bound to this tab, and a recovery
9116
+ // bounce only ever mints for the account its `login_hint` named (see
9117
+ // startRecoveryBounce). The redeemed result exposes just that account's
9118
+ // PRIMARY identifier — not necessarily the one the caller signed in
9119
+ // with — so re-checking it here could only discard valid sign-ins.
9119
9120
  const raw = await this.api.redeemHandoffCode(code, codeVerifier);
9120
9121
  const result = {
9121
9122
  token: raw.token,
@@ -9125,18 +9126,6 @@ class IdentityModule {
9125
9126
  sessionId: raw.session_id ?? undefined,
9126
9127
  refreshToken: raw.refresh_token ?? undefined,
9127
9128
  };
9128
- // A recovery bounce carries the identifier the interrupted sign-in was
9129
- // addressing, but /sso treats a hint matching no hub session like no hint
9130
- // at all (anti-oracle) — so the bounce can come back holding a DIFFERENT
9131
- // single-eligible account. The client is the one place that knows which
9132
- // account the person was signing in to: a mismatched result is discarded,
9133
- // never handed to the host (a wrong-account handoff). Silent — no
9134
- // onError — falling through to interactive sign-in, matching the recovery
9135
- // flow's non-oracle posture.
9136
- if (expectedRecoveryIdentity &&
9137
- normalizeIdentity(expectedRecoveryIdentity) !== normalizeIdentity(result.identity)) {
9138
- return null;
9139
- }
9140
9129
  options.onSuccess?.(result);
9141
9130
  return result;
9142
9131
  }
@@ -9157,22 +9146,12 @@ class IdentityModule {
9157
9146
  */
9158
9147
  async startRecoveryBounce(loginHint) {
9159
9148
  const sso = new URL(`${this.config.identityBaseUrl}/${this.config.accountId}/sso`);
9160
- // With several hub sessions, login_hint tells /sso which account the
9161
- // interrupted sign-in was addressing, so recovery never re-mints another.
9162
- // Also stash it one-shot: a hint matching no hub session deliberately
9163
- // behaves server-side like no hint at all (anti-oracle), so the return leg
9164
- // must verify the redeemed identity is the one this sign-in was for.
9165
- try {
9166
- if (loginHint) {
9167
- sessionStorage.setItem(RECOVERY_HINT_KEY, loginHint);
9168
- }
9169
- else {
9170
- sessionStorage.removeItem(RECOVERY_HINT_KEY);
9171
- }
9172
- }
9173
- catch {
9174
- // No storage → prepareHandoffNavigation bails below; nothing to verify.
9175
- }
9149
+ // With several hub sessions, login_hint names the account the interrupted
9150
+ // sign-in was addressing, and it is the whole wrong-account defence: /sso
9151
+ // mints silently ONLY for a hint that matched exactly one hub session's
9152
+ // verified identifiers — every unsatisfiable hint (a stranger's address, an
9153
+ // ambiguous match, an account not eligible here) comes back with no result
9154
+ // at all rather than a different account's.
9176
9155
  if (loginHint) {
9177
9156
  sso.searchParams.set('login_hint', loginHint);
9178
9157
  }
@@ -9180,16 +9159,6 @@ class IdentityModule {
9180
9159
  if (nav) {
9181
9160
  window.location.assign(nav);
9182
9161
  }
9183
- else {
9184
- // The bounce never left (no storage or WebCrypto) — retire the stash so
9185
- // it cannot linger into an unrelated later return leg.
9186
- try {
9187
- sessionStorage.removeItem(RECOVERY_HINT_KEY);
9188
- }
9189
- catch {
9190
- // Storage disabled: nothing was persisted, nothing to clear.
9191
- }
9192
- }
9193
9162
  }
9194
9163
  /**
9195
9164
  * Explicit `flow: 'redirect'` — navigate this tab to the interactive sign-in
@@ -9289,12 +9258,11 @@ class IdentityModule {
9289
9258
  window.history.replaceState(window.history.state, document.title, `${window.location.pathname}${window.location.search}`);
9290
9259
  }
9291
9260
  }
9292
- /** Drop the one-shot PKCE state/verifier (+ recovery hint) a bounce staged. */
9261
+ /** Drop the one-shot PKCE state/verifier a bounce staged. */
9293
9262
  clearOneShotAuthState() {
9294
9263
  try {
9295
9264
  sessionStorage.removeItem('sv_oauth_state');
9296
9265
  sessionStorage.removeItem('sv_pkce_verifier');
9297
- sessionStorage.removeItem(RECOVERY_HINT_KEY);
9298
9266
  }
9299
9267
  catch {
9300
9268
  // Storage disabled: nothing was persisted, nothing to clear.
@@ -14797,7 +14765,7 @@ class SparkVault {
14797
14765
  }
14798
14766
  /** Get the SDK version */
14799
14767
  static get version() {
14800
- return '9.0.0';
14768
+ return '9.1.0';
14801
14769
  }
14802
14770
  }
14803
14771
  if (typeof window !== 'undefined') {