88
99
1010def authentication_middleware (func : Any ) -> Any :
11+ """Decorator to enforce bearer token authentication on flask routes."""
12+
1113 def wrapper (* args : Any , ** kwargs : Any ) -> Any :
1214 token = AuthenticationMiddleware .get_token_from_auth_header (request )
1315 # Check if bearer token exists and validate it
@@ -23,13 +25,14 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
2325class AuthenticationMiddleware :
2426 @classmethod
2527 def validate_bearer_token (cls , token : str | None ) -> bool :
28+ """Validate the provided bearer token against the database."""
2629 try :
2730 if token is None :
2831 current_app .logger .error ("Authentication failed. Empty bearer token" )
2932 return False
3033 platform_key_table = f'"{ Env .DB_SCHEMA } ".{ DBTable .PLATFORM_KEY } '
31- query = f"SELECT * FROM { platform_key_table } WHERE key = ' { token } ' "
32- cursor = be_db .execute_sql (query )
34+ query = f"SELECT * FROM { platform_key_table } WHERE key = %s "
35+ cursor = be_db .execute_sql (query , ( token ,) )
3336 result_row = cursor .fetchone ()
3437 cursor .close ()
3538 if not result_row or len (result_row ) == 0 :
@@ -62,6 +65,7 @@ def validate_bearer_token(cls, token: str | None) -> bool:
6265
6366 @classmethod
6467 def get_token_from_auth_header (cls , request : Request ) -> str | None :
68+ """Extract the bearer token from the Authorization header."""
6569 try :
6670 bearer_token = request .headers .get ("Authorization" )
6771 if not bearer_token :
@@ -99,6 +103,7 @@ def get_organization_from_bearer_token(cls, token: str) -> tuple[int | None, str
99103
100104 @classmethod
101105 def execute_query (cls , query : str , params : tuple = ()) -> Any :
106+ """Execute a SQL query and return the first result."""
102107 cursor = be_db .execute_sql (query , params )
103108 result_row = cursor .fetchone ()
104109 cursor .close ()
0 commit comments