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
|
|
@ -28,9 +28,10 @@ import urllib.error
|
|||
import urllib.parse
|
||||
import urllib.request
|
||||
|
||||
SESSION_COOKIE = "llmhost_session"
|
||||
from .sessions import SESSION_COOKIE, SESSION_TTL, Sessions, parse_cookies # noqa: F401 (re-exported)
|
||||
from .sessions import cookie as make_cookie
|
||||
|
||||
STATE_COOKIE = "llmhost_oidc_state"
|
||||
SESSION_TTL = 12 * 3600
|
||||
LOGIN_TTL = 600 # how long a started sign-in may take to come back
|
||||
MAX_PENDING = 256 # unfinished sign-ins kept at once
|
||||
CLOCK_SKEW = 120
|
||||
|
|
@ -53,15 +54,6 @@ def jwt_claims(token: str) -> dict:
|
|||
raise AuthError("the provider sent an unreadable ID token")
|
||||
|
||||
|
||||
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 origin_of(url: str) -> str:
|
||||
u = urllib.parse.urlsplit(url)
|
||||
return f"{u.scheme}://{u.netloc}".lower()
|
||||
|
|
@ -83,7 +75,8 @@ class OIDC:
|
|||
self.http = http or self._http
|
||||
self.lock = threading.Lock()
|
||||
self.pending = {} # state -> {nonce, verifier, t}
|
||||
self.sessions = {} # sha256(session id) -> {user, name, exp}
|
||||
self.store = Sessions()
|
||||
self.sessions = self.store.items # sha256(session id) -> {user, name, exp}
|
||||
self._disco = None
|
||||
|
||||
# -- provider ---------------------------------------------------------------------
|
||||
|
|
@ -191,36 +184,16 @@ class OIDC:
|
|||
if user not in self.allowed_users and not (groups & self.allowed_groups):
|
||||
raise AuthError(f"signed in as {user}, but that account isn't allowed on this dashboard")
|
||||
|
||||
sid = secrets.token_urlsafe(32)
|
||||
with self.lock:
|
||||
for h in [h for h, s in self.sessions.items() if s["exp"] < now]:
|
||||
del self.sessions[h]
|
||||
self.sessions[self._h(sid)] = {"user": user, "name": info.get("name") or user,
|
||||
"exp": now + SESSION_TTL}
|
||||
sid = self.store.create(user, info.get("name") or user)
|
||||
return {"id": sid, "user": user, "name": info.get("name") or user}
|
||||
|
||||
# -- sessions ---------------------------------------------------------------------
|
||||
|
||||
@staticmethod
|
||||
def _h(sid: str) -> str:
|
||||
return hashlib.sha256(sid.encode()).hexdigest()
|
||||
|
||||
def session(self, cookie_header: str):
|
||||
sid = parse_cookies(cookie_header).get(SESSION_COOKIE)
|
||||
if not sid:
|
||||
return None
|
||||
with self.lock:
|
||||
s = self.sessions.get(self._h(sid))
|
||||
if s and s["exp"] < time.time():
|
||||
del self.sessions[self._h(sid)]
|
||||
s = None
|
||||
return s
|
||||
return self.store.get(cookie_header)
|
||||
|
||||
def end(self, cookie_header: str):
|
||||
sid = parse_cookies(cookie_header).get(SESSION_COOKIE)
|
||||
if sid:
|
||||
with self.lock:
|
||||
self.sessions.pop(self._h(sid), None)
|
||||
self.store.end(cookie_header)
|
||||
|
||||
def same_origin(self, headers) -> bool:
|
||||
"""For state-changing requests: the browser's Origin (or Referer) must be the
|
||||
|
|
@ -230,7 +203,4 @@ class OIDC:
|
|||
return o.lower() == self.origin
|
||||
|
||||
def cookie(self, name, value, max_age, path="/"):
|
||||
attrs = [f"{name}={value}", f"Path={path}", f"Max-Age={max_age}", "HttpOnly", "SameSite=Lax"]
|
||||
if self.secure:
|
||||
attrs.append("Secure")
|
||||
return "; ".join(attrs)
|
||||
return make_cookie(name, value, max_age, self.secure, path)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue