54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
"""
|
|
Token event middleware for handling authentication and event tracking.
|
|
"""
|
|
|
|
from functools import wraps
|
|
from typing import Callable, Dict, Any
|
|
from .auth_middleware import MiddlewareModule
|
|
import inspect
|
|
|
|
|
|
class TokenEventMiddleware:
|
|
"""
|
|
Module containing token and event handling functionality.
|
|
|
|
This class provides:
|
|
- Token and event context management
|
|
- Event validation decorator for endpoints
|
|
"""
|
|
|
|
@staticmethod
|
|
def event_required(
|
|
func: Callable[..., Dict[str, Any]]
|
|
) -> Callable[..., Dict[str, Any]]:
|
|
"""
|
|
Decorator for endpoints with token and event requirements.
|
|
This decorator:
|
|
1. First validates authentication using MiddlewareModule.auth_required
|
|
2. Then adds event tracking context
|
|
|
|
Args:
|
|
func: The function to be decorated
|
|
|
|
Returns:
|
|
Callable: The wrapped function with both auth and event handling
|
|
"""
|
|
# First apply authentication
|
|
authenticated_func = MiddlewareModule.auth_required(func)
|
|
|
|
@wraps(authenticated_func)
|
|
async def wrapper(*args, **kwargs) -> Dict[str, Any]:
|
|
# Create handler with context
|
|
function_code = (
|
|
"7192c2aa-5352-4e36-98b3-dafb7d036a3d" # Keep function_code as URL
|
|
)
|
|
|
|
# Make handler available to all functions in the chain
|
|
func.func_code = {"function_code": function_code}
|
|
# Call the authenticated function
|
|
if inspect.iscoroutinefunction(authenticated_func):
|
|
return await authenticated_func(*args, **kwargs)
|
|
return authenticated_func(*args, **kwargs)
|
|
|
|
return wrapper
|