mitra-interactions-sdk 1.0.60-beta.27 → 1.0.60-beta.28
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 +59 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +59 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -279,7 +279,15 @@ async function refreshTokenSilently(authUrl, _projectId) {
|
|
|
279
279
|
if (!refreshToken) {
|
|
280
280
|
throw new Error("refreshToken n\xE3o dispon\xEDvel para renova\xE7\xE3o. Fa\xE7a login novamente.");
|
|
281
281
|
}
|
|
282
|
+
if (!hasAppScope(refreshToken)) {
|
|
283
|
+
clearRefreshToken();
|
|
284
|
+
throw new Error("refreshToken sem escopo de app (sess\xE3o do console) \u2014 descartado. Fa\xE7a loginMitra({ projectId }) novamente.");
|
|
285
|
+
}
|
|
282
286
|
const data = await postBffAuth(buildBffAuthEndpoint(authUrl, "/refresh-token"), { refreshToken });
|
|
287
|
+
if (!hasAppScope(data.token)) {
|
|
288
|
+
clearRefreshToken();
|
|
289
|
+
throw new Error("refresh retornou token sem escopo de app \u2014 rejeitado. Fa\xE7a loginMitra({ projectId }) novamente.");
|
|
290
|
+
}
|
|
283
291
|
return {
|
|
284
292
|
token: data.token,
|
|
285
293
|
// Refresh não muda a base — mantém o authUrl (gateway).
|
|
@@ -810,8 +818,12 @@ function getGatewayOrigin() {
|
|
|
810
818
|
return new URL(config.baseURL).origin;
|
|
811
819
|
}
|
|
812
820
|
function getTaskWsUrl(taskId) {
|
|
821
|
+
const token = getRawToken();
|
|
822
|
+
if (!hasAppScope(token)) {
|
|
823
|
+
throw new Error("Agent Chat: o token configurado \xE9 uma sess\xE3o do console (sem app_id) \u2014 o WebSocket do copilot exige token de app. Fa\xE7a loginMitra({ projectId }).");
|
|
824
|
+
}
|
|
813
825
|
const origin = getGatewayOrigin().replace(/^http/, "ws");
|
|
814
|
-
return `${origin}/copilot/ws/tasks/${encodeURIComponent(taskId)}?token=${encodeURIComponent(
|
|
826
|
+
return `${origin}/copilot/ws/tasks/${encodeURIComponent(taskId)}?token=${encodeURIComponent(token)}`;
|
|
815
827
|
}
|
|
816
828
|
function getRawToken() {
|
|
817
829
|
const config = getConfig();
|
|
@@ -1251,6 +1263,42 @@ function persistRefreshToken(refreshToken) {
|
|
|
1251
1263
|
} catch (e) {
|
|
1252
1264
|
}
|
|
1253
1265
|
}
|
|
1266
|
+
function removePersistedRefreshToken() {
|
|
1267
|
+
const ls = safeLocalStorage();
|
|
1268
|
+
if (!ls) return;
|
|
1269
|
+
try {
|
|
1270
|
+
const raw = ls.getItem(SESSION_STORE_KEY);
|
|
1271
|
+
if (!raw) return;
|
|
1272
|
+
const obj = JSON.parse(raw);
|
|
1273
|
+
delete obj.refreshToken;
|
|
1274
|
+
ls.setItem(SESSION_STORE_KEY, JSON.stringify(obj));
|
|
1275
|
+
} catch (e) {
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
function decodeJwtPayload(token) {
|
|
1279
|
+
try {
|
|
1280
|
+
const part = stripBearer(token).split(".")[1];
|
|
1281
|
+
if (!part) return null;
|
|
1282
|
+
const b64 = part.replace(/-/g, "+").replace(/_/g, "/");
|
|
1283
|
+
const padded = b64 + "=".repeat((4 - b64.length % 4) % 4);
|
|
1284
|
+
const json = typeof atob === "function" ? atob(padded) : Buffer.from(padded, "base64").toString("utf8");
|
|
1285
|
+
return JSON.parse(json);
|
|
1286
|
+
} catch (e) {
|
|
1287
|
+
return null;
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
function hasAppScope(token) {
|
|
1291
|
+
const payload = decodeJwtPayload(token);
|
|
1292
|
+
if (!payload) return true;
|
|
1293
|
+
return typeof payload.app_id === "string" && payload.app_id.length > 0;
|
|
1294
|
+
}
|
|
1295
|
+
function clearRefreshToken() {
|
|
1296
|
+
if (globalConfig) {
|
|
1297
|
+
const { refreshToken: _drop, ...rest } = globalConfig;
|
|
1298
|
+
globalConfig = rest;
|
|
1299
|
+
}
|
|
1300
|
+
removePersistedRefreshToken();
|
|
1301
|
+
}
|
|
1254
1302
|
function readPersistedRefreshToken() {
|
|
1255
1303
|
const ls = safeLocalStorage();
|
|
1256
1304
|
if (!ls) return void 0;
|
|
@@ -1283,7 +1331,16 @@ function configureSdkMitra(config = {}) {
|
|
|
1283
1331
|
if (merged.token != null) merged.token = stripBearer(merged.token);
|
|
1284
1332
|
if (!merged.refreshToken) {
|
|
1285
1333
|
const persisted = readPersistedRefreshToken();
|
|
1286
|
-
if (persisted)
|
|
1334
|
+
if (persisted) {
|
|
1335
|
+
if (hasAppScope(persisted)) {
|
|
1336
|
+
merged.refreshToken = persisted;
|
|
1337
|
+
} else {
|
|
1338
|
+
removePersistedRefreshToken();
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
} else if (!hasAppScope(merged.refreshToken)) {
|
|
1342
|
+
delete merged.refreshToken;
|
|
1343
|
+
removePersistedRefreshToken();
|
|
1287
1344
|
} else {
|
|
1288
1345
|
persistRefreshToken(merged.refreshToken);
|
|
1289
1346
|
}
|