Skip to content

Commit 7d557f4

Browse files
author
Michael Brewer
committed
chore: add more docs
1 parent e450bfe commit 7d557f4

File tree

3 files changed

+88
-3
lines changed

3 files changed

+88
-3
lines changed

aws_lambda_powertools/utilities/data_classes/api_gateway_authorizer_event.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ def allow_all_routes(self):
437437
"""Adds a '*' allow to the policy to authorize access to all methods of an API"""
438438
self._add_route("Allow", HttpVerb.ALL, "*", [])
439439

440-
def deny_all_route(self):
440+
def deny_all_routes(self):
441441
"""Adds a '*' allow to the policy to deny access to all methods of an API"""
442442
self._add_route("Deny", HttpVerb.ALL, "*", [])
443443

docs/utilities/data_classes.md

+86-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Same example as above, but using the `event_source` decorator
5959

6060
Event Source | Data_class
6161
------------------------------------------------- | ---------------------------------------------------------------------------------
62+
[API Gateway Authorizer](#api-gateway-authorizer) | `APIGatewayAuthorizerRequestEvent`
6263
[API Gateway Authorizer V2](#api-gateway-authorizer-v2) | `APIGatewayAuthorizerEventV2`
6364
[API Gateway Proxy](#api-gateway-proxy) | `APIGatewayProxyEvent`
6465
[API Gateway Proxy V2](#api-gateway-proxy-v2) | `APIGatewayProxyEventV2`
@@ -82,6 +83,87 @@ Event Source | Data_class
8283
The examples provided below are far from exhaustive - the data classes themselves are designed to provide a form of
8384
documentation inherently (via autocompletion, types and docstrings).
8485

86+
### API Gateway Authorizer
87+
88+
> New in 1.20.0
89+
90+
It is used for API Gateway Rest API lambda authorizer payload. See docs on
91+
[Use API Gateway Lambda authorizers](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html){target="_blank"}
92+
for more details. Use `APIGatewayAuthorizerRequestEvent` for type "REQUEST" and `APIGatewayAuthorizerTokenEvent` for
93+
type "TOKEN".
94+
95+
Below is 2 examples of a Rest API lambda authorizer. One looking up user details by `Authorization` header and using
96+
`APIGatewayAuthorizerResponse` to return the declined response when user is not found or authorized and include
97+
the user details in the request context and full access for admin users. And another using
98+
`APIGatewayAuthorizerTokenEvent` to get the `authorization_token`.
99+
100+
=== "app_type_request.py"
101+
102+
```python
103+
from aws_lambda_powertools.utilities.data_classes import event_source
104+
from aws_lambda_powertools.utilities.data_classes.api_gateway_authorizer_event import (
105+
APIGatewayAuthorizerRequestEvent,
106+
APIGatewayAuthorizerResponse,
107+
HttpVerb,
108+
)
109+
from secrets import compare_digest
110+
111+
112+
def get_user_by_token(token):
113+
if compare_digest(token, "admin-foo"):
114+
return {"isAdmin": True, "name": "Admin"}
115+
elif compare_digest(token, "regular-foo"):
116+
return {"name": "Joe"}
117+
else:
118+
return None
119+
120+
121+
@event_source(data_class=APIGatewayAuthorizerRequestEvent)
122+
def handler(event: APIGatewayAuthorizerRequestEvent, context):
123+
user = get_user_by_token(event.get_header_value("Authorization"))
124+
125+
# parse the `methodArn` as an `APIGatewayRouteArn`
126+
arn = event.parsed_arn
127+
# Create the response builder from parts of the `methodArn`
128+
builder = APIGatewayAuthorizerResponse("user", arn.region, arn.aws_account_id, arn.api_id, arn.stage)
129+
130+
if user is None:
131+
# No user was found, so we return not authorized
132+
builder.deny_all_routes()
133+
return builder.asdict()
134+
135+
# Found the user and setting the details in the context
136+
builder.context = user
137+
138+
# Conditional IAM Policy
139+
if user.get("isAdmin", False):
140+
builder.allow_all_routes()
141+
else:
142+
builder.allow_route(HttpVerb.GET, "/user-profile")
143+
144+
return builder.asdict()
145+
```
146+
=== "app_type_token.py"
147+
148+
```python
149+
from aws_lambda_powertools.utilities.data_classes import event_source
150+
from aws_lambda_powertools.utilities.data_classes.api_gateway_authorizer_event import (
151+
APIGatewayAuthorizerTokenEvent,
152+
APIGatewayAuthorizerResponse,
153+
)
154+
155+
156+
@event_source(data_class=APIGatewayAuthorizerTokenEvent)
157+
def handler(event: APIGatewayAuthorizerTokenEvent, context):
158+
arn = event.parsed_arn
159+
builder = APIGatewayAuthorizerResponse("user", arn.region, arn.aws_account_id, arn.api_id, arn.stage)
160+
if event.authorization_token == "42":
161+
builder.allow_all_methods()
162+
else:
163+
builder.deny_all_methods()
164+
return builder.asdict()
165+
```
166+
85167
### API Gateway Authorizer V2
86168

87169
> New in 1.20.0
@@ -103,10 +185,13 @@ the user details in the request context.
103185
APIGatewayAuthorizerEventV2,
104186
APIGatewayAuthorizerResponseV2,
105187
)
188+
from secrets import compare_digest
106189

107190

108191
def get_user_by_token(token):
109-
...
192+
if compare_digest(token, "Foo"):
193+
return {"name": "Foo"}
194+
return None
110195

111196

112197
@event_source(data_class=APIGatewayAuthorizerEventV2)

tests/functional/data_classes/test_api_gateway_authorizer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def test_authorizer_response_allow_all_routes_with_context():
6161

6262

6363
def test_authorizer_response_deny_all_routes(builder: APIGatewayAuthorizerResponse):
64-
builder.deny_all_route()
64+
builder.deny_all_routes()
6565
assert builder.asdict() == {
6666
"principalId": "foo",
6767
"policyDocument": {

0 commit comments

Comments
 (0)