middleware and respnse models updated
This commit is contained in:
@@ -41,7 +41,7 @@ class TokenEventMiddleware:
|
||||
Returns:
|
||||
Tuple[str, list[str]]: The access token and a list of reachable event codes
|
||||
"""
|
||||
# Get token context from request
|
||||
# Get token context from request
|
||||
access_token = TokenService.get_access_token_from_request(request_from_scope)
|
||||
if not access_token:
|
||||
raise HTTPExceptionApi(
|
||||
@@ -50,13 +50,19 @@ class TokenEventMiddleware:
|
||||
loc=get_line_number_for_error(),
|
||||
sys_msg="Token not found",
|
||||
)
|
||||
|
||||
|
||||
# Get token context from Redis by access token and collect reachable event codes
|
||||
token_context = TokenService.get_object_via_access_key(access_token=access_token)
|
||||
token_context = TokenService.get_object_via_access_key(
|
||||
access_token=access_token
|
||||
)
|
||||
if token_context.is_employee:
|
||||
reachable_event_codes: list[str] = token_context.selected_company.reachable_event_codes
|
||||
reachable_event_codes: list[str] = (
|
||||
token_context.selected_company.reachable_event_codes
|
||||
)
|
||||
elif token_context.is_occupant:
|
||||
reachable_event_codes: list[str] = token_context.selected_occupant.reachable_event_codes
|
||||
reachable_event_codes: list[str] = (
|
||||
token_context.selected_occupant.reachable_event_codes
|
||||
)
|
||||
else:
|
||||
raise HTTPExceptionApi(
|
||||
error_code="",
|
||||
@@ -67,12 +73,15 @@ class TokenEventMiddleware:
|
||||
return token_context, reachable_event_codes
|
||||
|
||||
@staticmethod
|
||||
def retrieve_intersected_event_code(request: Request, reachable_event_codes: list[str]) -> str:
|
||||
def retrieve_intersected_event_code(
|
||||
request: Request, reachable_event_codes: list[str]
|
||||
) -> str:
|
||||
"""
|
||||
Match an endpoint with accessible events.
|
||||
|
||||
Args:
|
||||
request: The endpoint to match
|
||||
reachable_event_codes: The list of event codes accessible to the user
|
||||
|
||||
Returns:
|
||||
Dict containing the endpoint registration data
|
||||
@@ -81,9 +90,7 @@ class TokenEventMiddleware:
|
||||
endpoint_url = str(request.url.path)
|
||||
# Get the endpoint URL for matching with events
|
||||
function_codes_of_endpoint = RedisActions.get_json(
|
||||
list_keys=[
|
||||
RedisCategoryKeys.METHOD_FUNCTION_CODES, "*", endpoint_url
|
||||
]
|
||||
list_keys=[RedisCategoryKeys.METHOD_FUNCTION_CODES, "*", endpoint_url]
|
||||
)
|
||||
function_code_list_of_event = function_codes_of_endpoint.first
|
||||
if not function_codes_of_endpoint.status:
|
||||
@@ -93,9 +100,11 @@ class TokenEventMiddleware:
|
||||
loc=get_line_number_for_error(),
|
||||
sys_msg="Function code not found",
|
||||
)
|
||||
|
||||
|
||||
# Intersect function codes with user accers objects available event codes
|
||||
intersected_code = list(set(function_code_list_of_event) & set(reachable_event_codes))
|
||||
intersected_code = list(
|
||||
set(function_code_list_of_event) & set(reachable_event_codes)
|
||||
)
|
||||
if not len(intersected_code) == 1:
|
||||
raise HTTPExceptionApi(
|
||||
error_code="",
|
||||
@@ -122,18 +131,23 @@ class TokenEventMiddleware:
|
||||
|
||||
@wraps(func)
|
||||
async def wrapper(request: Request, *args, **kwargs) -> Dict[str, Any]:
|
||||
|
||||
|
||||
# Get and validate token context from request
|
||||
# token_context, reachable_event_codes = cls.retrieve_access_content(request)
|
||||
token_context, reachable_event_codes = {"token": "context", "context": {}}, ["g1j8i6j7-9k4h-0h6l-4i3j-2j0k1k0j0i0k"]
|
||||
endpoint_url, reachable_event_code = cls.retrieve_intersected_event_code(request, reachable_event_codes)
|
||||
event_context = EventContext(auth=token_context, code=reachable_event_code, url=endpoint_url)
|
||||
reachable_event_codes = ["g1j8i6j7-9k4h-0h6l-4i3j-2j0k1k0j0i0k"]
|
||||
token_context = {"token": "context","context": {}}
|
||||
endpoint_url, reachable_event_code = cls.retrieve_intersected_event_code(
|
||||
request, reachable_event_codes
|
||||
)
|
||||
event_context = EventContext(
|
||||
auth=token_context, code=reachable_event_code, url=endpoint_url, request=request,
|
||||
)
|
||||
|
||||
# Get auth context from the authenticated function's wrapper
|
||||
if token_context is not None:
|
||||
setattr(wrapper, 'event_context', event_context)
|
||||
setattr(func, 'event_context', event_context)
|
||||
|
||||
setattr(wrapper, "event_context", event_context)
|
||||
setattr(func, "event_context", event_context)
|
||||
|
||||
# Execute the authenticated function and get its result
|
||||
if inspect.iscoroutinefunction(func):
|
||||
result = await func(request, *args, **kwargs)
|
||||
@@ -179,4 +193,4 @@ class TokenEventMiddleware:
|
||||
# result = func(request, *args, **kwargs)
|
||||
# return result
|
||||
|
||||
# return wrapper
|
||||
# return wrapper
|
||||
|
||||
Reference in New Issue
Block a user