From 701e196f5ee532002025b365c3e34ca914d28d3d Mon Sep 17 00:00:00 2001 From: Andy Brandt Date: Tue, 21 Nov 2017 11:21:39 -0600 Subject: [PATCH] for http auth, if multiple credentials, wait until a user selects credentials from the popup. fixes #41 --- src/background/httpauth.js | 28 ++++++++++------------------ src/popups/popup_httpauth.js | 20 ++++++++++++++------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/background/httpauth.js b/src/background/httpauth.js index 324fb14..48dad95 100644 --- a/src/background/httpauth.js +++ b/src/background/httpauth.js @@ -1,11 +1,11 @@ var httpAuth = httpAuth || {}; -httpAuth.requests = {}; +httpAuth.requests = []; httpAuth.requestCompleted = function (details) { - var req = httpAuth.requests[details.requestId]; - if (req) { - delete httpAuth.requests[details.requestId]; + var idx = httpAuth.requests.indexOf(details.requestId); + if (idx >= 0) { + httpAuth.requests.splice(idx, 1); } } @@ -21,16 +21,12 @@ httpAuth.handleRequestCallback = function (details, callback) { httpAuth.processPendingCallbacks = function (details, resolve, reject) { - if (!page.tabs[details.tabId]) { + if (httpAuth.requests.indexOf(details.requestId) >= 0 || !page.tabs[details.tabId]) { reject({}); return; } - var creds = httpAuth.requests[details.requestId]; - if (creds) { - httpAuth.loginOrShowCredentials(creds, details, resolve, reject); - return; - } + httpAuth.requests.push(details.requestId); if (details.challenger) { details.proxyUrl = details.challenger.host; @@ -46,19 +42,15 @@ httpAuth.processPendingCallbacks = function (details, resolve, reject) { httpAuth.loginOrShowCredentials = function (logins, details, resolve, reject) { // at least one login found --> use first to login if (logins.length > 0) { - cipevent.onHTTPAuthPopup(null, { "id": details.tabId }, { "logins": logins, "url": details.searchUrl }); - //generate popup-list for HTTP Auth usernames + descriptions - if (page.settings.autoFillAndSend) { - var creds = logins.shift(); - httpAuth.requests[details.requestId] = logins; + if (logins.length == 1 && page.settings.autoFillAndSend) { resolve({ authCredentials: { - username: creds.Login, - password: creds.Password + username: logins[0].Login, + password: logins[0].Password } }); } else { - reject({}); + cipevent.onHTTPAuthPopup(null, { "id": details.tabId }, { "logins": logins, "url": details.searchUrl, 'resolve': resolve }); } } // no logins found diff --git a/src/popups/popup_httpauth.js b/src/popups/popup_httpauth.js index 873467c..63abd6a 100644 --- a/src/popups/popup_httpauth.js +++ b/src/popups/popup_httpauth.js @@ -1,6 +1,6 @@ -$(function() { - browser.runtime.getBackgroundPage().then(function(global) { - browser.tabs.query({"active": true, "currentWindow": true}).then(function(tabs) { +$(function () { + browser.runtime.getBackgroundPage().then(function (global) { + browser.tabs.query({ "active": true, "currentWindow": true }).then(function (tabs) { //var data = global.tab_httpauth_list["tab" + tab.id]; var tab = tabs[0]; var data = global.page.tabs[tab.id].loginList; @@ -10,9 +10,17 @@ $(function() { var a = document.createElement("a"); a.textContent = data.logins[i].Login + " (" + data.logins[i].Name + ")"; li.appendChild(a); - $(a).data("url", data.url.replace(/:\/\//g, "://" + data.logins[i].Login + ":" + data.logins[i].Password + "@")); - $(a).click(function() { - browser.tabs.update(tab.id, {"url": $(this).data("url")}); + $(a).data('creds', data.logins[i]); + $(a).click(function () { + if (data.resolve) { + var creds = $(this).data('creds'); + data.resolve({ + authCredentials: { + username: creds.Login, + password: creds.Password + } + }); + } close(); }); ul.appendChild(li);