Creates a window to show multiple credentials for HTTP authentication

This commit is contained in:
FURiOUS 2020-02-22 09:19:56 -03:00 committed by varjolintu
parent 45834d610c
commit 5288737e60
6 changed files with 71 additions and 39 deletions

View file

@ -166,6 +166,26 @@ kpxcEvent.onHTTPAuthPopup = async function(tab, data) {
page.tabs[tab.id].loginList = data;
browserAction.show(tab, popupData);
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;
}
dialogData.type = 'popup';
dialogData.url = `/popups/popup_httpauth.html?tab=${tab.id}`;
const wnd = await browser.windows.create(dialogData);
page.httpAuthDialog = wnd.id;
};
kpxcEvent.onUsernameFieldDetected = async function(tab, detected) {

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);
}
};
@ -90,7 +88,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

@ -21,13 +21,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);
}
});
@ -37,10 +35,18 @@ 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;
}
if (page.httpAuthDialog) {
browser.windows.remove(page.httpAuthDialog);
}
});
/**
@ -53,11 +59,17 @@ browser.tabs.onActivated.addListener(async function(activeInfo) {
const info = await browser.tabs.get(activeInfo.tabId);
if (info && info.id) {
page.currentTabId = info.id;
if (info.status === 'complete') {
if (!page.tabs[info.id]) {
page.createTabEntry(info.id);
}
page.switchTab(info);
if (page.httpAuthDialog) {
browser.windows.remove(page.httpAuthDialog);
}
}
}
} catch (err) {

View file

@ -26,6 +26,7 @@ var page = {};
page.blockedTabs = [];
page.currentRequest = {};
page.currentTabId = -1;
page.httpAuthDialog = undefined;
page.loginId = -1;
page.manualFill = ManualFill.NONE;
page.passwordFilled = false;

View file

@ -17,20 +17,8 @@
</head>
<body>
<div class="container">
<div id="settings" class="settings">
<button id="btn-options" class="btn btn-sm btn-success"><i class="fa fa-cog" aria-hidden="true"></i> <span data-i18n="popupSettingsText"></span></button>
<button id="btn-choose-credential-fields" class="btn btn-sm btn-warning"><i class="fa fa-list-alt" aria-hidden="true"></i> <span data-i18n="popupChooseCredentialsText"></span></button>
<button id="lock-database-button" class="btn btn-sm btn-danger" data-i18n="[title]lockDatabase"><i class="fa fa-lock" aria-hidden="true"></i></button>
<div id="update-available" class="alert alert-warning">
<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>
<button id="btn-dismiss" class="btn btn-sm btn-danger"><i class="fa fa-remove" aria-hidden="true"></i> <span data-i18n="popupButtonDismissHttpAuth"></span></button>

View file

@ -1,22 +1,29 @@
'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({
@ -26,8 +33,10 @@ $(async () => {
}
});
}
close();
});
ll.appendChild(a);
}
@ -38,16 +47,20 @@ $(async () => {
});
$('#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);
});