From 68e41d65c6abea7611f4d8c6d21e59afb9e96a6e Mon Sep 17 00:00:00 2001 From: varjolintu Date: Tue, 12 Sep 2017 14:18:47 +0300 Subject: [PATCH] Reverted back to old database system --- keepassxc-browser/background/browserAction.js | 64 +++++++------ keepassxc-browser/background/event.js | 41 ++++----- keepassxc-browser/background/init.js | 24 ++--- keepassxc-browser/background/keepass.js | 50 ++++------ keepassxc-browser/background/page.js | 92 ++++++------------- keepassxc-browser/keepassxc-browser.js | 14 +-- keepassxc-browser/options/options.js | 54 +++++------ keepassxc-browser/popups/popup.js | 6 +- keepassxc-browser/popups/popup_functions.js | 1 - 9 files changed, 143 insertions(+), 203 deletions(-) diff --git a/keepassxc-browser/background/browserAction.js b/keepassxc-browser/background/browserAction.js index 7c0dbfd..39fa94c 100644 --- a/keepassxc-browser/background/browserAction.js +++ b/keepassxc-browser/background/browserAction.js @@ -218,45 +218,43 @@ browserAction.removeRememberPopup = function(callback, tab, removeImmediately) { }; browserAction.setRememberPopup = function(tabId, username, password, url, usernameExists, credentialsList) { - browser.storage.local.get({'settings': {}}).then((item) => { - const settings = item.settings; - const id = tabId || page.currentTabId; - let timeoutMinMillis = Number(getValueOrDefault(settings, 'blinkMinTimeout', BLINK_TIMEOUT_REDIRECT_THRESHOLD_TIME_DEFAULT, 0)); + const settings = typeof(localStorage.settings) === 'undefined' ? {} : JSON.parse(localStorage.settings); + const id = tabId || page.currentTabId; + let timeoutMinMillis = Number(getValueOrDefault(settings, 'blinkMinTimeout', BLINK_TIMEOUT_REDIRECT_THRESHOLD_TIME_DEFAULT, 0)); - if (timeoutMinMillis > 0) { - timeoutMinMillis += Date.now(); - } + if (timeoutMinMillis > 0) { + timeoutMinMillis += Date.now(); + } - const blinkTimeout = getValueOrDefault(settings, 'blinkTimeout', BLINK_TIMEOUT_DEFAULT, 0); - const pageUpdateAllowance = getValueOrDefault(settings, 'allowedRedirect', BLINK_TIMEOUT_REDIRECT_COUNT_DEFAULT, 0); + const blinkTimeout = getValueOrDefault(settings, 'blinkTimeout', BLINK_TIMEOUT_DEFAULT, 0); + const pageUpdateAllowance = getValueOrDefault(settings, 'allowedRedirect', BLINK_TIMEOUT_REDIRECT_COUNT_DEFAULT, 0); - const stackData = { - visibleForMilliSeconds: blinkTimeout, - visibleForPageUpdates: pageUpdateAllowance, - redirectOffset: timeoutMinMillis, - level: 10, - intervalIcon: { - index: 0, - counter: 0, - max: 2, - icons: ['icon_remember_red_background_19x19.png', 'icon_remember_red_lock_19x19.png'] - }, - icon: 'icon_remember_red_background_19x19.png', - popup: 'popup_remember.html' - } + const stackData = { + visibleForMilliSeconds: blinkTimeout, + visibleForPageUpdates: pageUpdateAllowance, + redirectOffset: timeoutMinMillis, + level: 10, + intervalIcon: { + index: 0, + counter: 0, + max: 2, + icons: ['icon_remember_red_background_19x19.png', 'icon_remember_red_lock_19x19.png'] + }, + icon: 'icon_remember_red_background_19x19.png', + popup: 'popup_remember.html' + } - browserAction.stackPush(stackData, id); + browserAction.stackPush(stackData, id); - page.tabs[id].credentials = { - username: username, - password: password, - url: url, - usernameExists: usernameExists, - list: credentialsList - }; + page.tabs[id].credentials = { + username: username, + password: password, + url: url, + usernameExists: usernameExists, + list: credentialsList + }; - browserAction.show(null, {'id': id}); - }); + browserAction.show(null, {'id': id}); } function getValueOrDefault(settings, key, defaultVal, min) { diff --git a/keepassxc-browser/background/event.js b/keepassxc-browser/background/event.js index 5342edc..5070f69 100644 --- a/keepassxc-browser/background/event.js +++ b/keepassxc-browser/background/event.js @@ -101,32 +101,27 @@ event.showStatus = function(configured, tab, callback) { } event.onLoadSettings = function(callback, tab) { - browser.storage.local.get({'settings': {}}).then((item) => { - callback(item.settings); - }, (err) => { - console.log('error loading settings: ' + err); - }); + page.settings = (typeof(localStorage.settings) === 'undefined') ? {} : JSON.parse(localStorage.settings); } event.onLoadKeyRing = function(callback, tab) { - browser.storage.local.get({'keyRing': {}}).then(function(item) { - keepass.keyRing = JSON.parse(item.keyRing); - if (keepass.isAssociated() && !keepass.keyRing[keepass.associated.hash]) { - keepass.associated = { - "value": false, - "hash": null - }; - } - callback(JSON.parse(item.keyRing)); - }, (err) => { - console.log('error loading keyRing: ' + err); - }); + keepass.keyRing = (typeof(localStorage.keyRing) === 'undefined') ? {} : JSON.parse(localStorage.keyRing); + if (keepass.isAssociated() && !keepass.keyRing[keepass.associated.hash]) { + keepass.associated = { + value: false, + hash: null + }; + } +} + +event.onGetSettings = function(callback, tab) { + event.onLoadSettings(); + callback({ data: page.settings }); } event.onSaveSettings = function(callback, tab, settings) { - browser.storage.local.set({'settings': settings}).then(function() { - event.onLoadSettings(); - }); + localStorage.settings = JSON.stringify(settings); + event.onLoadSettings(); } event.onGetStatus = function(callback, tab) { @@ -233,11 +228,12 @@ event.onMultipleFieldsPopup = function(callback, tab) { browserAction.show(null, tab); } -event.onPageClearLogins = function(callback, tab) { +event.pageClearLogins = function(callback, tab) { page.clearLogins(tab.id); callback(); } + // all methods named in this object have to be declared BEFORE this! event.messageHandlers = { 'add_credentials': keepass.addCredentials, @@ -246,11 +242,12 @@ event.messageHandlers = { 'check_update_keepassxc': event.onCheckUpdateKeePassXC, 'get_connected_database': event.onGetConnectedDatabase, 'get_keepassxc_versions': event.onGetKeePassXCVersions, + 'get_settings': event.onGetSettings, 'get_status': event.onGetStatus, 'get_tab_information': event.onGetTabInformation, 'load_keyring': event.onLoadKeyRing, 'load_settings': event.onLoadSettings, - 'page_clear_logins': event.onPageClearLogins, + 'page_clear_logins': event.pageClearLogins, 'pop_stack': event.onPopStack, 'popup_login': event.onLoginPopup, 'popup_multiple-fields': event.onMultipleFieldsPopup, diff --git a/keepassxc-browser/background/init.js b/keepassxc-browser/background/init.js index 5b1ca27..f231fea 100644 --- a/keepassxc-browser/background/init.js +++ b/keepassxc-browser/background/init.js @@ -1,21 +1,13 @@ -// since version 2.0 the extension is using a keyRing instead of a single key-name-pair -keepass.migrateKeyRing().then(() => { - // load settings - page.initSettings().then(() => { - // initial connection with KeePassHttp - keepass.connectToNative(); - keepass.generateNewKeyPair(); - keepass.changePublicKeys(null, (pkRes) => { - // create tab information structure for every opened tab - page.initOpenedTabs().then(() => { - keepass.getDatabaseHash((gdRes) => { - - }, null); - }); - }); - }); +keepass.convertKeyToKeyRing(); +page.initSettings(); +page.initOpenedTabs(); +keepass.connectToNative(); +keepass.generateNewKeyPair(); +keepass.changePublicKeys(null, (pkRes) => { + keepass.getDatabaseHash((gdRes) => {}, null); }); + // Milliseconds for intervall (e.g. to update browserAction) let _interval = 250; diff --git a/keepassxc-browser/background/keepass.js b/keepassxc-browser/background/keepass.js index ebb6e39..70051cf 100644 --- a/keepassxc-browser/background/keepass.js +++ b/keepassxc-browser/background/keepass.js @@ -64,14 +64,6 @@ const kpErrors = { } }; -browser.storage.local.get({ - 'latestKeePassXC': {'version': 0, 'versionParsed': 0, 'lastChecked': null}, - 'keyRing': {}}) - .then((item) => { - keepass.latestKeePassXC = item.latestKeePassXC; - keepass.keyRing = JSON.parse(item.keyRing); -}); - keepass.addCredentials = function(callback, tab, username, password, url) { keepass.updateCredentials(callback, tab, null, username, password, url); } @@ -376,7 +368,7 @@ keepass.testAssociation = function (callback, tab, triggerUnlock) { const nonce = nacl.randomBytes(keepass.keySize); const {dbid, dbkey} = keepass.getCryptoKey(); - if (dbkey === null || dbid === null) { + if (dbkey === null || dbid === null) { if (tab && page.tabs[tab.id]) { keepass.handleError(tab, kpErrors.NO_SAVED_DATABASES_FOUND); } @@ -559,29 +551,23 @@ keepass.isAssociated = function() { return (keepass.associated.value && keepass.associated.hash && keepass.associated.hash === keepass.databaseHash); } -keepass.migrateKeyRing = () => { - return new Promise((resolve, reject) => { - browser.storage.local.get('keyRing').then((item) => { - let keyring = item.keyRing; +keepass.convertKeyToKeyRing = function() { + if (keepass.keyId in localStorage && keepass.keyBody in localStorage && !('keyRing' in localStorage)) { + keepass.getDatabaseHash((hash) => { + keepass.saveKey(hash, localStorage[keepass.keyId], localStorage[keepass.keyBody]); + if ('keyRing' in localStorage) { - if (!keyring) { - keyring = JSON.parse(localStorage['keyRing']); - browser.storage.local.set({'keyRing': JSON.stringify(keyring)}); - } - delete localStorage['keyRing']; - } - if (keepass.keyId in localStorage && keepass.keyBody in localStorage) { - if (!keyring) { - const hash = keepass.getDatabaseHash(null); - keepass.saveKey(hash, localStorage[keepass.keyId], localStorage[keepass.keyBody]); - } delete localStorage[keepass.keyId]; delete localStorage[keepass.keyBody]; } - resolve(); - }); - }); -}; + }, null); + } + + if ('keyRing' in localStorage) { + delete localStorage[keepass.keyId]; + delete localStorage[keepass.keyBody]; + } +} keepass.saveKey = function(hash, id, key) { if (!(hash in keepass.keyRing)) { @@ -598,19 +584,19 @@ keepass.saveKey = function(hash, id, key) { keepass.keyRing[hash].key = key; keepass.keyRing[hash].hash = hash; } - browser.storage.local.set({'keyRing': JSON.stringify(keepass.keyRing)}); + localStorage.keyRing = JSON.stringify(keepass.keyRing); } keepass.updateLastUsed = function(hash) { if ((hash in keepass.keyRing)) { keepass.keyRing[hash].lastUsed = new Date(); - browser.storage.local.set({'keyRing': JSON.stringify(keepass.keyRing)}); + localStorage.keyRing = JSON.stringify(keepass.keyRing); } } keepass.deleteKey = function(hash) { delete keepass.keyRing[hash]; - browser.storage.local.set({'keyRing': JSON.stringify(keepass.keyRing)}); + localStorage.keyRing = JSON.stringify(keepass.keyRing); } keepass.setcurrentKeePassXCVersion = function(version) { @@ -649,7 +635,7 @@ keepass.checkForNewKeePassXCVersion = function() { } if (version !== -1) { - browser.storage.local.set({'latestKeePassXC': keepass.latestKeePassXC}); + localStorage.latestKeePassXC = JSON.stringify(keepass.latestKeePassXC); } }; diff --git a/keepassxc-browser/background/page.js b/keepassxc-browser/background/page.js index 7044c6a..c825f59 100644 --- a/keepassxc-browser/background/page.js +++ b/keepassxc-browser/background/page.js @@ -12,74 +12,40 @@ var page = {}; page.tabs = {}; page.currentTabId = -1; +page.settings = (typeof(localStorage.settings) === 'undefined') ? {} : JSON.parse(localStorage.settings); page.blockedTabs = {}; -page.migrateSettings = () => { - return new Promise((resolve, reject) => { - const old = localStorage.getItem('settings'); - if (old) { - const settings = JSON.parse(old); - browser.storage.local.set({'settings': settings}).then(() => { - localStorage.removeItem('settings'); - resolve(obj); - }); - } else { - event.onLoadSettings((settings) => { - resolve(settings); - }); - } - }); -}; - page.initSettings = function() { - return new Promise((resolve, reject) => { - page.migrateSettings().then((settings) => { - page.settings = settings; - if (!('checkUpdateKeePassXC' in page.settings)) { - page.settings.checkUpdateKeePassXC = defaultSettings.checkUpdateKeePassXC; - } - if (!('autoCompleteUsernames' in page.settings)) { - page.settings.autoCompleteUsernames = defaultSettings.autoCompleteUsernames; - } - if (!('autoFillAndSend' in page.settings)) { - page.settings.autoFillAndSend = defaultSettings.autoFillAndSend; - } - if (!('usePasswordGenerator' in page.settings)) { - page.settings.usePasswordGenerator = defaultSettings.usePasswordGenerator; - } - if (!('autoFillSingleEntry' in page.settings)) { - page.settings.autoFillSingleEntry = defaultSettings.autoFillSingleEntry; - } - if (!('autoRetrieveCredentials' in page.settings)) { - page.settings.autoRetrieveCredentials = defaultSettings.autoRetrieveCredentials; - } - if (!('port' in page.settings)) { - page.settings.port = defaultSettings.proxyPort; - } - browser.storage.local.set({'settings': page.settings}); - resolve(); - }); - }); + event.onLoadSettings(); + if (!('checkUpdateKeePassXC' in page.settings)) { + page.settings.checkUpdateKeePassXC = defaultSettings.checkUpdateKeePassXC; + } + if (!('autoCompleteUsernames' in page.settings)) { + page.settings.autoCompleteUsernames = defaultSettings.autoCompleteUsernames; + } + if (!('autoFillAndSend' in page.settings)) { + page.settings.autoFillAndSend = defaultSettings.autoFillAndSend; + } + if (!('usePasswordGenerator' in page.settings)) { + page.settings.usePasswordGenerator = defaultSettings.usePasswordGenerator; + } + if (!('autoFillSingleEntry' in page.settings)) { + page.settings.autoFillSingleEntry = defaultSettings.autoFillSingleEntry; + } + if (!('autoRetrieveCredentials' in page.settings)) { + page.settings.autoRetrieveCredentials = defaultSettings.autoRetrieveCredentials; + } + if (!('port' in page.settings)) { + page.settings.port = defaultSettings.proxyPort; + } + localStorage.settings = JSON.stringify(page.settings); } page.initOpenedTabs = function() { - return new Promise((resolve, reject) => { - browser.tabs.query({}).then(function (tabs) { - for (var i = 0; i < tabs.length; i++) { - page.createTabEntry(tabs[i].id); - } - - // set initial tab-ID - browser.tabs.query({ "active": true, "currentWindow": true }).then(function (tabs) { - if (tabs.length === 0) { - resolve(); - return; // For example: only the background devtools or a popup are opened - } - page.currentTabId = tabs[0].id; - browserAction.show(null, tabs[0]); - resolve(); - }); - }); + browser.tabs.query({}).then((tabs) => { + for (const i of tabs) { + page.createTabEntry(i.id); + } }); } @@ -126,7 +92,7 @@ page.createTabEntry = function(tabId) { page.removePageInformationFromNotExistingTabs = function() { let rand = Math.floor(Math.random()*1001); if (rand === 28) { - browser.tabs.query({}).then(function(tabs) { + browser.tabs.query({}, (tabs) => { let $tabIds = {}; const $infoIds = Object.keys(page.tabs); diff --git a/keepassxc-browser/keepassxc-browser.js b/keepassxc-browser/keepassxc-browser.js index b117598..7975192 100644 --- a/keepassxc-browser/keepassxc-browser.js +++ b/keepassxc-browser/keepassxc-browser.js @@ -52,9 +52,9 @@ browser.runtime.onMessage.addListener(function(req, sender, callback) { } else if (req.action === 'redetect_fields') { browser.runtime.sendMessage({ - action: 'load_settings', - }).then((settings) => { - cip.settings = settings; + action: 'get_settings', + }).then((response) => { + cip.settings = response.data; cip.initCredentialFields(true); }); } @@ -1111,9 +1111,9 @@ jQuery(function() { cip.init = function() { browser.runtime.sendMessage({ - action: 'load_settings', - }).then((settings) => { - cip.settings = settings; + action: 'get_settings', + }).then((response) => { + cip.settings = response.data; cip.initCredentialFields(); }); } @@ -1175,7 +1175,7 @@ cip.receiveCredentialsIfNecessary = function () { } } -cip.retrieveCredentialsCallback = function(credentials, dontAutoFillIn) { +cip.retrieveCredentialsCallback = function (credentials, dontAutoFillIn) { if (cipFields.combinations.length > 0) { cip.u = _f(cipFields.combinations[0].username); cip.p = _f(cipFields.combinations[0].password); diff --git a/keepassxc-browser/options/options.js b/keepassxc-browser/options/options.js index b5c678a..2b94fd6 100644 --- a/keepassxc-browser/options/options.js +++ b/keepassxc-browser/options/options.js @@ -3,34 +3,17 @@ if (jQuery) { } $(function() { - browser.runtime.sendMessage({ action: 'load_settings' }).then((settings) => { - options.settings = settings; - browser.runtime.sendMessage({ action: 'load_keyring' }).then((keyRing) => { - options.keyRing = keyRing; - options.initMenu(); - options.initGeneralSettings(); - options.initConnectedDatabases(); - options.initSpecifiedCredentialFields(); - options.initAbout(); - }); - }); + options.initMenu(); + options.initGeneralSettings(); + options.initConnectedDatabases(); + options.initSpecifiedCredentialFields(); + options.initAbout(); }); var options = options || {}; -options.saveSettings = function() { - browser.storage.local.set({'settings': options.settings}); - browser.runtime.sendMessage({ - action: 'load_settings' - }); -}; - -options.saveKeyRing = function() { - browser.storage.local.set({'keyRing': JSON.stringify(options.keyRing)}); - browser.runtime.sendMessage({ - action: 'load_keyring' - }); -}; +options.settings = typeof(localStorage.settings) === 'undefined' ? {} : JSON.parse(localStorage.settings); +options.keyRing = typeof(localStorage.keyRing) === 'undefined' ? {} : JSON.parse(localStorage.keyRing); options.initMenu = function() { $('.navbar:first ul.nav:first li a').click(function(e) { @@ -48,7 +31,26 @@ options.saveSetting = function(name) { const $id = '#' + name; $($id).closest('.control-group').removeClass('error').addClass('success'); setTimeout(() => { $($id).closest('.control-group').removeClass('success') }, 2500); - options.saveSettings(); + + localStorage.settings = JSON.stringify(options.settings); + + browser.runtime.sendMessage({ + action: 'load_settings' + }); +} + +options.saveSettings = function() { + localStorage.settings = JSON.stringify(options.settings); + browser.runtime.sendMessage({ + action: 'load_settings' + }); +} + +options.saveKeyRing = function() { + localStorage.keyRing = JSON.stringify(options.keyRing); + browser.runtime.sendMessage({ + action: 'load_keyring' + }); } options.initGeneralSettings = function() { @@ -220,7 +222,7 @@ options.initSpecifiedCredentialFields = function() { delete options.settings['defined-credential-fields'][$url]; options.saveSettings(); - if($('#tab-specified-fields table tbody:first tr').length > 2) { + if ($('#tab-specified-fields table tbody:first tr').length > 2) { $('#tab-specified-fields table tbody:first tr.empty:first').hide(); } else { diff --git a/keepassxc-browser/popups/popup.js b/keepassxc-browser/popups/popup.js index f902671..7d12e60 100644 --- a/keepassxc-browser/popups/popup.js +++ b/keepassxc-browser/popups/popup.js @@ -63,7 +63,7 @@ $(function() { }); $('#redetect-fields-button').click(function() { - browser.tabs.query({"active": true, "currentWindow": true}).then((tabs) => { + browser.tabs.query({"active": true, "currentWindow": true}).then(function(tabs) { if (tabs.length === 0) return; // For example: only the background devtools or a popup are opened let tab = tabs[0]; @@ -75,6 +75,6 @@ $(function() { }); browser.runtime.sendMessage({ - action: 'get_status' + action: "get_status" }).then(status_response); -}); \ No newline at end of file +}); diff --git a/keepassxc-browser/popups/popup_functions.js b/keepassxc-browser/popups/popup_functions.js index 39fc703..3417757 100644 --- a/keepassxc-browser/popups/popup_functions.js +++ b/keepassxc-browser/popups/popup_functions.js @@ -1,5 +1,4 @@ var $ = jQuery.noConflict(true); -//var _settings = typeof(localStorage.settings) ==='undefined' ? {} : JSON.parse(localStorage.settings); function updateAvailableResponse(available) { if (available) {