Split the internet upgrades: API-only tunnel, OIDC, authenticator codes, expose-dashboard
install-newt now sets up the tunnel for the API only; the dashboard stays on the LAN. Dashboard sign-in is its own step: install-oidc (the OIDC flow, split out) or install-otp, which pairs authenticator apps with a QR code in the terminal (stdlib QR encoder, TOTP per RFC 6238) so a code also opens the dashboard, with replay and lockout rules. expose-dashboard walks through publishing the dashboard, only after install-newt, and warns while it's protected by the API key alone; with codes it offers to switch the key off for the dashboard. Each install-* has a remove-*. Also opencode-install.sh: a standalone script, fetched from the repo, that installs opencode if needed, saves the API key to a 0600 file and adds the provider to opencode.json for new and existing configs. /v1/models reports whether the model reasons, for that script. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
e6ff4ce909
commit
c8b70afdbf
20 changed files with 1715 additions and 239 deletions
|
|
@ -171,6 +171,9 @@ pre {
|
|||
.dialog .snip button { position: absolute; top: 6px; right: 6px; }
|
||||
#cx-keycmd { white-space: pre-wrap; overflow-wrap: anywhere; }
|
||||
.dialog .actions { display: flex; justify-content: flex-end; }
|
||||
.dialog details.manual { margin-bottom: 12px; font-size: 13px; }
|
||||
.dialog details.manual summary { cursor: pointer; color: var(--muted); margin-bottom: 8px; }
|
||||
#cx-install { white-space: pre-wrap; overflow-wrap: anywhere; }
|
||||
.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; }
|
||||
|
|
@ -278,9 +281,16 @@ pre {
|
|||
|
||||
<div class="overlay" id="auth">
|
||||
<form class="dialog" id="auth-form">
|
||||
<h3>API key</h3>
|
||||
<p>Enter the API key from <code>~/.config/llm-host/api_key</code> on the server (or run <code>llmhost key</code>). It's kept in this browser only.</p>
|
||||
<input id="auth-key" type="password" autocomplete="current-password" placeholder="sk-…" required>
|
||||
<h3>Sign in</h3>
|
||||
<div id="auth-key-part">
|
||||
<p>Enter the API key from <code>~/.config/llm-host/api_key</code> on the server (or run <code>llmhost key</code>). It's kept in this browser only.</p>
|
||||
<input id="auth-key" type="password" autocomplete="current-password" placeholder="sk-…" aria-label="API key">
|
||||
</div>
|
||||
<div id="auth-otp-part" hidden>
|
||||
<p id="auth-otp-text">Or a code from your authenticator app:</p>
|
||||
<input id="auth-code" inputmode="numeric" autocomplete="one-time-code" maxlength="7" placeholder="123456"
|
||||
aria-label="Authenticator code">
|
||||
</div>
|
||||
<div class="err" id="auth-err"></div>
|
||||
<button class="primary" type="submit">Continue</button>
|
||||
</form>
|
||||
|
|
@ -298,10 +308,16 @@ pre {
|
|||
<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>
|
||||
|
||||
<h4>Add to opencode</h4>
|
||||
<p>The API's address, as the machine running opencode reaches it:</p>
|
||||
<div class="keyrow"><input id="cx-url" type="url" spellcheck="false" autocomplete="off"
|
||||
placeholder="https://llm-api.example.com" aria-label="API base URL"></div>
|
||||
<p>Then run this there, or send it to whoever you're giving access, with the key. It needs only the API, not this
|
||||
dashboard: it installs opencode if it's missing, asks for the key (hidden), saves it, and adds this server to
|
||||
<code>opencode.json</code>, keeping their other settings:</p>
|
||||
<div class="snip" style="margin-bottom:14px"><pre id="cx-install"></pre><button class="small" data-copy="cx-install">Copy</button></div>
|
||||
<details class="manual">
|
||||
<summary>Or set it up by hand</summary>
|
||||
<ol>
|
||||
<li>The API's address, as this machine reaches it:
|
||||
<div class="keyrow" style="margin:6px 0 0"><input id="cx-url" type="url" spellcheck="false" autocomplete="off"
|
||||
placeholder="https://llm-api.example.com/v1" aria-label="API base URL"></div></li>
|
||||
<li>Save the key where the provider reads it (paste it at the prompt; it stays out of your shell history):
|
||||
<div class="snip"><pre id="cx-keycmd"></pre><button class="small" data-copy="cx-keycmd">Copy</button></div></li>
|
||||
<li>Put this in <code>~/.config/opencode/opencode.json</code>. If the file is empty or missing, paste the whole thing;
|
||||
|
|
@ -310,6 +326,7 @@ pre {
|
|||
<div class="snip"><pre id="cx-block"></pre><button class="small" data-copy="cx-block">Copy</button></div></li>
|
||||
<li>Pick <b id="cx-pick"></b> in opencode's model list.</li>
|
||||
</ol>
|
||||
</details>
|
||||
<div class="actions"><button id="cx-close">Close</button></div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -335,9 +352,14 @@ function toast(msg) {
|
|||
|
||||
async function api(path, opts = {}) {
|
||||
const headers = { "Content-Type": "application/json" };
|
||||
if (AUTH.mode === "key") headers["Authorization"] = "Bearer " + KEY;
|
||||
const useKey = AUTH.mode === "key" && KEY && AUTH.key !== false;
|
||||
if (useKey) headers["Authorization"] = "Bearer " + KEY;
|
||||
const r = await fetch(path, { ...opts, headers, credentials: "same-origin" });
|
||||
if (r.status === 401) { showAuth(AUTH.mode === "key" ? "That key was rejected." : ""); throw new Error("unauthorized"); }
|
||||
if (r.status === 401) {
|
||||
if (useKey) showAuth("That key was rejected.");
|
||||
else { AUTH.user = null; $("whoami").hidden = true; showAuth(""); }
|
||||
throw new Error("unauthorized");
|
||||
}
|
||||
const body = await r.json();
|
||||
if (!r.ok) throw new Error(body.error || r.statusText);
|
||||
return body;
|
||||
|
|
@ -349,12 +371,38 @@ function showAuth(err = "") {
|
|||
$("auth").classList.add("show"); $("auth-oidc-go").focus();
|
||||
return;
|
||||
}
|
||||
$("auth-err").textContent = err; $("auth").classList.add("show"); $("auth-key").focus();
|
||||
// Key mode: the API key (unless it's switched off for the dashboard) and, after
|
||||
// `llmhost install-otp`, a code from an authenticator app.
|
||||
const keyOn = AUTH.key !== false;
|
||||
$("auth-key-part").hidden = !keyOn;
|
||||
$("auth-otp-part").hidden = !AUTH.otp;
|
||||
$("auth-otp-text").textContent = keyOn ? "Or a code from your authenticator app:" : "Enter a code from your authenticator app:";
|
||||
$("auth-err").textContent = err; $("auth").classList.add("show");
|
||||
(keyOn ? $("auth-key") : $("auth-code")).focus();
|
||||
}
|
||||
$("auth-form").addEventListener("submit", (e) => {
|
||||
function signedIn(user, name) {
|
||||
AUTH.user = user; AUTH.name = name;
|
||||
$("whoami").textContent = name || user; $("whoami").hidden = !user;
|
||||
}
|
||||
$("auth-form").addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
KEY = $("auth-key").value.trim();
|
||||
try { localStorage.setItem("llmhost-key", KEY); } catch (e) {}
|
||||
const code = AUTH.otp ? $("auth-code").value.replace(/\D/g, "") : "";
|
||||
if (code) {
|
||||
let r, body = {};
|
||||
try {
|
||||
r = await fetch("/auth/otp", { method: "POST", credentials: "same-origin",
|
||||
headers: { "Content-Type": "application/json" }, body: JSON.stringify({ code }) });
|
||||
body = await r.json();
|
||||
} catch (err) { $("auth-err").textContent = "Couldn't reach the dashboard."; return; }
|
||||
$("auth-code").value = "";
|
||||
if (!r.ok) { $("auth-err").textContent = body.error || "That code didn't work."; return; }
|
||||
signedIn("authenticator", "Authenticator app");
|
||||
} else {
|
||||
const k = $("auth-key").value.trim();
|
||||
if (!k || AUTH.key === false) { $("auth-err").textContent = AUTH.otp ? "Enter a code from your authenticator app." : "Enter the API key."; return; }
|
||||
KEY = k;
|
||||
try { localStorage.setItem("llmhost-key", KEY); } catch (e) {}
|
||||
}
|
||||
$("auth").classList.remove("show");
|
||||
poll();
|
||||
});
|
||||
|
|
@ -402,6 +450,9 @@ function renderOpencode() {
|
|||
// One line: pasted as two, the first line's `read` would swallow the second as the key.
|
||||
`mkdir -p ~/.config/opencode && printf 'API key: ' && stty -echo && read -r k; stty echo; echo; ` +
|
||||
`[ -n "$k" ] && (umask 077 && printf '%s\\n' "$k" > ${keyfile}) && echo "saved ${keyfile}"; unset k`;
|
||||
$("cx-install").textContent = url
|
||||
? `/bin/bash -c "$(curl -fsSL ${CONNECT.installer})" -- --url ${url.replace(/\/v1$/, "")}`
|
||||
: "Type the API's address above first.";
|
||||
$("cx-pick").textContent = `${block.name} → ${Object.values(block.models)[0]?.name || CONNECT.model}`;
|
||||
try { localStorage.setItem("llmhost-api-url", $("cx-url").value.trim()); } catch (e) {}
|
||||
}
|
||||
|
|
@ -438,7 +489,9 @@ $("logout").onclick = async () => {
|
|||
AUTH.user = null; $("whoami").hidden = true; clearTimeout(pollTimer); closeConnect(); showAuth();
|
||||
return;
|
||||
}
|
||||
try { localStorage.removeItem("llmhost-key"); } catch (e) {} KEY = null; closeConnect(); showAuth();
|
||||
try { localStorage.removeItem("llmhost-key"); } catch (e) {} KEY = null;
|
||||
if (AUTH.user) { try { await fetch("/auth/logout", { method: "POST", credentials: "same-origin" }); } catch (e) {} }
|
||||
signedIn(null); clearTimeout(pollTimer); closeConnect(); showAuth();
|
||||
};
|
||||
|
||||
const fmt = {
|
||||
|
|
@ -829,10 +882,10 @@ $("logs").addEventListener("toggle", () => {
|
|||
let pollTimer = null;
|
||||
async function poll() {
|
||||
clearTimeout(pollTimer);
|
||||
if (AUTH.mode === "key" ? !KEY : !AUTH.user) { showAuth(); return; }
|
||||
if (AUTH.mode === "key" ? !(KEY && AUTH.key !== false) && !AUTH.user : !AUTH.user) { showAuth(); return; }
|
||||
try { render(await api("/api/status")); }
|
||||
catch (e) {
|
||||
if (e.message === "unauthorized" && AUTH.mode === "oidc") return; // session over; wait for sign-in
|
||||
if (e.message === "unauthorized" && !(AUTH.mode === "key" && KEY)) return; // session over; wait for sign-in
|
||||
if (e.message !== "unauthorized") { $("phase").textContent = "Dashboard unreachable"; $("phase").className = "pill failed"; }
|
||||
}
|
||||
pollTimer = setTimeout(poll, 2000);
|
||||
|
|
@ -843,10 +896,8 @@ async function poll() {
|
|||
const r = await fetch("/auth/config", { credentials: "same-origin" });
|
||||
if (r.ok) AUTH = await r.json();
|
||||
} catch (e) {}
|
||||
if (AUTH.mode === "oidc") {
|
||||
$("logout").title = "Sign out of this dashboard";
|
||||
if (AUTH.user) { $("whoami").textContent = AUTH.name || AUTH.user; $("whoami").hidden = false; }
|
||||
}
|
||||
if (AUTH.mode === "oidc" || AUTH.otp) $("logout").title = "Sign out of this dashboard";
|
||||
if (AUTH.user) signedIn(AUTH.user, AUTH.name);
|
||||
poll();
|
||||
})();
|
||||
</script>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue