Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /ram07eng_airflow-watcher /src /airflow_watcher /api /auth.py
| """Bearer token authentication dependency for the Airflow Watcher API.""" | |
| import logging | |
| import secrets | |
| from typing import Optional | |
| from fastapi import Depends, HTTPException, Request | |
| from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer | |
| from airflow_watcher.api.envelope import error_response | |
| logger = logging.getLogger(__name__) | |
| # Module-level reference set during app startup. | |
| _api_keys: list[str] = [] | |
| # Security scheme — shows the Authorize (lock) button in Swagger UI. | |
| _bearer_scheme = HTTPBearer(auto_error=False) | |
| def configure_auth(api_keys: list[str]) -> None: | |
| """Set the accepted API keys. Called once at startup.""" | |
| global _api_keys | |
| _api_keys = list(api_keys) | |
| if not _api_keys: | |
| logger.warning("API_KEYS not configured — authentication is DISABLED. Set AIRFLOW_WATCHER_API_KEYS to enable.") | |
| async def require_auth( | |
| request: Request, | |
| credentials: Optional[HTTPAuthorizationCredentials] = Depends(_bearer_scheme), | |
| ) -> Optional[str]: | |
| """FastAPI dependency that validates the ``Authorization: Bearer <token>`` header. | |
| Uses constant-time comparison to prevent timing side-channel attacks. | |
| When ``_api_keys`` is empty authentication is disabled and all requests | |
| pass through. | |
| Returns: | |
| The validated API key string, or ``None`` when auth is disabled. | |
| Raises: | |
| HTTPException: 401 when the token is missing or invalid. | |
| """ | |
| if not _api_keys: | |
| return None # Auth disabled | |
| if credentials is None: | |
| raise HTTPException(status_code=401, detail=error_response("Authentication required")) | |
| token = credentials.credentials.strip() | |
| # Constant-time comparison prevents timing attacks. | |
| matched_key = None | |
| for key in _api_keys: | |
| if secrets.compare_digest(token, key): | |
| matched_key = key | |
| break | |
| if matched_key is None: | |
| raise HTTPException(status_code=401, detail=error_response("Authentication required")) | |
| return matched_key | |
Xet Storage Details
- Size:
- 2.01 kB
- Xet hash:
- aedb4086c8d5d88fc9d26c9b771475343236542a55d46ecabf3d6ffa8c9caa9a
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.