@sparkvault/sdk-js 8.7.0 → 8.8.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 +106 -14
- package/dist/products/identity/api.d.ts +9 -1
- package/dist/products/identity/dialog-launcher.d.ts +0 -1
- package/dist/products/identity/index.d.ts +85 -14
- package/dist/products/identity/types.d.ts +30 -0
- package/dist/products/identity/utils.d.ts +33 -0
- package/dist/sparkvault.cjs.js +401 -45
- package/dist/sparkvault.cjs.js.map +1 -1
- package/dist/sparkvault.esm.js +401 -45
- package/dist/sparkvault.esm.js.map +1 -1
- package/dist/sparkvault.js +1 -1
- package/dist/sparkvault.js.map +1 -1
- package/package.json +1 -1
package/dist/sparkvault.cjs.js
CHANGED
|
@@ -778,6 +778,91 @@ function generateState() {
|
|
|
778
778
|
crypto.getRandomValues(array);
|
|
779
779
|
return Array.from(array, (b) => b.toString(16).padStart(2, '0')).join('');
|
|
780
780
|
}
|
|
781
|
+
/**
|
|
782
|
+
* Generate a PKCE code_verifier (RFC 7636): 32 random bytes, base64url —
|
|
783
|
+
* 43 URL-safe characters.
|
|
784
|
+
*/
|
|
785
|
+
function generateCodeVerifier() {
|
|
786
|
+
const array = new Uint8Array(32);
|
|
787
|
+
crypto.getRandomValues(array);
|
|
788
|
+
return base64urlEncode(array);
|
|
789
|
+
}
|
|
790
|
+
/**
|
|
791
|
+
* Compute the S256 PKCE code_challenge for a verifier:
|
|
792
|
+
* base64url(SHA-256(verifier)).
|
|
793
|
+
*/
|
|
794
|
+
async function computeS256Challenge(codeVerifier) {
|
|
795
|
+
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(codeVerifier));
|
|
796
|
+
return arrayBufferToBase64url(digest);
|
|
797
|
+
}
|
|
798
|
+
/**
|
|
799
|
+
* Build the sign-in page URL on the Identity origin. Single source for BOTH
|
|
800
|
+
* delivery modes: the popup (opener + postMessage) and the same-tab redirect
|
|
801
|
+
* (redirect_uri + state + code_challenge → one-time code in the fragment).
|
|
802
|
+
*/
|
|
803
|
+
function buildSigninUrl(config, options, extras) {
|
|
804
|
+
const params = new URLSearchParams({ origin: window.location.origin });
|
|
805
|
+
// Identifier prefill - the sign-in page collects it itself when absent.
|
|
806
|
+
if (options.email) {
|
|
807
|
+
params.set('email', options.email);
|
|
808
|
+
}
|
|
809
|
+
else if (options.phone) {
|
|
810
|
+
params.set('phone', options.phone);
|
|
811
|
+
}
|
|
812
|
+
// Auth context, in the query shape the sign-in page reads server-side:
|
|
813
|
+
// OIDC rides `auth_request`; simple redirect rides `mode=simple` + the
|
|
814
|
+
// opaque `session` (the page loads success/failure URLs from the session).
|
|
815
|
+
const authContext = options.authContext;
|
|
816
|
+
if (authContext?.authRequestId) {
|
|
817
|
+
params.set('auth_request', authContext.authRequestId);
|
|
818
|
+
}
|
|
819
|
+
else if (authContext?.simpleMode?.sessionId) {
|
|
820
|
+
params.set('mode', 'simple');
|
|
821
|
+
params.set('session', authContext.simpleMode.sessionId);
|
|
822
|
+
}
|
|
823
|
+
if (extras) {
|
|
824
|
+
params.set('redirect_uri', extras.redirectUri);
|
|
825
|
+
params.set('state', extras.state);
|
|
826
|
+
params.set('code_challenge', extras.codeChallenge);
|
|
827
|
+
}
|
|
828
|
+
return `${config.identityBaseUrl}/${config.accountId}/signin?${params}`;
|
|
829
|
+
}
|
|
830
|
+
/**
|
|
831
|
+
* Warm the connection to another origin by injecting `<link rel="preconnect">`
|
|
832
|
+
* hints (one plain for top-level navigations, one `crossorigin` for CORS
|
|
833
|
+
* fetches). The sign-in popup/redirect and every flow API call reuse the open
|
|
834
|
+
* connection, which removes the cold-connection stall mobile browsers hit on
|
|
835
|
+
* the first navigation to the Identity origin. No-op off-DOM, for the page's
|
|
836
|
+
* own origin, and when the hints already exist.
|
|
837
|
+
*/
|
|
838
|
+
function ensurePreconnect(baseUrl) {
|
|
839
|
+
if (typeof document === 'undefined' || typeof window === 'undefined' || !document.head) {
|
|
840
|
+
return;
|
|
841
|
+
}
|
|
842
|
+
let origin;
|
|
843
|
+
try {
|
|
844
|
+
origin = new URL(baseUrl).origin;
|
|
845
|
+
}
|
|
846
|
+
catch {
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
849
|
+
if (origin === window.location.origin) {
|
|
850
|
+
return;
|
|
851
|
+
}
|
|
852
|
+
for (const crossOrigin of [false, true]) {
|
|
853
|
+
const selector = `link[rel="preconnect"][href="${origin}"]${crossOrigin ? '[crossorigin]' : ':not([crossorigin])'}`;
|
|
854
|
+
if (document.head.querySelector(selector)) {
|
|
855
|
+
continue;
|
|
856
|
+
}
|
|
857
|
+
const link = document.createElement('link');
|
|
858
|
+
link.rel = 'preconnect';
|
|
859
|
+
link.href = origin;
|
|
860
|
+
if (crossOrigin) {
|
|
861
|
+
link.crossOrigin = 'anonymous';
|
|
862
|
+
}
|
|
863
|
+
document.head.appendChild(link);
|
|
864
|
+
}
|
|
865
|
+
}
|
|
781
866
|
|
|
782
867
|
/**
|
|
783
868
|
* Identity API Client
|
|
@@ -863,6 +948,19 @@ class IdentityApi extends SparkVaultApi {
|
|
|
863
948
|
const body = identityType === 'phone' ? { phone: identity } : { email: identity };
|
|
864
949
|
return this.request('POST', '/passkey/check', body);
|
|
865
950
|
}
|
|
951
|
+
/**
|
|
952
|
+
* Redeem a one-time sign-in code (with its PKCE verifier) for the sign-in
|
|
953
|
+
* result — the shared return leg for `flow: 'redirect'` and the silent SSO
|
|
954
|
+
* self-heal. The code arrives in the URL fragment after the hosted page
|
|
955
|
+
* navigates back; it is single-use, 60s-lived, and bound to this page's
|
|
956
|
+
* origin server-side.
|
|
957
|
+
*/
|
|
958
|
+
async redeemHandoffCode(code, codeVerifier) {
|
|
959
|
+
return this.request('POST', '/signin/redeem', {
|
|
960
|
+
code,
|
|
961
|
+
code_verifier: codeVerifier,
|
|
962
|
+
});
|
|
963
|
+
}
|
|
866
964
|
/**
|
|
867
965
|
* Build auth context params for API requests (OIDC/simple mode).
|
|
868
966
|
* Converts AuthContext to snake_case params expected by backend.
|
|
@@ -7817,7 +7915,7 @@ class DialogLauncher {
|
|
|
7817
7915
|
const generation = ++this.generation;
|
|
7818
7916
|
this.popupLauncher
|
|
7819
7917
|
.openPopup({
|
|
7820
|
-
url: this.
|
|
7918
|
+
url: buildSigninUrl(this.config, this.options),
|
|
7821
7919
|
windowName: 'sparkvault-login',
|
|
7822
7920
|
resultType: LOGIN_RESULT_TYPE,
|
|
7823
7921
|
readyType: LOGIN_READY_TYPE,
|
|
@@ -7838,28 +7936,6 @@ class DialogLauncher {
|
|
|
7838
7936
|
.catch((error) => { if (generation === this.generation)
|
|
7839
7937
|
this.handleError(error); });
|
|
7840
7938
|
}
|
|
7841
|
-
buildPopupUrl() {
|
|
7842
|
-
const params = new URLSearchParams({ origin: window.location.origin });
|
|
7843
|
-
// Identifier prefill - the popup collects it itself when absent.
|
|
7844
|
-
if (this.options.email) {
|
|
7845
|
-
params.set('email', this.options.email);
|
|
7846
|
-
}
|
|
7847
|
-
else if (this.options.phone) {
|
|
7848
|
-
params.set('phone', this.options.phone);
|
|
7849
|
-
}
|
|
7850
|
-
// Auth context, in the query shape the sign-in page reads server-side:
|
|
7851
|
-
// OIDC rides `auth_request`; simple redirect rides `mode=simple` + the
|
|
7852
|
-
// opaque `session` (the page loads success/failure URLs from the session).
|
|
7853
|
-
const authContext = this.options.authContext;
|
|
7854
|
-
if (authContext?.authRequestId) {
|
|
7855
|
-
params.set('auth_request', authContext.authRequestId);
|
|
7856
|
-
}
|
|
7857
|
-
else if (authContext?.simpleMode?.sessionId) {
|
|
7858
|
-
params.set('mode', 'simple');
|
|
7859
|
-
params.set('session', authContext.simpleMode.sessionId);
|
|
7860
|
-
}
|
|
7861
|
-
return `${this.config.identityBaseUrl}/${this.config.accountId}/signin?${params}`;
|
|
7862
|
-
}
|
|
7863
7939
|
handleResult(message) {
|
|
7864
7940
|
if (this.settled)
|
|
7865
7941
|
return;
|
|
@@ -8302,6 +8378,40 @@ function attachClickHandler(selector, onClick) {
|
|
|
8302
8378
|
* sv.products.identity.attach('.login-btn');
|
|
8303
8379
|
*/
|
|
8304
8380
|
const PORTAL_SECTIONS = ['home', 'identity', 'security', 'sessions', 'sites'];
|
|
8381
|
+
// A sign-in is "pending" from popup open until its result arrives; a marker
|
|
8382
|
+
// that outlives a lost result (a mobile opener-tab discard clears the message
|
|
8383
|
+
// listener but not sessionStorage) is what lets the next load self-heal.
|
|
8384
|
+
const PENDING_KEY = 'sv_signin_pending';
|
|
8385
|
+
const PENDING_TTL_MS = 10 * 60 * 1000;
|
|
8386
|
+
/** True when the pending marker is present and still within its TTL. */
|
|
8387
|
+
function hasFreshPendingMarker() {
|
|
8388
|
+
let raw = null;
|
|
8389
|
+
try {
|
|
8390
|
+
raw = sessionStorage.getItem(PENDING_KEY);
|
|
8391
|
+
}
|
|
8392
|
+
catch {
|
|
8393
|
+
return false;
|
|
8394
|
+
}
|
|
8395
|
+
const at = raw ? Number(raw) : NaN;
|
|
8396
|
+
return Number.isFinite(at) && Date.now() - at < PENDING_TTL_MS;
|
|
8397
|
+
}
|
|
8398
|
+
/**
|
|
8399
|
+
* Whether `handleRedirectResult()` has anything to do on this load — a returned
|
|
8400
|
+
* sign-in fragment to consume, or a pending sign-in to self-heal. Public + side
|
|
8401
|
+
* effect-free (peeks, never consumes) so a host that defers SDK init for
|
|
8402
|
+
* performance can cheaply decide whether to boot the SDK on a given page load,
|
|
8403
|
+
* WITHOUT reaching into the SDK's internal storage keys. Exposed as
|
|
8404
|
+
* `SparkVault.isRecoveryPending()`.
|
|
8405
|
+
*/
|
|
8406
|
+
function isSignInRecoveryPending() {
|
|
8407
|
+
if (typeof window === 'undefined') {
|
|
8408
|
+
return false;
|
|
8409
|
+
}
|
|
8410
|
+
if (/[#&](sv_code|sv_error|sso)=/.test(window.location.hash || '')) {
|
|
8411
|
+
return true;
|
|
8412
|
+
}
|
|
8413
|
+
return hasFreshPendingMarker();
|
|
8414
|
+
}
|
|
8305
8415
|
function isTokenClaims(payload) {
|
|
8306
8416
|
if (typeof payload !== 'object' || payload === null) {
|
|
8307
8417
|
return false;
|
|
@@ -8327,6 +8437,10 @@ class IdentityModule {
|
|
|
8327
8437
|
if (config.preloadConfig) {
|
|
8328
8438
|
this.api.preloadConfig();
|
|
8329
8439
|
}
|
|
8440
|
+
// Warm the connection to the Identity origin before sign-in is invoked:
|
|
8441
|
+
// the popup/redirect navigation and every flow API call reuse it, removing
|
|
8442
|
+
// the cold-connection stall mobile browsers hit on first navigation.
|
|
8443
|
+
ensurePreconnect(config.identityBaseUrl);
|
|
8330
8444
|
}
|
|
8331
8445
|
/**
|
|
8332
8446
|
* Build a deep link into the person's auth.sv Identity portal — where they
|
|
@@ -8433,12 +8547,18 @@ class IdentityModule {
|
|
|
8433
8547
|
typeof navigator.credentials.get === 'function');
|
|
8434
8548
|
}
|
|
8435
8549
|
/**
|
|
8436
|
-
*
|
|
8550
|
+
* Sign in through the first-party SparkVault sign-in popup.
|
|
8437
8551
|
*
|
|
8438
8552
|
* This is the single programmatic entry point; `attach()` binds it to element
|
|
8439
|
-
* clicks.
|
|
8440
|
-
*
|
|
8441
|
-
*
|
|
8553
|
+
* clicks. The popup runs the flow top-level on the Identity origin (so
|
|
8554
|
+
* login's `__Host-` hub cookie is a first-party write and cross-domain SSO
|
|
8555
|
+
* works) and posts the result back — the promise resolves with it.
|
|
8556
|
+
*
|
|
8557
|
+
* Resilience: pair this with `handleRedirectResult()` on page load. If the
|
|
8558
|
+
* popup's result is ever lost (a mobile browser discards the backgrounded
|
|
8559
|
+
* opener tab), the next load silently self-heals from the SSO session the
|
|
8560
|
+
* login established. `flow: 'redirect'` opts a caller out of the popup into a
|
|
8561
|
+
* same-tab navigation instead.
|
|
8442
8562
|
*
|
|
8443
8563
|
* @example
|
|
8444
8564
|
* const result = await sv.products.identity.signIn();
|
|
@@ -8448,9 +8568,10 @@ class IdentityModule {
|
|
|
8448
8568
|
return this.runFlow(options);
|
|
8449
8569
|
}
|
|
8450
8570
|
/**
|
|
8451
|
-
* Core flow runner.
|
|
8452
|
-
* (`renderMode: 'local'` /
|
|
8453
|
-
*
|
|
8571
|
+
* Core flow runner. Default mode (no `target`) opens the popup; `flow:
|
|
8572
|
+
* 'redirect'` navigates the tab instead; local mode (`renderMode: 'local'` /
|
|
8573
|
+
* `target`) renders into the current document and powers the
|
|
8574
|
+
* SparkVault-hosted sign-in surfaces (see the internal `render`).
|
|
8454
8575
|
*/
|
|
8455
8576
|
async runFlow(options = {}) {
|
|
8456
8577
|
const redirectResult = await this.consumeRedirectResult();
|
|
@@ -8476,9 +8597,10 @@ class IdentityModule {
|
|
|
8476
8597
|
backdropBlur: options.backdropBlur ?? this.config.backdropBlur,
|
|
8477
8598
|
};
|
|
8478
8599
|
// Inline rendering (target) and explicit local mode run the flow in this
|
|
8479
|
-
// document
|
|
8480
|
-
//
|
|
8481
|
-
//
|
|
8600
|
+
// document — that is how the SparkVault-hosted sign-in page itself runs
|
|
8601
|
+
// the flow. Otherwise sign-in leaves this page: a top-level redirect to
|
|
8602
|
+
// the sign-in page on the Identity origin (so login's first-party SSO
|
|
8603
|
+
// cookie persists), returning via handleRedirectResult().
|
|
8482
8604
|
const isInline = !!options.target;
|
|
8483
8605
|
const useLocal = options.renderMode === 'local' || isInline;
|
|
8484
8606
|
let resolveRef;
|
|
@@ -8514,14 +8636,35 @@ class IdentityModule {
|
|
|
8514
8636
|
if (!useLocal) {
|
|
8515
8637
|
// When the preloaded config has already failed *permanently* — this origin
|
|
8516
8638
|
// is not a verified domain, or the account hasn't configured Identity —
|
|
8517
|
-
// the
|
|
8639
|
+
// the flow cannot help: the IdP will reject the unverified origin. Render
|
|
8518
8640
|
// the error dialog in-page right away instead. Transient failures fall
|
|
8519
|
-
// through
|
|
8641
|
+
// through, and the popup/sign-in page surfaces them in its own dialog.
|
|
8520
8642
|
if (await this.preloadedConfigPermanentlyFailed()) {
|
|
8521
8643
|
startLocalRender(new ModalContainer());
|
|
8522
8644
|
return settledPromise;
|
|
8523
8645
|
}
|
|
8524
|
-
|
|
8646
|
+
// Explicit `flow: 'redirect'` navigates this tab to the sign-in page and
|
|
8647
|
+
// returns via handleRedirectResult() (the escape hatch for hosts that
|
|
8648
|
+
// want to skip the popup on mobile). Then this page is unloading, so the
|
|
8649
|
+
// promise never settles on success; it rejects only if the redirect
|
|
8650
|
+
// cannot start.
|
|
8651
|
+
if (mergedOptions.flow === 'redirect') {
|
|
8652
|
+
if (!await this.startRedirectFlow(mergedOptions)) {
|
|
8653
|
+
hostCallbacks.onError(new ValidationError('Sign-in requires browser session storage. Enable site data for this site and try again.'));
|
|
8654
|
+
}
|
|
8655
|
+
return settledPromise;
|
|
8656
|
+
}
|
|
8657
|
+
// Default: the first-party popup on the Identity origin. Mark a sign-in
|
|
8658
|
+
// as in flight so that if its result is lost (a mobile browser discards
|
|
8659
|
+
// the backgrounded opener tab, killing the postMessage listener),
|
|
8660
|
+
// handleRedirectResult() can silently self-heal on the next load from
|
|
8661
|
+
// the SSO session the popup login established.
|
|
8662
|
+
this.markSignInPending();
|
|
8663
|
+
this.launcher = new DialogLauncher(this.config, this.api, mergedOptions, {
|
|
8664
|
+
onSuccess: (result) => { this.clearSignInPending(); hostCallbacks.onSuccess(result); },
|
|
8665
|
+
onError: (error) => { this.clearSignInPending(); hostCallbacks.onError(error); },
|
|
8666
|
+
onCancel: () => { this.clearSignInPending(); hostCallbacks.onCancel(); },
|
|
8667
|
+
});
|
|
8525
8668
|
this.launcher.start();
|
|
8526
8669
|
return settledPromise;
|
|
8527
8670
|
}
|
|
@@ -8628,13 +8771,217 @@ class IdentityModule {
|
|
|
8628
8771
|
return payload;
|
|
8629
8772
|
}
|
|
8630
8773
|
/**
|
|
8631
|
-
* Complete
|
|
8632
|
-
*
|
|
8633
|
-
*
|
|
8634
|
-
*
|
|
8635
|
-
*
|
|
8636
|
-
*
|
|
8774
|
+
* Complete a sign-in on page load — the return leg of `signIn()`. Call once
|
|
8775
|
+
* on every page load; it resolves `null` when there is nothing to do.
|
|
8776
|
+
*
|
|
8777
|
+
* It handles two cases:
|
|
8778
|
+
* 1. A one-time code is in the URL fragment (`#sv_code=…`) — the popup's
|
|
8779
|
+
* result was lost and self-heal navigated here, or `flow: 'redirect'`
|
|
8780
|
+
* returned. Redeem it server-side and hand the full result to
|
|
8781
|
+
* `onSuccess` (and resolve with it).
|
|
8782
|
+
* 2. A sign-in was in flight but never delivered (a mobile browser
|
|
8783
|
+
* discarded the backgrounded opener tab, killing the popup's
|
|
8784
|
+
* postMessage) — silently re-mint from the SSO session the completed
|
|
8785
|
+
* popup login established, by navigating this tab through the silent SSO
|
|
8786
|
+
* endpoint and straight back with a code (case 1 on return). Nothing is
|
|
8787
|
+
* shown; if there is no live session it comes back logged-out, no oracle.
|
|
8788
|
+
*
|
|
8789
|
+
* The code is one-time, PKCE-bound, and origin-locked — never the token. The
|
|
8790
|
+
* host must still verify `result.token` server-side via the Identity JWKS.
|
|
8791
|
+
*
|
|
8792
|
+
* @example Call on page load; sign-in clicks stay unchanged.
|
|
8793
|
+
* sv.products.identity.handleRedirectResult({
|
|
8794
|
+
* onSuccess: (result) => createSession(result.token),
|
|
8795
|
+
* }).catch((error) => showError(error));
|
|
8637
8796
|
*/
|
|
8797
|
+
async handleRedirectResult(options = {}) {
|
|
8798
|
+
if (typeof window === 'undefined') {
|
|
8799
|
+
return null;
|
|
8800
|
+
}
|
|
8801
|
+
const params = new URLSearchParams((window.location.hash || '').slice(1));
|
|
8802
|
+
if (params.get('sv_code') || params.get('sv_error')) {
|
|
8803
|
+
return this.consumeHandoffFragment(params, options);
|
|
8804
|
+
}
|
|
8805
|
+
// A silent-SSO recovery that found no live session returns `#sso=…`; strip
|
|
8806
|
+
// it so the logged-out load is clean, retire the one-shot state/verifier the
|
|
8807
|
+
// outbound bounce staged (dead now — no code came back), and do not re-bounce
|
|
8808
|
+
// (handled below).
|
|
8809
|
+
if (params.get('sso')) {
|
|
8810
|
+
this.clearUrlFragment();
|
|
8811
|
+
this.clearOneShotAuthState();
|
|
8812
|
+
}
|
|
8813
|
+
// No returned result. If a sign-in was in flight and its result never
|
|
8814
|
+
// arrived, self-heal once from the SSO session (mobile opener-tab discard).
|
|
8815
|
+
if (this.consumeSignInPending()) {
|
|
8816
|
+
await this.startRecoveryBounce();
|
|
8817
|
+
}
|
|
8818
|
+
return null;
|
|
8819
|
+
}
|
|
8820
|
+
/** Redeem a returned `#sv_code` (or surface a returned `#sv_error`). */
|
|
8821
|
+
async consumeHandoffFragment(params, options) {
|
|
8822
|
+
const code = params.get('sv_code');
|
|
8823
|
+
const errorCode = params.get('sv_error');
|
|
8824
|
+
// A returned fragment means this sign-in resolved (redeem, cancel, or
|
|
8825
|
+
// error), so retire any pending marker — otherwise it could fire one
|
|
8826
|
+
// spurious silent bounce on a later load inside its TTL.
|
|
8827
|
+
this.clearSignInPending();
|
|
8828
|
+
// Consume exactly once: fragment and one-shot state/verifier are cleared
|
|
8829
|
+
// up front on every path, so a failing result cannot wedge future loads
|
|
8830
|
+
// (and a StrictMode double-invocation finds an empty hash and no-ops).
|
|
8831
|
+
let expectedState = null;
|
|
8832
|
+
let codeVerifier = null;
|
|
8833
|
+
try {
|
|
8834
|
+
expectedState = sessionStorage.getItem('sv_oauth_state');
|
|
8835
|
+
codeVerifier = sessionStorage.getItem('sv_pkce_verifier');
|
|
8836
|
+
this.clearOneShotAuthState();
|
|
8837
|
+
}
|
|
8838
|
+
catch {
|
|
8839
|
+
// Storage disabled (private mode): state/verifier were never persisted.
|
|
8840
|
+
}
|
|
8841
|
+
this.clearUrlFragment();
|
|
8842
|
+
try {
|
|
8843
|
+
if (errorCode) {
|
|
8844
|
+
if (errorCode === 'cancelled') {
|
|
8845
|
+
options.onCancel?.();
|
|
8846
|
+
return null;
|
|
8847
|
+
}
|
|
8848
|
+
throw new SparkVaultError('Sign-in could not be completed. Please try again.', 'REDIRECT_SIGNIN_FAILED');
|
|
8849
|
+
}
|
|
8850
|
+
if (expectedState && params.get('state') !== expectedState) {
|
|
8851
|
+
throw new ValidationError('Invalid OAuth state');
|
|
8852
|
+
}
|
|
8853
|
+
if (!codeVerifier) {
|
|
8854
|
+
throw new ValidationError('Sign-in session was lost — please sign in again.');
|
|
8855
|
+
}
|
|
8856
|
+
const raw = await this.api.redeemHandoffCode(code, codeVerifier);
|
|
8857
|
+
const result = {
|
|
8858
|
+
token: raw.token,
|
|
8859
|
+
identity: raw.identity,
|
|
8860
|
+
identityType: raw.identity_type,
|
|
8861
|
+
svid: raw.svid ?? undefined,
|
|
8862
|
+
sessionId: raw.session_id ?? undefined,
|
|
8863
|
+
refreshToken: raw.refresh_token ?? undefined,
|
|
8864
|
+
};
|
|
8865
|
+
options.onSuccess?.(result);
|
|
8866
|
+
return result;
|
|
8867
|
+
}
|
|
8868
|
+
catch (error) {
|
|
8869
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
8870
|
+
options.onError?.(err);
|
|
8871
|
+
throw err;
|
|
8872
|
+
}
|
|
8873
|
+
}
|
|
8874
|
+
/**
|
|
8875
|
+
* Silently re-mint the sign-in result from the SSO session the completed
|
|
8876
|
+
* popup login left behind. Navigates this tab through the silent SSO endpoint
|
|
8877
|
+
* (a top-level GET, so the `SameSite=Lax` hub cookie rides along) which,
|
|
8878
|
+
* PKCE-bound, returns a one-time code in the fragment — handled by case 1 on
|
|
8879
|
+
* return. No-op (stays put) if session storage or WebCrypto is unavailable,
|
|
8880
|
+
* so a hostile/degraded environment simply never recovers rather than
|
|
8881
|
+
* erroring.
|
|
8882
|
+
*/
|
|
8883
|
+
async startRecoveryBounce() {
|
|
8884
|
+
const nav = await this.prepareHandoffNavigation(`${this.config.identityBaseUrl}/${this.config.accountId}/sso`);
|
|
8885
|
+
if (nav) {
|
|
8886
|
+
window.location.assign(nav);
|
|
8887
|
+
}
|
|
8888
|
+
}
|
|
8889
|
+
/**
|
|
8890
|
+
* Explicit `flow: 'redirect'` — navigate this tab to the interactive sign-in
|
|
8891
|
+
* page. Plain sign-in carries redirect_uri + state + PKCE challenge and
|
|
8892
|
+
* returns via the fragment code; OIDC/simple modes navigate bare (their
|
|
8893
|
+
* server-rendered pages drive the server-validated redirect back).
|
|
8894
|
+
*
|
|
8895
|
+
* @returns false when the return leg cannot be armed (no session storage or
|
|
8896
|
+
* WebCrypto) — the caller surfaces an actionable error instead of a
|
|
8897
|
+
* silent dead-end.
|
|
8898
|
+
*/
|
|
8899
|
+
async startRedirectFlow(options) {
|
|
8900
|
+
const authContext = options.authContext;
|
|
8901
|
+
if (authContext?.authRequestId || authContext?.simpleMode?.sessionId) {
|
|
8902
|
+
window.location.assign(buildSigninUrl(this.config, options));
|
|
8903
|
+
return true;
|
|
8904
|
+
}
|
|
8905
|
+
const url = await this.prepareHandoffNavigation(buildSigninUrl(this.config, options), (extras) => buildSigninUrl(this.config, options, extras));
|
|
8906
|
+
if (!url) {
|
|
8907
|
+
return false;
|
|
8908
|
+
}
|
|
8909
|
+
window.location.assign(url);
|
|
8910
|
+
return true;
|
|
8911
|
+
}
|
|
8912
|
+
/**
|
|
8913
|
+
* Arm the PKCE + state return leg for a handoff navigation and return the URL
|
|
8914
|
+
* to navigate to (redirect_uri = this exact page; query preserved for app
|
|
8915
|
+
* state, fragment dropped since it is the result channel). Returns null when
|
|
8916
|
+
* session storage or WebCrypto is unavailable so callers can degrade.
|
|
8917
|
+
*/
|
|
8918
|
+
async prepareHandoffNavigation(base, build) {
|
|
8919
|
+
const state = generateState();
|
|
8920
|
+
const codeVerifier = generateCodeVerifier();
|
|
8921
|
+
// Derive the challenge BEFORE persisting: a WebCrypto failure (insecure
|
|
8922
|
+
// context) then leaves no orphaned state/verifier behind in storage.
|
|
8923
|
+
let codeChallenge;
|
|
8924
|
+
try {
|
|
8925
|
+
codeChallenge = await computeS256Challenge(codeVerifier);
|
|
8926
|
+
}
|
|
8927
|
+
catch {
|
|
8928
|
+
return null;
|
|
8929
|
+
}
|
|
8930
|
+
try {
|
|
8931
|
+
sessionStorage.setItem('sv_oauth_state', state);
|
|
8932
|
+
sessionStorage.setItem('sv_pkce_verifier', codeVerifier);
|
|
8933
|
+
}
|
|
8934
|
+
catch {
|
|
8935
|
+
return null;
|
|
8936
|
+
}
|
|
8937
|
+
const redirectUri = `${window.location.origin}${window.location.pathname}${window.location.search}`;
|
|
8938
|
+
if (build) {
|
|
8939
|
+
return build({ redirectUri, state, codeChallenge });
|
|
8940
|
+
}
|
|
8941
|
+
const url = new URL(base);
|
|
8942
|
+
url.searchParams.set('redirect_uri', redirectUri);
|
|
8943
|
+
url.searchParams.set('state', state);
|
|
8944
|
+
url.searchParams.set('code_challenge', codeChallenge);
|
|
8945
|
+
return url.toString();
|
|
8946
|
+
}
|
|
8947
|
+
markSignInPending() {
|
|
8948
|
+
try {
|
|
8949
|
+
sessionStorage.setItem(PENDING_KEY, String(Date.now()));
|
|
8950
|
+
}
|
|
8951
|
+
catch {
|
|
8952
|
+
// No storage → no self-heal; the popup still delivers when it can.
|
|
8953
|
+
}
|
|
8954
|
+
}
|
|
8955
|
+
clearSignInPending() {
|
|
8956
|
+
try {
|
|
8957
|
+
sessionStorage.removeItem(PENDING_KEY);
|
|
8958
|
+
}
|
|
8959
|
+
catch {
|
|
8960
|
+
// Nothing to clear.
|
|
8961
|
+
}
|
|
8962
|
+
}
|
|
8963
|
+
/** True once (then cleared) if a recent sign-in is still awaiting its result. */
|
|
8964
|
+
consumeSignInPending() {
|
|
8965
|
+
const fresh = hasFreshPendingMarker();
|
|
8966
|
+
this.clearSignInPending();
|
|
8967
|
+
return fresh;
|
|
8968
|
+
}
|
|
8969
|
+
/** Remove the result fragment from the URL bar (and history entry). */
|
|
8970
|
+
clearUrlFragment() {
|
|
8971
|
+
if (typeof window.history?.replaceState === 'function') {
|
|
8972
|
+
window.history.replaceState(window.history.state, document.title, `${window.location.pathname}${window.location.search}`);
|
|
8973
|
+
}
|
|
8974
|
+
}
|
|
8975
|
+
/** Drop the one-shot PKCE state/verifier a redirect bounce staged. */
|
|
8976
|
+
clearOneShotAuthState() {
|
|
8977
|
+
try {
|
|
8978
|
+
sessionStorage.removeItem('sv_oauth_state');
|
|
8979
|
+
sessionStorage.removeItem('sv_pkce_verifier');
|
|
8980
|
+
}
|
|
8981
|
+
catch {
|
|
8982
|
+
// Storage disabled: nothing was persisted, nothing to clear.
|
|
8983
|
+
}
|
|
8984
|
+
}
|
|
8638
8985
|
async consumeRedirectResult() {
|
|
8639
8986
|
if (typeof window === 'undefined' || !window.location.hash) {
|
|
8640
8987
|
return null;
|
|
@@ -8674,9 +9021,7 @@ class IdentityModule {
|
|
|
8674
9021
|
};
|
|
8675
9022
|
}
|
|
8676
9023
|
finally {
|
|
8677
|
-
|
|
8678
|
-
window.history.replaceState(window.history.state, document.title, `${window.location.pathname}${window.location.search}`);
|
|
8679
|
-
}
|
|
9024
|
+
this.clearUrlFragment();
|
|
8680
9025
|
try {
|
|
8681
9026
|
sessionStorage.removeItem('sv_oauth_state');
|
|
8682
9027
|
sessionStorage.removeItem('sv_oauth_email');
|
|
@@ -13285,6 +13630,17 @@ class SparkVault {
|
|
|
13285
13630
|
static init(config) {
|
|
13286
13631
|
return new SparkVault(config);
|
|
13287
13632
|
}
|
|
13633
|
+
/**
|
|
13634
|
+
* Whether a sign-in needs completing on this page load — a returned handoff
|
|
13635
|
+
* fragment, or a popup whose result was lost (a mobile opener-tab discard).
|
|
13636
|
+
* Side effect-free and instance-free, so a host that defers SDK init for
|
|
13637
|
+
* performance can cheaply decide whether to boot the SDK and call
|
|
13638
|
+
* `products.identity.handleRedirectResult()` on a given load, without reaching
|
|
13639
|
+
* into the SDK's internal storage keys.
|
|
13640
|
+
*/
|
|
13641
|
+
static isRecoveryPending() {
|
|
13642
|
+
return isSignInRecoveryPending();
|
|
13643
|
+
}
|
|
13288
13644
|
/** Get the SDK version */
|
|
13289
13645
|
static get version() {
|
|
13290
13646
|
return '__VERSION__';
|