Skip to content

Instantly share code, notes, and snippets.

@prestomation
Created April 12, 2017 01:14
Show Gist options
  • Save prestomation/8f87617a9405c1a1247a80dc2c1077a8 to your computer and use it in GitHub Desktop.
Save prestomation/8f87617a9405c1a1247a80dc2c1077a8 to your computer and use it in GitHub Desktop.
//Turn off param validation because of the hackery coming up
var service = new AWS.IotData({endpoint : "data.iot.us-east-1.amazonaws.com", credentials : credentials, paramValidation : false});
//MQTT broker uses a different servicename in the sigv4 algo
service.api.signingName = "iotdevicegateway";
//Connecting to MQTT isn't actually a published operation.
//soooo Let's copy another GET request for some hackery..
var connect = $.extend(true, {}, service.api.operations.getThingShadow);
//..change the path
connect.httpPath = "/mqtt";
//..and synthesize it as a valid operation
service.api.operations.connect = connect;
//Create a request and change it's 'name'
var req = service.getThingShadow({});
req.operation = "connect";
//We're no where near done hacking this thing
//1. Save off our session token
//2. Register to the event before signing to delete the token from our credentials
//3. Register an event handler that restores the token to our client, and adds it to our URL
var sessionToken = service.config.credentials.sessionToken;
req.on("afterBuild", function(data){
delete req.service.config.credentials.sessionToken;
});
req.on('sign', function(data){
console.log("onSign", data);
console.log(data.httpRequest.path);
req.service.config.credentials.sessionToken = sessionToken;
data.httpRequest.path += "&X-Amz-Security-Token=" + encodeURIComponent(credentials.sessionToken);
});
//Can you believe this actually works??!!
var wsURL = req.presign().replace("https:", "wss:");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment