@sanity/sdk 3.2.0 → 3.3.0-rc.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/_exports/_internal.d.ts +2 -2
- package/dist/_exports/_internal.js +4 -4
- package/dist/_exports/agent.js +1 -1
- package/dist/_exports/comlink.d.ts +1 -1
- package/dist/_exports/dashboard.d.ts +1 -1
- package/dist/{authStore-BO2vch3I.d.ts → authStore-BGoEGxOq.d.ts} +19 -4
- package/dist/authStore-BGoEGxOq.d.ts.map +1 -0
- package/dist/{clientStore-BI4D8HhJ.js → clientStore-QDrJNTM4.js} +14 -10
- package/dist/clientStore-QDrJNTM4.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +156 -123
- package/dist/index.js.map +1 -1
- package/dist/{topicStore-D0WqhQGF.d.ts → topicStore-DXvuH9h4.d.ts} +2 -2
- package/dist/{topicStore-D0WqhQGF.d.ts.map → topicStore-DXvuH9h4.d.ts.map} +1 -1
- package/dist/{version-BcRmCiYi.js → version-CIhX5ppJ.js} +3 -3
- package/dist/{version-BcRmCiYi.js.map → version-CIhX5ppJ.js.map} +1 -1
- package/package.json +3 -3
- package/src/auth/authStore.test.ts +18 -0
- package/src/auth/authStore.ts +4 -1
- package/src/auth/handleAuthCallback.test.ts +33 -0
- package/src/auth/handleAuthCallback.ts +11 -1
- package/src/auth/logout.test.ts +9 -1
- package/src/auth/logout.ts +18 -1
- package/src/auth/oauth/oauthActions.test.ts +136 -0
- package/src/auth/oauth/oauthActions.ts +78 -29
- package/src/auth/oauth/oauthAuth.test.ts +23 -0
- package/src/auth/oauth/oauthAuth.ts +18 -8
- package/dist/authStore-BO2vch3I.d.ts.map +0 -1
- package/dist/clientStore-BI4D8HhJ.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { D as getAuthLogger, E as REQUEST_TAG_PREFIX, O as configureLogging$1, S as AuthStateType, T as DEFAULT_BASE, b as getTokenFromLocation, c as getDashboardOrganizationId, d as getTokenState, f as setAuthToken, h as getCleanedUrl, i as authStore, k as createLogger, l as getIsInDashboardState, m as getAuthCode, n as getClient, o as getAuthState, p as createLoggedInAuthState, r as getClientState, s as getCurrentUserState, u as getLoginUrlState, w as DEFAULT_API_VERSION, y as getDefaultLocation } from "./clientStore-
|
|
1
|
+
import { D as getAuthLogger, E as REQUEST_TAG_PREFIX, O as configureLogging$1, S as AuthStateType, T as DEFAULT_BASE, b as getTokenFromLocation, c as getDashboardOrganizationId, d as getTokenState, f as setAuthToken, h as getCleanedUrl, i as authStore, k as createLogger, l as getIsInDashboardState, m as getAuthCode, n as getClient, o as getAuthState, p as createLoggedInAuthState, r as getClientState, s as getCurrentUserState, u as getLoginUrlState, w as DEFAULT_API_VERSION, y as getDefaultLocation } from "./clientStore-QDrJNTM4.js";
|
|
2
2
|
import { c as isReleasePerspective, d as isMediaLibraryResource, i as bindActionGlobally, l as isCanvasResource, n as bindActionByResource, o as defineStore, r as bindActionByResourceAndPerspective, t as createStateSourceAction, u as isDatasetResource } from "./createStateSourceAction-DdAhEfnZ.js";
|
|
3
|
-
import { A as clearPendingTransaction, B as assertDatasetResource, C as getCommentsState, D as toCommentsKeyParts, E as resolveComments, F as rollbackCommentUpdate, G as defineMutation, H as provisionAddonDataset, I as setCommentCreateError, K as randomId, L as setPendingTransaction, M as receiveComment, N as removeCommentById, O as addComment, P as restoreComments, R as normalizeComment, T as resolveCommentThreads, U as COMMENTS_API_VERSION, V as getAddonDatasetState, W as defineFetcher, _ as updateLastLoadMoreRequest, a as resolveQuery, b as commentsStore, c as getAllReleasesState, d as getUsersKey, f as initializeRequest, g as setUsersError, h as setUsersData, j as getCommentsKey, k as applyCommentUpdate, l as addSubscription, m as removeSubscription, o as getPerspectiveState, p as parseUsersKey, q as randomUuid, r as getQueryState, s as getActiveReleasesState, t as CORE_SDK_VERSION, u as cancelRequest, v as PROJECT_API_VERSION, x as getCommentThreadsState, y as USERS_STATE_CLEAR_DELAY, z as toCommentFieldPath } from "./version-
|
|
3
|
+
import { A as clearPendingTransaction, B as assertDatasetResource, C as getCommentsState, D as toCommentsKeyParts, E as resolveComments, F as rollbackCommentUpdate, G as defineMutation, H as provisionAddonDataset, I as setCommentCreateError, K as randomId, L as setPendingTransaction, M as receiveComment, N as removeCommentById, O as addComment, P as restoreComments, R as normalizeComment, T as resolveCommentThreads, U as COMMENTS_API_VERSION, V as getAddonDatasetState, W as defineFetcher, _ as updateLastLoadMoreRequest, a as resolveQuery, b as commentsStore, c as getAllReleasesState, d as getUsersKey, f as initializeRequest, g as setUsersError, h as setUsersData, j as getCommentsKey, k as applyCommentUpdate, l as addSubscription, m as removeSubscription, o as getPerspectiveState, p as parseUsersKey, q as randomUuid, r as getQueryState, s as getActiveReleasesState, t as CORE_SDK_VERSION, u as cancelRequest, v as PROJECT_API_VERSION, x as getCommentThreadsState, y as USERS_STATE_CLEAR_DELAY, z as toCommentFieldPath } from "./version-CIhX5ppJ.js";
|
|
4
4
|
import { r as omitProperty, t as isDeepEqual } from "./object-CxXlSVg7.js";
|
|
5
5
|
import { n as setCleanupTimeout } from "./setCleanupTimeout-BiM6q1KF.js";
|
|
6
6
|
import { t as _defineProperty } from "./defineProperty-Bho9Ngsi.js";
|
|
@@ -303,101 +303,6 @@ function observeOrganizationVerificationState(instance, projectIds) {
|
|
|
303
303
|
}), distinctUntilChanged((prev, curr) => prev.error === curr.error));
|
|
304
304
|
}
|
|
305
305
|
/**
|
|
306
|
-
* @public
|
|
307
|
-
*/
|
|
308
|
-
const handleAuthCallback = bindActionGlobally(authStore, async ({ state, instance }, locationHref = getDefaultLocation()) => {
|
|
309
|
-
let logger = getAuthLogger(instance), { providedToken, callbackUrl, clientFactory, apiHost, storageArea, storageKey } = state.get().options;
|
|
310
|
-
if (providedToken) return logger.debug("Skipping auth callback - token already provided"), !1;
|
|
311
|
-
let { authState } = state.get();
|
|
312
|
-
if (authState.type === "logging-in" && authState.isExchangingToken) return logger.debug("Skipping auth callback - token exchange already in progress"), !1;
|
|
313
|
-
let cleanedUrl = getCleanedUrl(locationHref), tokenFromUrl = getTokenFromLocation(locationHref);
|
|
314
|
-
if (tokenFromUrl) return logger.info("Auth token found in URL, logging in"), state.set("setTokenFromUrl", { authState: createLoggedInAuthState(tokenFromUrl, null) }), cleanedUrl;
|
|
315
|
-
let authCode = getAuthCode(callbackUrl, locationHref);
|
|
316
|
-
if (!authCode) return logger.debug("No auth code found in callback URL"), !1;
|
|
317
|
-
let parsedUrl = new URL(locationHref), dashboardContext = {};
|
|
318
|
-
try {
|
|
319
|
-
let contextParam = parsedUrl.searchParams.get("_context");
|
|
320
|
-
if (contextParam) {
|
|
321
|
-
let parsedContext = JSON.parse(contextParam);
|
|
322
|
-
parsedContext && typeof parsedContext == "object" && (delete parsedContext.sid, dashboardContext = parsedContext, logger.debug("Dashboard context parsed from callback URL", { hasDashboardContext: !0 }));
|
|
323
|
-
}
|
|
324
|
-
} catch (err) {
|
|
325
|
-
logger.warn("Failed to parse dashboard context from callback URL", { error: err });
|
|
326
|
-
}
|
|
327
|
-
logger.info("Exchanging auth code for token"), state.set("exchangeSessionForToken", {
|
|
328
|
-
authState: {
|
|
329
|
-
type: "logging-in",
|
|
330
|
-
isExchangingToken: !0
|
|
331
|
-
},
|
|
332
|
-
dashboardContext
|
|
333
|
-
});
|
|
334
|
-
try {
|
|
335
|
-
let client = clientFactory({
|
|
336
|
-
apiVersion: DEFAULT_API_VERSION,
|
|
337
|
-
requestTagPrefix: REQUEST_TAG_PREFIX,
|
|
338
|
-
useProjectHostname: !1,
|
|
339
|
-
useCdn: !1,
|
|
340
|
-
...apiHost && { apiHost }
|
|
341
|
-
});
|
|
342
|
-
logger.debug("Fetching token from auth endpoint");
|
|
343
|
-
let { token } = await client.request({
|
|
344
|
-
method: "GET",
|
|
345
|
-
url: "/auth/fetch",
|
|
346
|
-
query: { sid: authCode },
|
|
347
|
-
tag: "fetch-token"
|
|
348
|
-
});
|
|
349
|
-
return logger.info("Auth token obtained successfully, user logged in"), storageArea?.setItem(storageKey, JSON.stringify({ token })), state.set("setToken", { authState: createLoggedInAuthState(token, null) }), cleanedUrl;
|
|
350
|
-
} catch (error) {
|
|
351
|
-
return logger.error("Failed to exchange auth code for token", { error }), state.set("exchangeSessionForTokenError", { authState: {
|
|
352
|
-
type: "error",
|
|
353
|
-
error
|
|
354
|
-
} }), cleanedUrl;
|
|
355
|
-
}
|
|
356
|
-
}), logout = bindActionGlobally(authStore, async ({ state, instance }) => {
|
|
357
|
-
let logger = getAuthLogger(instance), { clientFactory, apiHost, providedToken, storageArea, storageKey } = state.get().options;
|
|
358
|
-
if (providedToken) {
|
|
359
|
-
logger.debug("Skipping logout - token is statically provided");
|
|
360
|
-
return;
|
|
361
|
-
}
|
|
362
|
-
let { authState } = state.get();
|
|
363
|
-
if (authState.type === "logged-out" && authState.isDestroyingSession) {
|
|
364
|
-
logger.debug("Skipping logout - already in progress");
|
|
365
|
-
return;
|
|
366
|
-
}
|
|
367
|
-
let token = authState.type === "logged-in" && authState.token;
|
|
368
|
-
try {
|
|
369
|
-
if (token) {
|
|
370
|
-
logger.info("Logging out user"), state.set("loggingOut", { authState: {
|
|
371
|
-
type: "logged-out",
|
|
372
|
-
isDestroyingSession: !0
|
|
373
|
-
} });
|
|
374
|
-
let client = clientFactory({
|
|
375
|
-
token,
|
|
376
|
-
requestTagPrefix: REQUEST_TAG_PREFIX,
|
|
377
|
-
apiVersion: DEFAULT_API_VERSION,
|
|
378
|
-
...apiHost && { apiHost },
|
|
379
|
-
useProjectHostname: !1,
|
|
380
|
-
useCdn: !1
|
|
381
|
-
});
|
|
382
|
-
logger.debug("Calling logout endpoint"), await client.request({
|
|
383
|
-
url: "/auth/logout",
|
|
384
|
-
method: "POST",
|
|
385
|
-
tag: "logout"
|
|
386
|
-
});
|
|
387
|
-
} else logger.debug("No token to logout - already logged out");
|
|
388
|
-
} catch (error) {
|
|
389
|
-
throw logger.error("Logout request failed", { error }), error;
|
|
390
|
-
} finally {
|
|
391
|
-
logger.info("User logged out, clearing stored tokens"), state.set("logoutSuccess", {
|
|
392
|
-
authState: {
|
|
393
|
-
type: "logged-out",
|
|
394
|
-
isDestroyingSession: !1
|
|
395
|
-
},
|
|
396
|
-
oauthTokens: void 0
|
|
397
|
-
}), storageArea?.removeItem(storageKey), storageArea?.removeItem(`${storageKey}_last_refresh`);
|
|
398
|
-
}
|
|
399
|
-
});
|
|
400
|
-
/**
|
|
401
306
|
* Encodes a byte array as a base64url string (RFC 4648 §5) with no padding,
|
|
402
307
|
* as required for PKCE `code_verifier`/`code_challenge` values.
|
|
403
308
|
*
|
|
@@ -440,7 +345,23 @@ async function generateCodeChallenge(codeVerifier) {
|
|
|
440
345
|
return base64UrlEncode(new Uint8Array(digest));
|
|
441
346
|
}
|
|
442
347
|
/** sessionStorage key for the PKCE `code_verifier`. */
|
|
443
|
-
const OAUTH_VERIFIER_KEY = "__sanity_oauth_verifier", OAUTH_STATE_KEY = "__sanity_oauth_state"
|
|
348
|
+
const OAUTH_VERIFIER_KEY = "__sanity_oauth_verifier", OAUTH_STATE_KEY = "__sanity_oauth_state", OAUTH_RETURN_TO_KEY = "__sanity_oauth_return_to", OAUTH_CALLBACK_PARAMS = [
|
|
349
|
+
"code",
|
|
350
|
+
"state",
|
|
351
|
+
"error",
|
|
352
|
+
"error_description"
|
|
353
|
+
];
|
|
354
|
+
/** Strips the OAuth callback params from a URL. */
|
|
355
|
+
function stripOAuthParams(href) {
|
|
356
|
+
let url = new URL(href, DEFAULT_BASE);
|
|
357
|
+
for (let param of OAUTH_CALLBACK_PARAMS) url.searchParams.delete(param);
|
|
358
|
+
return url.toString();
|
|
359
|
+
}
|
|
360
|
+
/** Whether `href` is the registered redirect URI (origin + pathname; query and hash ignored). */
|
|
361
|
+
function isCallbackRoute(href, redirectUri) {
|
|
362
|
+
let current = new URL(href, DEFAULT_BASE), redirect = new URL(redirectUri, DEFAULT_BASE);
|
|
363
|
+
return current.origin === redirect.origin && current.pathname === redirect.pathname;
|
|
364
|
+
}
|
|
444
365
|
/** Builds the RFC 8707 resource indicator for an organisation. */
|
|
445
366
|
function getResourceIndicator(organizationId) {
|
|
446
367
|
return `urn:io.sanity:organization:${organizationId}`;
|
|
@@ -498,37 +419,26 @@ function toOAuthTokens(response) {
|
|
|
498
419
|
const startOAuthAuthorization = bindActionGlobally(authStore, async ({ state, instance }) => {
|
|
499
420
|
let logger = getAuthLogger(instance), options = getOAuthOptions(state.get()), codeVerifier = generateCodeVerifier(), oauthState = generateState(), codeChallenge = await generateCodeChallenge(codeVerifier), session = typeof sessionStorage < "u" ? sessionStorage : void 0;
|
|
500
421
|
session?.setItem(OAUTH_VERIFIER_KEY, codeVerifier), session?.setItem(OAUTH_STATE_KEY, oauthState);
|
|
422
|
+
let currentHref = getDefaultLocation();
|
|
423
|
+
if (currentHref !== "http://localhost") {
|
|
424
|
+
let returnTo = isCallbackRoute(currentHref, options.oauth.redirectUri) ? stripOAuthParams(currentHref) : currentHref;
|
|
425
|
+
session?.setItem(OAUTH_RETURN_TO_KEY, returnTo);
|
|
426
|
+
}
|
|
501
427
|
let authorizeUrl = new URL("/v1/auth/oauth/authorize", options.apiHost ?? "https://api.sanity.io");
|
|
502
428
|
authorizeUrl.searchParams.set("response_type", "code"), authorizeUrl.searchParams.set("client_id", options.oauth.clientId), authorizeUrl.searchParams.set("redirect_uri", options.oauth.redirectUri), authorizeUrl.searchParams.set("state", oauthState), authorizeUrl.searchParams.set("code_challenge", codeChallenge), authorizeUrl.searchParams.set("code_challenge_method", "S256"), authorizeUrl.searchParams.append("resource", getResourceIndicator(options.oauth.organizationId)), logger.info("Starting OAuth authorization"), typeof window < "u" && typeof window.location?.assign == "function" && window.location.assign(authorizeUrl.toString());
|
|
503
429
|
}), handleOAuthCallback = bindActionGlobally(authStore, async ({ state, instance }, locationHref = getDefaultLocation()) => {
|
|
504
430
|
let logger = getAuthLogger(instance), options = getOAuthOptions(state.get()), { authState } = state.get();
|
|
505
431
|
if (authState.type === "logging-in" && authState.isExchangingToken) return logger.debug("Skipping OAuth callback - token exchange already in progress"), !1;
|
|
506
|
-
let callbackUrl = new URL(locationHref, DEFAULT_BASE), code = callbackUrl.searchParams.get("code"), returnedState = callbackUrl.searchParams.get("state"), error = callbackUrl.searchParams.get("error"), errorDescription = callbackUrl.searchParams.get("error_description"), session = typeof sessionStorage < "u" ? sessionStorage : void 0,
|
|
507
|
-
for (let param of [
|
|
508
|
-
"code",
|
|
509
|
-
"state",
|
|
510
|
-
"error",
|
|
511
|
-
"error_description"
|
|
512
|
-
]) cleanedUrlObj.searchParams.delete(param);
|
|
513
|
-
let cleanedUrl = cleanedUrlObj.toString();
|
|
514
|
-
if (error) return logger.warn("OAuth callback returned an error", {
|
|
515
|
-
error,
|
|
516
|
-
errorDescription
|
|
517
|
-
}), clearOAuthArtifacts(session), state.set("oauthCallbackError", { authState: {
|
|
432
|
+
let callbackUrl = new URL(locationHref, DEFAULT_BASE), code = callbackUrl.searchParams.get("code"), returnedState = callbackUrl.searchParams.get("state"), error = callbackUrl.searchParams.get("error"), errorDescription = callbackUrl.searchParams.get("error_description"), session = typeof sessionStorage < "u" ? sessionStorage : void 0, cleanedUrl = stripOAuthParams(locationHref), rejectCallback = (name, message) => authState.type === "logged-in" ? (logger.warn(`${message} — ignoring callback, session already established`), cleanedUrl) : (logger.error(`${message} — rejecting callback`), clearOAuthArtifacts(session), state.set(name, { authState: {
|
|
518
433
|
type: "error",
|
|
519
|
-
error: Error(
|
|
520
|
-
} }), cleanedUrl;
|
|
434
|
+
error: Error(message)
|
|
435
|
+
} }), cleanedUrl);
|
|
436
|
+
if (error) return rejectCallback("oauthCallbackError", errorDescription ? `${error}: ${errorDescription}` : error);
|
|
521
437
|
if (!code) return logger.debug("No OAuth code found in callback URL"), !1;
|
|
522
438
|
let storedState = session?.getItem("__sanity_oauth_state") ?? null;
|
|
523
|
-
if (!returnedState || !storedState || returnedState !== storedState) return
|
|
524
|
-
type: "error",
|
|
525
|
-
error: /* @__PURE__ */ Error("OAuth state mismatch")
|
|
526
|
-
} }), cleanedUrl;
|
|
439
|
+
if (!returnedState || !storedState || returnedState !== storedState) return rejectCallback("oauthStateMismatch", "OAuth state mismatch");
|
|
527
440
|
let codeVerifier = session?.getItem("__sanity_oauth_verifier") ?? null;
|
|
528
|
-
if (!codeVerifier) return
|
|
529
|
-
type: "error",
|
|
530
|
-
error: /* @__PURE__ */ Error("OAuth code verifier missing")
|
|
531
|
-
} }), cleanedUrl;
|
|
441
|
+
if (!codeVerifier) return rejectCallback("oauthVerifierMissing", "OAuth code verifier missing");
|
|
532
442
|
logger.info("Exchanging OAuth code for tokens"), state.set("oauthExchange", { authState: {
|
|
533
443
|
type: "logging-in",
|
|
534
444
|
isExchangingToken: !0
|
|
@@ -548,10 +458,12 @@ const startOAuthAuthorization = bindActionGlobally(authStore, async ({ state, in
|
|
|
548
458
|
body: params.toString(),
|
|
549
459
|
tag: "oauth.token"
|
|
550
460
|
}));
|
|
551
|
-
|
|
461
|
+
options.storageArea?.setItem(options.storageKey, serializeTokens(tokens));
|
|
462
|
+
let returnTo = getSameOriginReturnTo(session, options.oauth.redirectUri);
|
|
463
|
+
return clearOAuthArtifacts(session), logger.info("OAuth tokens obtained, user logged in"), state.set("oauthLoggedIn", {
|
|
552
464
|
authState: createLoggedInAuthState(tokens.accessToken, null),
|
|
553
465
|
oauthTokens: tokens
|
|
554
|
-
}), cleanedUrl;
|
|
466
|
+
}), returnTo ?? cleanedUrl;
|
|
555
467
|
} catch (exchangeError) {
|
|
556
468
|
return logger.error("Failed to exchange OAuth code for tokens", { error: exchangeError }), clearOAuthArtifacts(session), state.set("oauthExchangeError", { authState: {
|
|
557
469
|
type: "error",
|
|
@@ -653,8 +565,129 @@ const revokeOAuthTokens = bindActionGlobally(authStore, async ({ state, instance
|
|
|
653
565
|
}), getOAuthTokensState = bindActionGlobally(authStore, createStateSourceAction(({ state }) => state.oauthTokens ?? null));
|
|
654
566
|
/** Removes the transient PKCE artifacts from session storage. */
|
|
655
567
|
function clearOAuthArtifacts(session) {
|
|
656
|
-
session?.removeItem(OAUTH_VERIFIER_KEY), session?.removeItem(OAUTH_STATE_KEY);
|
|
568
|
+
session?.removeItem(OAUTH_VERIFIER_KEY), session?.removeItem(OAUTH_STATE_KEY), session?.removeItem(OAUTH_RETURN_TO_KEY);
|
|
657
569
|
}
|
|
570
|
+
/**
|
|
571
|
+
* Reads the stashed pre-authorize location, only honouring it when it shares
|
|
572
|
+
* the registered redirect URI's origin so a tampered value cannot redirect
|
|
573
|
+
* off-site. (`origin` includes the scheme, so non-http(s) URLs are rejected.)
|
|
574
|
+
*/
|
|
575
|
+
function getSameOriginReturnTo(session, redirectUri) {
|
|
576
|
+
let returnTo = session?.getItem("__sanity_oauth_return_to") ?? null;
|
|
577
|
+
if (!returnTo) return null;
|
|
578
|
+
try {
|
|
579
|
+
return new URL(returnTo).origin === new URL(redirectUri).origin ? returnTo : null;
|
|
580
|
+
} catch {
|
|
581
|
+
return null;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* Handles the auth redirect callback. In OAuth mode this delegates to
|
|
586
|
+
* {@link handleOAuthCallback}; otherwise it exchanges the legacy `sid` for a
|
|
587
|
+
* long-lived token.
|
|
588
|
+
*
|
|
589
|
+
* @public
|
|
590
|
+
*/
|
|
591
|
+
const handleAuthCallback = bindActionGlobally(authStore, async ({ state, instance }, locationHref = getDefaultLocation()) => {
|
|
592
|
+
let logger = getAuthLogger(instance), { providedToken, callbackUrl, clientFactory, apiHost, storageArea, storageKey, oauth } = state.get().options;
|
|
593
|
+
if (providedToken) return logger.debug("Skipping auth callback - token already provided"), !1;
|
|
594
|
+
if (oauth) return logger.debug("OAuth mode - delegating to handleOAuthCallback"), handleOAuthCallback(instance, locationHref);
|
|
595
|
+
let { authState } = state.get();
|
|
596
|
+
if (authState.type === "logging-in" && authState.isExchangingToken) return logger.debug("Skipping auth callback - token exchange already in progress"), !1;
|
|
597
|
+
let cleanedUrl = getCleanedUrl(locationHref), tokenFromUrl = getTokenFromLocation(locationHref);
|
|
598
|
+
if (tokenFromUrl) return logger.info("Auth token found in URL, logging in"), state.set("setTokenFromUrl", { authState: createLoggedInAuthState(tokenFromUrl, null) }), cleanedUrl;
|
|
599
|
+
let authCode = getAuthCode(callbackUrl, locationHref);
|
|
600
|
+
if (!authCode) return logger.debug("No auth code found in callback URL"), !1;
|
|
601
|
+
let parsedUrl = new URL(locationHref), dashboardContext = {};
|
|
602
|
+
try {
|
|
603
|
+
let contextParam = parsedUrl.searchParams.get("_context");
|
|
604
|
+
if (contextParam) {
|
|
605
|
+
let parsedContext = JSON.parse(contextParam);
|
|
606
|
+
parsedContext && typeof parsedContext == "object" && (delete parsedContext.sid, dashboardContext = parsedContext, logger.debug("Dashboard context parsed from callback URL", { hasDashboardContext: !0 }));
|
|
607
|
+
}
|
|
608
|
+
} catch (err) {
|
|
609
|
+
logger.warn("Failed to parse dashboard context from callback URL", { error: err });
|
|
610
|
+
}
|
|
611
|
+
logger.info("Exchanging auth code for token"), state.set("exchangeSessionForToken", {
|
|
612
|
+
authState: {
|
|
613
|
+
type: "logging-in",
|
|
614
|
+
isExchangingToken: !0
|
|
615
|
+
},
|
|
616
|
+
dashboardContext
|
|
617
|
+
});
|
|
618
|
+
try {
|
|
619
|
+
let client = clientFactory({
|
|
620
|
+
apiVersion: DEFAULT_API_VERSION,
|
|
621
|
+
requestTagPrefix: REQUEST_TAG_PREFIX,
|
|
622
|
+
useProjectHostname: !1,
|
|
623
|
+
useCdn: !1,
|
|
624
|
+
...apiHost && { apiHost }
|
|
625
|
+
});
|
|
626
|
+
logger.debug("Fetching token from auth endpoint");
|
|
627
|
+
let { token } = await client.request({
|
|
628
|
+
method: "GET",
|
|
629
|
+
url: "/auth/fetch",
|
|
630
|
+
query: { sid: authCode },
|
|
631
|
+
tag: "fetch-token"
|
|
632
|
+
});
|
|
633
|
+
return logger.info("Auth token obtained successfully, user logged in"), storageArea?.setItem(storageKey, JSON.stringify({ token })), state.set("setToken", { authState: createLoggedInAuthState(token, null) }), cleanedUrl;
|
|
634
|
+
} catch (error) {
|
|
635
|
+
return logger.error("Failed to exchange auth code for token", { error }), state.set("exchangeSessionForTokenError", { authState: {
|
|
636
|
+
type: "error",
|
|
637
|
+
error
|
|
638
|
+
} }), cleanedUrl;
|
|
639
|
+
}
|
|
640
|
+
}), logout = bindActionGlobally(authStore, async ({ state, instance }) => {
|
|
641
|
+
let logger = getAuthLogger(instance), { clientFactory, apiHost, providedToken, storageArea, storageKey, oauth } = state.get().options;
|
|
642
|
+
if (providedToken) {
|
|
643
|
+
logger.debug("Skipping logout - token is statically provided");
|
|
644
|
+
return;
|
|
645
|
+
}
|
|
646
|
+
let { authState } = state.get();
|
|
647
|
+
if (authState.type === "logged-out" && authState.isDestroyingSession) {
|
|
648
|
+
logger.debug("Skipping logout - already in progress");
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
if (oauth) {
|
|
652
|
+
logger.debug("OAuth mode - revoking tokens instead of calling /auth/logout"), state.set("loggingOut", { authState: {
|
|
653
|
+
type: "logged-out",
|
|
654
|
+
isDestroyingSession: !0
|
|
655
|
+
} }), await revokeOAuthTokens(instance);
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
let token = authState.type === "logged-in" && authState.token;
|
|
659
|
+
try {
|
|
660
|
+
if (token) {
|
|
661
|
+
logger.info("Logging out user"), state.set("loggingOut", { authState: {
|
|
662
|
+
type: "logged-out",
|
|
663
|
+
isDestroyingSession: !0
|
|
664
|
+
} });
|
|
665
|
+
let client = clientFactory({
|
|
666
|
+
token,
|
|
667
|
+
requestTagPrefix: REQUEST_TAG_PREFIX,
|
|
668
|
+
apiVersion: DEFAULT_API_VERSION,
|
|
669
|
+
...apiHost && { apiHost },
|
|
670
|
+
useProjectHostname: !1,
|
|
671
|
+
useCdn: !1
|
|
672
|
+
});
|
|
673
|
+
logger.debug("Calling logout endpoint"), await client.request({
|
|
674
|
+
url: "/auth/logout",
|
|
675
|
+
method: "POST",
|
|
676
|
+
tag: "logout"
|
|
677
|
+
});
|
|
678
|
+
} else logger.debug("No token to logout - already logged out");
|
|
679
|
+
} catch (error) {
|
|
680
|
+
throw logger.error("Logout request failed", { error }), error;
|
|
681
|
+
} finally {
|
|
682
|
+
logger.info("User logged out, clearing stored tokens"), state.set("logoutSuccess", {
|
|
683
|
+
authState: {
|
|
684
|
+
type: "logged-out",
|
|
685
|
+
isDestroyingSession: !1
|
|
686
|
+
},
|
|
687
|
+
oauthTokens: void 0
|
|
688
|
+
}), storageArea?.removeItem(storageKey), storageArea?.removeItem(`${storageKey}_last_refresh`);
|
|
689
|
+
}
|
|
690
|
+
});
|
|
658
691
|
function weaken(node) {
|
|
659
692
|
return Array.isArray(node) ? node.map(weaken) : node && typeof node == "object" ? "_ref" in node ? {
|
|
660
693
|
...node,
|