Spaces:
Sleeping
Sleeping
Download api/auth.py from quangdat05/AIChamcong: direct link, hf CLI and curl.
- Browser
- Download file 655 Bytes
-
https://huggingface.co/spaces/quangdat05/AIChamcong/resolve/main/api/auth.py
- Command line
-
hf download hf://spaces/quangdat05/AIChamcong/api/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/quangdat05/AIChamcong/resolve/main/api/auth.py
655 Bytes
| """ | |
| Simple API Key authentication for sensitive endpoints. | |
| """ | |
| import os | |
| from fastapi import Header, HTTPException | |
| API_SECRET_KEY = os.getenv("API_SECRET_KEY", "") | |
| async def verify_api_key(x_api_key: str = Header(default="")): | |
| """ | |
| Kiểm tra API key từ header 'X-API-Key'. | |
| Nếu API_SECRET_KEY chưa được cấu hình trong .env thì bỏ qua (dev mode). | |
| """ | |
| if not API_SECRET_KEY: | |
| # Dev mode: không yêu cầu API key | |
| return | |
| if x_api_key != API_SECRET_KEY: | |
| raise HTTPException( | |
| status_code=401, | |
| detail="Invalid or missing API key. Provide 'X-API-Key' header." | |
| ) | |