Spaces:
Running
Running
Download app/auth.py from Faysal1000/embedding-server: direct link, hf CLI and curl.
- Browser
- Download file 834 Bytes
-
https://huggingface.co/spaces/Faysal1000/embedding-server/resolve/main/app/auth.py
- Command line
-
hf download hf://spaces/Faysal1000/embedding-server/app/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/Faysal1000/embedding-server/resolve/main/app/auth.py
834 Bytes
| # app/auth.py | |
| """ | |
| Authorization dependency. | |
| """ | |
| from fastapi import Request, HTTPException, status, Header | |
| from .config import settings | |
| from .logger import logger | |
| async def api_key_guard( | |
| request: Request, | |
| x_api_key: str = Header(..., alias="x-api-key", description="API Key for authentication"), | |
| ) -> None: | |
| # Check API key | |
| # FastAPI's Header(...) ensures the key is present. behavior if missing is 422. | |
| # We still check the value. | |
| if x_api_key != settings.EMBED_API_KEY: | |
| logger.warning(f"Unauthorized: Invalid API key. Provided: {x_api_key[:4] if x_api_key else 'None'}...") | |
| raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Invalid API key") | |
| logger.debug(f"Auth passed for request from {request.client.host if request.client else 'unknown'}") | |