Creates a window to show multiple credentials for HTTP authentication

This commit is contained in:
FURiOUS 2020-02-22 09:19:56 -03:00
parent c6e5131753
commit 44cefe0ce8
5 changed files with 55 additions and 47 deletions

View file

@ -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();
};

View file

@ -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: [ '<all_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

View file

@ -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;

View file

@ -16,22 +16,10 @@
</head>
<body>
<div class="container">
<div id="settings" class="settings">
<button id="btn-options" class="btn btn-sm btn-success"><span class="glyphicon glyphicon-cog"></span> <span data-i18n="popupSettingsText"/></button>
<button id="btn-choose-credential-fields" class="btn btn-sm btn-warning"><span class="glyphicon glyphicon-list-alt"></span> <span data-i18n="popupChooseCredentialsText"/></button>
<button id="lock-database-button" class="btn btn-danger" title="Lock database"><span class="glyphicon glyphicon-lock"></span></button>
<div id="update-available" class="alert alert-danger">
<span data-i18n="popupUpdateAvailable"></span>
<br />
<a target="_blank" class="alert-link" href="https://keepassxc.org/download"><span data-i18n="popupDownloadNewVersion"></span></a>.
</div>
</div>
<div class="credentials">
<p data-i18n="popupAuthText"></p>
<p align="center" data-i18n="popupAuthText"></p>
<div id="login-list" class="list-group"></div>
<p>
<p align="center">
<button id="btn-dismiss" class="btn btn-sm btn-danger"><span class="glyphicon glyphicon-remove"></span> <span data-i18n="popupButtonDismissHttpAuth"/></button>
</p>
</div>

View file

@ -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);
});