qbo_auth: realmId шифруется, /callback отвечает 302, no-cache; требования Intuit: раздел E по оригиналу, ручные тесты через /tests
All checks were successful
ci / test (push) Successful in 38s
ci / deploy (push) Successful in 2m38s

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-23 09:21:39 +03:00
parent b0a68930b1
commit e90b9f352d
7 changed files with 65 additions and 34 deletions

View File

@@ -30,14 +30,14 @@ export function buildApi(auth: QboAuth, token: string, sync?: QboSync, webhook?:
});
// требования Intuit: ответы с токенами и страницы действий не кэшируются, адрес с code/state не уходит в Referer
app.addHook("onSend", async (_req, reply) => {
reply.header("Cache-Control", "no-store");
reply.header("Cache-Control", "no-cache, no-store");
reply.header("Pragma", "no-cache");
reply.header("Referrer-Policy", "no-referrer");
reply.header("X-Content-Type-Options", "nosniff");
});
app.addHook("onRequest", async (req, reply) => {
const path = req.url.split("?")[0];
if (path === "/healthz" || path === "/callback" || path === "/webhook" || path === "/tests") return;
if (path === "/healthz" || path === "/callback" || path === "/webhook" || path === "/tests" || path === "/connected") return;
if (req.headers.authorization !== `Bearer ${token}`) return reply.code(401).send({ error: "unauthorized" });
});
@@ -82,12 +82,19 @@ export function buildApi(auth: QboAuth, token: string, sync?: QboSync, webhook?:
const q = req.query as { code?: string; state?: string; realmId?: string; error?: string };
try {
await auth.handleCallback(q);
return reply.type("text/html").send(page("QuickBooks подключён", "Авторизация сохранена. Эту вкладку можно закрыть."));
return reply.redirect("/api/qbo/connected?ok=1", 302);
} catch (e) {
req.log.warn({ err: String(e) }, "callback отклонён");
return reply.code(400).type("text/html").send(page("Не удалось подключить QuickBooks", "Начните подключение заново. Причина записана в журнал сервиса."));
return reply.redirect("/api/qbo/connected?ok=0", 302);
}
});
/** Итоговая страница после возврата с Intuit. /callback отвечает 302 сюда (требование Intuit: адрес с code не отдаёт HTML-тело). */
app.get("/connected", async (req, reply) => {
const ok = (req.query as { ok?: string }).ok === "1";
return ok
? reply.type("text/html").send(page("QuickBooks подключён", "Авторизация сохранена. Эту вкладку можно закрыть."))
: reply.code(400).type("text/html").send(page("Не удалось подключить QuickBooks", "Начните подключение заново. Причина записана в журнал сервиса."));
});
/** Ссылка авторизации (для администратора компании QuickBooks). */
app.post("/connect", async () => auth.startConnect());