@sparkvault/sdk-js 8.7.0 → 8.8.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.d.ts +97 -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 +76 -14
- package/dist/products/identity/types.d.ts +30 -0
- package/dist/products/identity/utils.d.ts +33 -0
- package/dist/sparkvault.cjs.js +356 -45
- package/dist/sparkvault.cjs.js.map +1 -1
- package/dist/sparkvault.esm.js +356 -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;
|
|
@@ -8323,6 +8399,10 @@ class IdentityModule {
|
|
|
8323
8399
|
if (config.preloadConfig) {
|
|
8324
8400
|
this.api.preloadConfig();
|
|
8325
8401
|
}
|
|
8402
|
+
// Warm the connection to the Identity origin before sign-in is invoked:
|
|
8403
|
+
// the popup/redirect navigation and every flow API call reuse it, removing
|
|
8404
|
+
// the cold-connection stall mobile browsers hit on first navigation.
|
|
8405
|
+
ensurePreconnect(config.identityBaseUrl);
|
|
8326
8406
|
}
|
|
8327
8407
|
/**
|
|
8328
8408
|
* Build a deep link into the person's auth.sv Identity portal — where they
|
|
@@ -8429,12 +8509,18 @@ class IdentityModule {
|
|
|
8429
8509
|
typeof navigator.credentials.get === 'function');
|
|
8430
8510
|
}
|
|
8431
8511
|
/**
|
|
8432
|
-
*
|
|
8512
|
+
* Sign in through the first-party SparkVault sign-in popup.
|
|
8433
8513
|
*
|
|
8434
8514
|
* This is the single programmatic entry point; `attach()` binds it to element
|
|
8435
|
-
* clicks.
|
|
8436
|
-
*
|
|
8437
|
-
*
|
|
8515
|
+
* clicks. The popup runs the flow top-level on the Identity origin (so
|
|
8516
|
+
* login's `__Host-` hub cookie is a first-party write and cross-domain SSO
|
|
8517
|
+
* works) and posts the result back — the promise resolves with it.
|
|
8518
|
+
*
|
|
8519
|
+
* Resilience: pair this with `handleRedirectResult()` on page load. If the
|
|
8520
|
+
* popup's result is ever lost (a mobile browser discards the backgrounded
|
|
8521
|
+
* opener tab), the next load silently self-heals from the SSO session the
|
|
8522
|
+
* login established. `flow: 'redirect'` opts a caller out of the popup into a
|
|
8523
|
+
* same-tab navigation instead.
|
|
8438
8524
|
*
|
|
8439
8525
|
* @example
|
|
8440
8526
|
* const result = await sv.products.identity.signIn();
|
|
@@ -8444,9 +8530,10 @@ class IdentityModule {
|
|
|
8444
8530
|
return this.runFlow(options);
|
|
8445
8531
|
}
|
|
8446
8532
|
/**
|
|
8447
|
-
* Core flow runner.
|
|
8448
|
-
* (`renderMode: 'local'` /
|
|
8449
|
-
*
|
|
8533
|
+
* Core flow runner. Default mode (no `target`) opens the popup; `flow:
|
|
8534
|
+
* 'redirect'` navigates the tab instead; local mode (`renderMode: 'local'` /
|
|
8535
|
+
* `target`) renders into the current document and powers the
|
|
8536
|
+
* SparkVault-hosted sign-in surfaces (see the internal `render`).
|
|
8450
8537
|
*/
|
|
8451
8538
|
async runFlow(options = {}) {
|
|
8452
8539
|
const redirectResult = await this.consumeRedirectResult();
|
|
@@ -8472,9 +8559,10 @@ class IdentityModule {
|
|
|
8472
8559
|
backdropBlur: options.backdropBlur ?? this.config.backdropBlur,
|
|
8473
8560
|
};
|
|
8474
8561
|
// Inline rendering (target) and explicit local mode run the flow in this
|
|
8475
|
-
// document
|
|
8476
|
-
//
|
|
8477
|
-
//
|
|
8562
|
+
// document — that is how the SparkVault-hosted sign-in page itself runs
|
|
8563
|
+
// the flow. Otherwise sign-in leaves this page: a top-level redirect to
|
|
8564
|
+
// the sign-in page on the Identity origin (so login's first-party SSO
|
|
8565
|
+
// cookie persists), returning via handleRedirectResult().
|
|
8478
8566
|
const isInline = !!options.target;
|
|
8479
8567
|
const useLocal = options.renderMode === 'local' || isInline;
|
|
8480
8568
|
let resolveRef;
|
|
@@ -8510,14 +8598,35 @@ class IdentityModule {
|
|
|
8510
8598
|
if (!useLocal) {
|
|
8511
8599
|
// When the preloaded config has already failed *permanently* — this origin
|
|
8512
8600
|
// is not a verified domain, or the account hasn't configured Identity —
|
|
8513
|
-
// the
|
|
8601
|
+
// the flow cannot help: the IdP will reject the unverified origin. Render
|
|
8514
8602
|
// the error dialog in-page right away instead. Transient failures fall
|
|
8515
|
-
// through
|
|
8603
|
+
// through, and the popup/sign-in page surfaces them in its own dialog.
|
|
8516
8604
|
if (await this.preloadedConfigPermanentlyFailed()) {
|
|
8517
8605
|
startLocalRender(new ModalContainer());
|
|
8518
8606
|
return settledPromise;
|
|
8519
8607
|
}
|
|
8520
|
-
|
|
8608
|
+
// Explicit `flow: 'redirect'` navigates this tab to the sign-in page and
|
|
8609
|
+
// returns via handleRedirectResult() (the escape hatch for hosts that
|
|
8610
|
+
// want to skip the popup on mobile). Then this page is unloading, so the
|
|
8611
|
+
// promise never settles on success; it rejects only if the redirect
|
|
8612
|
+
// cannot start.
|
|
8613
|
+
if (mergedOptions.flow === 'redirect') {
|
|
8614
|
+
if (!await this.startRedirectFlow(mergedOptions)) {
|
|
8615
|
+
hostCallbacks.onError(new ValidationError('Sign-in requires browser session storage. Enable site data for this site and try again.'));
|
|
8616
|
+
}
|
|
8617
|
+
return settledPromise;
|
|
8618
|
+
}
|
|
8619
|
+
// Default: the first-party popup on the Identity origin. Mark a sign-in
|
|
8620
|
+
// as in flight so that if its result is lost (a mobile browser discards
|
|
8621
|
+
// the backgrounded opener tab, killing the postMessage listener),
|
|
8622
|
+
// handleRedirectResult() can silently self-heal on the next load from
|
|
8623
|
+
// the SSO session the popup login established.
|
|
8624
|
+
this.markSignInPending();
|
|
8625
|
+
this.launcher = new DialogLauncher(this.config, this.api, mergedOptions, {
|
|
8626
|
+
onSuccess: (result) => { this.clearSignInPending(); hostCallbacks.onSuccess(result); },
|
|
8627
|
+
onError: (error) => { this.clearSignInPending(); hostCallbacks.onError(error); },
|
|
8628
|
+
onCancel: () => { this.clearSignInPending(); hostCallbacks.onCancel(); },
|
|
8629
|
+
});
|
|
8521
8630
|
this.launcher.start();
|
|
8522
8631
|
return settledPromise;
|
|
8523
8632
|
}
|
|
@@ -8624,13 +8733,212 @@ class IdentityModule {
|
|
|
8624
8733
|
return payload;
|
|
8625
8734
|
}
|
|
8626
8735
|
/**
|
|
8627
|
-
* Complete
|
|
8628
|
-
*
|
|
8629
|
-
*
|
|
8630
|
-
*
|
|
8631
|
-
*
|
|
8632
|
-
*
|
|
8736
|
+
* Complete a sign-in on page load — the return leg of `signIn()`. Call once
|
|
8737
|
+
* on every page load; it resolves `null` when there is nothing to do.
|
|
8738
|
+
*
|
|
8739
|
+
* It handles two cases:
|
|
8740
|
+
* 1. A one-time code is in the URL fragment (`#sv_code=…`) — the popup's
|
|
8741
|
+
* result was lost and self-heal navigated here, or `flow: 'redirect'`
|
|
8742
|
+
* returned. Redeem it server-side and hand the full result to
|
|
8743
|
+
* `onSuccess` (and resolve with it).
|
|
8744
|
+
* 2. A sign-in was in flight but never delivered (a mobile browser
|
|
8745
|
+
* discarded the backgrounded opener tab, killing the popup's
|
|
8746
|
+
* postMessage) — silently re-mint from the SSO session the completed
|
|
8747
|
+
* popup login established, by navigating this tab through the silent SSO
|
|
8748
|
+
* endpoint and straight back with a code (case 1 on return). Nothing is
|
|
8749
|
+
* shown; if there is no live session it comes back logged-out, no oracle.
|
|
8750
|
+
*
|
|
8751
|
+
* The code is one-time, PKCE-bound, and origin-locked — never the token. The
|
|
8752
|
+
* host must still verify `result.token` server-side via the Identity JWKS.
|
|
8753
|
+
*
|
|
8754
|
+
* @example Call on page load; sign-in clicks stay unchanged.
|
|
8755
|
+
* sv.products.identity.handleRedirectResult({
|
|
8756
|
+
* onSuccess: (result) => createSession(result.token),
|
|
8757
|
+
* }).catch((error) => showError(error));
|
|
8758
|
+
*/
|
|
8759
|
+
async handleRedirectResult(options = {}) {
|
|
8760
|
+
if (typeof window === 'undefined') {
|
|
8761
|
+
return null;
|
|
8762
|
+
}
|
|
8763
|
+
const params = new URLSearchParams((window.location.hash || '').slice(1));
|
|
8764
|
+
if (params.get('sv_code') || params.get('sv_error')) {
|
|
8765
|
+
return this.consumeHandoffFragment(params, options);
|
|
8766
|
+
}
|
|
8767
|
+
// A silent-SSO recovery that found no live session returns `#sso=…`; strip
|
|
8768
|
+
// it so the logged-out load is clean, and do not re-bounce (handled below).
|
|
8769
|
+
if (params.get('sso')) {
|
|
8770
|
+
this.clearUrlFragment();
|
|
8771
|
+
}
|
|
8772
|
+
// No returned result. If a sign-in was in flight and its result never
|
|
8773
|
+
// arrived, self-heal once from the SSO session (mobile opener-tab discard).
|
|
8774
|
+
if (this.consumeSignInPending()) {
|
|
8775
|
+
await this.startRecoveryBounce();
|
|
8776
|
+
}
|
|
8777
|
+
return null;
|
|
8778
|
+
}
|
|
8779
|
+
/** Redeem a returned `#sv_code` (or surface a returned `#sv_error`). */
|
|
8780
|
+
async consumeHandoffFragment(params, options) {
|
|
8781
|
+
const code = params.get('sv_code');
|
|
8782
|
+
const errorCode = params.get('sv_error');
|
|
8783
|
+
// A returned fragment means this sign-in resolved (redeem, cancel, or
|
|
8784
|
+
// error), so retire any pending marker — otherwise it could fire one
|
|
8785
|
+
// spurious silent bounce on a later load inside its TTL.
|
|
8786
|
+
this.clearSignInPending();
|
|
8787
|
+
// Consume exactly once: fragment and one-shot state/verifier are cleared
|
|
8788
|
+
// up front on every path, so a failing result cannot wedge future loads
|
|
8789
|
+
// (and a StrictMode double-invocation finds an empty hash and no-ops).
|
|
8790
|
+
let expectedState = null;
|
|
8791
|
+
let codeVerifier = null;
|
|
8792
|
+
try {
|
|
8793
|
+
expectedState = sessionStorage.getItem('sv_oauth_state');
|
|
8794
|
+
codeVerifier = sessionStorage.getItem('sv_pkce_verifier');
|
|
8795
|
+
sessionStorage.removeItem('sv_oauth_state');
|
|
8796
|
+
sessionStorage.removeItem('sv_pkce_verifier');
|
|
8797
|
+
}
|
|
8798
|
+
catch {
|
|
8799
|
+
// Storage disabled (private mode): state/verifier were never persisted.
|
|
8800
|
+
}
|
|
8801
|
+
this.clearUrlFragment();
|
|
8802
|
+
try {
|
|
8803
|
+
if (errorCode) {
|
|
8804
|
+
if (errorCode === 'cancelled') {
|
|
8805
|
+
options.onCancel?.();
|
|
8806
|
+
return null;
|
|
8807
|
+
}
|
|
8808
|
+
throw new SparkVaultError('Sign-in could not be completed. Please try again.', 'REDIRECT_SIGNIN_FAILED');
|
|
8809
|
+
}
|
|
8810
|
+
if (expectedState && params.get('state') !== expectedState) {
|
|
8811
|
+
throw new ValidationError('Invalid OAuth state');
|
|
8812
|
+
}
|
|
8813
|
+
if (!codeVerifier) {
|
|
8814
|
+
throw new ValidationError('Sign-in session was lost — please sign in again.');
|
|
8815
|
+
}
|
|
8816
|
+
const raw = await this.api.redeemHandoffCode(code, codeVerifier);
|
|
8817
|
+
const result = {
|
|
8818
|
+
token: raw.token,
|
|
8819
|
+
identity: raw.identity,
|
|
8820
|
+
identityType: raw.identity_type,
|
|
8821
|
+
svid: raw.svid ?? undefined,
|
|
8822
|
+
sessionId: raw.session_id ?? undefined,
|
|
8823
|
+
refreshToken: raw.refresh_token ?? undefined,
|
|
8824
|
+
};
|
|
8825
|
+
options.onSuccess?.(result);
|
|
8826
|
+
return result;
|
|
8827
|
+
}
|
|
8828
|
+
catch (error) {
|
|
8829
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
8830
|
+
options.onError?.(err);
|
|
8831
|
+
throw err;
|
|
8832
|
+
}
|
|
8833
|
+
}
|
|
8834
|
+
/**
|
|
8835
|
+
* Silently re-mint the sign-in result from the SSO session the completed
|
|
8836
|
+
* popup login left behind. Navigates this tab through the silent SSO endpoint
|
|
8837
|
+
* (a top-level GET, so the `SameSite=Lax` hub cookie rides along) which,
|
|
8838
|
+
* PKCE-bound, returns a one-time code in the fragment — handled by case 1 on
|
|
8839
|
+
* return. No-op (stays put) if session storage or WebCrypto is unavailable,
|
|
8840
|
+
* so a hostile/degraded environment simply never recovers rather than
|
|
8841
|
+
* erroring.
|
|
8633
8842
|
*/
|
|
8843
|
+
async startRecoveryBounce() {
|
|
8844
|
+
const nav = await this.prepareHandoffNavigation(`${this.config.identityBaseUrl}/${this.config.accountId}/sso`);
|
|
8845
|
+
if (nav) {
|
|
8846
|
+
window.location.assign(nav);
|
|
8847
|
+
}
|
|
8848
|
+
}
|
|
8849
|
+
/**
|
|
8850
|
+
* Explicit `flow: 'redirect'` — navigate this tab to the interactive sign-in
|
|
8851
|
+
* page. Plain sign-in carries redirect_uri + state + PKCE challenge and
|
|
8852
|
+
* returns via the fragment code; OIDC/simple modes navigate bare (their
|
|
8853
|
+
* server-rendered pages drive the server-validated redirect back).
|
|
8854
|
+
*
|
|
8855
|
+
* @returns false when the return leg cannot be armed (no session storage or
|
|
8856
|
+
* WebCrypto) — the caller surfaces an actionable error instead of a
|
|
8857
|
+
* silent dead-end.
|
|
8858
|
+
*/
|
|
8859
|
+
async startRedirectFlow(options) {
|
|
8860
|
+
const authContext = options.authContext;
|
|
8861
|
+
if (authContext?.authRequestId || authContext?.simpleMode?.sessionId) {
|
|
8862
|
+
window.location.assign(buildSigninUrl(this.config, options));
|
|
8863
|
+
return true;
|
|
8864
|
+
}
|
|
8865
|
+
const url = await this.prepareHandoffNavigation(buildSigninUrl(this.config, options), (extras) => buildSigninUrl(this.config, options, extras));
|
|
8866
|
+
if (!url) {
|
|
8867
|
+
return false;
|
|
8868
|
+
}
|
|
8869
|
+
window.location.assign(url);
|
|
8870
|
+
return true;
|
|
8871
|
+
}
|
|
8872
|
+
/**
|
|
8873
|
+
* Arm the PKCE + state return leg for a handoff navigation and return the URL
|
|
8874
|
+
* to navigate to (redirect_uri = this exact page; query preserved for app
|
|
8875
|
+
* state, fragment dropped since it is the result channel). Returns null when
|
|
8876
|
+
* session storage or WebCrypto is unavailable so callers can degrade.
|
|
8877
|
+
*/
|
|
8878
|
+
async prepareHandoffNavigation(base, build) {
|
|
8879
|
+
const state = generateState();
|
|
8880
|
+
const codeVerifier = generateCodeVerifier();
|
|
8881
|
+
// Derive the challenge BEFORE persisting: a WebCrypto failure (insecure
|
|
8882
|
+
// context) then leaves no orphaned state/verifier behind in storage.
|
|
8883
|
+
let codeChallenge;
|
|
8884
|
+
try {
|
|
8885
|
+
codeChallenge = await computeS256Challenge(codeVerifier);
|
|
8886
|
+
}
|
|
8887
|
+
catch {
|
|
8888
|
+
return null;
|
|
8889
|
+
}
|
|
8890
|
+
try {
|
|
8891
|
+
sessionStorage.setItem('sv_oauth_state', state);
|
|
8892
|
+
sessionStorage.setItem('sv_pkce_verifier', codeVerifier);
|
|
8893
|
+
}
|
|
8894
|
+
catch {
|
|
8895
|
+
return null;
|
|
8896
|
+
}
|
|
8897
|
+
const redirectUri = `${window.location.origin}${window.location.pathname}${window.location.search}`;
|
|
8898
|
+
if (build) {
|
|
8899
|
+
return build({ redirectUri, state, codeChallenge });
|
|
8900
|
+
}
|
|
8901
|
+
const url = new URL(base);
|
|
8902
|
+
url.searchParams.set('redirect_uri', redirectUri);
|
|
8903
|
+
url.searchParams.set('state', state);
|
|
8904
|
+
url.searchParams.set('code_challenge', codeChallenge);
|
|
8905
|
+
return url.toString();
|
|
8906
|
+
}
|
|
8907
|
+
markSignInPending() {
|
|
8908
|
+
try {
|
|
8909
|
+
sessionStorage.setItem(IdentityModule.PENDING_KEY, String(Date.now()));
|
|
8910
|
+
}
|
|
8911
|
+
catch {
|
|
8912
|
+
// No storage → no self-heal; the popup still delivers when it can.
|
|
8913
|
+
}
|
|
8914
|
+
}
|
|
8915
|
+
clearSignInPending() {
|
|
8916
|
+
try {
|
|
8917
|
+
sessionStorage.removeItem(IdentityModule.PENDING_KEY);
|
|
8918
|
+
}
|
|
8919
|
+
catch {
|
|
8920
|
+
// Nothing to clear.
|
|
8921
|
+
}
|
|
8922
|
+
}
|
|
8923
|
+
/** True once (then cleared) if a recent sign-in is still awaiting its result. */
|
|
8924
|
+
consumeSignInPending() {
|
|
8925
|
+
let raw = null;
|
|
8926
|
+
try {
|
|
8927
|
+
raw = sessionStorage.getItem(IdentityModule.PENDING_KEY);
|
|
8928
|
+
sessionStorage.removeItem(IdentityModule.PENDING_KEY);
|
|
8929
|
+
}
|
|
8930
|
+
catch {
|
|
8931
|
+
return false;
|
|
8932
|
+
}
|
|
8933
|
+
const at = raw ? Number(raw) : NaN;
|
|
8934
|
+
return Number.isFinite(at) && Date.now() - at < IdentityModule.PENDING_TTL_MS;
|
|
8935
|
+
}
|
|
8936
|
+
/** Remove the result fragment from the URL bar (and history entry). */
|
|
8937
|
+
clearUrlFragment() {
|
|
8938
|
+
if (typeof window.history?.replaceState === 'function') {
|
|
8939
|
+
window.history.replaceState(window.history.state, document.title, `${window.location.pathname}${window.location.search}`);
|
|
8940
|
+
}
|
|
8941
|
+
}
|
|
8634
8942
|
async consumeRedirectResult() {
|
|
8635
8943
|
if (typeof window === 'undefined' || !window.location.hash) {
|
|
8636
8944
|
return null;
|
|
@@ -8670,9 +8978,7 @@ class IdentityModule {
|
|
|
8670
8978
|
};
|
|
8671
8979
|
}
|
|
8672
8980
|
finally {
|
|
8673
|
-
|
|
8674
|
-
window.history.replaceState(window.history.state, document.title, `${window.location.pathname}${window.location.search}`);
|
|
8675
|
-
}
|
|
8981
|
+
this.clearUrlFragment();
|
|
8676
8982
|
try {
|
|
8677
8983
|
sessionStorage.removeItem('sv_oauth_state');
|
|
8678
8984
|
sessionStorage.removeItem('sv_oauth_email');
|
|
@@ -8758,6 +9064,11 @@ class IdentityModule {
|
|
|
8758
9064
|
return this.runFlow(options);
|
|
8759
9065
|
}
|
|
8760
9066
|
}
|
|
9067
|
+
// A sign-in is "pending" from popup open until its result arrives; a marker
|
|
9068
|
+
// that outlives a lost result (mobile opener-tab discard clears the message
|
|
9069
|
+
// listener but not sessionStorage) is what lets the next load self-heal.
|
|
9070
|
+
IdentityModule.PENDING_KEY = 'sv_signin_pending';
|
|
9071
|
+
IdentityModule.PENDING_TTL_MS = 10 * 60 * 1000;
|
|
8761
9072
|
|
|
8762
9073
|
/**
|
|
8763
9074
|
* Products Module
|