Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
dds_glossary/auth.py: handled missing API_KEY envvar
Browse files Browse the repository at this point in the history
  • Loading branch information
sami-m-g committed Jun 26, 2024
1 parent 9ed5691 commit 8b81525
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions dds_glossary/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from fastapi import Depends, HTTPException
from fastapi.security import APIKeyHeader
from pydantic import ValidationError

from .settings import get_settings

Expand All @@ -24,10 +25,14 @@ def get_api_key(api_key: str = Depends(api_key_header)) -> dict[str, str]:
HTTPException: If the API key is missing or invalid. Or if the API key
environment variable is missing.
"""
correct_api_key: str | None = get_settings().API_KEY.get_secret_value()
if correct_api_key is None or api_key != correct_api_key:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN,
detail="Invalid API Key",
)
error = HTTPException(
status_code=HTTPStatus.FORBIDDEN,
detail="Invalid API Key",
)
try:
correct_api_key = get_settings().API_KEY.get_secret_value()
if api_key != correct_api_key:
raise error
except ValidationError as ve:
raise error from ve
return {"api_key": api_key}

0 comments on commit 8b81525

Please sign in to comment.