Skip to content

Commit b406365

Browse files
author
Michael Brewer
committed
feat(data-classes): Add a deny all response
Update the docs and add a deny all response
1 parent bfc1041 commit b406365

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

aws_lambda_powertools/utilities/data_classes/api_gateway_authorizer_event.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,21 @@ class HttpVerb(enum.Enum):
324324
ALL = "*"
325325

326326

327+
DENY_ALL_RESPONSE = {
328+
"principalId": "deny-all-user",
329+
"policyDocument": {
330+
"Version": "2012-10-17",
331+
"Statement": [
332+
{
333+
"Action": "execute-api:Invoke",
334+
"Effect": "Deny",
335+
"Resource": ["*"],
336+
}
337+
],
338+
},
339+
}
340+
341+
327342
class APIGatewayAuthorizerResponse:
328343
"""Api Gateway HTTP API V1 payload or Rest api authorizer response helper
329344

docs/utilities/data_classes.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,17 @@ Use **`APIGatewayAuthorizerRequestEvent`** for type `REQUEST` and **`APIGatewayA
9696

9797
When the user is found, it includes the user details in the request context that will be available to the back-end, and returns a full access policy for admin users.
9898

99-
```python hl_lines="2-5 29 36-42 47 49"
99+
```python hl_lines="2-6 29 36-42 47 49"
100100
from aws_lambda_powertools.utilities.data_classes import event_source
101101
from aws_lambda_powertools.utilities.data_classes.api_gateway_authorizer_event import (
102+
DENY_ALL_RESPONSE,
102103
APIGatewayAuthorizerRequestEvent,
103104
APIGatewayAuthorizerResponse,
104105
HttpVerb,
105106
)
106107
from secrets import compare_digest
107108

108109

109-
class UnAuthorizedError(Exception):
110-
...
111-
112-
113110
def get_user_by_token(token):
114111
if compare_digest(token, "admin-foo"):
115112
return {"id": 0, "name": "Admin", "isAdmin": True}
@@ -124,8 +121,11 @@ Use **`APIGatewayAuthorizerRequestEvent`** for type `REQUEST` and **`APIGatewayA
124121
user = get_user_by_token(event.get_header_value("Authorization"))
125122

126123
if user is None:
127-
# No user was found, so we raised a not authorized error
128-
raise UnAuthorizedError("Not authorized to perform this action")
124+
# No user was found
125+
# to return 401 - `{"message":"Unauthorized"}`, but polutes lambda metrics
126+
# raise Exception("Unauthorized")
127+
# to return 403 - `{"message":"Forbidden"}`
128+
return DENY_ALL_RESPONSE
129129

130130
# parse the `methodArn` as an `APIGatewayRouteArn`
131131
arn = event.parsed_arn

tests/functional/data_classes/test_api_gateway_authorizer.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

33
from aws_lambda_powertools.utilities.data_classes.api_gateway_authorizer_event import (
4+
DENY_ALL_RESPONSE,
45
APIGatewayAuthorizerResponse,
56
HttpVerb,
67
)
@@ -145,3 +146,14 @@ def test_authorizer_response_deny_route_with_conditions(builder: APIGatewayAutho
145146
],
146147
},
147148
}
149+
150+
151+
def test_deny_all():
152+
# CHECK we always explicitly deny all
153+
statements = DENY_ALL_RESPONSE["policyDocument"]["Statement"]
154+
assert len(statements) == 1
155+
assert statements[0] == {
156+
"Action": "execute-api:Invoke",
157+
"Effect": "Deny",
158+
"Resource": ["*"],
159+
}

0 commit comments

Comments
 (0)