diff --git a/app/main.py b/app/main.py index 071dd90..09317a9 100644 --- a/app/main.py +++ b/app/main.py @@ -1,19 +1,22 @@ """Admin Panel — co-op-wide member overview, spend, and balance management. -Security model: -- Authentication is done by Cloudron's proxyAuth wall (SSO). Cloudron injects - the authenticated user's username via X-Remote-User. -- Access is further restricted to a small ADMIN_USERNAMES allowlist (checked - against the injected username), so only designated admins see this panel. -- This app calls the member portal's /admin/overview and /admin/set-balance - endpoints, authenticated with the portal's dedicated ADMIN_TOKEN (the - X-Admin-Token header — NOT the webhook's URL token, which cannot unlock - /admin/*). +Security model — TWO roles, split by the app itself: +- Authentication is Cloudron OIDC. Cloudron only completes the login for users + the app's access restriction admits (the `managers` and `stewards` groups), + so "authenticated here" already means "the operator let this account in". +- READ: any authenticated identity may view the panel (overview, spend, dues, + guests, money series). The page says so and hides every write control. +- WRITE: only the ADMIN_USERNAMES allowlist (Cloudron usernames, comma- + separated) may change balances/credits or provision guests. Every POST route + re-checks this server-side — hiding a button is not access control. +- This app calls the member portal's /admin/* endpoints with the portal's + dedicated ADMIN_TOKEN (the X-Admin-Token header — NOT the webhook's URL + token, which cannot unlock /admin/*). Environment (Cloudron env vars): PORTAL_BASE — base URL of the member portal ADMIN_TOKEN — the portal's admin/webhook token (for /admin/* endpoints) - ADMIN_USERNAMES — comma-separated list of allowed Cloudron usernames + ADMIN_USERNAMES — comma-separated list of Cloudron usernames with WRITE access """ import os @@ -63,12 +66,18 @@ def get_identity(request: Request) -> str: def is_admin(request: Request) -> bool: + """WRITE access: the identity must be in ADMIN_USERNAMES.""" identity = get_identity(request) if not identity: return False return identity.lower() in ADMIN_USERNAMES +def is_reader(request: Request) -> bool: + """READ access: any authenticated Cloudron identity Cloudron admitted.""" + return bool(get_identity(request)) + + async def portal_get(path: str) -> dict: async with httpx.AsyncClient(timeout=20.0) as client: r = await client.get(f"{PORTAL_BASE}{path}", headers={"X-Admin-Token": ADMIN_TOKEN}) @@ -116,28 +125,25 @@ async def index(request: Request): "
This panel is restricted to administrators.
", status_code=403, ) - # Authenticated but not an admin → forbid. - if identity.lower() not in ADMIN_USERNAMES: - return HTMLResponse( - "This panel is restricted to administrators.
", - status_code=403, - ) + # Authenticated → READ is allowed for everyone Cloudron admitted to the app; + # WRITE stays with the ADMIN_USERNAMES roster (enforced on every POST). + read_only = identity.lower() not in ADMIN_USERNAMES # no-store: the page is one inline script with no validators, so a cached # copy runs the PREVIOUS deploy's JS and makes a working app look broken # (it did, 2026-11-13). - return HTMLResponse(render_page(), headers={"Cache-Control": "no-store"}) + return HTMLResponse(render_page(read_only), headers={"Cache-Control": "no-store"}) @app.get("/api/overview") async def api_overview(request: Request): - if not is_admin(request): + if not is_reader(request): return JSONResponse({"error": "forbidden"}, status_code=403) return await portal_get("/admin/overview") @app.get("/api/model-usage") async def api_model_usage(request: Request): - if not is_admin(request): + if not is_reader(request): return JSONResponse({"error": "forbidden"}, status_code=403) try: return await portal_get("/admin/model-usage") @@ -153,7 +159,7 @@ async def api_metrics(request: Request, days: int = 730): only reads it. Fetched once at the widest range and sliced client-side, so the period toggle never refetches. """ - if not is_admin(request): + if not is_reader(request): return JSONResponse({"error": "forbidden"}, status_code=403) try: return await portal_get(f"/admin/metrics/series?days={max(1, min(int(days), 730))}") @@ -169,7 +175,7 @@ async def api_money_series(request: Request): the panel only reads it. The full history comes back in one payload — the period and spend-mode toggles slice client-side and never refetch. """ - if not is_admin(request): + if not is_reader(request): return JSONResponse({"error": "forbidden"}, status_code=403) try: return await portal_get("/admin/money-series") @@ -293,7 +299,7 @@ async def api_guest_ledger(request: Request): approval gate, removed 2026-11-08: member invitations provision immediately, so there is nothing to approve.) """ - if not is_admin(request): + if not is_reader(request): return JSONResponse({"error": "forbidden"}, status_code=403) try: return await portal_get("/admin/guest-ledger") @@ -358,8 +364,18 @@ def _esc(s: str) -> str: return s.replace("&", "&").replace("<", "<").replace(">", ">").replace('"', """) -def render_page() -> str: - return PAGE_HTML +def render_page(read_only: bool) -> str: + """Render the panel for one role. + + The role is injected into the page's JS as one assignment after + `let READ_ONLY = false;` — the placeholder stays a JS comment, so the + literal is still `node --check`-clean for tests/check-page-js.sh. Hiding + the write controls here is UX only; every POST route re-checks the roster. + """ + html = PAGE_HTML + html = html.replace("@@WRITERS@@", ", ".join(sorted(ADMIN_USERNAMES)) or "the operator") + html = html.replace("/*@@READ_ONLY@@*/", "READ_ONLY = true;" if read_only else "") + return html # The shared chart module (canonical: ops-docs scripts/shared/coop_chart.js, @@ -450,6 +466,10 @@ PAGE_HTML = """ .flash.show { display: block; } .flash.ok { background: #eaf3ea; color: #2f5c30; } .flash.err { background: #f6e9e5; color: var(--danger); } + /* Role banner: the page states which role you have at the top, where it is + unmissable, instead of leaving write controls just absent. */ + .readonly-banner { background: #eef3ee; border: 1px solid #d7e2d6; border-radius: 10px; padding: 12px 16px; font-size: 13px; color: var(--ink); margin: 0 0 4px; } + .readonly-banner strong { color: var(--green-deep); } .adjust { display: inline-flex; gap: 6px; align-items: center; white-space: nowrap; } .adjust input { width: 56px; padding: 4px 6px; border: 1px solid var(--border); border-radius: 6px; font-size: 12px; flex-shrink: 0; } .adjust button { cursor: pointer; border: none; border-radius: 6px; background: var(--green-deep); color: #fff; font-size: 11px; font-weight: 600; padding: 4px 8px; flex-shrink: 0; } @@ -483,6 +503,13 @@ PAGE_HTML = """ + +