From 44cefe0ce82b9cf756f46dbcae1389e3b32735b6 Mon Sep 17 00:00:00 2001 From: FURiOUS Date: Sat, 22 Feb 2020 09:19:56 -0300 Subject: [PATCH] Creates a window to show multiple credentials for HTTP authentication --- keepassxc-browser/background/event.js | 21 ++++++++++- keepassxc-browser/background/httpauth.js | 14 ++++---- keepassxc-browser/background/init.js | 14 ++++---- keepassxc-browser/popups/popup_httpauth.html | 16 ++------- keepassxc-browser/popups/popup_httpauth.js | 37 ++++++++++---------- 5 files changed, 55 insertions(+), 47 deletions(-) diff --git a/keepassxc-browser/background/event.js b/keepassxc-browser/background/event.js index 393aa33..8839275 100755 --- a/keepassxc-browser/background/event.js +++ b/keepassxc-browser/background/event.js @@ -168,7 +168,7 @@ kpxcEvent.initHttpAuth = function() { return Promise.resolve(); } -kpxcEvent.onHTTPAuthPopup = function(tab, data) { +kpxcEvent.onHTTPAuthPopup = async function(tab, data) { const stackData = { level: 1, iconType: 'questionmark', @@ -178,6 +178,25 @@ kpxcEvent.onHTTPAuthPopup = function(tab, data) { browserAction.stackUnshift(stackData, tab.id); page.tabs[tab.id].loginList = data; browserAction.show(tab); + + const dialogData = { + width: 480, height: 250, + left: (window.screen.width/2) - (480/2), + top: (window.screen.height/2) - (250/2) + }; + + const httpAuthDialog = page.tabs[tab.id].httpAuthDialog; + if (httpAuthDialog && await browser.windows.get(httpAuthDialog)) { + dialogData.focused = true; + browser.windows.update(httpAuthDialog, dialogData); + return Promise.resolve(); + } + + dialogData.type = "popup"; + dialogData.url = `/popups/popup_httpauth.html?tab=${tab.id}`; + const wnd = await browser.windows.create(dialogData); + page.tabs[tab.id].httpAuthDialog = wnd.id; + return Promise.resolve(); }; diff --git a/keepassxc-browser/background/httpauth.js b/keepassxc-browser/background/httpauth.js index 0389247..90d2a30 100755 --- a/keepassxc-browser/background/httpauth.js +++ b/keepassxc-browser/background/httpauth.js @@ -14,19 +14,17 @@ httpAuth.init = function() { reqType = 'asyncBlocking'; } - if (browser.webRequest.onAuthRequired.hasListener(handleReq)) { - browser.webRequest.onAuthRequired.removeListener(handleReq); - browser.webRequest.onCompleted.removeListener(httpAuth.requestCompleted); - browser.webRequest.onErrorOccurred.removeListener(httpAuth.requestCompleted); - } - // Only intercept http auth requests if the option is turned on. - if (page.settings.autoFillAndSend) { + if (page.settings.autoFillAndSend && !browser.webRequest.onAuthRequired.hasListener(handleReq)) { const opts = { urls: [ '' ] }; browser.webRequest.onAuthRequired.addListener(handleReq, opts, [ reqType ]); browser.webRequest.onCompleted.addListener(httpAuth.requestCompleted, opts); browser.webRequest.onErrorOccurred.addListener(httpAuth.requestCompleted, opts); + } else if (browser.webRequest.onAuthRequired.hasListener(handleReq)) { + browser.webRequest.onAuthRequired.removeListener(handleReq); + browser.webRequest.onCompleted.removeListener(httpAuth.requestCompleted); + browser.webRequest.onErrorOccurred.removeListener(httpAuth.requestCompleted); } }; @@ -86,7 +84,7 @@ httpAuth.loginOrShowCredentials = function(logins, details, resolve, reject) { if (page.settings.showNotifications) { showNotification(tr('multipleCredentialsDetected')); } - kpxcEvent.onHTTPAuthPopup({ 'id': details.tabId }, { 'logins': logins, 'url': details.searchUrl, 'resolve': resolve }); + kpxcEvent.onHTTPAuthPopup({ 'id': details.tabId }, { 'logins': logins, 'details': details, 'resolve': resolve, 'reject': reject }); } } else { reject({ cancel: false }); // No logins found diff --git a/keepassxc-browser/background/init.js b/keepassxc-browser/background/init.js index fec0ccb..86bb943 100644 --- a/keepassxc-browser/background/init.js +++ b/keepassxc-browser/background/init.js @@ -20,13 +20,11 @@ */ browser.tabs.onCreated.addListener((tab) => { if (tab.id > 0) { - if (tab.selected) { - page.currentTabId = tab.id; - if (!page.tabs[tab.id]) { - page.createTabEntry(tab.id); - } - page.switchTab(tab); + page.currentTabId = tab.id; + if (!page.tabs[tab.id]) { + page.createTabEntry(tab.id); } + page.switchTab(tab); } }); @@ -36,6 +34,10 @@ browser.tabs.onCreated.addListener((tab) => { * @param {object} removeInfo */ browser.tabs.onRemoved.addListener((tabId, removeInfo) => { + if (page.tabs[tabId].httpAuthDialog) { + browser.windows.remove(page.tabs[tabId].httpAuthDialog); + } + delete page.tabs[tabId]; if (page.currentTabId === tabId) { page.currentTabId = -1; diff --git a/keepassxc-browser/popups/popup_httpauth.html b/keepassxc-browser/popups/popup_httpauth.html index 917bdb1..8c2c0bd 100644 --- a/keepassxc-browser/popups/popup_httpauth.html +++ b/keepassxc-browser/popups/popup_httpauth.html @@ -16,22 +16,10 @@
-
- - - - -
- -
- . -
-
-
-

+

-

+

diff --git a/keepassxc-browser/popups/popup_httpauth.js b/keepassxc-browser/popups/popup_httpauth.js index 6d87397..5859f8a 100644 --- a/keepassxc-browser/popups/popup_httpauth.js +++ b/keepassxc-browser/popups/popup_httpauth.js @@ -1,22 +1,26 @@ 'use strict'; +let tabid = -1; const getLoginData = async () => { const global = await browser.runtime.getBackgroundPage(); const tabs = await browser.tabs.query({ active: true, currentWindow: true }); - return global.page.tabs[tabs[0].id].loginList; + tabid = parseInt(window.location.search.split('=')[1]) || tabs[0].id; + global.page.currentTabId = tabid; + return global.page.tabs[tabid].loginList; }; $(async () => { await initColorTheme(); - const data = await getLoginData(); const ll = document.getElementById('login-list'); + let resolve = true; for (let i = 0; i < data.logins.length; ++i) { const a = document.createElement('a'); a.setAttribute('class', 'list-group-item'); a.textContent = data.logins[i].login + ' (' + data.logins[i].name + ')'; $(a).data('creds', data.logins[i]); $(a).click(function() { + resolve = false; if (data.resolve) { const creds = $(this).data('creds'); data.resolve({ @@ -31,23 +35,20 @@ $(async () => { ll.appendChild(a); } - $('#lock-database-button').click(function() { - browser.runtime.sendMessage({ - action: 'lock-database' - }).then(statusResponse); - }); - $('#btn-dismiss').click(async () => { - const loginData = await getLoginData(); - // Using reject won't work with every browser. So return empty credentials instead. - if (loginData.resolve) { - loginData.resolve({ - authCredentials: { - username: '', - password: '' - } - }); - } + resolve = false; + data.resolve({cancel: false}); close(); }); + + window.addEventListener('beforeunload', async () => { + const global = await browser.runtime.getBackgroundPage(); + + if (global.page.tabs[tabid]) { + global.page.tabs[tabid].httpAuthDialog = false; + if (resolve && data.resolve) { + data.resolve({cancel: false}); + } + } + }, false); });