mitra-interactions-sdk 1.0.60-beta.0 → 1.0.60-beta.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.js CHANGED
@@ -317,12 +317,25 @@ var OAUTH_RESULT_TYPE = "mitra-oauth-result";
317
317
  var POPUP_W = 480;
318
318
  var POPUP_H = 600;
319
319
  var POPUP_TIMEOUT_MS = 5 * 60 * 1e3;
320
- var PASSTHROUGH_PATH = "/sdk-auth/oauth";
321
- function buildPassthroughBaseUrl(baseURL) {
322
- return baseURL.replace(/\/+$/, "") + PASSTHROUGH_PATH;
320
+ async function fetchSsoAuthPageUrl(baseURL) {
321
+ const fetchFn = getFetch();
322
+ const response = await fetchFn(buildEndpoint(baseURL, "/sso/config"), {
323
+ method: "GET",
324
+ headers: { accept: "application/json" }
325
+ });
326
+ if (!response.ok) {
327
+ throw new Error(`SSO discovery falhou (HTTP ${response.status}).`);
328
+ }
329
+ const data = await response.json().catch(() => null);
330
+ const url = data && typeof data.authPageUrl === "string" ? data.authPageUrl : "";
331
+ if (!url) {
332
+ throw new Error("SSO discovery n\xE3o retornou authPageUrl.");
333
+ }
334
+ return url;
323
335
  }
324
- function buildStartUrl(baseURL, provider, state, appId) {
325
- return buildPassthroughBaseUrl(baseURL) + "?" + new URLSearchParams({ provider, state, appId }).toString();
336
+ function buildStartUrl(authPageUrl, provider, state, appId, apiUrl) {
337
+ const sep = authPageUrl.includes("?") ? "&" : "?";
338
+ return authPageUrl + sep + new URLSearchParams({ provider, state, appId, apiUrl }).toString();
326
339
  }
327
340
  function getOriginFromUrl2(url) {
328
341
  try {
@@ -408,10 +421,11 @@ async function loginWithProvider(provider, options) {
408
421
  const { authUrl, projectId } = resolveLoginOptions2(options);
409
422
  const state = generateState();
410
423
  const appId = String(projectId);
411
- const expectedOrigin = getOriginFromUrl2(authUrl);
412
- const startUrl = buildStartUrl(authUrl, provider, state, appId);
413
- const redirectUri = buildPassthroughBaseUrl(authUrl);
414
- const { code } = await openOAuthPopup(startUrl, expectedOrigin, state);
424
+ const authPageUrl = await fetchSsoAuthPageUrl(authUrl);
425
+ const pageOrigin = getOriginFromUrl2(authPageUrl);
426
+ const redirectUri = authPageUrl;
427
+ const startUrl = buildStartUrl(authPageUrl, provider, state, appId, authUrl);
428
+ const { code } = await openOAuthPopup(startUrl, pageOrigin, state);
415
429
  const tokens = await postNewAuth(buildEndpoint(authUrl, `/${provider}`), {
416
430
  code,
417
431
  redirectUri,