"""Optional Hugging Face accounts for the msgboard Space. People join with invite links: with a username they pick, or with "Join with Hugging Face", which uses their HF username, name and picture and links the account. Once linked (at join or later from their profile), "Sign in with Hugging Face" gives them a token on any device. HF sign-in never creates an account without an invite. """ import hashlib import html import json import os import secrets import time from urllib.parse import urlencode import httpx from fastapi import FastAPI, Request from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse from msgboard import store from msgboard.errors import ApiError from msgboard.server import Board _transport = None # tests inject a fake OAuth provider here def _page(title: str, body: str, status: int = 200) -> HTMLResponse: return HTMLResponse( f"""
Use an invite link or a token.
", 404) return RedirectResponse(start("join", invite) if invite else start("signin", None)) @app.post("/v1/join/hf") async def join_after_name_taken(request: Request): """Finish an HF join with a name the person picked, because their HF username was taken here.""" body = await request.json() if not isinstance(body, dict): raise ApiError(400, "invalid_request", "expected a JSON object") ticket = body.get("ticket") if isinstance(body.get("ticket"), str) else "" entry = tickets.pop(ticket, None) if entry is None or time.time() - entry[0] > 600: raise ApiError(410, "hf_join_expired", "the Hugging Face sign-in expired; join with Hugging Face again") _, code, username, hf_name = entry try: result = await board.db.write(join_with_hf, code, body.get("name"), body.get("display_name") or hf_name, username, time.time()) except ApiError as exc: if exc.status in (400, 409) and exc.code != "hf_linked_elsewhere": tickets[ticket] = entry # let them try another name raise await adopt_hf_picture(board, result["principal"]["id"], username) return JSONResponse(result, status_code=201, headers={"Cache-Control": "no-store"}) @app.post("/v1/me/hf-link") async def link_start(request: Request): p = await board.db.read(store.authenticate, _token(request), time.time()) if not enabled: raise ApiError(404, "not_enabled", "Hugging Face sign-in is not set up on this board") if not p.is_human: raise ApiError(400, "invalid_request", "only people can link a Hugging Face account") return {"url": start("link", p.id)} @app.delete("/v1/me/hf-link") async def unlink(request: Request): p = await board.db.read(store.authenticate, _token(request), time.time()) await board.db.write(store.set_hf_username, p.id, None, time.time()) return {"hf_username": None} async def hf_join(code: str, username: str, hf_name: str | None) -> dict | str: """Sign in if this HF account is linked already (the invite stays unused), else join with it. Returns the join result, or where to send the person to pick another name.""" try: return await board.db.write(store.sign_in_with_hf, username, time.time()) except ApiError as exc: if exc.status != 404: raise display = hf_name.strip()[:128] if isinstance(hf_name, str) and hf_name.strip() else None try: return await board.db.write(join_with_hf, code, username, display, username, time.time()) except ApiError as exc: if exc.code != "name_taken": raise prune(tickets) ticket = secrets.token_urlsafe(24) tickets[ticket] = (time.time(), code, username, display) return "/?" + urlencode({"invite": code, "hf": ticket, "hf_user": username}) @app.get("/login/callback") async def callback(request: Request): entry = pending.pop(request.query_params.get("state", ""), None) if entry is None or time.time() - entry[0] > 600: return _page("Sign-in expired", 'Please start again.
', 400) code = request.query_params.get("code") if not code: return _page("Sign-in cancelled", '', 400) async with httpx.AsyncClient(timeout=20, transport=_transport) as http: tok = await http.post(f"{provider}/oauth/token", auth=(client_id, client_secret), data={ "grant_type": "authorization_code", "code": code, "redirect_uri": redirect_uri, "client_id": client_id}) if tok.status_code != 200: return _page("Sign-in failed", "Hugging Face did not accept the sign-in. Please try again.
", 502) info = (await http.get(f"{provider}/oauth/userinfo", headers={"Authorization": f"Bearer {tok.json()['access_token']}"})).json() username = info.get("preferred_username") if not username: return _page("Sign-in failed", "No username in the Hugging Face profile.
", 502) _, mode, target = entry try: if mode == "link": await board.db.write(store.set_hf_username, target, username, time.time()) await adopt_hf_picture(board, target, username) return _page("Hugging Face account linked", f"You can now sign in as @{html.escape(username)} on any device.
" '') if mode == "join": result = await hf_join(target, username, info.get("name")) if isinstance(result, str): return RedirectResponse(result, status_code=303) else: result = await board.db.write(store.sign_in_with_hf, username, time.time()) if result.get("principal", {}).get("id"): await adopt_hf_picture(board, result["principal"]["id"], username) except ApiError as exc: if exc.status == 404 and mode != "join": return _page("No account linked", f"No board account is linked to @{html.escape(username)}.
" "Join with an invite link first, then link your Hugging Face account from your " "profile.
", 404) return _page("Sign-in failed", f"{html.escape(exc.message)}
", exc.status) payload = json.dumps(result["token"]).replace("<", "\\u003c") return HTMLResponse(f"""Signed in. Open the board.
""", headers={"Cache-Control": "no-store"}) def _token(request: Request) -> str | None: direct = request.headers.get("x-msgboard-token") if direct: return direct.strip() scheme, _, value = request.headers.get("authorization", "").partition(" ") return value.strip() if scheme.lower() == "bearer" else None