Skip to content

Commit e91932c

Browse files
author
Michael Brewer
authored
fix(event-sources): Pass authorizer data to APIGatewayEventAuthorizer (#897)
1 parent 8406c9b commit e91932c

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

aws_lambda_powertools/utilities/data_classes/api_gateway_proxy_event.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,22 @@
1111
class APIGatewayEventAuthorizer(DictWrapper):
1212
@property
1313
def claims(self) -> Optional[Dict[str, Any]]:
14-
return self["requestContext"]["authorizer"].get("claims")
14+
return self.get("claims")
1515

1616
@property
1717
def scopes(self) -> Optional[List[str]]:
18-
return self["requestContext"]["authorizer"].get("scopes")
18+
return self.get("scopes")
19+
20+
@property
21+
def principal_id(self) -> Optional[str]:
22+
"""The principal user identification associated with the token sent by the client and returned from an
23+
API Gateway Lambda authorizer (formerly known as a custom authorizer)"""
24+
return self.get("principalId")
25+
26+
@property
27+
def integration_latency(self) -> Optional[int]:
28+
"""The authorizer latency in ms."""
29+
return self.get("integrationLatency")
1930

2031

2132
class APIGatewayEventRequestContext(BaseRequestContext):
@@ -56,7 +67,7 @@ def route_key(self) -> Optional[str]:
5667

5768
@property
5869
def authorizer(self) -> APIGatewayEventAuthorizer:
59-
return APIGatewayEventAuthorizer(self._data)
70+
return APIGatewayEventAuthorizer(self._data["requestContext"]["authorizer"])
6071

6172

6273
class APIGatewayProxyEvent(BaseProxyEvent):

aws_lambda_powertools/utilities/data_classes/common.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ def __eq__(self, other: Any) -> bool:
1818

1919
return self._data == other._data
2020

21-
def get(self, key: str) -> Optional[Any]:
22-
return self._data.get(key)
21+
def get(self, key: str, default: Optional[Any] = None) -> Optional[Any]:
22+
return self._data.get(key, default)
2323

2424
@property
2525
def raw_event(self) -> Dict[str, Any]:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"resource": "/trip",
3+
"path": "/trip",
4+
"httpMethod": "POST",
5+
"requestContext": {
6+
"requestId": "34972478-2843-4ced-a657-253108738274",
7+
"authorizer": {
8+
"user_id": "fake_username",
9+
"principalId": "fake",
10+
"integrationLatency": 451
11+
}
12+
}
13+
}

tests/functional/test_data_classes.py

+14
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,20 @@ def test_api_gateway_proxy_event():
897897
assert request_context.identity.client_cert.subject_dn == "www.example.com"
898898

899899

900+
def test_api_gateway_proxy_event_with_principal_id():
901+
event = APIGatewayProxyEvent(load_event("apiGatewayProxyEventPrincipalId.json"))
902+
903+
request_context = event.request_context
904+
authorizer = request_context.authorizer
905+
assert authorizer.claims is None
906+
assert authorizer.scopes is None
907+
assert authorizer["principalId"] == "fake"
908+
assert authorizer.get("principalId") == "fake"
909+
assert authorizer.principal_id == "fake"
910+
assert authorizer.integration_latency == 451
911+
assert authorizer.get("integrationStatus", "failed") == "failed"
912+
913+
900914
def test_api_gateway_proxy_v2_event():
901915
event = APIGatewayProxyEventV2(load_event("apiGatewayProxyV2Event.json"))
902916

0 commit comments

Comments
 (0)