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