Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(event-sources): Pass authorizer data to APIGatewayEventAuthorizer #897

Merged
merged 2 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes!!!!



class APIGatewayProxyEvent(BaseProxyEvent):
Expand Down
4 changes: 2 additions & 2 deletions aws_lambda_powertools/utilities/data_classes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
13 changes: 13 additions & 0 deletions tests/events/apiGatewayProxyEventPrincipalId.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}
14 changes: 14 additions & 0 deletions tests/functional/test_data_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand Down