Skip to content

Commit b3e2820

Browse files
daviskohbmoffatt
authored andcommitted
add AppSyncIdentity (#173)
* add AppSyncIdentity * update Gopkg.lock & add /vendor to gitignore * fix json tags * Separate AppSyncIdentity into 2 separate types for IAM and Cognito * fix tests to check marshalling / unmarshalling of identity json * update initialisims Arn -> ARN Id -> ID Ip -> IP
1 parent e12c711 commit b3e2820

File tree

6 files changed

+98
-1
lines changed

6 files changed

+98
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Go Dep
2+
vendor

Gopkg.lock

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

events/appsync.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@ type AppSyncResolverTemplate struct {
99
Payload json.RawMessage `json:"payload"`
1010
}
1111

12+
// AppSyncIAMIdentity contains information about the caller authed via IAM.
13+
type AppSyncIAMIdentity struct {
14+
AccountID string `json:"accountId"`
15+
CognitoIdentityPoolID string `json:"cognitoIdentityPoolId"`
16+
CognitoIdentityID string `json:"cognitoIdentityId"`
17+
SourceIP []string `json:"sourceIp"`
18+
Username string `json:"username"`
19+
UserARN string `json:"userArn"`
20+
}
21+
22+
// AppSyncCognitoIdentity contains information about the caller authed via Cognito.
23+
type AppSyncCognitoIdentity struct {
24+
Sub string `json:"sub"`
25+
Issuer string `json:"issuer"`
26+
Username string `json:"username"`
27+
Claims map[string]interface{} `json:"claims"`
28+
SourceIP []string `json:"sourceIp"`
29+
DefaultAuthStrategy string `json:"defaultAuthStrategy"`
30+
}
31+
1232
// AppSyncOperation specifies the operation type supported by Lambda operations
1333
type AppSyncOperation string
1434

events/appsync_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,41 @@ func TestAppSyncResolverTemplate_batchinvoke(t *testing.T) {
4747

4848
assert.JSONEq(t, string(inputJSON), string(outputJSON))
4949
}
50+
51+
func TestAppSyncIdentity_IAM(t *testing.T) {
52+
inputJSON, err := ioutil.ReadFile("./testdata/appsync-identity-iam.json")
53+
if err != nil {
54+
t.Errorf("could not open test file. details: %v", err)
55+
}
56+
57+
var inputIdentity AppSyncIAMIdentity
58+
if err = json.Unmarshal(inputJSON, &inputIdentity); err != nil {
59+
t.Errorf("could not unmarshal identity. details: %v", err)
60+
}
61+
62+
outputJSON, err := json.Marshal(inputIdentity)
63+
if err != nil {
64+
t.Errorf("could not marshal identity. details: %v", err)
65+
}
66+
67+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
68+
}
69+
70+
func TestAppSyncIdentity_Cognito(t *testing.T) {
71+
inputJSON, err := ioutil.ReadFile("./testdata/appsync-identity-cognito.json")
72+
if err != nil {
73+
t.Errorf("could not open test file. details: %v", err)
74+
}
75+
76+
var inputIdentity AppSyncCognitoIdentity
77+
if err = json.Unmarshal(inputJSON, &inputIdentity); err != nil {
78+
t.Errorf("could not unmarshal identity. details: %v", err)
79+
}
80+
81+
outputJSON, err := json.Marshal(inputIdentity)
82+
if err != nil {
83+
t.Errorf("could not marshal identity. details: %v", err)
84+
}
85+
86+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
87+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"sub": "123-456",
3+
"issuer": "https://cognito-idp.us-east-1.amazonaws.com/us-east-1_abc",
4+
"username": "user1",
5+
"claims": {
6+
"sub": "123-456",
7+
"aud": "abcdefg",
8+
"event_id": "123-123-123",
9+
"token_use": "id",
10+
"auth_time": 1551226125,
11+
"iss": "https://cognito-idp.us-east-1.amazonaws.com/us-east-1_abc",
12+
"cognito:username": "user1",
13+
"exp": 1551228178628,
14+
"iat": 1551228178629
15+
},
16+
"sourceIp": ["192.168.196.186", "193.168.196.186"],
17+
"defaultAuthStrategy": "ALLOW"
18+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"accountId": "accountid123",
3+
"cognitoIdentityPoolId": "identitypoolid123",
4+
"cognitoIdentityId": "identityid123",
5+
"sourceIp": ["192.168.196.186", "193.168.196.186"],
6+
"username": "user1",
7+
"userArn": "arn:aws:iam::123456789012:user/appsync"
8+
}

0 commit comments

Comments
 (0)