fix: bind Moallem auth tokens to correct host

This commit is contained in:
SepehrYahyaee
2026-10-26 16:10:32 +03:30
parent 8d73d4996c
commit 63b1bb3e2c
4 changed files with 75 additions and 12 deletions
+46 -11
View File
@@ -29,6 +29,7 @@ loadRootDotEnv();
let running = false;
let sandhubToken = null;
let sandhubTokenAt = 0;
let incompatibleTokenRefresh = null;
function loadRootDotEnv() {
const file = path.join(ROOT, ".env");
@@ -394,13 +395,7 @@ async function fetchRemoteLookup(client, name) {
const rel = LOOKUP_PATHS[name];
if (!rel) throw new Error("Unknown lookup " + name);
const env = loadEnv();
const tokens = loadAuthTokens(client);
if (!tokens || !tokens.AUTHENTICATION_TOKEN) {
throw new Error(
"توکن " + client + " نیست. یک بار Submit بزنید تا لاگین انجام شود، یا scripts/fanavaran-auth.sh " + client,
);
}
const base = fanavaranBimeBaseForToken(client, tokens);
const { tokens, base } = await getCompatibleAuthContext(client);
const result = await curlJson(
"GET",
`${base}/${rel}`,
@@ -464,6 +459,44 @@ function loadAuthTokens(client) {
return parseEnvText(fs.readFileSync(file, "utf8"));
}
/**
* A token cannot be transferred between Fanavaran API hosts. Older Moallem
* flow envs could create a cache on apimanager.iraneit.com; heal that cache on
* first use instead of making the operator discover and delete it manually.
*/
async function getCompatibleAuthContext(client) {
const normalizedClient = String(client || "tejaratno").toLowerCase();
let tokens = loadAuthTokens(normalizedClient);
if (!tokens || !tokens.AUTHENTICATION_TOKEN) {
throw new Error(
"توکن " + normalizedClient + " نیست. ابتدا از بخش Auth وارد شوید.",
);
}
try {
return { tokens, base: fanavaranBimeBaseForToken(normalizedClient, tokens) };
} catch (error) {
const message = String(error?.message || error);
if (!message.startsWith("Cached ") || normalizedClient !== "moallem") throw error;
if (!incompatibleTokenRefresh) {
incompatibleTokenRefresh = runStage("auth", { forceLogin: true }).finally(() => {
incompatibleTokenRefresh = null;
});
}
const refreshed = await incompatibleTokenRefresh;
if (!refreshed.ok) {
throw new Error(
"توکن ناسازگار Moallem به صورت خودکار نوسازی نشد. جزئیات را در خروجی سرور بررسی کنید.",
);
}
tokens = loadAuthTokens(normalizedClient);
if (!tokens || !tokens.AUTHENTICATION_TOKEN) {
throw new Error("نوسازی توکن Moallem انجام شد، اما توکن جدید ذخیره نشد.");
}
return { tokens, base: fanavaranBimeBaseForToken(normalizedClient, tokens) };
}
}
function curlJson(method, url, headers, proxy) {
return new Promise((resolve) => {
const args = ["-sS", "--max-time", "40", "-X", method, url];
@@ -493,11 +526,13 @@ function curlJson(method, url, headers, proxy) {
}
async function inquireParties({ nationalCode, birthYear, birthMonth, birthDay, client, location, proxy }) {
const tokens = loadAuthTokens(client || "tejaratno");
if (!tokens || !tokens.AUTHENTICATION_TOKEN) {
return { ok: false, error: "No cached Fanavaran token. Submit a stage once or run scripts/fanavaran-auth.sh " + (client || "tejaratno") };
let auth;
try {
auth = await getCompatibleAuthContext(client || "tejaratno");
} catch (error) {
return { ok: false, error: String(error?.message || error) };
}
const base = fanavaranBimeBaseForToken(client, tokens);
const { tokens, base } = auth;
const qs = new URLSearchParams({
NationalCode: String(nationalCode || ""),
BirthYear: String(birthYear || ""),