Skip to content

Commit f267736

Browse files
author
Michael Brewer
committed
feat(data-classes): add missing usageIdentifierKey
1 parent b406365 commit f267736

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

aws_lambda_powertools/utilities/data_classes/api_gateway_authorizer_event.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,15 @@ class HttpVerb(enum.Enum):
340340

341341

342342
class APIGatewayAuthorizerResponse:
343-
"""Api Gateway HTTP API V1 payload or Rest api authorizer response helper
343+
"""The IAM Policy Response required for API Gateway REST APIs and HTTP APIs.
344344
345345
Based on: - https://github.com/awslabs/aws-apigateway-lambda-authorizer-blueprints/blob/\
346346
master/blueprints/python/api-gateway-authorizer-python.py
347+
348+
Documentation:
349+
-------------
350+
- https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-lambda-authorizer.html
351+
- https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-lambda-authorizer-output.html
347352
"""
348353

349354
version = "2012-10-17"
@@ -360,6 +365,7 @@ def __init__(
360365
api_id: str,
361366
stage: str,
362367
context: Optional[Dict] = None,
368+
usage_identifier_key: Optional[str] = None,
363369
):
364370
"""
365371
Parameters
@@ -388,13 +394,18 @@ def __init__(
388394
context : Dict, optional
389395
Optional, context.
390396
Note: only names of type string and values of type int, string or boolean are supported
397+
usage_identifier_key: str, optional
398+
If the API uses a usage plan (the apiKeySource is set to `AUTHORIZER`), the Lambda authorizer function
399+
must return one of the usage plan's API keys as the usageIdentifierKey property value.
400+
> **Note:** This only applies for REST APIs.
391401
"""
402+
self.principal_id = principal_id
392403
self.region = region
393404
self.aws_account_id = aws_account_id
394405
self.api_id = api_id
395406
self.stage = stage
396-
self.principal_id = principal_id
397407
self.context = context
408+
self.usage_identifier_key = usage_identifier_key
398409
self._allow_routes: List[Dict] = []
399410
self._deny_routes: List[Dict] = []
400411

@@ -506,6 +517,9 @@ def asdict(self) -> Dict[str, Any]:
506517
response["policyDocument"]["Statement"].extend(self._get_statement_for_effect("Allow", self._allow_routes))
507518
response["policyDocument"]["Statement"].extend(self._get_statement_for_effect("Deny", self._deny_routes))
508519

520+
if self.usage_identifier_key:
521+
response["usageIdentifierKey"] = self.usage_identifier_key
522+
509523
if self.context:
510524
response["context"] = self.context
511525

tests/functional/data_classes/test_api_gateway_authorizer.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_authorizer_response_invalid_resource(builder: APIGatewayAuthorizerRespo
3737

3838

3939
def test_authorizer_response_allow_all_routes_with_context():
40-
builder = APIGatewayAuthorizerResponse("foo", "us-west-1", "123456789", "fantom", "dev", {"name": "Foo"})
40+
builder = APIGatewayAuthorizerResponse("foo", "us-west-1", "123456789", "fantom", "dev", context={"name": "Foo"})
4141
builder.allow_all_routes()
4242
assert builder.asdict() == {
4343
"principalId": "foo",
@@ -55,6 +55,25 @@ def test_authorizer_response_allow_all_routes_with_context():
5555
}
5656

5757

58+
def test_authorizer_response_allow_all_routes_with_usage_identifier_key():
59+
builder = APIGatewayAuthorizerResponse("cow", "us-east-1", "1111111111", "api", "dev", usage_identifier_key="key")
60+
builder.allow_all_routes()
61+
assert builder.asdict() == {
62+
"principalId": "cow",
63+
"policyDocument": {
64+
"Version": "2012-10-17",
65+
"Statement": [
66+
{
67+
"Action": "execute-api:Invoke",
68+
"Effect": "Allow",
69+
"Resource": ["arn:aws:execute-api:us-east-1:1111111111:api/dev/*/*"],
70+
}
71+
],
72+
},
73+
"usageIdentifierKey": "key",
74+
}
75+
76+
5877
def test_authorizer_response_deny_all_routes(builder: APIGatewayAuthorizerResponse):
5978
builder.deny_all_routes()
6079
assert builder.asdict() == {

0 commit comments

Comments
 (0)