""" Server-side authentication & authorization for admin/protected routes. The single source of truth for who may call /api/admin/* lives here, not in the frontend. localStorage and client-side email filters are advisory only; these dependencies are the actual gate. Configuration (set as Hugging Face Space secrets): FIREBASE_SERVICE_ACCOUNT -> full JSON contents of the Firebase service account private key (NOT a VITE_ value) ADMIN_EMAILS -> comma-separated admin emails SHEET_REFRESH_TOKEN -> (optional) static token the Apps Script onEdit trigger uses to call refresh-sheet """ import os import json import logging import firebase_admin from firebase_admin import credentials, auth as fb_auth from fastapi import Depends, HTTPException, status, Header from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials from typing import Optional import db _log = logging.getLogger("auth") # --------------------------------------------------------------------------- # Firebase Admin init — verifies ID tokens against Google's signing keys. # --------------------------------------------------------------------------- _FIREBASE_READY = False if not firebase_admin._apps: _sa_raw = os.environ.get("FIREBASE_SERVICE_ACCOUNT") if _sa_raw: try: firebase_admin.initialize_app( credentials.Certificate(json.loads(_sa_raw)) ) _FIREBASE_READY = True except Exception as e: # malformed secret — fail closed, log loudly _log.error("[auth] FIREBASE_SERVICE_ACCOUNT invalid: %s", e) else: _log.error( "[auth] FIREBASE_SERVICE_ACCOUNT not set — admin routes will 503" ) else: _FIREBASE_READY = True ADMIN_EMAILS = { e.strip().lower() for e in os.environ.get("ADMIN_EMAILS", "").split(",") if e.strip() } # Legacy fixed allowlist. Kept as a fallback ONLY for when the Aurora data # layer is unreachable (the cluster auto-pauses after idle and can take 30s+ # to resume) — so a transient DB hiccup never locks out existing ALU users. # The real, multi-tenant source of truth is the `organizations` table # (see `_domain_is_known_org` below); this tuple should never grow. ALLOWED_DOMAINS = ("@alustudent.com", "@alueducation.com") def _domain_is_known_org(email: str) -> bool: """ True if `email`'s domain belongs to an active organization. Looks up the `organizations` table first (the multi-tenant source of truth). If the data layer is unavailable, falls back to the legacy fixed ALLOWED_DOMAINS tuple so existing ALU users are never locked out by a database hiccup — this fallback covers exactly the domains that worked before multi-tenancy existed, nothing more. """ email = (email or "").lower() if "@" not in email: return False domain = email.split("@", 1)[1] org = db.get_org_by_domain(domain) if org is not None: return True if not db.db_enabled(): return email.endswith(ALLOWED_DOMAINS) return False # auto_error=False so we can return clean 503/401 instead of FastAPI's default bearer = HTTPBearer(auto_error=False) def _verify(cred: Optional[HTTPAuthorizationCredentials]) -> dict: """Verify a Firebase ID token and return its decoded claims.""" if not _FIREBASE_READY: raise HTTPException( status.HTTP_503_SERVICE_UNAVAILABLE, "Auth not configured on the server", ) if cred is None or not cred.credentials: raise HTTPException( status.HTTP_401_UNAUTHORIZED, "Missing bearer token" ) try: return fb_auth.verify_id_token(cred.credentials) except Exception: raise HTTPException( status.HTTP_401_UNAUTHORIZED, "Invalid or expired token" ) async def require_user( cred: Optional[HTTPAuthorizationCredentials] = Depends(bearer), ) -> dict: """ Any authenticated user from a known, active organization. Enforces the signup allowlist server-side: the caller's email domain must match an active row in `organizations` (see `_domain_is_known_org`), not a fixed set of ALU domains — this is what lets a second university's students sign in without a code change once their org row exists. """ user = _verify(cred) email = (user.get("email") or "").lower() if not _domain_is_known_org(email): raise HTTPException(status.HTTP_403_FORBIDDEN, "Email domain not recognized") return user async def require_admin(user: dict = Depends(require_user)) -> dict: """An ALU user whose email is in ADMIN_EMAILS.""" if (user.get("email") or "").lower() not in ADMIN_EMAILS: raise HTTPException( status.HTTP_403_FORBIDDEN, "Admin access required" ) return user # --------------------------------------------------------------------------- # Platform-operator gate — deliberately separate from ADMIN_EMAILS. # # ADMIN_EMAILS is scoped to running ONE organization's admin dashboard (an # ALU staff member managing ALU's opportunities/students/curators). Creating # or deactivating an organization is a different, higher-privilege action — # it's the Student Companion team onboarding a new university, not any one # university managing itself. Without this separation, any existing ALU # admin could create arbitrary new "universities." # --------------------------------------------------------------------------- PLATFORM_ADMIN_EMAILS = { e.strip().lower() for e in os.environ.get("PLATFORM_ADMIN_EMAILS", "").split(",") if e.strip() } async def require_platform_admin( cred: Optional[HTTPAuthorizationCredentials] = Depends(bearer), ) -> dict: """ A verified user whose email is in PLATFORM_ADMIN_EMAILS. Deliberately does NOT go through require_user / _domain_is_known_org — a platform operator's own email domain need not belong to any onboarded university at all, and requiring that would make it impossible to add the very first organization from a fresh deployment. """ user = _verify(cred) email = (user.get("email") or "").lower() if not PLATFORM_ADMIN_EMAILS: raise HTTPException( status.HTTP_503_SERVICE_UNAVAILABLE, "PLATFORM_ADMIN_EMAILS not configured on the server", ) if email not in PLATFORM_ADMIN_EMAILS: raise HTTPException( status.HTTP_403_FORBIDDEN, "Platform admin access required" ) return user # --------------------------------------------------------------------------- # Dual gate for routes that must also be callable by non-interactive automation # (the Google Sheet onEdit trigger, which cannot perform a Firebase login). # Accepts EITHER a valid Firebase admin token OR the static refresh token. # --------------------------------------------------------------------------- _SHEET_REFRESH_TOKEN = os.environ.get("SHEET_REFRESH_TOKEN", "") async def require_admin_or_token( cred: Optional[HTTPAuthorizationCredentials] = Depends(bearer), x_refresh_token: Optional[str] = Header(default=None, alias="X-Refresh-Token"), ) -> dict: # Path 1: the Apps Script automation presents the static token. if _SHEET_REFRESH_TOKEN and (x_refresh_token or "").strip() == _SHEET_REFRESH_TOKEN: return {"email": "apps-script@automation", "via": "refresh_token"} # Path 2: a logged-in human admin presents a Firebase token. if cred is not None and cred.credentials: user = _verify(cred) email = (user.get("email") or "").lower() if _domain_is_known_org(email) and email in ADMIN_EMAILS: return user raise HTTPException(status.HTTP_403_FORBIDDEN, "Admin access required") raise HTTPException( status.HTTP_401_UNAUTHORIZED, "Provide a valid admin token or X-Refresh-Token", )