Skip to content
Snippets Groups Projects
Commit 45b9d0bb authored by Robert Goldmann's avatar Robert Goldmann
Browse files

secure comparison of api keys (safer against timing attacks)

parent 195da5a6
No related branches found
No related tags found
No related merge requests found
import secrets
from fastapi import Security, HTTPException from fastapi import Security, HTTPException
from fastapi.security import APIKeyHeader from fastapi.security import APIKeyHeader
from starlette.status import HTTP_403_FORBIDDEN from starlette.status import HTTP_403_FORBIDDEN
...@@ -18,5 +20,5 @@ API_KEY_HEADER = APIKeyHeader(name='apiKey') ...@@ -18,5 +20,5 @@ API_KEY_HEADER = APIKeyHeader(name='apiKey')
async def check_api_key(apiKey: str = Security(API_KEY_HEADER)): async def check_api_key(apiKey: str = Security(API_KEY_HEADER)):
from main import API_KEY from main import API_KEY
if apiKey != API_KEY: if not secrets.compare_digest(API_KEY, apiKey):
raise HTTPException(status_code=HTTP_403_FORBIDDEN, detail='apiKey invalid') raise HTTPException(status_code=HTTP_403_FORBIDDEN, detail='apiKey invalid')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment