Skip to content
Snippets Groups Projects
Commit 892c32ea authored by Neda Moeini's avatar Neda Moeini
Browse files

Added token to url when authetication is enabled.

parent 5dcb98a6
No related branches found
No related tags found
1 merge request!142Added middleware to modify porecess details response and replace the callback...
...@@ -3,11 +3,13 @@ ...@@ -3,11 +3,13 @@
from typing import Any from typing import Any
from uuid import UUID from uuid import UUID
from fastapi import APIRouter, HTTPException, status from fastapi import APIRouter, HTTPException, status, Depends
from orchestrator.db import ProcessStepTable from orchestrator.db import ProcessStepTable
from orchestrator.schemas.base import OrchestratorBaseModel from orchestrator.schemas.base import OrchestratorBaseModel
router = APIRouter(prefix="/processes", tags=["Processes"]) from gso.auth.security import opa_security_default
router = APIRouter(prefix="/processes", tags=["Processes"], dependencies=[Depends(opa_security_default)])
class CallBackResultsBaseModel(OrchestratorBaseModel): class CallBackResultsBaseModel(OrchestratorBaseModel):
......
...@@ -38,7 +38,7 @@ class ModifyProcessEndpointResponse(BaseHTTPMiddleware): ...@@ -38,7 +38,7 @@ class ModifyProcessEndpointResponse(BaseHTTPMiddleware):
response_body += chunk response_body += chunk
try: try:
json_content = json.loads(response_body) json_content = json.loads(response_body)
self.modify_response_body(json_content, request) await self.modify_response_body(json_content, request)
modified_response_body = json.dumps(json_content).encode() modified_response_body = json.dumps(json_content).encode()
headers = dict(response.headers) headers = dict(response.headers)
headers["content-length"] = str(len(modified_response_body)) headers["content-length"] = str(len(modified_response_body))
...@@ -55,7 +55,26 @@ class ModifyProcessEndpointResponse(BaseHTTPMiddleware): ...@@ -55,7 +55,26 @@ class ModifyProcessEndpointResponse(BaseHTTPMiddleware):
return response return response
@staticmethod @staticmethod
def modify_response_body(response_body: dict[str, Any], request: Request) -> None: async def _get_token(request: Request) -> str:
"""Get the token from the request headers.
Args:
----
request (Request): The incoming HTTP request.
Returns:
-------
str: The token from the request headers in specific format.
"""
bearer_prefix = "Bearer "
authorization_header = request.headers.get("Authorization")
if authorization_header:
# Remove the "Bearer " prefix from the token
token = authorization_header.replace(bearer_prefix, "")
return f"?token={token}"
else:
return ""
async def modify_response_body(self, response_body: dict[str, Any], request: Request) -> None:
"""Modify the response body as needed. """Modify the response body as needed.
Args: Args:
...@@ -63,7 +82,8 @@ class ModifyProcessEndpointResponse(BaseHTTPMiddleware): ...@@ -63,7 +82,8 @@ class ModifyProcessEndpointResponse(BaseHTTPMiddleware):
response_body (Dict[str, Any]): The response body in dictionary format. response_body (Dict[str, Any]): The response body in dictionary format.
request (Request): The incoming HTTP request. request (Request): The incoming HTTP request.
""" """
max_output_length = 1000 max_output_length = 500
token = await self._get_token(request)
try: try:
for step in response_body["steps"]: for step in response_body["steps"]:
if step["state"].get("callback_result", None): if step["state"].get("callback_result", None):
...@@ -73,7 +93,7 @@ class ModifyProcessEndpointResponse(BaseHTTPMiddleware): ...@@ -73,7 +93,7 @@ class ModifyProcessEndpointResponse(BaseHTTPMiddleware):
if callback_result.get("output") and len(callback_result["output"]) > max_output_length: if callback_result.get("output") and len(callback_result["output"]) > max_output_length:
callback_result[ callback_result[
"output" "output"
] = f'{request.base_url}api/v1/processes/steps/{step["step_id"]}/callback-results/' ] = f'{request.base_url}api/v1/processes/steps/{step["step_id"]}/callback-results{token}'
step["state"]["callback_result"] = callback_result step["state"]["callback_result"] = callback_result
except (AttributeError, KeyError, TypeError): except (AttributeError, KeyError, TypeError):
pass pass
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment