async function refreshPending() { const res = await fetch("api/pending"); const data = await res.json(); const tbody = document.querySelector("#pending-table tbody"); const empty = document.getElementById("pending-empty"); tbody.innerHTML = ""; const ids = Object.keys(data); empty.style.display = ids.length ? "none" : "block"; for (const deviceId of ids) { const entry = data[deviceId]; const row = document.createElement("tr"); const requested = new Date(entry.requested_at * 1000).toLocaleString(); const meta = Object.entries(entry.meta || {}).map(([k, v]) => `${k}: ${v}`).join(", "); row.innerHTML = ` ${deviceId} ${entry.resolved_common_name} ${requested} ${meta || "-"} `; tbody.appendChild(row); } tbody.querySelectorAll(".approve").forEach((btn) => btn.addEventListener("click", () => approve(btn.dataset.id)) ); tbody.querySelectorAll(".reject").forEach((btn) => btn.addEventListener("click", () => reject(btn.dataset.id)) ); } async function refreshIssued() { const [issuedRes, revokedRes] = await Promise.all([fetch("api/issued"), fetch("api/revoked")]); const data = await issuedRes.json(); const revoked = await revokedRes.json(); const tbody = document.querySelector("#issued-table tbody"); const empty = document.getElementById("issued-empty"); tbody.innerHTML = ""; const ids = Object.keys(data); empty.style.display = ids.length ? "none" : "block"; for (const deviceId of ids) { const entry = data[deviceId]; const row = document.createElement("tr"); const issued = new Date(entry.issued_at * 1000).toLocaleString(); const expires = new Date(entry.not_after).toLocaleDateString(); const isRevoked = Object.prototype.hasOwnProperty.call(revoked, deviceId); const action = isRevoked ? `revoked` : ``; row.innerHTML = `${deviceId}${entry.common_name}${issued}${expires}${action}`; tbody.appendChild(row); } tbody.querySelectorAll(".revoke").forEach((btn) => btn.addEventListener("click", () => revoke(btn.dataset.id)) ); } async function refreshRevoked() { const res = await fetch("api/revoked"); const data = await res.json(); const tbody = document.querySelector("#revoked-table tbody"); const empty = document.getElementById("revoked-empty"); tbody.innerHTML = ""; const ids = Object.keys(data); empty.style.display = ids.length ? "none" : "block"; for (const deviceId of ids) { const entry = data[deviceId]; const row = document.createElement("tr"); const revokedAt = new Date(entry.revoked_at * 1000).toLocaleString(); row.innerHTML = `${deviceId}${revokedAt}${entry.reason}`; tbody.appendChild(row); } } async function approve(deviceId) { const res = await fetch(`api/approve/${encodeURIComponent(deviceId)}`, { method: "POST" }); if (!res.ok) { const err = await res.json(); alert(`Could not issue certificate: ${err.error}`); } await refresh(); } async function reject(deviceId) { await fetch(`api/reject/${encodeURIComponent(deviceId)}`, { method: "POST" }); await refresh(); } async function revoke(deviceId) { if (!confirm(`Revoke the certificate for "${deviceId}"? This can't be undone, and requires a Mosquitto restart to take effect.`)) return; const res = await fetch(`api/revoke/${encodeURIComponent(deviceId)}`, { method: "POST" }); if (!res.ok) { const err = await res.json(); alert(`Could not revoke: ${err.error}`); } await refresh(); } async function refreshBrokerCert() { const res = await fetch("api/broker-cert"); const data = await res.json(); const statusEl = document.getElementById("broker-cert-status"); const configEl = document.getElementById("broker-cert-config"); if (!data.info) { statusEl.textContent = "Not yet issued."; } else { const expires = new Date(data.info.not_after).toLocaleDateString(); statusEl.textContent = `Covers: ${data.info.hostnames.join(", ")} — expires ${expires}`; } configEl.textContent = `certfile: ${data.cert_filename}\n` + `keyfile: ${data.key_filename}\n` + `require_certificate: true\n` + `cafile: ${data.ca_filename}`; } document.getElementById("reissue-broker-cert").addEventListener("click", async () => { if (!confirm("Reissue the broker's TLS certificate? Mosquitto will need a restart to pick up the new files.")) return; const res = await fetch("api/broker-cert/reissue", { method: "POST" }); if (!res.ok) { const err = await res.json(); alert(`Could not reissue: ${err.error}`); } await refreshBrokerCert(); }); async function refresh() { await refreshPending(); await refreshIssued(); await refreshRevoked(); await refreshBrokerCert(); } refresh(); setInterval(refresh, 5000);