Skip to content

Commit f9e86f1

Browse files
dnys1bmoffatt
andauthored
Add AppSync Lambda Authorizer Events (#393)
* Add AppSync Lambda Authorizer * Fix typo Co-authored-by: Bryan Moffatt <[email protected]>
1 parent 33465f7 commit f9e86f1

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

events/appsync.go

+25
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,28 @@ const (
3838
// OperationBatchInvoke instructs AWS AppSync to batch requests for the current GraphQL field
3939
OperationBatchInvoke AppSyncOperation = "BatchInvoke"
4040
)
41+
42+
// AppSyncLambdaAuthorizerRequest contains an authorization request from AppSync.
43+
type AppSyncLambdaAuthorizerRequest struct {
44+
AuthorizationToken string `json:"authorizationToken"`
45+
RequestContext AppSyncLambdaAuthorizerRequestContext `json:"requestContext"`
46+
}
47+
48+
// AppSyncLambdaAuthorizerRequestContext contains the parameters of the AppSync invocation which triggered
49+
// this authorization request.
50+
type AppSyncLambdaAuthorizerRequestContext struct {
51+
APIID string `json:"apiId"`
52+
AccountID string `json:"accountId"`
53+
RequestID string `json:"requestId"`
54+
QueryString string `json:"queryString"`
55+
OperationName string `json:"operationName"`
56+
Variables map[string]interface{} `json:"variables"`
57+
}
58+
59+
// AppSyncLambdaAuthorizerResponse represents the expected format of an authorization response to AppSync.
60+
type AppSyncLambdaAuthorizerResponse struct {
61+
IsAuthorized bool `json:"isAuthorized"`
62+
ResolverContext map[string]interface{} `json:"resolverContext,omitempty"`
63+
DeniedFields []string `json:"deniedFields,omitempty"`
64+
TTLOverride *int `json:"ttlOverride,omitempty"`
65+
}

events/appsync_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io/ioutil"
66
"testing"
77

8+
"github.com/aws/aws-lambda-go/events/test"
89
"github.com/stretchr/testify/assert"
910
)
1011

@@ -85,3 +86,49 @@ func TestAppSyncIdentity_Cognito(t *testing.T) {
8586

8687
assert.JSONEq(t, string(inputJSON), string(outputJSON))
8788
}
89+
90+
func TestAppSyncLambdaAuthorizerRequestMarshalling(t *testing.T) {
91+
inputJSON, err := ioutil.ReadFile("./testdata/appsync-lambda-auth-request.json")
92+
if err != nil {
93+
t.Errorf("could not open test file. details: %v", err)
94+
}
95+
96+
var inputEvent AppSyncLambdaAuthorizerRequest
97+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
98+
t.Errorf("could not unmarshal event. details: %v", err)
99+
}
100+
101+
outputJSON, err := json.Marshal(inputEvent)
102+
if err != nil {
103+
t.Errorf("could not marshal event. details: %v", err)
104+
}
105+
106+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
107+
}
108+
109+
func TestAppSyncLambdaAuthorizerRequestMalformedJson(t *testing.T) {
110+
test.TestMalformedJson(t, AppSyncLambdaAuthorizerRequest{})
111+
}
112+
113+
func TestAppSyncLambdaAuthorizerResponseMarshalling(t *testing.T) {
114+
inputJSON, err := ioutil.ReadFile("./testdata/appsync-lambda-auth-response.json")
115+
if err != nil {
116+
t.Errorf("could not open test file. details: %v", err)
117+
}
118+
119+
var inputEvent AppSyncLambdaAuthorizerResponse
120+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
121+
t.Errorf("could not unmarshal event. details: %v", err)
122+
}
123+
124+
outputJSON, err := json.Marshal(inputEvent)
125+
if err != nil {
126+
t.Errorf("could not marshal event. details: %v", err)
127+
}
128+
129+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
130+
}
131+
132+
func TestAppSyncLambdaAuthorizerResponseMalformedJson(t *testing.T) {
133+
test.TestMalformedJson(t, AppSyncLambdaAuthorizerResponse{})
134+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"authorizationToken": "ExampleAUTHtoken123123123",
3+
"requestContext": {
4+
"apiId": "aaaaaa123123123example123",
5+
"accountId": "111122223333",
6+
"requestId": "f4081827-1111-4444-5555-5cf4695f339f",
7+
"queryString": "mutation CreateEvent {...}\n\nquery MyQuery {...}\n",
8+
"operationName": "MyQuery",
9+
"variables": {}
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"isAuthorized": true,
3+
"resolverContext": {
4+
"banana": "very yellow",
5+
"apple": "very green"
6+
}
7+
}

0 commit comments

Comments
 (0)