Skip to content

Commit f24acb2

Browse files
authored
Update apigatewayv2 request context (#328)
* Update apigatewayv2 request context * Fix naming
1 parent 6b93d48 commit f24acb2

5 files changed

+227
-3
lines changed

events/apigw.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ type APIGatewayV2HTTPRequestContext struct {
8080

8181
// APIGatewayV2HTTPRequestContextAuthorizerDescription contains authorizer information for the request context.
8282
type APIGatewayV2HTTPRequestContextAuthorizerDescription struct {
83-
JWT APIGatewayV2HTTPRequestContextAuthorizerJWTDescription `json:"jwt"`
83+
JWT *APIGatewayV2HTTPRequestContextAuthorizerJWTDescription `json:"jwt,omitempty"`
84+
Lambda map[string]interface{} `json:"lambda,omitempty"`
85+
IAM *APIGatewayV2HTTPRequestContextAuthorizerIAMDescription `json:"iam,omitempty"`
8486
}
8587

8688
// APIGatewayV2HTTPRequestContextAuthorizerJWTDescription contains JWT authorizer information for the request context.
@@ -89,6 +91,24 @@ type APIGatewayV2HTTPRequestContextAuthorizerJWTDescription struct {
8991
Scopes []string `json:"scopes,omitempty"`
9092
}
9193

94+
// APIGatewayV2HTTPRequestContextAuthorizerIAMDescription contains IAM information for the request context.
95+
type APIGatewayV2HTTPRequestContextAuthorizerIAMDescription struct {
96+
AccessKey string `json:"accessKey"`
97+
AccountID string `json:"accountId"`
98+
CallerID string `json:"callerId"`
99+
CognitoIdentity APIGatewayV2HTTPRequestContextAuthorizerCognitoIdentity `json:"cognitoIdentity,omitempty"`
100+
PrincipalOrgID string `json:"principalOrgId"`
101+
UserARN string `json:"userArn"`
102+
UserID string `json:"userId"`
103+
}
104+
105+
// APIGatewayV2HTTPRequestContextAuthorizerCognitoIdentity contains Cognito identity information for the request context.
106+
type APIGatewayV2HTTPRequestContextAuthorizerCognitoIdentity struct {
107+
AMR []string `json:"amr"`
108+
IdentityID string `json:"identityId"`
109+
IdentityPoolID string `json:"identityPoolId"`
110+
}
111+
92112
// APIGatewayV2HTTPRequestContextHTTPDescription contains HTTP information for the request context.
93113
type APIGatewayV2HTTPRequestContextHTTPDescription struct {
94114
Method string `json:"method"`

events/apigw_test.go

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,10 @@ func TestApiGatewayRestApiOpenApiRequestMarshaling(t *testing.T) {
210210
assert.JSONEq(t, string(inputJSON), string(outputJSON))
211211
}
212212

213-
func TestApiGatewayV2HTTPRequestMarshaling(t *testing.T) {
213+
func TestApiGatewayV2HTTPRequestJWTAuthorizerMarshaling(t *testing.T) {
214214

215215
// read json from file
216-
inputJSON, err := ioutil.ReadFile("./testdata/apigw-v2-request.json")
216+
inputJSON, err := ioutil.ReadFile("./testdata/apigw-v2-request-jwt-authorizer.json")
217217
if err != nil {
218218
t.Errorf("could not open test file. details: %v", err)
219219
}
@@ -245,6 +245,100 @@ func TestApiGatewayV2HTTPRequestMarshaling(t *testing.T) {
245245
assert.JSONEq(t, string(inputJSON), string(outputJSON))
246246
}
247247

248+
func TestApiGatewayV2HTTPRequestLambdaAuthorizerMarshaling(t *testing.T) {
249+
250+
// read json from file
251+
inputJSON, err := ioutil.ReadFile("./testdata/apigw-v2-request-lambda-authorizer.json")
252+
if err != nil {
253+
t.Errorf("could not open test file. details: %v", err)
254+
}
255+
256+
// de-serialize into Go object
257+
var inputEvent APIGatewayV2HTTPRequest
258+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
259+
t.Errorf("could not unmarshal event. details: %v", err)
260+
}
261+
262+
// validate custom authorizer context
263+
authContext := inputEvent.RequestContext.Authorizer
264+
if authContext.Lambda["key"] != "value" {
265+
t.Errorf("could not extract authorizer information from Lambda authorizer: %v", authContext)
266+
}
267+
268+
// validate HTTP details
269+
http := inputEvent.RequestContext.HTTP
270+
if http.Path != "/my/path" {
271+
t.Errorf("could not extract HTTP details: %v", http)
272+
}
273+
274+
// serialize to json
275+
outputJSON, err := json.Marshal(inputEvent)
276+
if err != nil {
277+
t.Errorf("could not marshal event. details: %v", err)
278+
}
279+
280+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
281+
}
282+
283+
func TestApiGatewayV2HTTPRequestIAMMarshaling(t *testing.T) {
284+
285+
// read json from file
286+
inputJSON, err := ioutil.ReadFile("./testdata/apigw-v2-request-iam.json")
287+
if err != nil {
288+
t.Errorf("could not open test file. details: %v", err)
289+
}
290+
291+
// de-serialize into Go object
292+
var inputEvent APIGatewayV2HTTPRequest
293+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
294+
t.Errorf("could not unmarshal event. details: %v", err)
295+
}
296+
297+
// validate custom authorizer context
298+
authContext := inputEvent.RequestContext.Authorizer
299+
if authContext.IAM.AccessKey != "ARIA2ZJZYVUEREEIHAKY" {
300+
t.Errorf("could not extract accessKey from IAM authorizer: %v", authContext)
301+
}
302+
if authContext.IAM.AccountID != "1234567890" {
303+
t.Errorf("could not extract accountId from IAM authorizer: %v", authContext)
304+
}
305+
if authContext.IAM.CallerID != "AROA7ZJZYVRE7C3DUXHH6:CognitoIdentityCredentials" {
306+
t.Errorf("could not extract callerId from IAM authorizer: %v", authContext)
307+
}
308+
if authContext.IAM.CognitoIdentity.AMR[0] != "foo" {
309+
t.Errorf("could not extract amr from CognitoIdentity: %v", authContext)
310+
}
311+
if authContext.IAM.CognitoIdentity.IdentityID != "us-east-1:3f291106-8703-466b-8f2b-3ecee1ca56ce" {
312+
t.Errorf("could not extract identityId from CognitoIdentity: %v", authContext)
313+
}
314+
if authContext.IAM.CognitoIdentity.IdentityPoolID != "us-east-1:4f291106-8703-466b-8f2b-3ecee1ca56ce" {
315+
t.Errorf("could not extract identityPoolId from CognitoIdentity: %v", authContext)
316+
}
317+
if authContext.IAM.PrincipalOrgID != "AwsOrgId" {
318+
t.Errorf("could not extract principalOrgId from IAM authorizer: %v", authContext)
319+
}
320+
if authContext.IAM.UserARN != "arn:aws:iam::1234567890:user/Admin" {
321+
t.Errorf("could not extract userArn from IAM authorizer: %v", authContext)
322+
}
323+
if authContext.IAM.UserID != "AROA2ZJZYVRE7Y3TUXHH6" {
324+
t.Errorf("could not extract userId from IAM authorizer: %v", authContext)
325+
}
326+
327+
// validate HTTP details
328+
http := inputEvent.RequestContext.HTTP
329+
if http.Path != "/my/path" {
330+
t.Errorf("could not extract HTTP details: %v", http)
331+
}
332+
333+
// serialize to json
334+
outputJSON, err := json.Marshal(inputEvent)
335+
if err != nil {
336+
t.Errorf("could not marshal event. details: %v", err)
337+
}
338+
339+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
340+
}
341+
248342
func TestApiGatewayV2HTTPRequestNoAuthorizerMarshaling(t *testing.T) {
249343

250344
// read json from file
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"version": "2.0",
3+
"routeKey": "$default",
4+
"rawPath": "/my/path",
5+
"rawQueryString": "parameter1=value1&parameter1=value2&parameter2=value",
6+
"cookies": [
7+
"cookie1",
8+
"cookie2"
9+
],
10+
"headers": {
11+
"Header1": "value1",
12+
"Header2": "value2"
13+
},
14+
"queryStringParameters": {
15+
"parameter1": "value1,value2",
16+
"parameter2": "value"
17+
},
18+
"pathParameters": {
19+
"proxy": "hello/world"
20+
},
21+
"requestContext": {
22+
"routeKey": "$default",
23+
"accountId": "123456789012",
24+
"stage": "$default",
25+
"requestId": "id",
26+
"authorizer": {
27+
"iam": {
28+
"accessKey": "ARIA2ZJZYVUEREEIHAKY",
29+
"accountId": "1234567890",
30+
"callerId": "AROA7ZJZYVRE7C3DUXHH6:CognitoIdentityCredentials",
31+
"cognitoIdentity": {
32+
"amr" : ["foo"],
33+
"identityId": "us-east-1:3f291106-8703-466b-8f2b-3ecee1ca56ce",
34+
"identityPoolId": "us-east-1:4f291106-8703-466b-8f2b-3ecee1ca56ce"
35+
},
36+
"principalOrgId": "AwsOrgId",
37+
"userArn": "arn:aws:iam::1234567890:user/Admin",
38+
"userId": "AROA2ZJZYVRE7Y3TUXHH6"
39+
}
40+
},
41+
"apiId": "api-id",
42+
"domainName": "id.execute-api.us-east-1.amazonaws.com",
43+
"domainPrefix": "id",
44+
"time": "12/Mar/2020:19:03:58+0000",
45+
"timeEpoch": 1583348638390,
46+
"http": {
47+
"method": "GET",
48+
"path": "/my/path",
49+
"protocol": "HTTP/1.1",
50+
"sourceIp": "IP",
51+
"userAgent": "agent"
52+
}
53+
},
54+
"stageVariables": {
55+
"stageVariable1": "value1",
56+
"stageVariable2": "value2"
57+
},
58+
"body": "{\r\n\t\"a\": 1\r\n}",
59+
"isBase64Encoded": false
60+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"version": "2.0",
3+
"routeKey": "$default",
4+
"rawPath": "/my/path",
5+
"rawQueryString": "parameter1=value1&parameter1=value2&parameter2=value",
6+
"cookies": [
7+
"cookie1",
8+
"cookie2"
9+
],
10+
"headers": {
11+
"Header1": "value1",
12+
"Header2": "value2"
13+
},
14+
"queryStringParameters": {
15+
"parameter1": "value1,value2",
16+
"parameter2": "value"
17+
},
18+
"pathParameters": {
19+
"proxy": "hello/world"
20+
},
21+
"requestContext": {
22+
"routeKey": "$default",
23+
"accountId": "123456789012",
24+
"stage": "$default",
25+
"requestId": "id",
26+
"authorizer": {
27+
"lambda": {
28+
"key": "value"
29+
}
30+
},
31+
"apiId": "api-id",
32+
"domainName": "id.execute-api.us-east-1.amazonaws.com",
33+
"domainPrefix": "id",
34+
"time": "12/Mar/2020:19:03:58+0000",
35+
"timeEpoch": 1583348638390,
36+
"http": {
37+
"method": "GET",
38+
"path": "/my/path",
39+
"protocol": "HTTP/1.1",
40+
"sourceIp": "IP",
41+
"userAgent": "agent"
42+
}
43+
},
44+
"stageVariables": {
45+
"stageVariable1": "value1",
46+
"stageVariable2": "value2"
47+
},
48+
"body": "{\r\n\t\"a\": 1\r\n}",
49+
"isBase64Encoded": false
50+
}

0 commit comments

Comments
 (0)