Skip to content

Commit d02dd2e

Browse files
author
Michael Brewer
authored
feat(data-classes): authorizer for http api and rest api (#620)
1 parent 16563bb commit d02dd2e

11 files changed

+1347
-231
lines changed

Diff for: aws_lambda_powertools/utilities/data_classes/api_gateway_authorizer_event.py

+479
Large diffs are not rendered by default.

Diff for: aws_lambda_powertools/utilities/data_classes/api_gateway_proxy_event.py

+13-228
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,11 @@
11
from typing import Any, Dict, List, Optional
22

3-
from aws_lambda_powertools.utilities.data_classes.common import BaseProxyEvent, DictWrapper
4-
5-
6-
class APIGatewayEventIdentity(DictWrapper):
7-
@property
8-
def access_key(self) -> Optional[str]:
9-
return self["requestContext"]["identity"].get("accessKey")
10-
11-
@property
12-
def account_id(self) -> Optional[str]:
13-
"""The AWS account ID associated with the request."""
14-
return self["requestContext"]["identity"].get("accountId")
15-
16-
@property
17-
def api_key(self) -> Optional[str]:
18-
"""For API methods that require an API key, this variable is the API key associated with the method request.
19-
For methods that don't require an API key, this variable is null."""
20-
return self["requestContext"]["identity"].get("apiKey")
21-
22-
@property
23-
def api_key_id(self) -> Optional[str]:
24-
"""The API key ID associated with an API request that requires an API key."""
25-
return self["requestContext"]["identity"].get("apiKeyId")
26-
27-
@property
28-
def caller(self) -> Optional[str]:
29-
"""The principal identifier of the caller making the request."""
30-
return self["requestContext"]["identity"].get("caller")
31-
32-
@property
33-
def cognito_authentication_provider(self) -> Optional[str]:
34-
"""A comma-separated list of the Amazon Cognito authentication providers used by the caller
35-
making the request. Available only if the request was signed with Amazon Cognito credentials."""
36-
return self["requestContext"]["identity"].get("cognitoAuthenticationProvider")
37-
38-
@property
39-
def cognito_authentication_type(self) -> Optional[str]:
40-
"""The Amazon Cognito authentication type of the caller making the request.
41-
Available only if the request was signed with Amazon Cognito credentials."""
42-
return self["requestContext"]["identity"].get("cognitoAuthenticationType")
43-
44-
@property
45-
def cognito_identity_id(self) -> Optional[str]:
46-
"""The Amazon Cognito identity ID of the caller making the request.
47-
Available only if the request was signed with Amazon Cognito credentials."""
48-
return self["requestContext"]["identity"].get("cognitoIdentityId")
49-
50-
@property
51-
def cognito_identity_pool_id(self) -> Optional[str]:
52-
"""The Amazon Cognito identity pool ID of the caller making the request.
53-
Available only if the request was signed with Amazon Cognito credentials."""
54-
return self["requestContext"]["identity"].get("cognitoIdentityPoolId")
55-
56-
@property
57-
def principal_org_id(self) -> Optional[str]:
58-
"""The AWS organization ID."""
59-
return self["requestContext"]["identity"].get("principalOrgId")
60-
61-
@property
62-
def source_ip(self) -> str:
63-
"""The source IP address of the TCP connection making the request to API Gateway."""
64-
return self["requestContext"]["identity"]["sourceIp"]
65-
66-
@property
67-
def user(self) -> Optional[str]:
68-
"""The principal identifier of the user making the request."""
69-
return self["requestContext"]["identity"].get("user")
70-
71-
@property
72-
def user_agent(self) -> Optional[str]:
73-
"""The User Agent of the API caller."""
74-
return self["requestContext"]["identity"].get("userAgent")
75-
76-
@property
77-
def user_arn(self) -> Optional[str]:
78-
"""The Amazon Resource Name (ARN) of the effective user identified after authentication."""
79-
return self["requestContext"]["identity"].get("userArn")
3+
from aws_lambda_powertools.utilities.data_classes.common import (
4+
BaseProxyEvent,
5+
BaseRequestContext,
6+
BaseRequestContextV2,
7+
DictWrapper,
8+
)
809

8110

8211
class APIGatewayEventAuthorizer(DictWrapper):
@@ -89,21 +18,7 @@ def scopes(self) -> Optional[List[str]]:
8918
return self["requestContext"]["authorizer"].get("scopes")
9019

9120

92-
class APIGatewayEventRequestContext(DictWrapper):
93-
@property
94-
def account_id(self) -> str:
95-
"""The AWS account ID associated with the request."""
96-
return self["requestContext"]["accountId"]
97-
98-
@property
99-
def api_id(self) -> str:
100-
"""The identifier API Gateway assigns to your API."""
101-
return self["requestContext"]["apiId"]
102-
103-
@property
104-
def authorizer(self) -> APIGatewayEventAuthorizer:
105-
return APIGatewayEventAuthorizer(self._data)
106-
21+
class APIGatewayEventRequestContext(BaseRequestContext):
10722
@property
10823
def connected_at(self) -> Optional[int]:
10924
"""The Epoch-formatted connection time. (WebSocket API)"""
@@ -114,40 +29,11 @@ def connection_id(self) -> Optional[str]:
11429
"""A unique ID for the connection that can be used to make a callback to the client. (WebSocket API)"""
11530
return self["requestContext"].get("connectionId")
11631

117-
@property
118-
def domain_name(self) -> Optional[str]:
119-
"""A domain name"""
120-
return self["requestContext"].get("domainName")
121-
122-
@property
123-
def domain_prefix(self) -> Optional[str]:
124-
return self["requestContext"].get("domainPrefix")
125-
12632
@property
12733
def event_type(self) -> Optional[str]:
12834
"""The event type: `CONNECT`, `MESSAGE`, or `DISCONNECT`. (WebSocket API)"""
12935
return self["requestContext"].get("eventType")
13036

131-
@property
132-
def extended_request_id(self) -> Optional[str]:
133-
"""An automatically generated ID for the API call, which contains more useful information
134-
for debugging/troubleshooting."""
135-
return self["requestContext"].get("extendedRequestId")
136-
137-
@property
138-
def protocol(self) -> str:
139-
"""The request protocol, for example, HTTP/1.1."""
140-
return self["requestContext"]["protocol"]
141-
142-
@property
143-
def http_method(self) -> str:
144-
"""The HTTP method used. Valid values include: DELETE, GET, HEAD, OPTIONS, PATCH, POST, and PUT."""
145-
return self["requestContext"]["httpMethod"]
146-
147-
@property
148-
def identity(self) -> APIGatewayEventIdentity:
149-
return APIGatewayEventIdentity(self._data)
150-
15137
@property
15238
def message_direction(self) -> Optional[str]:
15339
"""Message direction (WebSocket API)"""
@@ -159,46 +45,18 @@ def message_id(self) -> Optional[str]:
15945
return self["requestContext"].get("messageId")
16046

16147
@property
162-
def path(self) -> str:
163-
return self["requestContext"]["path"]
164-
165-
@property
166-
def stage(self) -> str:
167-
"""The deployment stage of the API request"""
168-
return self["requestContext"]["stage"]
169-
170-
@property
171-
def request_id(self) -> str:
172-
"""The ID that API Gateway assigns to the API request."""
173-
return self["requestContext"]["requestId"]
174-
175-
@property
176-
def request_time(self) -> Optional[str]:
177-
"""The CLF-formatted request time (dd/MMM/yyyy:HH:mm:ss +-hhmm)"""
178-
return self["requestContext"].get("requestTime")
179-
180-
@property
181-
def request_time_epoch(self) -> int:
182-
"""The Epoch-formatted request time."""
183-
return self["requestContext"]["requestTimeEpoch"]
184-
185-
@property
186-
def resource_id(self) -> str:
187-
return self["requestContext"]["resourceId"]
188-
189-
@property
190-
def resource_path(self) -> str:
191-
return self["requestContext"]["resourcePath"]
48+
def operation_name(self) -> Optional[str]:
49+
"""The name of the operation being performed"""
50+
return self["requestContext"].get("operationName")
19251

19352
@property
19453
def route_key(self) -> Optional[str]:
19554
"""The selected route key."""
19655
return self["requestContext"].get("routeKey")
19756

19857
@property
199-
def operation_name(self) -> Optional[str]:
200-
"""The name of the operation being performed"""
201-
return self["requestContext"].get("operationName")
58+
def authorizer(self) -> APIGatewayEventAuthorizer:
59+
return APIGatewayEventAuthorizer(self._data)
20260

20361

20462
class APIGatewayProxyEvent(BaseProxyEvent):
@@ -238,31 +96,6 @@ def stage_variables(self) -> Optional[Dict[str, str]]:
23896
return self.get("stageVariables")
23997

24098

241-
class RequestContextV2Http(DictWrapper):
242-
@property
243-
def method(self) -> str:
244-
return self["requestContext"]["http"]["method"]
245-
246-
@property
247-
def path(self) -> str:
248-
return self["requestContext"]["http"]["path"]
249-
250-
@property
251-
def protocol(self) -> str:
252-
"""The request protocol, for example, HTTP/1.1."""
253-
return self["requestContext"]["http"]["protocol"]
254-
255-
@property
256-
def source_ip(self) -> str:
257-
"""The source IP address of the TCP connection making the request to API Gateway."""
258-
return self["requestContext"]["http"]["sourceIp"]
259-
260-
@property
261-
def user_agent(self) -> str:
262-
"""The User Agent of the API caller."""
263-
return self["requestContext"]["http"]["userAgent"]
264-
265-
26699
class RequestContextV2AuthorizerIam(DictWrapper):
267100
@property
268101
def access_key(self) -> Optional[str]:
@@ -334,60 +167,12 @@ def iam(self) -> Optional[RequestContextV2AuthorizerIam]:
334167
return None if iam is None else RequestContextV2AuthorizerIam(iam)
335168

336169

337-
class RequestContextV2(DictWrapper):
338-
@property
339-
def account_id(self) -> str:
340-
"""The AWS account ID associated with the request."""
341-
return self["requestContext"]["accountId"]
342-
343-
@property
344-
def api_id(self) -> str:
345-
"""The identifier API Gateway assigns to your API."""
346-
return self["requestContext"]["apiId"]
347-
170+
class RequestContextV2(BaseRequestContextV2):
348171
@property
349172
def authorizer(self) -> Optional[RequestContextV2Authorizer]:
350173
authorizer = self["requestContext"].get("authorizer")
351174
return None if authorizer is None else RequestContextV2Authorizer(authorizer)
352175

353-
@property
354-
def domain_name(self) -> str:
355-
"""A domain name"""
356-
return self["requestContext"]["domainName"]
357-
358-
@property
359-
def domain_prefix(self) -> str:
360-
return self["requestContext"]["domainPrefix"]
361-
362-
@property
363-
def http(self) -> RequestContextV2Http:
364-
return RequestContextV2Http(self._data)
365-
366-
@property
367-
def request_id(self) -> str:
368-
"""The ID that API Gateway assigns to the API request."""
369-
return self["requestContext"]["requestId"]
370-
371-
@property
372-
def route_key(self) -> str:
373-
"""The selected route key."""
374-
return self["requestContext"]["routeKey"]
375-
376-
@property
377-
def stage(self) -> str:
378-
"""The deployment stage of the API request"""
379-
return self["requestContext"]["stage"]
380-
381-
@property
382-
def time(self) -> str:
383-
"""The CLF-formatted request time (dd/MMM/yyyy:HH:mm:ss +-hhmm)."""
384-
return self["requestContext"]["time"]
385-
386-
@property
387-
def time_epoch(self) -> int:
388-
"""The Epoch-formatted request time."""
389-
return self["requestContext"]["timeEpoch"]
390-
391176

392177
class APIGatewayProxyEventV2(BaseProxyEvent):
393178
"""AWS Lambda proxy V2 event

0 commit comments

Comments
 (0)