🐛 fix(endpoints.py): add check for missing api_key and raise HTTPException with status code 401 and detail message "Invalid API Key"

🐛 fix(crud.py): remove unnecessary return statement and update_total_uses function call if api_key_object is None
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-08-28 11:17:34 -03:00
commit f922214d2a
2 changed files with 14 additions and 11 deletions

View file

@ -100,6 +100,11 @@ async def process_flow(
""" """
try: try:
if api_key is None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid API Key",
)
api_key_user = api_key.user api_key_user = api_key.user
# Get the flow that matches the flow_id and belongs to the user # Get the flow that matches the flow_id and belongs to the user
flow = ( flow = (

View file

@ -50,9 +50,7 @@ def check_key(session: Session, api_key: str) -> Optional[ApiKey]:
"""Check if the API key is valid.""" """Check if the API key is valid."""
query = select(ApiKey).where(ApiKey.api_key == api_key) query = select(ApiKey).where(ApiKey.api_key == api_key)
api_key_object: Optional[ApiKey] = session.exec(query).first() api_key_object: Optional[ApiKey] = session.exec(query).first()
if api_key_object is None: if api_key_object is not None:
return api_key_object
threading.Thread( threading.Thread(
target=update_total_uses, target=update_total_uses,
args=( args=(
@ -60,7 +58,7 @@ def check_key(session: Session, api_key: str) -> Optional[ApiKey]:
api_key_object, api_key_object,
), ),
).start() ).start()
return api_key_object.user return api_key_object
def update_total_uses(session, api_key: ApiKey): def update_total_uses(session, api_key: ApiKey):