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