88
99
1010def authentication_middleware (func : Any ) -> Any :
11+ """Decorator to enforce bearer token authentication on flask routes."""
1112 def wrapper (* args : Any , ** kwargs : Any ) -> Any :
1213 token = AuthenticationMiddleware .get_token_from_auth_header (request )
1314 # Check if bearer token exists and validate it
@@ -23,13 +24,14 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
2324class AuthenticationMiddleware :
2425 @classmethod
2526 def validate_bearer_token (cls , token : str | None ) -> bool :
27+ """Validate the provided bearer token against the database."""
2628 try :
2729 if token is None :
2830 current_app .logger .error ("Authentication failed. Empty bearer token" )
2931 return False
3032 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 )
33+ query = f"SELECT * FROM { platform_key_table } WHERE key = %s "
34+ cursor = be_db .execute_sql (query , ( token ,) )
3335 result_row = cursor .fetchone ()
3436 cursor .close ()
3537 if not result_row or len (result_row ) == 0 :
@@ -62,6 +64,7 @@ def validate_bearer_token(cls, token: str | None) -> bool:
6264
6365 @classmethod
6466 def get_token_from_auth_header (cls , request : Request ) -> str | None :
67+ """Extract the bearer token from the Authorization header."""
6568 try :
6669 bearer_token = request .headers .get ("Authorization" )
6770 if not bearer_token :
@@ -99,6 +102,7 @@ def get_organization_from_bearer_token(cls, token: str) -> tuple[int | None, str
99102
100103 @classmethod
101104 def execute_query (cls , query : str , params : tuple = ()) -> Any :
105+ """Execute a SQL query and return the first result."""
102106 cursor = be_db .execute_sql (query , params )
103107 result_row = cursor .fetchone ()
104108 cursor .close ()
0 commit comments