15
15
16
16
class APIGatewayEventAuthorizer (DictWrapper ):
17
17
@property
18
- def claims (self ) -> Optional [ Dict [str , Any ] ]:
19
- return self .get ("claims" )
18
+ def claims (self ) -> Dict [str , Any ]:
19
+ return self .get ("claims" ) or {} # key might exist but can be `null`
20
20
21
21
@property
22
- def scopes (self ) -> Optional [ List [str ] ]:
23
- return self .get ("scopes" )
22
+ def scopes (self ) -> List [str ]:
23
+ return self .get ("scopes" ) or [] # key might exist but can be `null`
24
24
25
25
@property
26
- def principal_id (self ) -> Optional [ str ] :
26
+ def principal_id (self ) -> str :
27
27
"""The principal user identification associated with the token sent by the client and returned from an
28
28
API Gateway Lambda authorizer (formerly known as a custom authorizer)"""
29
- return self .get ("principalId" )
29
+ return self .get ("principalId" ) or "" # key might exist but can be `null`
30
30
31
31
@property
32
32
def integration_latency (self ) -> Optional [int ]:
@@ -91,7 +91,8 @@ def route_key(self) -> Optional[str]:
91
91
92
92
@property
93
93
def authorizer (self ) -> APIGatewayEventAuthorizer :
94
- return APIGatewayEventAuthorizer (self ._data ["requestContext" ]["authorizer" ])
94
+ authz_data = self ._data .get ("requestContext" , {}).get ("authorizer" , {})
95
+ return APIGatewayEventAuthorizer (authz_data )
95
96
96
97
97
98
class APIGatewayProxyEvent (BaseProxyEvent ):
@@ -112,11 +113,11 @@ def resource(self) -> str:
112
113
113
114
@property
114
115
def multi_value_headers (self ) -> Dict [str , List [str ]]:
115
- return self .get ("multiValueHeaders" ) or {}
116
+ return self .get ("multiValueHeaders" ) or {} # key might exist but can be `null`
116
117
117
118
@property
118
119
def multi_value_query_string_parameters (self ) -> Dict [str , List [str ]]:
119
- return self .get ("multiValueQueryStringParameters" ) or {}
120
+ return self .get ("multiValueQueryStringParameters" ) or {} # key might exist but can be `null`
120
121
121
122
@property
122
123
def resolved_query_string_parameters (self ) -> Dict [str , List [str ]]:
@@ -154,72 +155,72 @@ def header_serializer(self) -> BaseHeadersSerializer:
154
155
155
156
class RequestContextV2AuthorizerIam (DictWrapper ):
156
157
@property
157
- def access_key (self ) -> Optional [ str ] :
158
+ def access_key (self ) -> str :
158
159
"""The IAM user access key associated with the request."""
159
- return self .get ("accessKey" )
160
+ return self .get ("accessKey" ) or "" # key might exist but can be `null`
160
161
161
162
@property
162
- def account_id (self ) -> Optional [ str ] :
163
+ def account_id (self ) -> str :
163
164
"""The AWS account ID associated with the request."""
164
- return self .get ("accountId" )
165
+ return self .get ("accountId" ) or "" # key might exist but can be `null`
165
166
166
167
@property
167
- def caller_id (self ) -> Optional [ str ] :
168
+ def caller_id (self ) -> str :
168
169
"""The principal identifier of the caller making the request."""
169
- return self .get ("callerId" )
170
+ return self .get ("callerId" ) or "" # key might exist but can be `null`
170
171
171
172
def _cognito_identity (self ) -> Dict :
172
- return self .get ("cognitoIdentity" , {} ) or {} # not available in FunctionURL
173
+ return self .get ("cognitoIdentity" ) or {} # not available in FunctionURL; key might exist but can be `null`
173
174
174
175
@property
175
- def cognito_amr (self ) -> Optional [ List [str ] ]:
176
+ def cognito_amr (self ) -> List [str ]:
176
177
"""This represents how the user was authenticated.
177
178
AMR stands for Authentication Methods References as per the openid spec"""
178
- return self ._cognito_identity ().get ("amr" )
179
+ return self ._cognito_identity ().get ("amr" , [] )
179
180
180
181
@property
181
- def cognito_identity_id (self ) -> Optional [ str ] :
182
+ def cognito_identity_id (self ) -> str :
182
183
"""The Amazon Cognito identity ID of the caller making the request.
183
184
Available only if the request was signed with Amazon Cognito credentials."""
184
- return self ._cognito_identity ().get ("identityId" )
185
+ return self ._cognito_identity ().get ("identityId" , "" )
185
186
186
187
@property
187
- def cognito_identity_pool_id (self ) -> Optional [ str ] :
188
+ def cognito_identity_pool_id (self ) -> str :
188
189
"""The Amazon Cognito identity pool ID of the caller making the request.
189
190
Available only if the request was signed with Amazon Cognito credentials."""
190
- return self ._cognito_identity ().get ("identityPoolId" )
191
+ return self ._cognito_identity ().get ("identityPoolId" ) or "" # key might exist but can be `null`
191
192
192
193
@property
193
- def principal_org_id (self ) -> Optional [ str ] :
194
+ def principal_org_id (self ) -> str :
194
195
"""The AWS organization ID."""
195
- return self .get ("principalOrgId" )
196
+ return self .get ("principalOrgId" ) or "" # key might exist but can be `null`
196
197
197
198
@property
198
- def user_arn (self ) -> Optional [ str ] :
199
+ def user_arn (self ) -> str :
199
200
"""The Amazon Resource Name (ARN) of the effective user identified after authentication."""
200
- return self .get ("userArn" )
201
+ return self .get ("userArn" ) or "" # key might exist but can be `null`
201
202
202
203
@property
203
- def user_id (self ) -> Optional [ str ] :
204
+ def user_id (self ) -> str :
204
205
"""The IAM user ID of the effective user identified after authentication."""
205
- return self .get ("userId" )
206
+ return self .get ("userId" ) or "" # key might exist but can be `null`
206
207
207
208
208
209
class RequestContextV2Authorizer (DictWrapper ):
209
210
@property
210
- def jwt_claim (self ) -> Optional [ Dict [str , Any ] ]:
211
- jwt = self .get ("jwt" ) or {} # not available in FunctionURL
212
- return jwt .get ("claims" )
211
+ def jwt_claim (self ) -> Dict [str , Any ]:
212
+ jwt = self .get ("jwt" ) or {} # not available in FunctionURL; key might exist but can be `null`
213
+ return jwt .get ("claims" ) or {} # key might exist but can be `null`
213
214
214
215
@property
215
- def jwt_scopes (self ) -> Optional [ List [str ] ]:
216
- jwt = self .get ("jwt" ) or {} # not available in FunctionURL
217
- return jwt .get ("scopes" )
216
+ def jwt_scopes (self ) -> List [str ]:
217
+ jwt = self .get ("jwt" ) or {} # not available in FunctionURL; key might exist but can be `null`
218
+ return jwt .get ("scopes" , [] )
218
219
219
220
@property
220
- def get_lambda (self ) -> Optional [ Dict [str , Any ] ]:
221
+ def get_lambda (self ) -> Dict [str , Any ]:
221
222
"""Lambda authorization context details"""
222
- return self .get ("lambda" )
223
+ return self .get ("lambda" ) or {} # key might exist but can be `null`
223
224
224
225
def get_context (self ) -> Dict [str , Any ]:
225
226
"""Retrieve the authorization context details injected by a Lambda Authorizer.
@@ -238,20 +239,20 @@ def get_context(self) -> Dict[str, Any]:
238
239
Dict[str, Any]
239
240
A dictionary containing Lambda authorization context details.
240
241
"""
241
- return self .get ( "lambda" , {}) or {}
242
+ return self .get_lambda
242
243
243
244
@property
244
- def iam (self ) -> Optional [ RequestContextV2AuthorizerIam ] :
245
+ def iam (self ) -> RequestContextV2AuthorizerIam :
245
246
"""IAM authorization details used for making the request."""
246
- iam = self .get ("iam" )
247
- return None if iam is None else RequestContextV2AuthorizerIam (iam )
247
+ iam = self .get ("iam" ) or {} # key might exist but can be `null`
248
+ return RequestContextV2AuthorizerIam (iam )
248
249
249
250
250
251
class RequestContextV2 (BaseRequestContextV2 ):
251
252
@property
252
- def authorizer (self ) -> Optional [ RequestContextV2Authorizer ] :
253
- authorizer = self [ "requestContext" ] .get ("authorizer" )
254
- return None if authorizer is None else RequestContextV2Authorizer (authorizer )
253
+ def authorizer (self ) -> RequestContextV2Authorizer :
254
+ ctx = self .get ("requestContext" ) or {} # key might exist but can be `null`
255
+ return RequestContextV2Authorizer (ctx . get ( " authorizer" , {}) )
255
256
256
257
257
258
class APIGatewayProxyEventV2 (BaseProxyEvent ):
0 commit comments