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
65
llmhost/web/sessions.py
Normal file
65
llmhost/web/sessions.py
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
"""Signed-in dashboard sessions, for OIDC and authenticator-code sign-in. Stdlib only.
|
||||
|
||||
A session is a random id in an HttpOnly, SameSite=Lax cookie; only its SHA-256 is kept,
|
||||
in memory, so a dashboard restart signs everyone out.
|
||||
"""
|
||||
import hashlib
|
||||
import secrets
|
||||
import threading
|
||||
import time
|
||||
|
||||
SESSION_COOKIE = "llmhost_session"
|
||||
SESSION_TTL = 12 * 3600
|
||||
|
||||
|
||||
def parse_cookies(header: str) -> dict:
|
||||
out = {}
|
||||
for part in (header or "").split(";"):
|
||||
k, sep, v = part.strip().partition("=")
|
||||
if sep:
|
||||
out.setdefault(k, v)
|
||||
return out
|
||||
|
||||
|
||||
def cookie(name, value, max_age, secure, path="/"):
|
||||
attrs = [f"{name}={value}", f"Path={path}", f"Max-Age={max_age}", "HttpOnly", "SameSite=Lax"]
|
||||
if secure:
|
||||
attrs.append("Secure")
|
||||
return "; ".join(attrs)
|
||||
|
||||
|
||||
class Sessions:
|
||||
def __init__(self, ttl=SESSION_TTL):
|
||||
self.ttl = ttl
|
||||
self.lock = threading.Lock()
|
||||
self.items = {} # sha256(id) -> {user, name, exp}
|
||||
|
||||
@staticmethod
|
||||
def _h(sid: str) -> str:
|
||||
return hashlib.sha256(sid.encode()).hexdigest()
|
||||
|
||||
def create(self, user, name=None) -> str:
|
||||
sid = secrets.token_urlsafe(32)
|
||||
now = time.time()
|
||||
with self.lock:
|
||||
for h in [h for h, s in self.items.items() if s["exp"] < now]:
|
||||
del self.items[h]
|
||||
self.items[self._h(sid)] = {"user": user, "name": name or user, "exp": now + self.ttl}
|
||||
return sid
|
||||
|
||||
def get(self, cookie_header: str):
|
||||
sid = parse_cookies(cookie_header).get(SESSION_COOKIE)
|
||||
if not sid:
|
||||
return None
|
||||
with self.lock:
|
||||
s = self.items.get(self._h(sid))
|
||||
if s and s["exp"] < time.time():
|
||||
del self.items[self._h(sid)]
|
||||
s = None
|
||||
return s
|
||||
|
||||
def end(self, cookie_header: str):
|
||||
sid = parse_cookies(cookie_header).get(SESSION_COOKIE)
|
||||
if sid:
|
||||
with self.lock:
|
||||
self.items.pop(self._h(sid), None)
|
||||
Loading…
Add table
Add a link
Reference in a new issue