Skip to content
This repository has been archived by the owner on Oct 9, 2021. It is now read-only.

Commit

Permalink
Terrible hack to use contextual identity.
Browse files Browse the repository at this point in the history
  • Loading branch information
agashlin committed Oct 23, 2018
1 parent b3c53c1 commit a48db7f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
76 changes: 74 additions & 2 deletions background.js
Expand Up @@ -4,6 +4,7 @@ const PHABRICATOR_REVIEW_HEADERS = [
"Must Review",
"Ready to Review",
];
const MULTI_ACCOUNT_CONTAINERS_EXTENSION_ID = "@testpilot-containers";
const BUGZILLA_API = "https://bugzilla.mozilla.org/jsonrpc.cgi";
const GITHUB_API = "https://api.github.com/search/issues";

Expand Down Expand Up @@ -137,6 +138,60 @@ const MyQOnly = {
return data.total_count;
},

async getCookieStoreForUrl(url) {
let assignment = await browser.runtime.sendMessage(
MULTI_ACCOUNT_CONTAINERS_EXTENSION_ID,
{
url,
method: "getAssignment",
},
{},
);

if (assignment && "userContextId" in assignment) {
return "firefox-container-" + String(assignment.userContextId);
} else {
return null;
}
},

async fetchWithCookies(req, cookies) {
req.credentials = "omit";

let cookieStr = cookies.map(pair => `${pair.name}=${pair.value}`).join("; ");
let originUrl = `${window.origin}/_generated_background_page.html`;

let headersRewriteListener = details => {
if (details.originUrl !== originUrl) {
return {};
}

// Remove any cookies.
let requestHeaders =
details.requestHeaders.filter(header =>
header.name.toLowerCase() !== "cookie");

requestHeaders.push({
name: "Cookie",
value: cookieStr,
});

return { requestHeaders };
};

browser.webRequest.onBeforeSendHeaders.addListener(headersRewriteListener,
{
urls: [req.url],
types: ["xmlhttprequest"],
tabId: browser.tabs.TAB_ID_NONE,
},
["blocking", "requestHeaders"],
);
let resp = await window.fetch(req);
browser.webRequest.onBeforeSendHeaders.removeListener(headersRewriteListener);
return resp;
},

/**
* Contacts Phabricator, Bugzilla, and Github (if the API keys for them exist),
* and attempts to get a review count for each.
Expand All @@ -150,8 +205,9 @@ const MyQOnly = {
url: PHABRICATOR_ROOT,
name: "phsid",
});
let cookieStoreId = await this.getCookieStoreForUrl(PHABRICATOR_ROOT);

if (phabCookie) {
if (phabCookie || cookieStoreId) {
console.log("Phabricator session found! Attempting to get dashboard page.");
let url = [PHABRICATOR_ROOT, PHABRICATOR_DASHBOARD].join("/");
let req = new Request(url, {
Expand All @@ -163,6 +219,22 @@ const MyQOnly = {
});

let resp = await window.fetch(req);

if (!resp.ok && cookieStoreId) {
// Try to use a login from a container.

phabCookie = await browser.cookies.get({
url: PHABRICATOR_ROOT,
name: "phsid",
storeId: cookieStoreId,
});

if (phabCookie) {
resp = await this.fetchWithCookies(req.clone(),
[ {name: "phsid", value: phabCookie.value} ]);
}
}

let pageBody = await resp.text();
let parser = new DOMParser();
let doc = parser.parseFromString(pageBody, "text/html");
Expand Down Expand Up @@ -250,4 +322,4 @@ const MyQOnly = {
},
};

MyQOnly.init();
MyQOnly.init();
3 changes: 3 additions & 0 deletions manifest.json
Expand Up @@ -34,8 +34,11 @@
},
"permissions": [
"alarms",
"contextualIdentities",
"cookies",
"storage",
"webRequest",
"webRequestBlocking",
"https://phabricator.services.mozilla.com/*",
"https://bugzilla.mozilla.org/*"
]
Expand Down

0 comments on commit a48db7f

Please sign in to comment.