Spaces:
Running
Running
Claude Opus 5.5 (1M context)
Stop paying twice, measure every run, and make the picture, voice and props hold up
13354e2 Download app/api/auth.py from userIdc2024/creative-brain-os: direct link, hf CLI and curl.
- Browser
- Download file 3.07 kB
-
https://huggingface.co/spaces/userIdc2024/creative-brain-os/resolve/main/app/api/auth.py
- Command line
-
hf download hf://spaces/userIdc2024/creative-brain-os/app/api/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/userIdc2024/creative-brain-os/resolve/main/app/api/auth.py
3.07 kB
| import httpx | |
| import structlog | |
| from fastapi import APIRouter, HTTPException, status | |
| from pydantic import BaseModel | |
| from app.core.config import settings | |
| from app.core.security import create_access_token, verify_credentials | |
| from app.schemas import LoginRequest, TokenResponse | |
| router = APIRouter() | |
| logger = structlog.get_logger() | |
| _BAD_LOGIN = "Invalid username or password" | |
| class SessionResponse(TokenResponse): | |
| # Only set in supabase mode; excluded when None, so legacy responses are unchanged. | |
| refresh_token: str | None = None | |
| expires_in: int | None = None | |
| class RefreshRequest(BaseModel): | |
| refresh_token: str | |
| def _gotrue_token(grant_type: str, body: dict, failure: str) -> SessionResponse: | |
| """POST {SUPABASE_URL}/auth/v1/token?grant_type=... with the anon key. Any 400/401 | |
| from GoTrue becomes one generic 401, so it never reveals whether an email exists.""" | |
| if not settings.supabase_anon_key: | |
| logger.error("supabase_auth_misconfigured", reason="SUPABASE_ANON_KEY is not set") | |
| raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Sign-in is unavailable") | |
| try: | |
| res = httpx.post( | |
| f"{settings.supabase_url.rstrip('/')}/auth/v1/token", | |
| params={"grant_type": grant_type}, | |
| headers={"apikey": settings.supabase_anon_key}, | |
| json=body, | |
| timeout=10.0, | |
| ) | |
| except httpx.HTTPError as exc: | |
| logger.error("supabase_auth_unreachable", error=str(exc)) | |
| raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Sign-in is unavailable") | |
| if res.status_code in (400, 401, 403, 422): | |
| raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail=failure) | |
| if res.status_code != 200: | |
| logger.error("supabase_auth_failed", status=res.status_code) | |
| raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="Sign-in is unavailable") | |
| data = res.json() | |
| return SessionResponse( | |
| access_token=data["access_token"], | |
| refresh_token=data.get("refresh_token"), | |
| expires_in=data.get("expires_in"), | |
| ) | |
| def login(payload: LoginRequest) -> SessionResponse: | |
| if settings.auth_mode == "supabase": | |
| return _gotrue_token("password", {"email": payload.username, "password": payload.password}, _BAD_LOGIN) | |
| if not verify_credentials(payload.username, payload.password): | |
| raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail=_BAD_LOGIN) | |
| return SessionResponse(access_token=create_access_token(payload.username)) | |
| def refresh(payload: RefreshRequest) -> SessionResponse: | |
| if settings.auth_mode != "supabase": | |
| raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Not Found") | |
| return _gotrue_token("refresh_token", {"refresh_token": payload.refresh_token}, "Session expired") | |