Skip to content

feat(event_sources): add get_context() to standardize API Gateway Lambda Authorizer context in v1 and v2 #3454

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

Merged
merged 9 commits into from
Dec 6, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,6 @@ def jwt_scopes(self) -> Optional[List[str]]:
jwt = self.get("jwt") or {} # not available in FunctionURL
return jwt.get("scopes")

@property
def get_lambda(self) -> Optional[Dict[str, Any]]:
"""Lambda authorization context details"""
return self.get("lambda")

@property
def iam(self) -> Optional[RequestContextV2AuthorizerIam]:
"""IAM authorization details used for making the request."""
Expand Down
3 changes: 1 addition & 2 deletions aws_lambda_powertools/utilities/parser/models/apigwv2.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
from typing import Any, Dict, List, Optional, Type, Union

from pydantic import BaseModel, Field
from pydantic import BaseModel
from pydantic.networks import IPvAnyNetwork

from aws_lambda_powertools.utilities.parser.types import Literal
Expand Down Expand Up @@ -31,7 +31,6 @@ class RequestContextV2AuthorizerJwt(BaseModel):
class RequestContextV2Authorizer(BaseModel):
jwt: Optional[RequestContextV2AuthorizerJwt] = None
iam: Optional[RequestContextV2AuthorizerIam] = None
lambda_value: Optional[Dict[str, Any]] = Field(None, alias="lambda")


class RequestContextV2Http(BaseModel):
Expand Down
14 changes: 10 additions & 4 deletions tests/events/apiGatewayProxyV2LambdaAuthorizerEvent.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@
"stage": "$default",
"requestId": "id",
"authorizer": {
"lambda": {
"key": "value"
"jwt": {
"claims": {
"claim1": "value1"
},
"scopes": [
"scope1",
"scope2"
]
}
},
},
"apiId": "api-id",
"domainName": "id.execute-api.us-east-1.amazonaws.com",
"domainPrefix": "id",
Expand All @@ -47,4 +53,4 @@
},
"body": "{\r\n\t\"a\": 1\r\n}",
"isBase64Encoded": false
}
}
11 changes: 8 additions & 3 deletions tests/unit/data_classes/test_api_gateway_proxy_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,14 @@ def test_api_gateway_proxy_v2_lambda_authorizer_event():

request_context = parsed_event.request_context
assert request_context is not None
lambda_props = request_context.authorizer.get_lambda
assert lambda_props is not None
assert lambda_props.get("key") == "value"
jwt_claims = request_context.authorizer.jwt_claim
assert jwt_claims is not None
assert jwt_claims.get("claim1") == raw_event["requestContext"]["authorizer"]["jwt"]["claims"]["claim1"]

jwt_scopes = request_context.authorizer.jwt_scopes
assert jwt_scopes is not None
assert jwt_scopes[0] == raw_event["requestContext"]["authorizer"]["jwt"]["scopes"][0]
assert jwt_scopes[1] == raw_event["requestContext"]["authorizer"]["jwt"]["scopes"][1]


def test_api_gateway_proxy_v2_iam_event():
Expand Down
1 change: 0 additions & 1 deletion tests/unit/data_classes/test_lambda_function_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ def test_lambda_function_url_event_iam():
assert authorizer is not None
assert authorizer.jwt_claim is None
assert authorizer.jwt_scopes is None
assert authorizer.get_lambda is None

iam = authorizer.iam
iam_raw = raw_event["requestContext"]["authorizer"]["iam"]
Expand Down
11 changes: 8 additions & 3 deletions tests/unit/parser/test_apigwv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,14 @@ def test_api_gateway_proxy_v2_event_lambda_authorizer():
request_context: RequestContextV2 = parsed_event.requestContext
assert request_context is not None

lambda_props: RequestContextV2Authorizer = request_context.authorizer.lambda_value
assert lambda_props is not None
assert lambda_props["key"] == raw_event["requestContext"]["authorizer"]["lambda"]["key"]
jwt_claims: RequestContextV2Authorizer = request_context.authorizer.jwt.claims
assert jwt_claims is not None
assert jwt_claims["claim1"] == raw_event["requestContext"]["authorizer"]["jwt"]["claims"]["claim1"]

jwt_scopes: RequestContextV2Authorizer = request_context.authorizer.jwt.scopes
assert jwt_scopes is not None
assert jwt_scopes[0] == raw_event["requestContext"]["authorizer"]["jwt"]["scopes"][0]
assert jwt_scopes[1] == raw_event["requestContext"]["authorizer"]["jwt"]["scopes"][1]


def test_api_gateway_proxy_v2_event_iam_authorizer():
Expand Down
1 change: 0 additions & 1 deletion tests/unit/parser/test_lambda_function_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ def test_lambda_function_url_event_iam():
authorizer = request_context.authorizer
assert authorizer is not None
assert authorizer.jwt is None
assert authorizer.lambda_value is None

iam = authorizer.iam
iam_raw = raw_event["requestContext"]["authorizer"]["iam"]
Expand Down