Skip to content

Commit 1405648

Browse files
committed
Adding structures for iot custom authorizer request/response
1 parent 16345c9 commit 1405648

File tree

6 files changed

+117
-6
lines changed

6 files changed

+117
-6
lines changed

events/apigw.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,3 @@ type APIGatewayCustomAuthorizerPolicy struct {
115115
Version string
116116
Statement []IAMPolicyStatement
117117
}
118-
119-
type IAMPolicyStatement struct {
120-
Action []string
121-
Effect string
122-
Resource []string
123-
}

events/iam.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package events
2+
3+
type IAMPolicyStatement struct {
4+
Action []string
5+
Effect string
6+
Resource []string
7+
}

events/iot.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package events
2+
3+
// IoTCustomAuthorizerRequest contains data coming in to a custom IoT device gateway authorizer function.
4+
type IoTCustomAuthorizerRequest struct {
5+
AuthorizationToken string `json:"token"`
6+
}
7+
8+
// IoTCustomAuthorizerResponse represents the expected format of an IoT device gateway authorization response.
9+
type IoTCustomAuthorizerResponse struct {
10+
IsAuthenticated bool `json:"isAuthenticated"`
11+
PrincipalID string `json:"principalId"`
12+
DisconnectAfterInSeconds int32 `json:"disconnectAfterInSeconds"`
13+
RefreshAfterInSeconds int32 `json:"refreshAfterInSeconds"`
14+
PolicyDocuments []IoTCustomAuthorizerPolicy `json:"policyDocuments"`
15+
Context map[string]interface{} `json:"context,omitempty"`
16+
}
17+
18+
// IoTCustomAuthorizerPolicy represents an IAM policy
19+
type IoTCustomAuthorizerPolicy struct {
20+
Version string
21+
Statement []IAMPolicyStatement
22+
}
23+

events/iot_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package events
2+
3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
"testing"
7+
8+
"github.com/aws/aws-lambda-go/events/test"
9+
)
10+
11+
func TestIoTCustomAuthorizerRequestMarshaling(t *testing.T) {
12+
13+
// read json from file
14+
inputJSON, err := ioutil.ReadFile("./testdata/iot-custom-auth-request.json")
15+
if err != nil {
16+
t.Errorf("could not open test file. details: %v", err)
17+
}
18+
19+
// de-serialize into Go object
20+
var inputEvent IoTCustomAuthorizerRequest
21+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
22+
t.Errorf("could not unmarshal event. details: %v", err)
23+
}
24+
25+
// serialize to json
26+
outputJSON, err := json.Marshal(inputEvent)
27+
if err != nil {
28+
t.Errorf("could not marshal event. details: %v", err)
29+
}
30+
31+
test.AssertJsonsEqual(t, inputJSON, outputJSON)
32+
}
33+
34+
func TestIoTCustomAuthorizerRequestMalformedJson(t *testing.T) {
35+
test.TestMalformedJson(t, IoTCustomAuthorizerRequest{})
36+
}
37+
38+
func TestIoTCustomAuthorizerResponseMarshaling(t *testing.T) {
39+
40+
// read json from file
41+
inputJSON, err := ioutil.ReadFile("./testdata/iot-custom-auth-response.json")
42+
if err != nil {
43+
t.Errorf("could not open test file. details: %v", err)
44+
}
45+
46+
// de-serialize into Go object
47+
var inputEvent IoTCustomAuthorizerResponse
48+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
49+
t.Errorf("could not unmarshal event. details: %v", err)
50+
}
51+
52+
// serialize to json
53+
outputJSON, err := json.Marshal(inputEvent)
54+
if err != nil {
55+
t.Errorf("could not marshal event. details: %v", err)
56+
}
57+
58+
test.AssertJsonsEqual(t, inputJSON, outputJSON)
59+
}
60+
61+
func TestIoTCustomAuthorizerResponseMalformedJson(t *testing.T) {
62+
test.TestMalformedJson(t, IoTCustomAuthorizerResponse{})
63+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"token":"allow"
3+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"isAuthenticated":true,
3+
"principalId": "xxxxxxxx",
4+
"disconnectAfterInSeconds": 86400,
5+
"refreshAfterInSeconds": 300,
6+
"policyDocuments": [
7+
{ "Version": "2012-10-17", "Statement": [
8+
{
9+
"Action": ["iot:Subscribe"],
10+
"Effect": "Allow",
11+
"Resource": ["*"]
12+
}
13+
]
14+
}
15+
],
16+
"context": {
17+
"username" : "johnDoe123",
18+
"city" : "Seattle",
19+
"country" : "USA"
20+
}
21+
}

0 commit comments

Comments
 (0)