Skip to content

Commit

Permalink
feat: fix to work with new SSO experience (#14)
Browse files Browse the repository at this point in the history
Co-authored-by: Sean Meyer <[email protected]>
  • Loading branch information
slinkymanbyday and Sean Meyer authored Jan 14, 2024
1 parent a921990 commit 63dc42b
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 36 deletions.
111 changes: 81 additions & 30 deletions containerize.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const availableContainerColors = [

let containerNameTemplate = "name role";

let accountMap = {};

function randomIcon() {
return availableContainerIcons[Math.random() * availableContainerIcons.length | 0]
}
Expand All @@ -40,15 +42,15 @@ function randomColor() {
function prepareContainer({ name, color, icon, cb }) {
browser.contextualIdentities.query({
name: name,
}).then(function(containers) {
}).then(function (containers) {
if (containers.length >= 1) {
cb(containers[0]);
} else {
browser.contextualIdentities.create({
name: name,
color: color || randomColor(),
icon: icon || randomIcon(),
}).then(function(container) {
}).then(function (container) {
cb(container);
});
}
Expand All @@ -65,27 +67,28 @@ function listener(details) {
}*/

// Intercept our response

let filter = browser.webRequest.filterResponseData(details.requestId);

let queryString = new URLSearchParams(details.url.split("?")[1]);
// Parse some params for container name
let accountRole = details.url.split("=")[2];
// account is account ID and account name in parens
let account = decodeURIComponent(details.originUrl.split("/")[7]);
// getting fancy w/ regex to capture account names with parens
let capture = /^(\d+) \((.+)\)$/.exec(account);
let accountNumber = capture[1];
let accountName = capture[2];
let accountRole = queryString.get("role_name");
let accountNumber = queryString.get("account_id");

// pull subdomain for folks that might have multiple SSO
// portals that have access to the same account and role names
let host = /:\/\/([^\/]+)/.exec(details.originUrl)[1];
let subdomain = host.split(".")[0];

const params = {
'name': accountName,
let params = {
'number': accountNumber,
'role': accountRole,
'subdomain': subdomain
};
if(accountMap[accountNumber] !== undefined){
params["name"] = accountMap[accountNumber]["name"];
params["email"] = accountMap[accountNumber]["email"];
}

let name = containerNameTemplate;

Expand All @@ -98,7 +101,7 @@ function listener(details) {
let encoder = new TextEncoder();

filter.ondata = event => {
str += decoder.decode(event.data, {stream: true});
str += decoder.decode(event.data, { stream: true });
};

filter.onstop = event => {
Expand All @@ -119,19 +122,21 @@ function listener(details) {

// Generate our federation URI and open it in a container
const url = object.signInFederationLocation + "?Action=login&SigninToken=" + object.signInToken + "&Issuer=" + encodeURIComponent(details.originUrl) + "&Destination=" + encodeURIComponent(destination);
prepareContainer({name: name, cb: function(container) {
const createTabParams = {
cookieStoreId: container.cookieStoreId,
url: url,
pinned: false
};
// get index of tab we're about to remove, put ours at that spot
browser.tabs.get(details.tabId).then(function(tab) {
createTabParams.index = tab.index;
browser.tabs.create(createTabParams);
});
browser.tabs.remove(details.tabId);
}});
prepareContainer({
name: name, cb: function (container) {
const createTabParams = {
cookieStoreId: container.cookieStoreId,
url: url,
pinned: false
};
// get index of tab we're about to remove, put ours at that spot
browser.tabs.get(details.tabId).then(function (tab) {
createTabParams.index = tab.index;
browser.tabs.create(createTabParams);
});
browser.tabs.remove(details.tabId);
}
});
} else {
filter.write(encoder.encode(str));
}
Expand All @@ -141,6 +146,41 @@ function listener(details) {

return {};
}
function accountNameListener(details) {
// Intercept our response
let filter = browser.webRequest.filterResponseData(details.requestId);

let str = '';
let decoder = new TextDecoder("utf-8");
let encoder = new TextEncoder();

filter.ondata = event => {
str += decoder.decode(event.data, { stream: true });
};
filter.onstop = event => {
filter.write(encoder.encode(str));
// The first OPTIONS request has no response body
if (str.length > 0) {
// signInToken
// signInFederationLocation
// destination
const object = JSON.parse(str);

for (result of object.result) {
if(result["searchMetadata"]){
accountMap[result["searchMetadata"]["AccountId"]] = {
"name": result["searchMetadata"]["AccountName"],
"email": result["searchMetadata"]["AccountEmail"]
}
}
}
}
filter.close();
}

return {};

}

// Fetch our custom defined container name template
function onGot(item) {
Expand All @@ -156,10 +196,21 @@ getting.then(onGot, onError);

browser.webRequest.onBeforeRequest.addListener(
listener,
{urls: [
"https://*.amazonaws.com/federation/console?*",
"https://*.amazonaws-us-gov.com/federation/console?*",
"https://*.amazonaws.cn/federation/console?*"
], types: ["xmlhttprequest"]},
{
urls: [
"https://*.amazonaws.com/federation/console?*",
"https://*.amazonaws-us-gov.com/federation/console?*",
"https://*.amazonaws.cn/federation/console?*"
], types: ["xmlhttprequest"]
},
["blocking"]
);
browser.webRequest.onBeforeRequest.addListener(
accountNameListener,
{
urls: [
"https://*.amazonaws.com/instance/appinstances"
], types: ["xmlhttprequest"]
},
["blocking"]
);
7 changes: 5 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
"storage",
"webRequest",
"webRequestBlocking",
"https://*.awsapps.com/start/*",
"https://*.awsapps.com/*",
"https://*.amazonaws.com/federation/console?*",
"https://*.amazonaws-us-gov.com/federation/console?*",
"https://*.amazonaws.cn/federation/console?*"
"https://*.amazonaws.cn/federation/console?*",
"https://*.amazonaws.com/federation/instance/appinstances",
"https://*.amazonaws-us-gov.com/federation/instance/appinstances",
"https://*.amazonaws.cn/federation/instance/appinstances"
],
"background": {
"scripts": [
Expand Down
6 changes: 3 additions & 3 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
<br/>
<p>Available variables:</p>
<ul>
<li>number</li>
<li>name</li>
<li>email</li>
<li>number</li>
<li>role</li>
<li>subdomain</li>
</ul>
Expand All @@ -23,5 +24,4 @@

<script src="options.js"></script>
</body>
</html>

</html>
3 changes: 2 additions & 1 deletion options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
let examples = {
'name': 'Prod',
'email': '[email protected]',
'number': '123456',
'role': 'InfraEng',
'subdomain': 'MegaCorp'
Expand Down Expand Up @@ -38,4 +39,4 @@ document.addEventListener("DOMContentLoaded", restoreOptions);
document.querySelector("form").addEventListener("submit", saveOptions);
document.querySelector("#template").addEventListener("input", function(evt) {
populatePreview(this.value);
});
});

0 comments on commit 63dc42b

Please sign in to comment.