Skip to content

Commit 4d19ede

Browse files
Adding new class to WebSocket Authorizer
1 parent 835943c commit 4d19ede

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

docs/utilities/data_classes.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,18 @@ It is used for [API Gateway Rest API Lambda Authorizer payload](https://docs.aws
131131

132132
Use **`APIGatewayAuthorizerRequestEvent`** for type `REQUEST` and **`APIGatewayAuthorizerTokenEvent`** for type `TOKEN`.
133133

134-
=== "app.py"
134+
=== "Rest APIs"
135135

136-
```python hl_lines="2-4 8"
136+
```python hl_lines="2-4 8 18"
137137
--8<-- "examples/event_sources/src/apigw_authorizer_request.py"
138138
```
139139

140+
=== "WebSocket APIs"
141+
142+
```python hl_lines="2-4 8 18"
143+
--8<-- "examples/event_sources/src/apigw_authorizer_request_websocket.py"
144+
```
145+
140146
=== "API Gateway Authorizer Request Example Event"
141147

142148
```json hl_lines="3 11"
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from aws_lambda_powertools.utilities.data_classes import event_source
2+
from aws_lambda_powertools.utilities.data_classes.api_gateway_authorizer_event import (
3+
APIGatewayAuthorizerRequestEvent,
4+
APIGatewayAuthorizerResponseWebSocket,
5+
)
6+
7+
8+
@event_source(data_class=APIGatewayAuthorizerRequestEvent)
9+
def lambda_handler(event: APIGatewayAuthorizerRequestEvent, context):
10+
# Simple auth check (replace with your actual auth logic)
11+
is_authorized = event.headers.get("HeaderAuth1") == "headerValue1"
12+
13+
if not is_authorized:
14+
return {"principalId": "", "policyDocument": {"Version": "2012-10-17", "Statement": []}}
15+
16+
arn = event.parsed_arn
17+
18+
policy = APIGatewayAuthorizerResponseWebSocket(
19+
principal_id="user",
20+
context={"user": "example"},
21+
region=arn.region,
22+
aws_account_id=arn.aws_account_id,
23+
api_id=arn.api_id,
24+
stage=arn.stage,
25+
)
26+
27+
policy.allow_all_routes()
28+
29+
return policy.asdict()

0 commit comments

Comments
 (0)