-
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.
feat: Secure Sourcing related backend APIs (#22)
Co-authored-by: mfteloglu <mfteloglu@gmail.com>
- Loading branch information
1 parent
62b17a3
commit 80e94e9
Showing
19 changed files
with
216 additions
and
21 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 |
---|---|---|
|
@@ -25,3 +25,4 @@ dependencies: | |
- pydantic >=2 | ||
- pyyaml | ||
- uvicorn >=0.23.2 | ||
- python-jose >=3.3.0 |
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,8 @@ | ||
"""This package contains dependency functions used across the endpoints. | ||
These dependencies are designed to provide reusable utility functions, such as | ||
authentication and authorization checks, that can be injected into FastAPI route | ||
handlers. By centralizing these dependencies, the application's code remains clean, | ||
modular, and easy to maintain. Each module in this package is tailored to specific sets | ||
of functionalities. | ||
""" |
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,55 @@ | ||
"""This module contains authentication and authorization dependencies. | ||
Specifically designed for sourcing modules in the FastAPI application, it includes | ||
functions to authenticate requests using JWTs and to authorize these requests by | ||
validating the JWTs against defined secret keys. The module ensures that only valid and | ||
authorized sourcing modules can access certain endpoints. | ||
""" | ||
import logging | ||
|
||
from fastapi import HTTPException, Header, status | ||
|
||
from parma_mining.mining_common.jwt_handler import JWTHandler | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def authenticate( | ||
authorization: str = Header(None), | ||
) -> str: | ||
"""Authenticate the incoming request using the JWT in the Authorization header. | ||
Args: | ||
authorization: The Authorization header containing the JWT. | ||
Returns: | ||
Extracted token from the Authorization header. | ||
(Whenever a request is needed to be made to the Analytics Backend, | ||
This token can be used to authenticate the request.) | ||
Raises: | ||
HTTPException: If the JWT is invalid. | ||
HTTPException: If the JWT is expired. | ||
HTTPException: If the Authorization header is missing. | ||
""" | ||
if authorization is None: | ||
logger.error("Authorization header is required!") | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Authorization header is required!", | ||
) | ||
|
||
token = ( | ||
authorization.split(" ")[1] | ||
if authorization.startswith("Bearer ") | ||
else authorization | ||
) | ||
is_verified: bool = JWTHandler.verify_jwt(token) | ||
if is_verified is False: | ||
logger.error("Invalid shared token or expired token") | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Invalid shared token or expired token", | ||
) | ||
|
||
return token |
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 @@ | ||
"""Common functions for mining modules.""" |
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,44 @@ | ||
"""Module for JWT (JSON Web Token) handling. | ||
This module contains the JWTHandler class which is designed to verify JWTs. The | ||
verification process supports shared secret keys to enable authentication. | ||
""" | ||
import logging | ||
import os | ||
|
||
from jose import jwt | ||
from jose.exceptions import ExpiredSignatureError, JWTError | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class JWTHandler: | ||
"""A handler for verifying JWTs.""" | ||
|
||
SHARED_SECRET_KEY: str = str( | ||
os.getenv("PARMA_SHARED_SECRET_KEY") or "PARMA_SHARED_SECRET_KEY" | ||
) | ||
ALGORITHM: str = "HS256" | ||
|
||
@staticmethod | ||
def verify_jwt(token: str) -> bool: | ||
"""Verify a JWT using the shared secret key. | ||
Args: | ||
token: The JWT token to verify. | ||
Returns: | ||
True if the verification is successful. | ||
False otherwise. | ||
""" | ||
try: | ||
jwt.decode( | ||
token, JWTHandler.SHARED_SECRET_KEY, algorithms=[JWTHandler.ALGORITHM] | ||
) | ||
return True | ||
except ExpiredSignatureError: | ||
logger.error("JWT has expired.") | ||
except JWTError: | ||
logger.error("Invalid JWT, unable to decode.") | ||
|
||
return False |
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
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
Empty file.
Empty file.
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,25 @@ | ||
"""Mock implementations of auth function for testing. | ||
This module provides mock versions of authentication function. This mock function is | ||
designed for use in test environments where actual authentication process is not | ||
required. | ||
""" | ||
import logging | ||
|
||
from fastapi import Header | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def mock_authenticate( | ||
authorization: str = Header(None), | ||
) -> str: | ||
"""Authenticate the incoming request using the JWT in the Authorization header. | ||
Args: | ||
authorization: The Authorization header containing the JWT. | ||
Returns: | ||
Dummy token for testing purposes. | ||
""" | ||
return "dummytoken" |
Oops, something went wrong.