File size: 964 Bytes
75ba57a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""
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"},
        )