Refactor code structure for improved readability and maintainability
This commit is contained in:
+20
@@ -0,0 +1,20 @@
|
||||
import secrets
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import Depends, HTTPException, status
|
||||
from fastapi.security import APIKeyHeader
|
||||
|
||||
from app.config import Settings, get_settings
|
||||
|
||||
api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)
|
||||
|
||||
|
||||
async def require_api_key(
|
||||
supplied_key: Annotated[str | None, Depends(api_key_header)],
|
||||
current_settings: Annotated[Settings, Depends(get_settings)],
|
||||
) -> None:
|
||||
if supplied_key is None or not secrets.compare_digest(supplied_key, current_settings.api_key):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Invalid or missing API key",
|
||||
)
|
||||
Reference in New Issue
Block a user