diff --git a/app/main.py b/app/main.py index f71e034..42ed92c 100644 --- a/app/main.py +++ b/app/main.py @@ -109,6 +109,26 @@ async def broker_delete(identity: str, path: str) -> dict: return r.json() if r.content else {} +async def broker_call(identity: str, method: str, path: str, payload: dict | None = None) -> tuple[int, dict]: + """Portal call that returns the portal's OWN status + payload. + + The guest path's 429 refusals are member-facing text ("one guest per + member per month") — transport-error wrapping (502) would bury them, so + the guest routes surface the portal's status and detail verbatim. + """ + async with httpx.AsyncClient(timeout=20.0) as client: + r = await client.request(method, f"{PORTAL_BASE}{path}", + headers=broker_headers(identity), json=payload) + try: + data = r.json() + except Exception: + data = {"error": r.text or "portal returned no body"} + if r.status_code == 401: + # Broker credentials broken — an app fault, not a member condition. + data = {"error": "The portal rejected this dashboard's credentials"} + return r.status_code, data + + # --------------------------------------------------------------------------- # Routes # --------------------------------------------------------------------------- @@ -254,6 +274,48 @@ async def api_cancel_guest(request: Request): return JSONResponse({"error": e.detail}, status_code=e.status_code) +@app.get("/api/guests") +async def api_guests(request: Request): + """The caller's guest-access view: invitation allowance + their trail. + + One hop to the portal broker's GET /broker/guests: whether this member + can send a guest invitation now (the rolling one-per-30-days rule, + counting pending and approved requests) and every invitation they have + made, with its status. The portal's refusal statuses pass through as-is. + """ + identity = get_user_identity(request) + if not identity: + return JSONResponse({"error": "unauthenticated"}, status_code=401) + status, data = await broker_call(identity, "GET", "/broker/guests") + return JSONResponse(data, status_code=status) + + +@app.post("/api/guests") +async def api_guest_invite(request: Request): + """Invite a guest, from the member dashboard (self-serve guest access). + + Body: {"email": "", "name": ""}. One + hop to the portal broker, which holds every protection (active-member + inviter, one invitation per member per 30 days, recipient never an + existing member / prior guest / pending request / manual account) and + the trial gate: with approval on, the request waits for the co-op's + approve/reject and nothing goes to the recipient until then. The + portal's refusals are member-facing text and pass through verbatim. + """ + identity = get_user_identity(request) + if not identity: + return JSONResponse({"error": "unauthenticated"}, status_code=401) + body = await request.json() + payload = { + "email": (body.get("email") or "").strip().lower(), + "name": (body.get("name") or "").strip(), + } + if not payload["email"] or "@" not in payload["email"]: + return JSONResponse({"error": "Enter their email address"}, status_code=400) + status, data = await broker_call(identity, "POST", "/broker/guests", payload) + return JSONResponse(data, status_code=status) + + # --------------------------------------------------------------------------- # OIDC routes — authorization-code flow against Cloudron's OIDC provider. # Active only when the `oidc` addon is configured (CLOUDRON_OIDC_* present). @@ -565,6 +627,24 @@ PAGE_HTML = """ .flash.show { display: block; } .flash.ok { background: #eaf3ea; color: #2f5c30; } .flash.err { background: #f6e9e5; color: var(--danger); } + + /* ---------- Guest access (members inviting a friend) ---------- */ + .guest-hint { color: var(--muted); font-size: 12px; margin-bottom: 12px; } + .new-key .name-input { max-width: 200px; } + .guest-trail { margin-top: 6px; } + .guest-row { + display: flex; align-items: baseline; justify-content: space-between; gap: 12px; + padding: 10px 0; border-bottom: 1px solid #f0ede6; + } + .guest-row:last-child { border-bottom: none; } + .guest-who { font-weight: 600; font-size: 13px; } + .guest-meta { color: var(--muted); font-size: 12px; margin-top: 2px; } + .gpill { display: inline-block; padding: 2px 9px; border-radius: 10px; font-size: 11px; font-weight: 600; white-space: nowrap; } + .gpill.pending { background: #fdf3e3; color: #a97a2a; } + .gpill.ok { background: #eaf3ea; color: #2f5c30; } + .gpill.done { background: #f1e8da; color: #a97a4a; } + .gpill.bad { background: #f6e9e5; color: var(--danger); } + footer { border-top: 1px solid var(--border); padding: 20px 24px; margin-top: 32px; @@ -759,6 +839,18 @@ PAGE_HTML = """
+ +