Download src/core/auth.py from XDOLI2008/FLOW2API: direct link, hf CLI and curl.
- Browser
- Download file 1.39 kB
-
https://huggingface.co/spaces/XDOLI2008/FLOW2API/resolve/main/src/core/auth.py
- Command line
-
hf download hf://spaces/XDOLI2008/FLOW2API/src/core/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/XDOLI2008/FLOW2API/resolve/main/src/core/auth.py
1.39 kB
| """Authentication module""" | |
| import bcrypt | |
| from typing import Optional | |
| from fastapi import HTTPException, Security | |
| from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials | |
| from .config import config | |
| security = HTTPBearer() | |
| class AuthManager: | |
| """Authentication manager""" | |
| def verify_api_key(api_key: str) -> bool: | |
| """Verify API key""" | |
| return api_key == config.api_key | |
| def verify_admin(username: str, password: str) -> bool: | |
| """Verify admin credentials""" | |
| # Compare with current config (which may be from database or config file) | |
| return username == config.admin_username and password == config.admin_password | |
| def hash_password(password: str) -> str: | |
| """Hash password""" | |
| return bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode() | |
| def verify_password(password: str, hashed: str) -> bool: | |
| """Verify password""" | |
| return bcrypt.checkpw(password.encode(), hashed.encode()) | |
| async def verify_api_key_header(credentials: HTTPAuthorizationCredentials = Security(security)) -> str: | |
| """Verify API key from Authorization header""" | |
| api_key = credentials.credentials | |
| if not AuthManager.verify_api_key(api_key): | |
| raise HTTPException(status_code=401, detail="Invalid API key") | |
| return api_key | |