2025-02-20 03:26:39 +08:00
|
|
|
"""
|
|
|
|
Utility functions for the LightRAG API.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from typing import Optional
|
|
|
|
from fastapi import HTTPException, Security
|
|
|
|
from fastapi.security import APIKeyHeader
|
|
|
|
from starlette.status import HTTP_403_FORBIDDEN
|
|
|
|
|
2025-02-20 04:12:21 +08:00
|
|
|
|
2025-02-20 03:26:39 +08:00
|
|
|
def get_api_key_dependency(api_key: Optional[str]):
|
|
|
|
"""
|
|
|
|
Create an API key dependency for route protection.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
api_key (Optional[str]): The API key to validate against.
|
|
|
|
If None, no authentication is required.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Callable: A dependency function that validates the API key.
|
|
|
|
"""
|
|
|
|
if not api_key:
|
|
|
|
# If no API key is configured, return a dummy dependency that always succeeds
|
|
|
|
async def no_auth():
|
|
|
|
return None
|
|
|
|
|
|
|
|
return no_auth
|
|
|
|
|
|
|
|
# If API key is configured, use proper authentication
|
|
|
|
api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)
|
|
|
|
|
|
|
|
async def api_key_auth(
|
|
|
|
api_key_header_value: Optional[str] = Security(api_key_header),
|
|
|
|
):
|
|
|
|
if not api_key_header_value:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=HTTP_403_FORBIDDEN, detail="API Key required"
|
|
|
|
)
|
|
|
|
if api_key_header_value != api_key:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=HTTP_403_FORBIDDEN, detail="Invalid API Key"
|
|
|
|
)
|
|
|
|
return api_key_header_value
|
|
|
|
|
|
|
|
return api_key_auth
|