-
Notifications
You must be signed in to change notification settings - Fork 558
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
Showing
4 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package events | ||
|
||
// IoTCustomAuthorizerRequest contains data coming in to a custom IoT device gateway authorizer function. | ||
type IoTCustomAuthorizerRequest struct { | ||
HTTPContext *IoTHTTPContext `json:"httpContext,omitempty"` | ||
MQTTContext *IoTMQTTContext `json:"mqttContext,omitempty"` | ||
TLSContext *IoTTLSContext `json:"tlsContext,omitempty"` | ||
AuthorizationToken string `json:"token"` | ||
TokenSignature string `json:"tokenSignature"` | ||
} | ||
|
||
type IoTHTTPContext struct { | ||
Headers map[string]string `json:"headers,omitempty"` | ||
QueryString string `json:"queryString"` | ||
} | ||
|
||
type IoTMQTTContext struct { | ||
ClientID string `json:"clientId"` | ||
Password []byte `json:"password"` | ||
Username string `json:"username"` | ||
} | ||
|
||
type IoTTLSContext struct { | ||
ServerName string `json:"serverName"` | ||
} | ||
|
||
// IoTCustomAuthorizerResponse represents the expected format of an IoT device gateway authorization response. | ||
type IoTCustomAuthorizerResponse struct { | ||
IsAuthenticated bool `json:"isAuthenticated"` | ||
PrincipalID string `json:"principalId"` | ||
DisconnectAfterInSeconds int32 `json:"disconnectAfterInSeconds"` | ||
RefreshAfterInSeconds int32 `json:"refreshAfterInSeconds"` | ||
PolicyDocuments []string `json:"policyDocuments"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package events | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/aws/aws-lambda-go/events/test" | ||
) | ||
|
||
func TestIoTCustomAuthorizerRequestMarshaling(t *testing.T) { | ||
|
||
// read json from file | ||
inputJSON, err := ioutil.ReadFile("./testdata/iot-custom-auth-request.json") | ||
if err != nil { | ||
t.Errorf("could not open test file. details: %v", err) | ||
} | ||
|
||
// de-serialize into Go object | ||
var inputEvent IoTCustomAuthorizerRequest | ||
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil { | ||
t.Errorf("could not unmarshal event. details: %v", err) | ||
} | ||
|
||
// serialize to json | ||
outputJSON, err := json.Marshal(inputEvent) | ||
if err != nil { | ||
t.Errorf("could not marshal event. details: %v", err) | ||
} | ||
|
||
test.AssertJsonsEqual(t, inputJSON, outputJSON) | ||
} | ||
|
||
func TestIoTCustomAuthorizerRequestMalformedJson(t *testing.T) { | ||
test.TestMalformedJson(t, IoTCustomAuthorizerRequest{}) | ||
} | ||
|
||
func TestIoTCustomAuthorizerResponseMarshaling(t *testing.T) { | ||
|
||
// read json from file | ||
inputJSON, err := ioutil.ReadFile("./testdata/iot-custom-auth-response.json") | ||
if err != nil { | ||
t.Errorf("could not open test file. details: %v", err) | ||
} | ||
|
||
// de-serialize into Go object | ||
var inputEvent IoTCustomAuthorizerResponse | ||
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil { | ||
t.Errorf("could not unmarshal event. details: %v", err) | ||
} | ||
|
||
// serialize to json | ||
outputJSON, err := json.Marshal(inputEvent) | ||
if err != nil { | ||
t.Errorf("could not marshal event. details: %v", err) | ||
} | ||
|
||
test.AssertJsonsEqual(t, inputJSON, outputJSON) | ||
} | ||
|
||
func TestIoTCustomAuthorizerResponseMalformedJson(t *testing.T) { | ||
test.TestMalformedJson(t, IoTCustomAuthorizerResponse{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"httpContext": { | ||
"headers": { | ||
"Accept-Language" : "en" | ||
}, | ||
"queryString": "abc" | ||
}, | ||
"mqttContext": { | ||
"clientId": "someclient", | ||
"password": "aslkfjwoeiuwekrujwlrueowieurowieurowiuerwleuroiwueroiwueroiuweoriuweoriuwoeiruwoeiur", | ||
"username": "thebestuser" | ||
}, | ||
"tlsContext": { | ||
"serverName": "server.stuff.com" | ||
}, | ||
"token": "someToken", | ||
"tokenSignature": "somelongtokensignature" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"isAuthenticated":true, | ||
"principalId": "xxxxxxxx", | ||
"disconnectAfterInSeconds": 86400, | ||
"refreshAfterInSeconds": 300, | ||
"policyDocuments": [ | ||
"{ \"Version\": \"2012-10-17\", \"Statement\": [ { \"Action\": [\"iot:Subscribe\"], \"Effect\": \"Allow\", \"Resource\": [\"*\"] } ] }" | ||
] | ||
} |