diff --git a/aws_lambda_powertools/utilities/data_classes/api_gateway_proxy_event.py b/aws_lambda_powertools/utilities/data_classes/api_gateway_proxy_event.py index 34ac8d83993..adce2d4b11b 100644 --- a/aws_lambda_powertools/utilities/data_classes/api_gateway_proxy_event.py +++ b/aws_lambda_powertools/utilities/data_classes/api_gateway_proxy_event.py @@ -11,11 +11,22 @@ class APIGatewayEventAuthorizer(DictWrapper): @property def claims(self) -> Optional[Dict[str, Any]]: - return self["requestContext"]["authorizer"].get("claims") + return self.get("claims") @property def scopes(self) -> Optional[List[str]]: - return self["requestContext"]["authorizer"].get("scopes") + return self.get("scopes") + + @property + def principal_id(self) -> Optional[str]: + """The principal user identification associated with the token sent by the client and returned from an + API Gateway Lambda authorizer (formerly known as a custom authorizer)""" + return self.get("principalId") + + @property + def integration_latency(self) -> Optional[int]: + """The authorizer latency in ms.""" + return self.get("integrationLatency") class APIGatewayEventRequestContext(BaseRequestContext): @@ -56,7 +67,7 @@ def route_key(self) -> Optional[str]: @property def authorizer(self) -> APIGatewayEventAuthorizer: - return APIGatewayEventAuthorizer(self._data) + return APIGatewayEventAuthorizer(self._data["requestContext"]["authorizer"]) class APIGatewayProxyEvent(BaseProxyEvent): diff --git a/aws_lambda_powertools/utilities/data_classes/common.py b/aws_lambda_powertools/utilities/data_classes/common.py index 566e1c56259..f209fc8c192 100644 --- a/aws_lambda_powertools/utilities/data_classes/common.py +++ b/aws_lambda_powertools/utilities/data_classes/common.py @@ -18,8 +18,8 @@ def __eq__(self, other: Any) -> bool: return self._data == other._data - def get(self, key: str) -> Optional[Any]: - return self._data.get(key) + def get(self, key: str, default: Optional[Any] = None) -> Optional[Any]: + return self._data.get(key, default) @property def raw_event(self) -> Dict[str, Any]: diff --git a/tests/events/apiGatewayProxyEventPrincipalId.json b/tests/events/apiGatewayProxyEventPrincipalId.json new file mode 100644 index 00000000000..f18a2a44bbd --- /dev/null +++ b/tests/events/apiGatewayProxyEventPrincipalId.json @@ -0,0 +1,13 @@ +{ + "resource": "/trip", + "path": "/trip", + "httpMethod": "POST", + "requestContext": { + "requestId": "34972478-2843-4ced-a657-253108738274", + "authorizer": { + "user_id": "fake_username", + "principalId": "fake", + "integrationLatency": 451 + } + } +} diff --git a/tests/functional/test_data_classes.py b/tests/functional/test_data_classes.py index ded32639233..7a211ec2e01 100644 --- a/tests/functional/test_data_classes.py +++ b/tests/functional/test_data_classes.py @@ -897,6 +897,20 @@ def test_api_gateway_proxy_event(): assert request_context.identity.client_cert.subject_dn == "www.example.com" +def test_api_gateway_proxy_event_with_principal_id(): + event = APIGatewayProxyEvent(load_event("apiGatewayProxyEventPrincipalId.json")) + + request_context = event.request_context + authorizer = request_context.authorizer + assert authorizer.claims is None + assert authorizer.scopes is None + assert authorizer["principalId"] == "fake" + assert authorizer.get("principalId") == "fake" + assert authorizer.principal_id == "fake" + assert authorizer.integration_latency == 451 + assert authorizer.get("integrationStatus", "failed") == "failed" + + def test_api_gateway_proxy_v2_event(): event = APIGatewayProxyEventV2(load_event("apiGatewayProxyV2Event.json"))