Skip to content

Commit 0d9038e

Browse files
ynori7bmoffatt
andauthored
Adding structures for iot custom authorizer request/response (#67)
* Adding structures for iot custom authorizer request/response * Making the iot policy documents an array of strings to match the documentation * Rebasing and updating iot events since the contract has chagned * Making the api gateway custom authorizer policy more generic * Fixed formatting * Update iot.go to satisfy the linter * Update iot.go * Delete workspace.xml * Delete vcs.xml * Delete misc.xml * Delete encodings.xml * Delete modules.xml * Delete aws-lambda-go.iml * Delete policy.go Co-authored-by: Bryan Moffatt <[email protected]>
1 parent ac2f18e commit 0d9038e

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

events/iot.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package events
2+
3+
// IoTCustomAuthorizerRequest contains data coming in to a custom IoT device gateway authorizer function.
4+
type IoTCustomAuthorizerRequest struct {
5+
HTTPContext *IoTHTTPContext `json:"httpContext,omitempty"`
6+
MQTTContext *IoTMQTTContext `json:"mqttContext,omitempty"`
7+
TLSContext *IoTTLSContext `json:"tlsContext,omitempty"`
8+
AuthorizationToken string `json:"token"`
9+
TokenSignature string `json:"tokenSignature"`
10+
}
11+
12+
type IoTHTTPContext struct {
13+
Headers map[string]string `json:"headers,omitempty"`
14+
QueryString string `json:"queryString"`
15+
}
16+
17+
type IoTMQTTContext struct {
18+
ClientID string `json:"clientId"`
19+
Password []byte `json:"password"`
20+
Username string `json:"username"`
21+
}
22+
23+
type IoTTLSContext struct {
24+
ServerName string `json:"serverName"`
25+
}
26+
27+
// IoTCustomAuthorizerResponse represents the expected format of an IoT device gateway authorization response.
28+
type IoTCustomAuthorizerResponse struct {
29+
IsAuthenticated bool `json:"isAuthenticated"`
30+
PrincipalID string `json:"principalId"`
31+
DisconnectAfterInSeconds int32 `json:"disconnectAfterInSeconds"`
32+
RefreshAfterInSeconds int32 `json:"refreshAfterInSeconds"`
33+
PolicyDocuments []string `json:"policyDocuments"`
34+
}

events/iot_test.go

+63
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"httpContext": {
3+
"headers": {
4+
"Accept-Language" : "en"
5+
},
6+
"queryString": "abc"
7+
},
8+
"mqttContext": {
9+
"clientId": "someclient",
10+
"password": "aslkfjwoeiuwekrujwlrueowieurowieurowiuerwleuroiwueroiwueroiuweoriuweoriuwoeiruwoeiur",
11+
"username": "thebestuser"
12+
},
13+
"tlsContext": {
14+
"serverName": "server.stuff.com"
15+
},
16+
"token": "someToken",
17+
"tokenSignature": "somelongtokensignature"
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"isAuthenticated":true,
3+
"principalId": "xxxxxxxx",
4+
"disconnectAfterInSeconds": 86400,
5+
"refreshAfterInSeconds": 300,
6+
"policyDocuments": [
7+
"{ \"Version\": \"2012-10-17\", \"Statement\": [ { \"Action\": [\"iot:Subscribe\"], \"Effect\": \"Allow\", \"Resource\": [\"*\"] } ] }"
8+
]
9+
}

0 commit comments

Comments
 (0)