feat: allow the use of an ENV variable to set the API key if the ParameterStore isn't used. (#40)
This commit is contained in:
@@ -3,16 +3,19 @@ from typing import Annotated
|
||||
|
||||
import boto3
|
||||
from fastapi import Depends, HTTPException, status
|
||||
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
||||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
||||
|
||||
from api.setting import DEFAULT_API_KEYS
|
||||
|
||||
api_key_param = os.environ.get("API_KEY_PARAM_NAME")
|
||||
api_key_env = os.environ.get("API_KEY")
|
||||
if api_key_param:
|
||||
ssm = boto3.client("ssm")
|
||||
api_key = ssm.get_parameter(Name=api_key_param, WithDecryption=True)["Parameter"][
|
||||
"Value"
|
||||
]
|
||||
elif api_key_env:
|
||||
api_key = api_key_env
|
||||
else:
|
||||
api_key = DEFAULT_API_KEYS
|
||||
|
||||
@@ -20,7 +23,7 @@ security = HTTPBearer()
|
||||
|
||||
|
||||
def api_key_auth(
|
||||
credentials: Annotated[HTTPAuthorizationCredentials, Depends(security)]
|
||||
credentials: Annotated[HTTPAuthorizationCredentials, Depends(security)],
|
||||
):
|
||||
if credentials.credentials != api_key:
|
||||
raise HTTPException(
|
||||
|
||||
Reference in New Issue
Block a user