diff --git a/keepassxc-browser/background/event.js b/keepassxc-browser/background/event.js index be7b1e6..8d1b39c 100755 --- a/keepassxc-browser/background/event.js +++ b/keepassxc-browser/background/event.js @@ -158,22 +158,8 @@ kpxcEvent.onReconnect = function(callback, tab) { // Add a small timeout after reconnecting. Just to make sure. It's not pretty, I know :( setTimeout(() => { - keepass.generateNewKeyPair(); - keepass.changePublicKeys(tab).then((pkRes) => { - keepass.getDatabaseHash((gdRes) => { - if (gdRes) { - keepass.testAssociation((response) => { - keepass.isConfigured().then((configured) => { - browser.tabs.sendMessage(tab.id, { - action: 'redetect_fields' - }); - kpxcEvent.showStatus(configured, tab, callback); - }).catch((e) => { - console.log(e); - }); - }, tab); - } - }, null); + keepass.reconnect(callback, tab).then((configured) => { + kpxcEvent.showStatus(configured, tab, callback); }); }, 500); }; diff --git a/keepassxc-browser/background/init.js b/keepassxc-browser/background/init.js index ce7fed7..0ecc02e 100644 --- a/keepassxc-browser/background/init.js +++ b/keepassxc-browser/background/init.js @@ -5,6 +5,7 @@ keepass.migrateKeyRing().then(() => { page.initOpenedTabs().then(() => { httpAuth.init(); keepass.connectToNative(); + keepass.enableAutomaticReconnect(); keepass.generateNewKeyPair(); keepass.changePublicKeys(null).then((pkRes) => { keepass.getDatabaseHash((gdRes) => {}, null); diff --git a/keepassxc-browser/background/keepass.js b/keepassxc-browser/background/keepass.js index 6dec6aa..e8fe811 100755 --- a/keepassxc-browser/background/keepass.js +++ b/keepassxc-browser/background/keepass.js @@ -838,20 +838,7 @@ keepass.onNativeMessage = function(response) { if (response.action === kpActions.DATABASE_LOCKED || response.action === kpActions.DATABASE_UNLOCKED) { keepass.testAssociation((associationResponse) => { keepass.isConfigured().then((configured) => { - let data = page.tabs[page.currentTabId].stack[page.tabs[page.currentTabId].stack.length - 1]; - data.iconType = configured ? 'normal' : 'cross'; - browserAction.show(null, {'id': page.currentTabId}); - - // Send message to content script - browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => { - if (tabs.length) { - browser.tabs.sendMessage(tabs[0].id, { - action: 'check_database_hash', - hash: {old: kpxcEvent.previousDatabaseHash, new: keepass.databaseHash} - }); - keepass.previousDatabaseHash = keepass.databaseHash; - } - }); + keepass.updatePopup(configured ? 'normal' : 'cross'); }); }, null); } @@ -864,6 +851,8 @@ function onDisconnected() { keepass.isKeePassXCAvailable = false; keepass.associated.value = false; keepass.associated.hash = null; + page.clearCredentials(page.currentTabId, true); + keepass.updatePopup('cross'); console.log('Failed to connect: ' + (browser.runtime.lastError === null ? 'Unknown error' : browser.runtime.lastError.message)); } @@ -1017,3 +1006,42 @@ keepass.decrypt = function(input, nonce) { const res = nacl.box.open(m, n, keepass.serverPublicKey, keepass.keyPair.secretKey); return res; }; + +keepass.enableAutomaticReconnect = function() { + setInterval(() => { + if (!keepass.isKeePassXCAvailable) { + keepass.connectToNative(); + keepass.reconnect(); + } + }, 1000); +}; + +keepass.reconnect = function(callback, tab) { + return new Promise((resolve, reject) => { + keepass.generateNewKeyPair(); + keepass.changePublicKeys(tab).then((pkRes) => { + // Database hash should be received if the reconnection succeeded + keepass.getDatabaseHash((gdRes) => { + if (!gdRes) { + reject(false); + return; + } + + keepass.testAssociation((response) => { + keepass.isConfigured().then((configured) => { + resolve(configured); + }).catch((e) => { + console.log(e); + reject(e); + }); + }, tab); + }, null); + }); + }); +}; + +keepass.updatePopup = function(iconType) { + const data = page.tabs[page.currentTabId].stack[page.tabs[page.currentTabId].stack.length - 1]; + data.iconType = iconType; + browserAction.show(null, {'id': page.currentTabId}); +};