from aws_lambda_powertools.event_handler.api_gateway import ( APIGatewayHttpResolver, Response, ) from aws_lambda_powertools.event_handler.middlewares import ( BaseMiddlewareHandler, NextMiddleware, ) from pydantic import UUID4, BaseModel, Field class AuthorizerMiddleware(BaseMiddlewareHandler): def handler( self, app: APIGatewayHttpResolver, next_middleware: NextMiddleware, ) -> Response: # Gets the Lambda authorizer associated with the current API Gateway event. # You can check the file `auth.py` for more details. authorizer = app.current_event.request_context.authorizer.get_lambda if 'user' in authorizer: user = authorizer['user'] app.append_context(user=AuthenticatedUser(**user)) return next_middleware(app) class AuthenticatedUser(BaseModel): id: str = Field(alias='custom:user_id') name: str email: str email_verified: bool sub: UUID4