feat(houseofrc): save the club API key server-side and test the connection (1.78.0)
This commit is contained in:
1 parent
7f7af2600d
commit
0c54e8b58a
11 files changed
+280
-4
No files matched your search
+36
@@ -2184,6 +2184,24 @@ const TRANSLATIONS = {
|
||||
"tts.download": "Hämta",
|
||||
"tts.downloading": "Hämtar...",
|
||||
"tts.test_failed": "Kunde inte spela upp den lokala rösten - kontrollera installationen.",
|
||||
"houseofrc.title": "House of RC",
|
||||
"houseofrc.hint": "Klubbens API-nyckel från House of RC. Den sparas bara på servern (data/houseofrc.json) och visas aldrig igen, inte ens här.",
|
||||
"houseofrc.key": "API-nyckel",
|
||||
"houseofrc.save": "Spara nyckel",
|
||||
"houseofrc.test": "Testa anslutning",
|
||||
"houseofrc.testing": "Testar…",
|
||||
"houseofrc.remove": "Ta bort nyckel",
|
||||
"houseofrc.remove_confirm": "Ta bort House of RC-nyckeln från servern?",
|
||||
"houseofrc.state_none": "Ingen nyckel sparad.",
|
||||
"houseofrc.state_saved": "Nyckel sparad på servern ({hint}).",
|
||||
"houseofrc.state_env": "Nyckel från miljövariabeln HOUSEOFRC_API_KEY ({hint}).",
|
||||
"houseofrc.connected": "✅ Ansluten som {name} (id {id}) · {limit} anrop/min",
|
||||
"houseofrc.err_not_configured": "Ingen nyckel sparad.",
|
||||
"houseofrc.err_unauthorized": "House of RC godkände inte nyckeln. Kontrollera att den är rätt kopierad.",
|
||||
"houseofrc.err_rate_limited": "För många anrop. Vänta {seconds} s och försök igen.",
|
||||
"houseofrc.err_unreachable": "Når inte House of RC. Har servern internet?",
|
||||
"houseofrc.err_invalid_key": "Det ser inte ut som en API-nyckel.",
|
||||
"houseofrc.err_other": "House of RC svarade med fel ({status}).",
|
||||
"auth.title": "Admin-PIN",
|
||||
"auth.hint": "Skyddar alla ändringar (race, resultat, inställningar, uppdatering) med en PIN. Läsning och publika overlay-/träningslänkar påverkas inte. Inloggningen sparas i webbläsaren i 30 dagar.",
|
||||
"auth.state_off": "Av - vem som helst på nätverket kan ändra.",
|
||||
@@ -4336,6 +4354,24 @@ const TRANSLATIONS = {
|
||||
"tts.download": "Download",
|
||||
"tts.downloading": "Downloading...",
|
||||
"tts.test_failed": "Could not play the local voice - check the installation.",
|
||||
"houseofrc.title": "House of RC",
|
||||
"houseofrc.hint": "The club's House of RC API key. It is stored on the server only (data/houseofrc.json) and never shown again, not even here.",
|
||||
"houseofrc.key": "API key",
|
||||
"houseofrc.save": "Save key",
|
||||
"houseofrc.test": "Test connection",
|
||||
"houseofrc.testing": "Testing…",
|
||||
"houseofrc.remove": "Remove key",
|
||||
"houseofrc.remove_confirm": "Remove the House of RC key from the server?",
|
||||
"houseofrc.state_none": "No key saved.",
|
||||
"houseofrc.state_saved": "Key saved on the server ({hint}).",
|
||||
"houseofrc.state_env": "Key from the HOUSEOFRC_API_KEY environment variable ({hint}).",
|
||||
"houseofrc.connected": "✅ Connected as {name} (id {id}) · {limit} requests/min",
|
||||
"houseofrc.err_not_configured": "No key saved.",
|
||||
"houseofrc.err_unauthorized": "House of RC rejected the key. Check that it was copied correctly.",
|
||||
"houseofrc.err_rate_limited": "Too many requests. Wait {seconds} s and try again.",
|
||||
"houseofrc.err_unreachable": "Can't reach House of RC. Does the server have internet?",
|
||||
"houseofrc.err_invalid_key": "That doesn't look like an API key.",
|
||||
"houseofrc.err_other": "House of RC answered with an error ({status}).",
|
||||
"auth.title": "Admin PIN",
|
||||
"auth.hint": "Protects every change (races, results, settings, updates) with a PIN. Reading and public overlay/training links are unaffected. The login is remembered in this browser for 30 days.",
|
||||
"auth.state_off": "Off - anyone on the network can make changes.",
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
// Settings → System → House of RC: save the club's API key on the server and test it.
|
||||
// The key is never read back; the server only tells whether one is set (and its last 4 chars).
|
||||
async function postJson(path, body) {
|
||||
try {
|
||||
const response = await fetch(path, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body || {}) });
|
||||
return { status: response.status, body: await response.json().catch(() => ({})) };
|
||||
} catch {
|
||||
return { status: 0, body: { error: "unreachable" } };
|
||||
}
|
||||
}
|
||||
|
||||
export function describeHouseOfRcError(t, code, body = {}) {
|
||||
if (code === 401 && body.error === "auth_required") return t("auth.login_required");
|
||||
const key = {
|
||||
not_configured: "houseofrc.err_not_configured",
|
||||
unauthorized: "houseofrc.err_unauthorized",
|
||||
rate_limited: "houseofrc.err_rate_limited",
|
||||
timeout: "houseofrc.err_unreachable",
|
||||
unreachable: "houseofrc.err_unreachable",
|
||||
invalid_key: "houseofrc.err_invalid_key",
|
||||
}[body.error];
|
||||
return key ? t(key, { seconds: body.retryAfter || 60 }) : t("houseofrc.err_other", { status: body.status || code });
|
||||
}
|
||||
|
||||
export async function mountHouseOfRcSettings(container, { t, escapeHtml }) {
|
||||
if (!container) return;
|
||||
const status = await fetch("/api/houseofrc/status").then((res) => res.json()).catch(() => null);
|
||||
if (!status) {
|
||||
container.innerHTML = `<p class="error">${escapeHtml(t("houseofrc.err_unreachable"))}</p>`;
|
||||
return;
|
||||
}
|
||||
const remount = () => mountHouseOfRcSettings(container, { t, escapeHtml });
|
||||
const stateText = !status.configured ? t("houseofrc.state_none")
|
||||
: status.source === "env" ? t("houseofrc.state_env", { hint: status.keyHint })
|
||||
: t("houseofrc.state_saved", { hint: status.keyHint });
|
||||
container.innerHTML = `
|
||||
<p><strong id="houseOfRcState">${escapeHtml(stateText)}</strong></p>
|
||||
<p class="hint">${escapeHtml(t("houseofrc.hint"))}</p>
|
||||
<form id="houseOfRcKeyForm" class="form-grid cols-2">
|
||||
<label>${escapeHtml(t("houseofrc.key"))}
|
||||
<input type="password" name="apiKey" id="houseOfRcKey" autocomplete="off" spellcheck="false" placeholder="hrc_…" ${status.source === "env" ? "disabled" : ""} />
|
||||
</label>
|
||||
<div class="actions">
|
||||
<button class="btn btn-primary" type="submit" ${status.source === "env" ? "disabled" : ""}>${escapeHtml(t("houseofrc.save"))}</button>
|
||||
<button class="btn" type="button" id="houseOfRcTest" ${status.configured ? "" : "disabled"}>${escapeHtml(t("houseofrc.test"))}</button>
|
||||
${status.source === "file" ? `<button class="btn btn-danger" type="button" id="houseOfRcRemove">${escapeHtml(t("houseofrc.remove"))}</button>` : ""}
|
||||
</div>
|
||||
</form>
|
||||
<p class="hint" id="houseOfRcResult" role="status" hidden></p>`;
|
||||
const result = container.querySelector("#houseOfRcResult");
|
||||
const show = (text, isError = false) => {
|
||||
result.textContent = text;
|
||||
result.className = isError ? "error" : "hint";
|
||||
result.hidden = false;
|
||||
};
|
||||
container.querySelector("#houseOfRcKeyForm").addEventListener("submit", async (event) => {
|
||||
event.preventDefault();
|
||||
const apiKey = String(new FormData(event.currentTarget).get("apiKey") || "").trim();
|
||||
if (!apiKey) return;
|
||||
const { status: code, body } = await postJson("/api/houseofrc/key", { apiKey });
|
||||
if (code !== 200) return show(describeHouseOfRcError(t, code, body), true);
|
||||
await remount();
|
||||
container.querySelector("#houseOfRcTest")?.click();
|
||||
});
|
||||
container.querySelector("#houseOfRcTest")?.addEventListener("click", async () => {
|
||||
show(t("houseofrc.testing"));
|
||||
const { status: code, body } = await postJson("/api/houseofrc/test");
|
||||
if (code === 200 && body.ok) show(t("houseofrc.connected", { name: body.user?.name || "?", id: body.user?.id ?? "?", limit: body.rateLimit ?? "?" }));
|
||||
else show(describeHouseOfRcError(t, code, body), true);
|
||||
});
|
||||
container.querySelector("#houseOfRcRemove")?.addEventListener("click", async () => {
|
||||
if (!window.confirm(t("houseofrc.remove_confirm"))) return;
|
||||
const { status: code, body } = await postJson("/api/houseofrc/key", { apiKey: "" });
|
||||
if (code !== 200) return show(describeHouseOfRcError(t, code, body), true);
|
||||
remount();
|
||||
});
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { normalizePublicBaseUrl } from "./public_url.js";
|
||||
import { decoderDiagnosticsMarkup, mountDecoderDiagnostics } from "./decoder_diagnostics_view.js";
|
||||
import { mountAuthSettings } from "./auth_view.js";
|
||||
import { mountHouseOfRcSettings } from "./houseofrc_view.js";
|
||||
import { mountTtsSettings } from "./tts_settings_view.js";
|
||||
import { mountDecoderSimSettings } from "./decoder_sim_view.js";
|
||||
import { formatSpokenLapTime, formatShortLapCall } from "./race_audio.js";
|
||||
@@ -464,6 +465,11 @@ export function renderSettings(deps) {
|
||||
<div class="panel-body" id="authSettings"></div>
|
||||
</section>
|
||||
|
||||
<section class="panel mt-16" id="settingsHouseOfRc">
|
||||
<div class="panel-header"><h3>🏠 ${t("houseofrc.title")}</h3></div>
|
||||
<div class="panel-body" id="houseOfRcSettings"></div>
|
||||
</section>
|
||||
|
||||
<section class="panel mt-16" id="settingsController">
|
||||
<div class="panel-header"><h3>🎙 ${t("controller.title")}</h3></div>
|
||||
<div class="panel-body">
|
||||
@@ -632,6 +638,7 @@ Erik Eriksson;654321;4WD;Associated;;</code></pre>
|
||||
pendingSettingsFocusId = "";
|
||||
}
|
||||
mountAuthSettings(document.getElementById("authSettings"), { t, escapeHtml });
|
||||
mountHouseOfRcSettings(document.getElementById("houseOfRcSettings"), { t, escapeHtml });
|
||||
// Per computer, not in the shared settings: sent with the next heartbeat of the master lock.
|
||||
document.getElementById("controllerName")?.addEventListener("change", (event) => {
|
||||
try { localStorage.setItem("rc_controller_name", event.target.value.trim()); } catch { /* storage blocked */ }
|
||||
|
||||
Reference in new issue
Block a user