mitra-interactions-sdk 1.0.60-beta.1 → 1.0.60-beta.3

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
@@ -259,6 +259,10 @@ function buildEndpoint(authUrl, path) {
259
259
  const base = authUrl.replace(/\/+$/, "");
260
260
  return `${base}/iam/api/v1/auth${path}`;
261
261
  }
262
+ function buildBffAuthEndpoint(gateway, path) {
263
+ const base = gateway.replace(/\/+$/, "");
264
+ return `${base}/auth${path}`;
265
+ }
262
266
  function resolveLoginOptions2(options) {
263
267
  var _a;
264
268
  const config = isConfigured() ? getConfig() : null;
@@ -302,6 +306,26 @@ async function postNewAuth(url, body) {
302
306
  }
303
307
  return response.json();
304
308
  }
309
+ async function postBffAuth(url, body) {
310
+ const fetchFn = getFetch();
311
+ const response = await fetchFn(url, {
312
+ method: "POST",
313
+ headers: { "Content-Type": "application/json" },
314
+ body: JSON.stringify(body)
315
+ });
316
+ if (!response.ok) {
317
+ const text2 = await response.text().catch(() => "");
318
+ let parsed = null;
319
+ try {
320
+ parsed = text2 ? JSON.parse(text2) : null;
321
+ } catch (e) {
322
+ }
323
+ const msg = (parsed == null ? void 0 : parsed.message) || (parsed == null ? void 0 : parsed.error) || `HTTP ${response.status}`;
324
+ throw { message: msg, status: response.status, details: parsed };
325
+ }
326
+ const text = await response.text().catch(() => "");
327
+ return text ? JSON.parse(text) : null;
328
+ }
305
329
  function normalizeResponse(tokens, authUrl) {
306
330
  return {
307
331
  token: tokens.accessToken,
@@ -317,25 +341,13 @@ var OAUTH_RESULT_TYPE = "mitra-oauth-result";
317
341
  var POPUP_W = 480;
318
342
  var POPUP_H = 600;
319
343
  var POPUP_TIMEOUT_MS = 5 * 60 * 1e3;
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;
344
+ var SSO_AUTH_PAGE_URL = "https://d2by6yrxhbu2rl.cloudfront.net/sdk-auth.html";
345
+ async function fetchSsoAuthPageUrl(_baseURL) {
346
+ return SSO_AUTH_PAGE_URL;
335
347
  }
336
348
  function buildStartUrl(authPageUrl, provider, state, appId, apiUrl) {
337
349
  const sep = authPageUrl.includes("?") ? "&" : "?";
338
- return authPageUrl + sep + new URLSearchParams({ provider, state, appId, apiUrl }).toString();
350
+ return authPageUrl + sep + new URLSearchParams({ provider, state, appId, apiUrl, origin: window.location.origin }).toString();
339
351
  }
340
352
  function getOriginFromUrl2(url) {
341
353
  try {
@@ -358,6 +370,7 @@ function openOAuthPopup(providerUrl, expectedOrigin, expectedState) {
358
370
  const outerH = window.outerHeight || screen.height;
359
371
  const left = Math.max(0, (window.screenX || 0) + (outerW - POPUP_W) / 2);
360
372
  const top = Math.max(0, (window.screenY || 0) + (outerH - POPUP_H) / 2);
373
+ console.log("[mitra-sso] window.open ->", providerUrl);
361
374
  const popup = window.open(
362
375
  providerUrl,
363
376
  "mitra-oauth",
@@ -392,11 +405,11 @@ function openOAuthPopup(providerUrl, expectedOrigin, expectedState) {
392
405
  reject(new Error(data.error || "Erro na autentica\xE7\xE3o OAuth."));
393
406
  return;
394
407
  }
395
- if (!data.code) {
396
- reject(new Error("Resposta OAuth sem code."));
408
+ if (!data.token) {
409
+ reject(new Error("Resposta OAuth sem token."));
397
410
  return;
398
411
  }
399
- resolve({ code: data.code });
412
+ resolve({ token: data.token });
400
413
  }
401
414
  function cleanup() {
402
415
  clearTimeout(timer);
@@ -425,13 +438,9 @@ async function loginWithProvider(provider, options) {
425
438
  const pageOrigin = getOriginFromUrl2(authPageUrl);
426
439
  const redirectUri = authPageUrl;
427
440
  const startUrl = buildStartUrl(authPageUrl, provider, state, appId, authUrl);
428
- const { code } = await openOAuthPopup(startUrl, pageOrigin, state);
429
- const tokens = await postNewAuth(buildEndpoint(authUrl, `/${provider}`), {
430
- code,
431
- redirectUri,
432
- appId
433
- });
434
- const response = normalizeResponse(tokens, authUrl);
441
+ console.log("[mitra-sso] authPageUrl:", authPageUrl, "| startUrl:", startUrl, "| redirectUri:", redirectUri);
442
+ const { token } = await openOAuthPopup(startUrl, pageOrigin, state);
443
+ const response = normalizeResponse(token, authUrl);
435
444
  autoConfigureFromLogin2(response, authUrl, projectId);
436
445
  return response;
437
446
  } finally {
@@ -456,25 +465,27 @@ async function loginMitra2(method, options) {
456
465
  }
457
466
  async function emailLoginMitra2(options) {
458
467
  const { authUrl, projectId } = resolveLoginOptions2(options);
459
- const tokens = await postNewAuth(buildEndpoint(authUrl, "/login"), {
468
+ const data = await postBffAuth(buildBffAuthEndpoint(authUrl, "/email/login"), {
469
+ projectId: String(projectId),
460
470
  email: options.email,
461
- password: options.password,
462
- appId: String(projectId)
471
+ password: options.password
463
472
  });
464
- const response = normalizeResponse(tokens, authUrl);
473
+ const response = {
474
+ token: data.token,
475
+ baseURL: data.baseURL || authUrl,
476
+ ...data.integrationURL ? { integrationURL: data.integrationURL } : {}
477
+ };
465
478
  autoConfigureFromLogin2(response, authUrl, projectId);
466
479
  return response;
467
480
  }
468
481
  async function emailSignupMitra2(options) {
469
482
  const { authUrl, projectId } = resolveLoginOptions2(options);
470
- const tokens = await postNewAuth(buildEndpoint(authUrl, "/register"), {
483
+ await postBffAuth(buildBffAuthEndpoint(authUrl, "/email/signup"), {
484
+ projectId: String(projectId),
471
485
  name: options.name,
472
486
  email: options.email,
473
- password: options.password,
474
- appId: String(projectId)
487
+ password: options.password
475
488
  });
476
- const response = normalizeResponse(tokens, authUrl);
477
- autoConfigureFromLogin2(response, authUrl, projectId);
478
489
  }
479
490
  async function emailVerifyCodeMitra2(_options) {
480
491
  throw new Error(NOT_SUPPORTED_VERIFY);