lecturelens-api / app /auth.py
aliSaac510's picture
feat: initial LectureLens API v1.0 β€” full audio/video quality analysis service
75ba57a
Raw
History Blame Contribute Delete
964 Bytes
"""
LectureLens β€” API Key Authentication
Provides a FastAPI Dependency that validates the X-API-Key header.
"""
from fastapi import Depends, Header, HTTPException, status
from fastapi.security import APIKeyHeader
from app.config import Settings, get_settings
_API_KEY_SCHEME = APIKeyHeader(name="X-API-Key", auto_error=False)
async def verify_api_key(
api_key: str | None = Depends(_API_KEY_SCHEME),
settings: Settings = Depends(get_settings),
) -> None:
"""
FastAPI dependency β€” raise HTTP 401 if the X-API-Key header is
missing or does not match the configured secret.
"""
if not api_key or api_key != settings.lecturelens_api_key:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail={
"error_code": "UNAUTHORIZED",
"detail": "Missing or invalid X-API-Key header.",
},
headers={"WWW-Authenticate": "ApiKey"},
)