-
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.
Merge pull request #20 from ScilifelabDataCentre/allow_CORS
Setting up some minor protection placeholders for backend API
- Loading branch information
Showing
6 changed files
with
54 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
__pycache__/ | ||
.github/ | ||
.venv/ | ||
.env | ||
.envExample | ||
.gitignore | ||
LICENSE | ||
README.md |
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,6 @@ | ||
# example showing which variables are set in .env or a kubernetes ConfigMap | ||
# needs to be set for apps to run correctly | ||
FRONTEND_URL = "" | ||
# The request header from the frontend will contain an 'X-api-key' which | ||
# must match with this key to allow API operations | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ flask-smorest | |
python-dotenv | ||
sqlalchemy | ||
flask-sqlalchemy | ||
flask-migrate | ||
flask-migrate | ||
flask_cors |
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,27 @@ | ||
import os | ||
|
||
import functools | ||
from flask import request | ||
from flask_smorest import abort | ||
|
||
def is_valid(api_key): | ||
return os.getenv("API_KEY") == api_key | ||
|
||
def api_key_required(func): | ||
@functools.wraps(func) | ||
def decorator(*args, **kwargs): | ||
api_key = request.headers.get("x-api-key") | ||
if not api_key: | ||
abort( | ||
400, | ||
message="Please provide an API key." | ||
) | ||
# Check if API key is correct and valid | ||
if is_valid(api_key): | ||
return func(*args, **kwargs) | ||
else: | ||
abort( | ||
403, | ||
message="The provided API key is not valid." | ||
) | ||
return decorator |