Skip to content

Commit a3eac28

Browse files
committed
docs(authorizer): fix typos, make it more concise
1 parent d02dd2e commit a3eac28

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

docs/utilities/data_classes.md

+38-30
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,17 @@ Event Source | Data_class
8787

8888
> New in 1.20.0
8989
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".
90+
It is used for [API Gateway Rest API Lambda Authorizer payload](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html){target="_blank"}.
9491

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`.
92+
Use **`APIGatewayAuthorizerRequestEvent`** for type `REQUEST` and **`APIGatewayAuthorizerTokenEvent`** for type `TOKEN`.
9993

10094
=== "app_type_request.py"
10195

102-
```python
96+
This example uses the `APIGatewayAuthorizerResponse` to decline a given request if the user is not found.
97+
98+
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.
99+
100+
```python hl_lines="2-5 26-31 36-37 40 44 46"
103101
from aws_lambda_powertools.utilities.data_classes import event_source
104102
from aws_lambda_powertools.utilities.data_classes.api_gateway_authorizer_event import (
105103
APIGatewayAuthorizerRequestEvent,
@@ -125,27 +123,33 @@ the user details in the request context and full access for admin users. And ano
125123
# parse the `methodArn` as an `APIGatewayRouteArn`
126124
arn = event.parsed_arn
127125
# Create the response builder from parts of the `methodArn`
128-
builder = APIGatewayAuthorizerResponse("user", arn.region, arn.aws_account_id, arn.api_id, arn.stage)
126+
policy = APIGatewayAuthorizerResponse(
127+
principal_id="user",
128+
region=arn.region,
129+
aws_account_id=arn.aws_account_id,
130+
api_id=arn.api_id,
131+
stage=arn.stage
132+
)
129133

130134
if user is None:
131135
# No user was found, so we return not authorized
132-
builder.deny_all_routes()
133-
return builder.asdict()
136+
policy.deny_all_routes()
137+
return policy.asdict()
134138

135139
# Found the user and setting the details in the context
136-
builder.context = user
140+
policy.context = user
137141

138142
# Conditional IAM Policy
139143
if user.get("isAdmin", False):
140-
builder.allow_all_routes()
144+
policy.allow_all_routes()
141145
else:
142-
builder.allow_route(HttpVerb.GET, "/user-profile")
146+
policy.allow_route(HttpVerb.GET, "/user-profile")
143147

144-
return builder.asdict()
148+
return policy.asdict()
145149
```
146150
=== "app_type_token.py"
147151

148-
```python
152+
```python hl_lines="2-5 12-18 21 23-24"
149153
from aws_lambda_powertools.utilities.data_classes import event_source
150154
from aws_lambda_powertools.utilities.data_classes.api_gateway_authorizer_event import (
151155
APIGatewayAuthorizerTokenEvent,
@@ -156,30 +160,34 @@ the user details in the request context and full access for admin users. And ano
156160
@event_source(data_class=APIGatewayAuthorizerTokenEvent)
157161
def handler(event: APIGatewayAuthorizerTokenEvent, context):
158162
arn = event.parsed_arn
159-
builder = APIGatewayAuthorizerResponse("user", arn.region, arn.aws_account_id, arn.api_id, arn.stage)
163+
164+
policy = APIGatewayAuthorizerResponse(
165+
principal_id="user",
166+
region=arn.region,
167+
aws_account_id=arn.aws_account_id,
168+
api_id=arn.api_id,
169+
stage=arn.stage
170+
)
171+
160172
if event.authorization_token == "42":
161-
builder.allow_all_methods()
173+
policy.allow_all_methods()
162174
else:
163-
builder.deny_all_methods()
164-
return builder.asdict()
175+
policy.deny_all_methods()
176+
return policy.asdict()
165177
```
166178

167179
### API Gateway Authorizer V2
168180

169181
> New in 1.20.0
170182
171-
It is used for API Gateway HTTP API lambda authorizer payload version 2. See blog post
172-
[Introducing IAM and Lambda authorizers for Amazon API Gateway HTTP APIs](https://aws.amazon.com/blogs/compute/introducing-iam-and-lambda-authorizers-for-amazon-api-gateway-http-apis/){target="_blank"}
173-
or [Working with AWS Lambda authorizers for HTTP APIs](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-lambda-authorizer.html){target="_blank"}
174-
for more details
175-
176-
Below is a simple example of an HTTP API lambda authorizer looking up user details by `x-token` header and using
177-
`APIGatewayAuthorizerResponseV2` to return the declined response when user is not found or authorized and include
178-
the user details in the request context.
183+
It is used for [API Gateway HTTP API Lambda Authorizer payload version 2](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-lambda-authorizer.html){target="_blank"}.
184+
See also [this blog post](https://aws.amazon.com/blogs/compute/introducing-iam-and-lambda-authorizers-for-amazon-api-gateway-http-apis/){target="_blank"} for more details.
179185

180186
=== "app.py"
181187

182-
```python
188+
This example looks up user details via `x-token` header. It uses `APIGatewayAuthorizerResponseV2` to return a deny policy when user is not found or authorized.
189+
190+
```python hl_lines="2-5 21 24"
183191
from aws_lambda_powertools.utilities.data_classes import event_source
184192
from aws_lambda_powertools.utilities.data_classes.api_gateway_authorizer_event import (
185193
APIGatewayAuthorizerEventV2,

0 commit comments

Comments
 (0)