Dashboard: an API key button that shows the key with a copy button

Behind the same sign-in as the rest of the dashboard (GET /api/key, no-store), so whoever
can open the dashboard can hand the key to a client without a shell on the server.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
neil 2026-11-05 22:51:21 -08:00
parent e814a2359d
commit 6fd82448c5
4 changed files with 58 additions and 3 deletions

View file

@ -158,6 +158,9 @@ pre {
background: var(--panel-2); color: var(--text); margin-bottom: 12px;
}
.dialog .err { color: var(--bad); font-size: 12.5px; min-height: 18px; margin: -6px 0 8px; }
.dialog .keyrow { display: flex; gap: 6px; margin-bottom: 14px; }
.dialog .keyrow input { margin: 0; flex: 1; min-width: 0; }
.dialog .actions { display: flex; justify-content: flex-end; }
.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg);
padding: 8px 14px; border-radius: 8px; font-size: 13px; opacity: 0; transition: opacity .25s; pointer-events: none; }
.toast.show { opacity: 1; }
@ -171,6 +174,7 @@ pre {
<span class="spacer"></span>
<span class="meta" id="uptime"></span>
<span class="meta" id="whoami" hidden></span>
<button id="connect" title="The API key, for clients">API key</button>
<button id="logout" title="Forget API key">Sign out</button>
</header>
@ -276,6 +280,14 @@ pre {
<a class="btn primary" href="/auth/login" id="auth-oidc-go">Sign in</a>
</div>
</div>
<div class="overlay" id="connect-modal">
<div class="dialog" role="dialog" aria-modal="true" aria-labelledby="connect-title">
<h3 id="connect-title">API key</h3>
<p>For OpenAI-compatible clients, sent as <code>Authorization: Bearer &lt;key&gt;</code>. Use the model name <code>default</code>, which always means the loaded model.</p>
<div class="keyrow"><input id="cx-key" type="password" readonly autocomplete="off" aria-label="API key"><button id="cx-show">Show</button><button id="cx-copy">Copy</button></div>
<div class="actions"><button id="cx-close">Close</button></div>
</div>
</div>
<div class="toast" id="toast"></div>
<script>
@ -321,13 +333,42 @@ $("auth-form").addEventListener("submit", (e) => {
$("auth").classList.remove("show");
poll();
});
async function copyText(text) {
try { await navigator.clipboard.writeText(text); return true; }
catch (e) {
// No clipboard API on plain http (a LAN address): fall back to a selected textarea.
const t = document.createElement("textarea"); t.value = text; t.style.position = "fixed"; t.style.opacity = "0";
document.body.appendChild(t); t.select();
let ok = false; try { ok = document.execCommand("copy"); } catch (e2) {}
t.remove(); return ok;
}
}
function closeConnect() {
$("connect-modal").classList.remove("show");
$("cx-key").value = ""; $("cx-key").type = "password"; $("cx-show").textContent = "Show";
}
$("connect").onclick = async () => {
let c;
try { c = await api("/api/key"); } catch (e) { if (e.message !== "unauthorized") toast("Couldn't load: " + e.message); return; }
$("cx-key").value = c.key;
$("connect-modal").classList.add("show"); $("cx-close").focus();
};
$("cx-copy").onclick = async () => toast(await copyText($("cx-key").value) ? "Copied" : "Couldn't copy; select it and copy by hand");
$("cx-show").onclick = () => {
const k = $("cx-key"), hide = k.type === "text";
k.type = hide ? "password" : "text"; $("cx-show").textContent = hide ? "Show" : "Hide";
};
$("cx-close").onclick = closeConnect;
$("connect-modal").addEventListener("click", (e) => { if (e.target === $("connect-modal")) closeConnect(); });
document.addEventListener("keydown", (e) => { if (e.key === "Escape" && $("connect-modal").classList.contains("show")) closeConnect(); });
$("logout").onclick = async () => {
if (AUTH.mode === "oidc") {
try { await fetch("/auth/logout", { method: "POST", credentials: "same-origin" }); } catch (e) {}
AUTH.user = null; $("whoami").hidden = true; clearTimeout(pollTimer); showAuth();
AUTH.user = null; $("whoami").hidden = true; clearTimeout(pollTimer); closeConnect(); showAuth();
return;
}
try { localStorage.removeItem("llmhost-key"); } catch (e) {} KEY = null; showAuth();
try { localStorage.removeItem("llmhost-key"); } catch (e) {} KEY = null; closeConnect(); showAuth();
};
const fmt = {

View file

@ -517,6 +517,11 @@ def make_handler(sampler, key, oidc=None):
elif path == "/api/status":
if self.authed():
self.send_json(200, sampler.snapshot())
elif path == "/api/key":
# For handing to clients. Behind the same sign-in as everything else here;
# send_json marks it no-store.
if self.authed():
self.send_json(200, {"key": key})
elif path == "/api/logs":
if self.authed():
lines = [l for l in service.logs("model", 1500)