@@ -21,15 +21,88 @@
|
||||
"/api/analytics/",
|
||||
"/api/feed-quality/",
|
||||
"/api/periods",
|
||||
"/api/sklad",
|
||||
"/api/feed-accounting",
|
||||
"/api/daily-plan",
|
||||
"/api/lab",
|
||||
"/api/auth/",
|
||||
];
|
||||
|
||||
const TOKEN_KEY = "compton.access_token";
|
||||
const ENTERPRISE_KEY = "compton.enterprise_id";
|
||||
|
||||
function readToken() {
|
||||
return (global.__WESP_ORCH__ && global.__WESP_ORCH__.accessToken) || "";
|
||||
return (
|
||||
(global.__WESP_ORCH__ && global.__WESP_ORCH__.accessToken) ||
|
||||
global.localStorage.getItem(TOKEN_KEY) ||
|
||||
""
|
||||
);
|
||||
}
|
||||
|
||||
function readEnterpriseId() {
|
||||
return (global.__WESP_ORCH__ && global.__WESP_ORCH__.enterpriseId) || "";
|
||||
return (
|
||||
(global.__WESP_ORCH__ && global.__WESP_ORCH__.enterpriseId) ||
|
||||
global.localStorage.getItem(ENTERPRISE_KEY) ||
|
||||
""
|
||||
);
|
||||
}
|
||||
|
||||
function persistAccessToken(token, user) {
|
||||
if (!token) return;
|
||||
global.localStorage.setItem(TOKEN_KEY, token);
|
||||
const entId = readEnterpriseId();
|
||||
if (global.ComptonWespAuth && typeof global.ComptonWespAuth.persistSession === "function") {
|
||||
global.ComptonWespAuth.persistSession(token, user || null, entId);
|
||||
return;
|
||||
}
|
||||
global.__WESP_ORCH__ = Object.assign({}, global.__WESP_ORCH__ || {}, {
|
||||
accessToken: token,
|
||||
enterpriseId: entId,
|
||||
});
|
||||
}
|
||||
|
||||
let refreshInFlight = null;
|
||||
|
||||
function tryRefreshAccessToken() {
|
||||
if (refreshInFlight) return refreshInFlight;
|
||||
refreshInFlight = nativeFetch("/api/v1/auth/refresh", {
|
||||
method: "POST",
|
||||
credentials: "include",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
})
|
||||
.then(function (resp) {
|
||||
if (!resp.ok) return null;
|
||||
return resp.json();
|
||||
})
|
||||
.then(function (data) {
|
||||
if (!data || !data.access_token) return null;
|
||||
persistAccessToken(data.access_token, data.user);
|
||||
return data.access_token;
|
||||
})
|
||||
.catch(function () {
|
||||
return null;
|
||||
})
|
||||
.finally(function () {
|
||||
refreshInFlight = null;
|
||||
});
|
||||
return refreshInFlight;
|
||||
}
|
||||
|
||||
function shouldAttemptRefresh(url, init) {
|
||||
if (!url || (init && init._authRetried)) return false;
|
||||
if (url.indexOf("/api/v1/auth/refresh") >= 0) return false;
|
||||
if (url.indexOf("/api/auth/login") >= 0) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function retryWithToken(patched, authInit, token) {
|
||||
const retryHeaders = new Headers((authInit && authInit.headers) || {});
|
||||
retryHeaders.set("Authorization", "Bearer " + token);
|
||||
const retryInit = Object.assign({}, authInit || {}, {
|
||||
headers: retryHeaders,
|
||||
_authRetried: true,
|
||||
});
|
||||
return nativeFetch(patched, retryInit);
|
||||
}
|
||||
|
||||
function isExternalUrl(url) {
|
||||
@@ -52,7 +125,8 @@
|
||||
}
|
||||
}
|
||||
if (url.startsWith("/api/") && !url.startsWith("/api/v1/")) {
|
||||
return API_BASE + url.slice(4);
|
||||
const rewritten = API_BASE + url.slice(4);
|
||||
return rewritten;
|
||||
}
|
||||
return url;
|
||||
}
|
||||
@@ -83,11 +157,38 @@
|
||||
return url + sep + "enterprise_id=" + encodeURIComponent(enterpriseId);
|
||||
}
|
||||
|
||||
function needsEnterpriseScope(url) {
|
||||
if (isExternalUrl(url)) return false;
|
||||
for (let i = 0; i < WESP_API_PREFIXES.length; i++) {
|
||||
if (url.startsWith(WESP_API_PREFIXES[i])) {
|
||||
return url.indexOf("enterprise_id=") < 0;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function awaitOrchestratorReady() {
|
||||
const ready = global.__WESP_ORCH__ && global.__WESP_ORCH__.ready;
|
||||
return ready && typeof ready.then === "function" ? ready : Promise.resolve();
|
||||
}
|
||||
|
||||
const nativeFetch = global.fetch.bind(global);
|
||||
global.fetch = function (input, init) {
|
||||
if (typeof input === "string") {
|
||||
const patched = appendEnterpriseQuery(patchUrl(input));
|
||||
return nativeFetch(patched, withAuth(patched, init));
|
||||
const scopeReady = needsEnterpriseScope(input) ? awaitOrchestratorReady() : Promise.resolve();
|
||||
return scopeReady.then(function () {
|
||||
const patched = appendEnterpriseQuery(patchUrl(input));
|
||||
const authInit = withAuth(patched, init);
|
||||
return nativeFetch(patched, authInit).then(function (resp) {
|
||||
if (resp.status !== 401 || !shouldAttemptRefresh(patched, authInit)) {
|
||||
return resp;
|
||||
}
|
||||
return tryRefreshAccessToken().then(function (newToken) {
|
||||
if (!newToken) return resp;
|
||||
return retryWithToken(patched, authInit, newToken);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
return nativeFetch(input, withAuth("", init));
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user