Spaces:
Running
Running
Download core/auth.py from iamfebin/algospaced-dsa: direct link, hf CLI and curl.
- Browser
- Download file 1.37 kB
-
https://huggingface.co/spaces/iamfebin/algospaced-dsa/resolve/main/core/auth.py
- Command line
-
hf download hf://spaces/iamfebin/algospaced-dsa/core/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/iamfebin/algospaced-dsa/resolve/main/core/auth.py
1.37 kB
| from typing import Any | |
| from fastapi import Header, HTTPException | |
| from firebase_admin import auth as firebase_auth | |
| import sandbox_db | |
| type Client = Any | |
| class UserSession: | |
| def __init__(self, uid: str, email: str = ""): | |
| self.current_user_id = uid | |
| self.uid = uid | |
| self.email = email | |
| def get_auth_session(authorization: str = Header(None)) -> Any: | |
| if not authorization or not authorization.startswith("Bearer "): | |
| raise HTTPException(status_code=401, detail="Missing or invalid Authorization header") | |
| access_token = authorization.split(" ")[1] | |
| if access_token == "sandbox": | |
| return sandbox_db.get_sandbox_client() | |
| try: | |
| decoded_token = firebase_auth.verify_id_token(access_token) | |
| uid = decoded_token.get("uid") | |
| if not uid: | |
| raise HTTPException(status_code=401, detail="Invalid Firebase authentication session") | |
| if not decoded_token.get("email_verified", False): | |
| raise HTTPException( | |
| status_code=403, | |
| detail="Email address is not verified. Please verify your email to access this resource.", | |
| ) | |
| return UserSession(uid, decoded_token.get("email", "")) | |
| except HTTPException: | |
| raise | |
| except Exception as e: | |
| raise HTTPException(status_code=401, detail=f"Authentication failed: {str(e)}") | |