Fixed HTTP auth

This commit is contained in:
varjolintu 2017-12-13 11:26:22 +02:00
parent cbc88d3493
commit 030257203c
5 changed files with 58 additions and 25 deletions

View file

@ -100,8 +100,8 @@ kpxcEvent.showStatus = function(configured, tab, callback) {
};
kpxcEvent.onLoadSettings = function(callback, tab) {
browser.storage.local.get({'settings': {}}).then((item) => {
callback(item.settings);
page.initSettings().then((settings) => {
callback(settings);
}, (err) => {
console.log('error loading settings: ' + err);
});
@ -230,6 +230,11 @@ kpxcEvent.onLoginPopup = function(callback, tab, logins) {
browserAction.show(null, tab);
};
kpxcEvent.initHttpAuth = function(callback) {
httpAuth.init();
callback();
}
kpxcEvent.onHTTPAuthPopup = function(callback, tab, data) {
let stackData = {
level: 1,
@ -274,6 +279,7 @@ kpxcEvent.messageHandlers = {
'get_keepassxc_versions': kpxcEvent.onGetKeePassXCVersions,
'get_status': kpxcEvent.onGetStatus,
'get_tab_information': kpxcEvent.onGetTabInformation,
'init_http_auth': kpxcEvent.initHttpAuth,
'load_keyring': kpxcEvent.onLoadKeyRing,
'load_settings': kpxcEvent.onLoadSettings,
'page_clear_logins': kpxcEvent.pageClearLogins,

View file

@ -3,6 +3,31 @@ const httpAuth = {};
httpAuth.requests = [];
httpAuth.pendingCallbacks = [];
httpAuth.init = function() {
let handleReq = httpAuth.handleRequestPromise;
let reqType = 'blocking';
if (!isFirefox()) {
handleReq = httpAuth.handleRequestCallback;
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) {
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);
}
};
httpAuth.requestCompleted = function(details) {
let index = httpAuth.requests.indexOf(details.requestId);
if (index >= 0) {
@ -41,8 +66,8 @@ 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) {
if (logins.length == 1 && page.settings.autoFillAndSend) {
if (logins.length > 0 && page.settings.autoFillAndSend) {
if (logins.length === 1) {
resolve({
authCredentials: {
username: logins[0].login,

View file

@ -1,6 +1,7 @@
keepass.migrateKeyRing().then(() => {
page.initSettings().then(() => {
page.initOpenedTabs().then(() => {
httpAuth.init();
keepass.connectToNative();
keepass.generateNewKeyPair();
keepass.changePublicKeys(null).then((pkRes) => {
@ -73,22 +74,6 @@ browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
}
});
// Retrieve Credentials and try auto-login for HTTPAuth requests
if (browser.webRequest.onAuthRequired) {
let handleReq = httpAuth.handleRequestPromise;
let reqType = 'blocking';
let opts = { urls: ['<all_urls>'] };
if (!isFirefox()) {
handleReq = httpAuth.handleRequestCallback;
reqType = 'asyncBlocking';
}
browser.webRequest.onAuthRequired.addListener(handleReq, opts, [reqType]);
browser.webRequest.onCompleted.addListener(httpAuth.requestCompleted, opts);
browser.webRequest.onErrorOccurred.addListener(httpAuth.requestCompleted, opts);
}
browser.runtime.onMessage.addListener(kpxcEvent.onMessage);
const contextMenuItems = [

View file

@ -14,8 +14,8 @@ page.blockedTabs = {};
page.initSettings = function() {
return new Promise((resolve, reject) => {
kpxcEvent.onLoadSettings((settings) => {
page.settings = settings;
browser.storage.local.get({'settings': {}}).then((item) => {
page.settings = item.settings;
if (!('checkUpdateKeePassXC' in page.settings)) {
page.settings.checkUpdateKeePassXC = defaultSettings.checkUpdateKeePassXC;
}
@ -35,7 +35,7 @@ page.initSettings = function() {
page.settings.autoRetrieveCredentials = defaultSettings.autoRetrieveCredentials;
}
browser.storage.local.set({'settings': page.settings});
resolve();
resolve(page.settings);
});
});
};

View file

@ -30,6 +30,18 @@ options.initMenu = function() {
$('div.tab:first').show();
};
options.saveSettingsPromise = function() {
return new Promise((resolve, reject) => {
browser.storage.local.set({'settings': options.settings}).then((item) => {
browser.runtime.sendMessage({
action: 'load_settings'
}).then((settings) => {
resolve(settings);
});
});
});
}
options.saveSetting = function(name) {
const $id = '#' + name;
$($id).closest('.control-group').removeClass('error').addClass('success');
@ -61,8 +73,13 @@ options.initGeneralSettings = function() {
});
$('#tab-general-settings input[type=checkbox]').change(function() {
options.settings[$(this).attr('name')] = $(this).is(':checked');
options.saveSettings();
const name = $(this).attr('name');
options.settings[name] = $(this).is(':checked');
options.saveSettingsPromise().then((x) => {
if (name === 'autoFillAndSend') {
browser.runtime.sendMessage({action: 'init_http_auth'});
}
});
});
$('#tab-general-settings input[type=radio]').each(function() {