Skip to content

Commit 9fc2f1d

Browse files
committed
fix auth token lookup logic and add request timeouts
1 parent b312f03 commit 9fc2f1d

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

x2text-service/app/authentication_middleware.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99

1010
def 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:
2325
class 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()

x2text-service/app/controllers/controller.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@
2626

2727
@basic.route("/health", methods=["GET"])
2828
def health() -> str:
29+
"""Check the health status of the service."""
2930
logging.info("Checking health from : %s", request.remote_addr)
3031
return "OK"
3132

3233

3334
@basic.route("/test-connection", methods=["POST"])
3435
@authentication_middleware
3536
def test_connection() -> Any:
37+
"""Test the connection to the Unstructured API."""
3638
logging.info("Received a test connection request from %s", request.remote_addr)
3739
form_data = dict(request.form)
3840
unstructured_api_key = X2TextUtil.get_value_for_key(UNSTRUCTURED_API_KEY, form_data)
@@ -54,7 +56,7 @@ def test_connection() -> Any:
5456
headers=headers,
5557
data=None,
5658
files=files,
57-
timeout=None,
59+
timeout=60,
5860
)
5961

6062
if response.status_code == 400:
@@ -76,6 +78,7 @@ def test_connection() -> Any:
7678
@basic.route("/process", methods=["POST"])
7779
@authentication_middleware
7880
def process() -> Any:
81+
"""Process a document for text extraction."""
7982
logging.info("Received a doc processing request from %s", request.remote_addr)
8083
form_data = dict(request.form)
8184
url = X2TextUtil.get_value_for_key(UNSTRUCTURED_URL, form_data)
@@ -122,7 +125,7 @@ def process() -> Any:
122125
headers=headers,
123126
data=payload,
124127
files=files,
125-
timeout=None,
128+
timeout=60,
126129
)
127130
if response.ok:
128131
json_response = response.json()

0 commit comments

Comments
 (0)