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:
parent
e814a2359d
commit
6fd82448c5
4 changed files with 58 additions and 3 deletions
|
|
@ -112,7 +112,8 @@ It ends by printing the two Pangolin HTTP resources to create, both pointing at
|
|||
| dashboard, at the `--public-url` host | `<LAN IP>:<web port>` | llm-host, through your OIDC provider |
|
||||
| API, at a host of your choice | `<LAN IP>:<API port>` | llm-host, with the API key |
|
||||
|
||||
Clients then use `https://<API host>/v1` with the key from `llmhost key`.
|
||||
Clients then use `https://<API host>/v1` with the key from `llmhost key`, or from the dashboard's **API key** button,
|
||||
which shows it to anyone signed in, with a copy button.
|
||||
|
||||
For Authelia, the client looks like this:
|
||||
|
||||
|
|
|
|||
|
|
@ -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 <key></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 = {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -98,6 +98,10 @@ check("config reports key mode", st == 200 and json.loads(body)["mode"] == "key"
|
|||
check("status without key -> 401", req(srv, "GET", "/api/status")[0] == 401)
|
||||
check("status with key -> 200", req(srv, "GET", "/api/status", {"Authorization": f"Bearer {KEY}"})[0] == 200)
|
||||
check("no /auth/login without OIDC", req(srv, "GET", "/auth/login")[0] == 404)
|
||||
check("/api/key needs the key", req(srv, "GET", "/api/key")[0] == 401)
|
||||
st, h, body, _ = req(srv, "GET", "/api/key", {"Authorization": f"Bearer {KEY}"})
|
||||
check("/api/key returns it", st == 200 and json.loads(body) == {"key": KEY}, body)
|
||||
check("/api/key isn't cached", h.get("cache-control") == "no-store", h)
|
||||
srv.shutdown()
|
||||
|
||||
print("\n2. OIDC mode refuses the key and unsigned visitors")
|
||||
|
|
@ -165,6 +169,10 @@ st, _, _, _ = req(srv, "POST", "/api/stats/reset", {**PUB_HOST, "Cookie": sess,
|
|||
check("POST from another origin -> 403", st == 403, st)
|
||||
st, _, _, _ = req(srv, "POST", "/api/stats/reset", {**PUB_HOST, "Cookie": sess, "Origin": PUBLIC})
|
||||
check("POST from the dashboard's origin -> 200", st == 200, st)
|
||||
check("/api/key refused without a session", req(srv, "GET", "/api/key", PUB_HOST)[0] == 401)
|
||||
check("/api/key refused for the key alone", req(srv, "GET", "/api/key", {**PUB_HOST, "Authorization": f"Bearer {KEY}"})[0] == 401)
|
||||
st, _, body, _ = req(srv, "GET", "/api/key", {**PUB_HOST, "Cookie": sess})
|
||||
check("signed in: /api/key returns it", st == 200 and json.loads(body) == {"key": KEY}, body)
|
||||
check("only the session's hash is stored", sess.split("=", 1)[1] not in o.sessions)
|
||||
|
||||
print("\n5. sign-ins that must fail")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue