Skip to content

Instantly share code, notes, and snippets.

@M4C4R
Last active July 15, 2021 15:25
Show Gist options
  • Select an option

  • Save M4C4R/bbd76e5b7c9ee0d3da1bce50842c1f98 to your computer and use it in GitHub Desktop.

Select an option

Save M4C4R/bbd76e5b7c9ee0d3da1bce50842c1f98 to your computer and use it in GitHub Desktop.
Adds a favourites section to the AWS SAML Sign In page, useful if you want to highlight a few roles out of a long list. Click Raw to install on script managers like GreaseMonkey and Tampermonkey.
// ==UserScript==
// @author Mert Acar
// @name Favourites for AWS SAML
// @version 1.1
// @include https://signin.aws.amazon.com/saml*
// @icon https://aws.amazon.com/favicon.ico
// @updateURL https://gist.github.com/M4C4R/bbd76e5b7c9ee0d3da1bce50842c1f98/raw/6150d5802d33b5f04d96033d748edd006d59a87a/favourites_for_aws_saml_sign_in.user.js
// ==/UserScript==
// *** START Configuration ***
// Enter favourites as account number to list of roles e.g. { "account number": ["role_1", "role_2"], ... }
const favourites = {
}
// *** END Configuration ***
// *** START Helper functions ***
function getSAMLAccountElement(accountNumber) {
for (const account of document.querySelectorAll(".saml-account-name")) {
if (account.textContent.includes(accountNumber)) {
return account.closest(".saml-account");
}
}
}
// *** END Helper functions ***
// -------------- Script Itself --------------
// Create the favourites container
const favouritesContainer = document.createElement("div");
favouritesContainer.style.margin = "30px";
// Create a title for the favourites container
const title = document.createElement("h1");
title.appendChild(document.createTextNode("Favourites:"));
favouritesContainer.appendChild(title);
// Loop over favourited accounts and add them
for (const [key, value] of Object.entries(favourites)) {
// Create a title for this account and name it
const accountTitle = document.createElement("h2");
accountTitle.appendChild(document.createTextNode(getSAMLAccountElement(key).querySelector(".saml-account-name").textContent.split(":")[1]));
// Create a list of roles for this account
const rolesList = document.createElement("li");
rolesList.setAttribute("style", "list-style-type: none;");
const listItem = document.createElement("ul");
listItem.setAttribute("style", "margin: 2px; padding: 0;");
// Loop over the favourited roles for this account and add them
value.forEach(role => {
// Create a sign in button for this role with the needed code
const button = document.createElement("button");
button.style.width = "130px";
button.style.fontSize = "medium";
button.appendChild(document.createTextNode(role));
button.onclick = function(){
getSAMLAccountElement(key).querySelector("input[id$=" + role + "]").checked = true;
document.getElementById("signin_button").click();
};
// Add the button to the list of roles for this account
listItem.appendChild(button)
});
rolesList.appendChild(listItem)
// Create a container for this favourited account and add it to the favourites
const accountContainer = document.createElement("div");
accountContainer.appendChild(accountTitle);
accountContainer.appendChild(rolesList);
favouritesContainer.appendChild(accountContainer);
}
// Add favourites to screen
document.getElementById("content").prepend(favouritesContainer);
// Bonus: make the radio buttons act as buttons
document.querySelectorAll('.saml-role input').forEach(function(radio){
radio.onclick = function(){
document.getElementById("signin_button").click();
}
})
@M4C4R
Copy link
Copy Markdown
Author

M4C4R commented Jun 19, 2020

Example:

image

Which would require the favourites to be set to:

const favourites = {
    "<account_number_1>": ["Admin", "View"],
    "<account_number_2>": ["Admin"]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment