Skip to content

Commit 58e8f39

Browse files
author
Michael Brewer
committed
fix(event-sources): Pass authorizer data to APIGatewayEventAuthorizer
Changes: - contract APIGatewayEventAuthorizer from the authorizer data - add test cases for the additional fields that can optional be in the authorizer dict - Update DictWrapper to allow for a default in `get`
1 parent 8406c9b commit 58e8f39

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

Diff for: aws_lambda_powertools/utilities/data_classes/api_gateway_proxy_event.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
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")
1919

2020

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

5757
@property
5858
def authorizer(self) -> APIGatewayEventAuthorizer:
59-
return APIGatewayEventAuthorizer(self._data)
59+
return APIGatewayEventAuthorizer(self._data["requestContext"]["authorizer"])
6060

6161

6262
class APIGatewayProxyEvent(BaseProxyEvent):

Diff for: 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]:

Diff for: tests/events/apiGatewayProxyEventPrincipalId.json

+13
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+
}

Diff for: tests/functional/test_data_classes.py

+12
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,18 @@ 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.get("foo", "default") == "default"
910+
911+
900912
def test_api_gateway_proxy_v2_event():
901913
event = APIGatewayProxyEventV2(load_event("apiGatewayProxyV2Event.json"))
902914

0 commit comments

Comments
 (0)