Skip to content

Commit fbba662

Browse files
Add APIGatewayv2 Lambda Authorizer support in events (#399)
* add authorizer structs to apigw.go * Fix lint failures on "Id" * Change Context to a map[string]interface{} in IAMPolicyResponse * Edit apigw.go to resolve linting errors * Add nolint tags to "Arn" lint failures * add test data * add missing files Co-authored-by: Bryan Moffatt <[email protected]> Co-authored-by: Bryan Moffatt <[email protected]>
1 parent 1900022 commit fbba662

File tree

4 files changed

+194
-0
lines changed

4 files changed

+194
-0
lines changed

events/apigw.go

+50
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,50 @@ type APIGatewayV2HTTPRequestContextAuthenticationClientCertValidity struct {
232232
NotBefore string `json:"notBefore"`
233233
}
234234

235+
type APIGatewayV2CustomAuthorizerV1RequestTypeRequestContext struct {
236+
Path string `json:"path"`
237+
AccountID string `json:"accountId"`
238+
ResourceID string `json:"resourceId"`
239+
Stage string `json:"stage"`
240+
RequestID string `json:"requestId"`
241+
Identity APIGatewayCustomAuthorizerRequestTypeRequestIdentity `json:"identity"`
242+
ResourcePath string `json:"resourcePath"`
243+
HTTPMethod string `json:"httpMethod"`
244+
APIID string `json:"apiId"`
245+
}
246+
247+
type APIGatewayV2CustomAuthorizerV1Request struct {
248+
Version string `json:"version"`
249+
Type string `json:"type"`
250+
MethodArn string `json:"methodArn"` //nolint: stylecheck
251+
IdentitySource string `json:"identitySource"`
252+
AuthorizationToken string `json:"authorizationToken"`
253+
Resource string `json:"resource"`
254+
Path string `json:"path"`
255+
HTTPMethod string `json:"httpMethod"`
256+
Headers map[string]string `json:"headers"`
257+
QueryStringParameters map[string]string `json:"queryStringParameters"`
258+
PathParameters map[string]string `json:"pathParameters"`
259+
StageVariables map[string]string `json:"stageVariables"`
260+
RequestContext APIGatewayV2CustomAuthorizerV1RequestTypeRequestContext `json:"requestContext"`
261+
}
262+
263+
type APIGatewayV2CustomAuthorizerV2Request struct {
264+
Version string `json:"version"`
265+
Type string `json:"type"`
266+
RouteArn string `json:"routeArn"` //nolint: stylecheck
267+
IdentitySource []string `json:"identitySource"`
268+
RouteKey string `json:"routeKey"`
269+
RawPath string `json:"rawPath"`
270+
RawQueryString string `json:"rawQueryString"`
271+
Cookies []string `json:"cookies"`
272+
Headers map[string]string `json:"headers"`
273+
QueryStringParameters map[string]string `json:"queryStringParameters"`
274+
RequestContext APIGatewayV2HTTPRequestContext `json:"requestContext"`
275+
PathParameters map[string]string `json:"pathParameters"`
276+
StageVariables map[string]string `json:"stageVariables"`
277+
}
278+
235279
// APIGatewayCustomAuthorizerContext represents the expected format of an API Gateway custom authorizer response.
236280
// Deprecated. Code should be updated to use the Authorizer map from APIGatewayRequestIdentity. Ex: Authorizer["principalId"]
237281
type APIGatewayCustomAuthorizerContext struct {
@@ -291,6 +335,12 @@ type APIGatewayV2CustomAuthorizerSimpleResponse struct {
291335
Context map[string]interface{} `json:"context,omitempty"`
292336
}
293337

338+
type APIGatewayV2CustomAuthorizerIAMPolicyResponse struct {
339+
PrincipalID string `json:"principalId"`
340+
PolicyDocument APIGatewayCustomAuthorizerPolicy `json:"policyDocument"`
341+
Context map[string]interface{} `json:"context,omitempty"`
342+
}
343+
294344
// APIGatewayCustomAuthorizerPolicy represents an IAM policy
295345
type APIGatewayCustomAuthorizerPolicy struct {
296346
Version string

events/apigw_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,45 @@ func TestApiGatewayV2HTTPRequestNoAuthorizerMarshaling(t *testing.T) {
434434

435435
assert.JSONEq(t, string(inputJSON), string(outputJSON))
436436
}
437+
438+
func TestApiGatewayV2CustomAuthorizerV1RequestMarshaling(t *testing.T) {
439+
inputJSON, err := ioutil.ReadFile("./testdata/apigw-v2-custom-authorizer-v1-request.json")
440+
if err != nil {
441+
t.Errorf("could not open test file. details: %v", err)
442+
}
443+
444+
// de-serialize into Go object
445+
var inputEvent APIGatewayV2CustomAuthorizerV1Request
446+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
447+
t.Errorf("could not unmarshal event. details: %v", err)
448+
}
449+
450+
// serialize to json
451+
outputJSON, err := json.Marshal(inputEvent)
452+
if err != nil {
453+
t.Errorf("could not marshal event. details: %v", err)
454+
}
455+
456+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
457+
}
458+
459+
func TestApiGatewayV2CustomAuthorizerV2RequestMarshaling(t *testing.T) {
460+
inputJSON, err := ioutil.ReadFile("./testdata/apigw-v2-custom-authorizer-v2-request.json")
461+
if err != nil {
462+
t.Errorf("could not open test file. details: %v", err)
463+
}
464+
465+
// de-serialize into Go object
466+
var inputEvent APIGatewayV2CustomAuthorizerV2Request
467+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
468+
t.Errorf("could not unmarshal event. details: %v", err)
469+
}
470+
471+
// serialize to json
472+
outputJSON, err := json.Marshal(inputEvent)
473+
if err != nil {
474+
t.Errorf("could not marshal event. details: %v", err)
475+
}
476+
477+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
478+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"version": "1.0",
3+
"type": "REQUEST",
4+
"methodArn": "arn:aws:execute-api:us-east-1:123456789012:abcdef123/test/GET/request",
5+
"identitySource": "user1,123",
6+
"authorizationToken": "user1,123",
7+
"resource": "/request",
8+
"path": "/request",
9+
"httpMethod": "GET",
10+
"headers": {
11+
"X-AMZ-Date": "20170718T062915Z",
12+
"Accept": "*/*",
13+
"HeaderAuth1": "headerValue1",
14+
"CloudFront-Viewer-Country": "US",
15+
"CloudFront-Forwarded-Proto": "https",
16+
"CloudFront-Is-Tablet-Viewer": "false",
17+
"CloudFront-Is-Mobile-Viewer": "false",
18+
"User-Agent": "..."
19+
},
20+
"queryStringParameters": {
21+
"QueryString1": "queryValue1"
22+
},
23+
"pathParameters": {},
24+
"stageVariables": {
25+
"StageVar1": "stageValue1"
26+
},
27+
"requestContext": {
28+
"path": "/request",
29+
"accountId": "123456789012",
30+
"resourceId": "05c7jb",
31+
"stage": "test",
32+
"requestId": "...",
33+
"identity": {
34+
"apiKey": "...",
35+
"sourceIp": "...",
36+
"clientCert": {
37+
"clientCertPem": "CERT_CONTENT",
38+
"subjectDN": "www.example.com",
39+
"issuerDN": "Example issuer",
40+
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1",
41+
"validity": {
42+
"notBefore": "May 28 12:30:02 2019 GMT",
43+
"notAfter": "Aug 5 09:36:04 2021 GMT"
44+
}
45+
}
46+
},
47+
"resourcePath": "/request",
48+
"httpMethod": "GET",
49+
"apiId": "abcdef123"
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"version": "2.0",
3+
"type": "REQUEST",
4+
"routeArn": "arn:aws:execute-api:us-east-1:123456789012:abcdef123/test/GET/request",
5+
"identitySource": ["user1", "123"],
6+
"routeKey": "$default",
7+
"rawPath": "/my/path",
8+
"rawQueryString": "parameter1=value1&parameter1=value2&parameter2=value",
9+
"cookies": ["cookie1", "cookie2"],
10+
"headers": {
11+
"Header1": "value1",
12+
"Header2": "value2"
13+
},
14+
"queryStringParameters": {
15+
"parameter1": "value1,value2",
16+
"parameter2": "value"
17+
},
18+
"requestContext": {
19+
"accountId": "123456789012",
20+
"apiId": "api-id",
21+
"authentication": {
22+
"clientCert": {
23+
"clientCertPem": "CERT_CONTENT",
24+
"subjectDN": "www.example.com",
25+
"issuerDN": "Example issuer",
26+
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1",
27+
"validity": {
28+
"notBefore": "May 28 12:30:02 2019 GMT",
29+
"notAfter": "Aug 5 09:36:04 2021 GMT"
30+
}
31+
}
32+
},
33+
"domainName": "id.execute-api.us-east-1.amazonaws.com",
34+
"domainPrefix": "id",
35+
"http": {
36+
"method": "POST",
37+
"path": "/my/path",
38+
"protocol": "HTTP/1.1",
39+
"sourceIp": "IP",
40+
"userAgent": "agent"
41+
},
42+
"requestId": "id",
43+
"routeKey": "$default",
44+
"stage": "$default",
45+
"time": "12/Mar/2020:19:03:58 +0000",
46+
"timeEpoch": 1583348638390
47+
},
48+
"pathParameters": { "parameter1": "value1" },
49+
"stageVariables": { "stageVariable1": "value1", "stageVariable2": "value2" }
50+
}
51+

0 commit comments

Comments
 (0)