Skip to content

Instantly share code, notes, and snippets.

@prestomation
Last active April 18, 2017 21:35
Show Gist options
  • Save prestomation/b0f15b4492146a64b9deffaf7ef011eb to your computer and use it in GitHub Desktop.
Save prestomation/b0f15b4492146a64b9deffaf7ef011eb to your computer and use it in GitHub Desktop.
var presignIOTMQTTConnection = function(endpoint, credentials) {
//Turn off param validation because of the hackery coming up
var service = new AWS.IotData({endpoint : endpoint, credentials : credentials, paramValidation: false});
//MQTT broker uses a different servicename in the sigv4 algo
service.api.signingName = "iotdevicegateway";
//1. Create a request object for another, sorta similar request
//2. Save off our session token and delete it from the credentials(so it doesn't get signed)
//3. Register to an event in the state machine to adjust the path
//4. Sign it!
//5. Restore session token, and add it to the url
var req = service.getThingShadow({});
var sessionToken = service.config.credentials.sessionToken;
delete req.service.config.credentials.sessionToken;
req.on("afterBuild", function(data){ data.httpRequest.path = "/mqtt"; });
var url = req.presign().replace("https:", "wss:");
req.service.config.credentials.sessionToken = sessionToken;
url += "&X-Amz-Security-Token=" + encodeURIComponent(credentials.sessionToken);
return url;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment