This repository has been archived by the owner on Dec 31, 2024. It is now read-only.
generated from childmindresearch/template-python-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API key support and authentication (#85)
- Loading branch information
1 parent
bcee2e3
commit 6f25562
Showing
6 changed files
with
79 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""Security module for the CTK API.""" | ||
import fastapi | ||
from fastapi import security, status | ||
|
||
from linguaweb_api.core import config | ||
|
||
settings = config.get_settings() | ||
API_KEY = settings.API_KEY | ||
|
||
|
||
def check_api_key( | ||
api_key_header: str = fastapi.Security(security.APIKeyHeader(name="x-api-key")), | ||
) -> None: | ||
"""Checks the validity of the API key provided in the API key header. | ||
At present, only a single API key is supported and is stored in the environment. | ||
Args: | ||
api_key_header: The API key provided in the API key header. | ||
Raises: | ||
fastapi.HTTPException: 401 If the API key is invalid or missing. | ||
""" | ||
if api_key_header == API_KEY.get_secret_value(): | ||
return | ||
raise fastapi.HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Invalid or missing API Key.", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters