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
@@ -0,0 +1,79 @@
|
||||
// House of RC (api.houseofrc.com) connection. The API key is a club secret: it lives only in
|
||||
// data/houseofrc.json (mode 600) or the HOUSEOFRC_API_KEY env var, never in app_state, so it
|
||||
// can't leak through GET /api/state, a JSON export or a backup. The browser only ever sees
|
||||
// whether a key is set and its last four characters.
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
const DEFAULT_BASE_URL = "https://api.houseofrc.com";
|
||||
const TIMEOUT_MS = 10000;
|
||||
|
||||
function createHouseOfRc({ dataDir, env = process.env, fetchImpl = globalThis.fetch }) {
|
||||
const configPath = path.join(dataDir, "houseofrc.json");
|
||||
const baseUrl = String(env.HOUSEOFRC_BASE_URL || DEFAULT_BASE_URL).replace(/\/+$/, "");
|
||||
|
||||
const readStoredKey = () => {
|
||||
try {
|
||||
return String(JSON.parse(fs.readFileSync(configPath, "utf8")).apiKey || "");
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
};
|
||||
const getKey = () => String(env.HOUSEOFRC_API_KEY || "").trim() || readStoredKey();
|
||||
|
||||
function status() {
|
||||
const key = getKey();
|
||||
return {
|
||||
configured: Boolean(key),
|
||||
source: env.HOUSEOFRC_API_KEY ? "env" : key ? "file" : "none",
|
||||
keyHint: key ? `…${key.slice(-4)}` : "",
|
||||
};
|
||||
}
|
||||
|
||||
function setKey(value) {
|
||||
const key = String(value || "").trim();
|
||||
if (!key) {
|
||||
fs.rmSync(configPath, { force: true });
|
||||
return status();
|
||||
}
|
||||
if (key.length < 16 || key.length > 200 || /\s/.test(key)) throw new Error("invalid_key");
|
||||
fs.mkdirSync(dataDir, { recursive: true });
|
||||
fs.writeFileSync(configPath, JSON.stringify({ apiKey: key, savedAt: new Date().toISOString() }), { mode: 0o600 });
|
||||
fs.chmodSync(configPath, 0o600);
|
||||
return status();
|
||||
}
|
||||
|
||||
// GET against the API with the club key. Returns { ok, status, data, error, retryAfter }.
|
||||
async function get(apiPath, query = {}) {
|
||||
const key = getKey();
|
||||
if (!key) return { ok: false, status: 0, error: "not_configured" };
|
||||
const url = new URL(baseUrl + apiPath);
|
||||
Object.entries(query).forEach(([name, value]) => {
|
||||
if (value !== undefined && value !== null && value !== "") url.searchParams.set(name, String(value));
|
||||
});
|
||||
try {
|
||||
const response = await fetchImpl(url, {
|
||||
headers: { Authorization: `Bearer ${key}`, Accept: "application/json" },
|
||||
signal: AbortSignal.timeout(TIMEOUT_MS),
|
||||
});
|
||||
const data = await response.json().catch(() => null);
|
||||
if (response.ok) return { ok: true, status: response.status, data };
|
||||
const error = response.status === 401 || response.status === 403 ? "unauthorized"
|
||||
: response.status === 429 ? "rate_limited" : "http_error";
|
||||
return { ok: false, status: response.status, error, retryAfter: Number(response.headers.get("retry-after")) || 0 };
|
||||
} catch (error) {
|
||||
return { ok: false, status: 0, error: error?.name === "TimeoutError" ? "timeout" : "unreachable" };
|
||||
}
|
||||
}
|
||||
|
||||
async function testConnection() {
|
||||
const result = await get("/v1/me");
|
||||
if (!result.ok) return result;
|
||||
const body = result.data?.data || {};
|
||||
return { ok: true, status: result.status, user: body.user || null, rateLimit: body.rate_limit ?? null };
|
||||
}
|
||||
|
||||
return { status, setKey, get, testConnection };
|
||||
}
|
||||
|
||||
module.exports = { createHouseOfRc, DEFAULT_BASE_URL };
|
||||
Reference in new issue
Block a user