Skip to content

Instantly share code, notes, and snippets.

@alsmola
Created September 12, 2020 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alsmola/15ec1d5c86105e5a17bfdc3af37d8186 to your computer and use it in GitHub Desktop.
Save alsmola/15ec1d5c86105e5a17bfdc3af37d8186 to your computer and use it in GitHub Desktop.
Chrome extension to use AWS IoT Credentials Endpoint
'use strict';
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if (request.contentScriptQuery == "getCredentials") {
const url = "https://{credentials-endpoint}.credentials.iot.us-east-1.amazonaws.com/role-aliases/chromeiot/credentials";
const params = { headers: { "x-amzn-iot-thingname": "{thingName}" } }
fetch(url, params)
.then(response => response.json())
.then(data => sendResponse(data.credentials));
}
return true;
}
);
chrome.runtime.sendMessage({contentScriptQuery: "getCredentials"}, function(credentials) {
document.getElementById("accessKeyID").innerText = credentials.accessKeyId;
document.getElementById("secretAccessKey").innerText = credentials.secretAccessKey;
document.getElementById("sessionToken").innerText = credentials.sessionToken;
document.getElementById("expiration").innerText = credentials.expiration;
});
<html>
<head>
</head>
<body>
<div id="accessKeyID"></div>
<div id="secretAccessKey"></div>
<div id="sessionToken"></div>
<div id="expiration"></div>
</body>
</html>
{
"name": "Chrome IOT",
"version": "1.0",
"description": "Get AWS IAM creds via a certificate",
"background": {
"scripts": ["background.js"],
"persistent": false
},
"content_scripts": [
{
"matches": ["https://{your-domain}.com/*"],
"js": ["contentScript.js"]
}
],
"permissions": [
"activeTab",
"https://{credentials-endpoint}.credentials.iot.us-east-1.amazonaws.com/"
],
"manifest_version": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment