diff --git a/keepassxc-browser/background/event.js b/keepassxc-browser/background/event.js index 2c0a831..e8d4a6f 100755 --- a/keepassxc-browser/background/event.js +++ b/keepassxc-browser/background/event.js @@ -16,17 +16,6 @@ kpxcEvent.getColorTheme = async function(tab) { }; kpxcEvent.getConnectedDatabase = async function() { - /*let hash = keepass.associated.hash; - - // Check if one database is open but not active - // TODO: Does not work if two are open and active one is closed and non-associated - const associatedDatabases = keepass.databaseStatuses.statuses.filter(ds => ds.associated); - if (associatedDatabases.length === 1 - && keepass.databaseAssociationStatuses.isAnyAssociated - && !keepass.associated.value) { - hash = associatedDatabases[0].hash; - }*/ - return Promise.resolve({ count: Object.keys(keepass.keyRing).length, identifier: (keepass.keyRing[keepass.associated.hash]) ? keepass.keyRing[keepass.associated.hash].id : null @@ -38,16 +27,9 @@ kpxcEvent.getIsKeePassXCAvailable = async function() { }; kpxcEvent.getKeePassXCVersions = async function(tab) { - // TODO: Maybe this is not needed? - /*if (keepass.currentKeePassXC === '') { - await keepass.getDatabaseHash(tab); // TODO: How to get just the version? A separate API call? - return { 'current': keepass.currentKeePassXC, 'latest': keepass.latestKeePassXC.version }; - }*/ - return { 'current': keepass.currentKeePassXC, 'latest': keepass.latestKeePassXC.version }; }; -// TODO: Refactor. This is ugly. internalPoll needs to be handled with V2. kpxcEvent.getStatus = async function(tab, args = []) { // When internalPoll is true the event is triggered from content script in intervals -> don't poll KeePassXC try { @@ -55,38 +37,21 @@ kpxcEvent.getStatus = async function(tab, args = []) { let configured = false; if (keepass.protocolV2) { - if (!internalPoll) { - const response = await protocol.testAssociationFromDatabaseStatuses(tab, [ true, triggerUnlock ]); - configured = response.isAnyAssociated; - } else { - // TODO: This does not update when db is locked or just opened - configured = keepass.databaseAssociationStatuses?.isAnyAssociated; // ? - } - } else { - if (!internalPoll) { - const response = await keepassProtocol.testAssociation(tab, [ true, triggerUnlock ]); - if (!response) { - return kpxcEvent.showStatus(tab, false); - } - } - - configured = await keepass.isConfigured(); + configured = internalPoll + ? keepass.databaseAssociationStatuses?.isAnyAssociated + : await protocol.testAssociationFromDatabaseStatuses(tab, [ true, triggerUnlock ])?.isAnyAssociated; + return kpxcEvent.showStatus(tab, configured, internalPoll); } - /*if (keepass.protocolV2) { - const response = await protocol.testAssociationFromDatabaseStatuses(tab, [ true, triggerUnlock ]); - configured = response.isAnyAssociated; - } else { - if (!internalPoll) { - const response = await keepassProtocol.testAssociation(tab, [ true, triggerUnlock ]); - if (!response) { - return kpxcEvent.showStatus(tab, false); - } + // Protocol V1 + if (!internalPoll) { + const response = await keepassProtocol.testAssociation(tab, [ true, triggerUnlock ]); + if (!response) { + return kpxcEvent.showStatus(tab, false); } + } - configured = await keepass.isConfigured(); - }*/ - + configured = await keepass.isConfigured(); return kpxcEvent.showStatus(tab, configured, internalPoll); } catch (err) { logError('No status shown: ' + err); diff --git a/keepassxc-browser/background/protocol.js b/keepassxc-browser/background/protocol.js index fb3f9a7..c082d0d 100644 --- a/keepassxc-browser/background/protocol.js +++ b/keepassxc-browser/background/protocol.js @@ -116,7 +116,7 @@ protocol.createNewGroup = async function(tab, args = []) { // TODO: Handle errors const response = await protocolClient.sendMessage(tab, messageData); if (response) { - keepass.updateLastUsed(keepass.databaseHash); // TODO: Remove? + keepass.updateLastUsed(keepass.databaseHash); return response; } else { logError('getDatabaseGroups rejected'); @@ -152,7 +152,7 @@ protocol.generatePassword = async function(tab, args = []) { } const password = response.entries ?? response.password; - keepass.updateLastUsed(keepass.databaseHash); // TODO: Remove? + keepass.updateLastUsed(keepass.databaseHash); return password; } else { logError('generatePassword rejected'); @@ -198,7 +198,7 @@ protocol.getCredentials = async function(tab, args = []) { } entries = keepass.removeDuplicateEntries(response.entries); - keepass.updateLastUsed(keepass.databaseHash); // TODO: Remove? + keepass.updateLastUsed(keepass.databaseHash); if (entries.length === 0) { // Questionmark-icon is not triggered, so we have to trigger for the normal symbol @@ -242,7 +242,7 @@ protocol.getDatabaseGroups = async function(tab, args = []) { groups = response.groups; groups.defaultGroup = page.settings.defaultGroup; groups.defaultGroupAlwaysAsk = page.settings.defaultGroupAlwaysAsk; - keepass.updateLastUsed(keepass.databaseHash); // TODO: Remove? + keepass.updateLastUsed(keepass.databaseHash); return groups; } @@ -375,7 +375,8 @@ protocol.testAssociationFromDatabaseStatuses = async function(tab, args = []) { areAllLocked: true, associationNeeded: false, databaseHash: undefined, - isAnyAssociated: false + isAnyAssociated: false, + isCurrentLocked: true }; if (!databaseStatuses || databaseStatuses.statuses.length === 0) { @@ -407,12 +408,13 @@ protocol.testAssociationFromDatabaseStatuses = async function(tab, args = []) { keepass.associated.value = isCurrentAssociated; // This should be true only if all databases are locked - keepass.isDatabaseClosed = areAllLocked; // ? + keepass.isDatabaseClosed = areAllLocked; result.areAllLocked = areAllLocked; result.associationNeeded = !isCurrentAssociated && !isCurrentLocked; result.databaseHash = databaseStatuses.hash; result.isAnyAssociated = isAnyAssociated; + result.isCurrentLocked = isCurrentLocked; keepass.databaseStatuses = databaseStatuses; keepass.databaseAssociationStatuses = result; @@ -451,6 +453,8 @@ protocol.updateCredentials = async function(tab, args = []) { try { const response = await protocolClient.sendMessage(tab, messageData); if (response) { + keepass.updateLastUsed(keepass.databaseHash); + if (response?.result === true) { return entryId ? AddCredentials.UPDATED : AddCredentials.CREATED; } diff --git a/keepassxc-browser/content/keepassxc-browser.js b/keepassxc-browser/content/keepassxc-browser.js index eb234f7..8894efc 100755 --- a/keepassxc-browser/content/keepassxc-browser.js +++ b/keepassxc-browser/content/keepassxc-browser.js @@ -16,6 +16,7 @@ const sendMessage = async function(action, args) { * The main content script object. */ const kpxc = {}; +kpxc.associationStatus = undefined; kpxc.combinations = []; kpxc.credentials = []; kpxc.databaseState = DatabaseState.DISCONNECTED; @@ -24,8 +25,8 @@ kpxc.improvedFieldDetectionEnabledForPage = false; kpxc.inputs = []; kpxc.settings = {}; kpxc.singleInputEnabledForPage = false; -kpxc.submitUrl = null; -kpxc.url = null; +kpxc.submitUrl = null; // TODO: To undefined +kpxc.url = null; // TODO: To undefined // Add page to Site Preferences with Username-only detection enabled. Set from the popup kpxc.addToSitePreferences = async function() { @@ -98,27 +99,20 @@ kpxc.createCombination = async function(activeElement, passOnly) { // Switch credentials if database is changed or closed kpxc.detectDatabaseChange = async function(response) { + kpxc.associationStatus = response?.associateResult; kpxc.databaseState = DatabaseState.LOCKED; kpxc.clearAllFromPage(); kpxcIcons.switchIcons(); - // TODO: This doesn't work well anymore. if (document.visibilityState !== 'hidden') { if (response.hash.new !== '') { _called.retrieveCredentials = false; - // Why is this needed? For the Connection Keys? const settings = await sendMessage('load_settings'); kpxc.settings = settings; - // TODO: Cleanup this.. - if (response.associateResult) { - if (!response.associateResult.areAllLocked) { - kpxc.databaseState = DatabaseState.UNLOCKED; - } - } else { - kpxc.databaseState = DatabaseState.UNLOCKED; // This is important to set correctly! - } + kpxc.databaseState = response?.associateResult?.areAllLocked + ? DatabaseState.LOCKED : DatabaseState.UNLOCKED; await kpxc.initCredentialFields(); kpxcIcons.switchIcons(); @@ -340,6 +334,16 @@ kpxc.initCredentialFields = async function() { await kpxcIcons.initIcons(kpxc.combinations); + // TODO: In optimal case, this should only trigger when a database is opened. How to detect that? + // Protocol V2 + if (kpxc.associationStatus) { + if (!kpxc.associationStatus.isCurrentLocked) { + await kpxc.retrieveCredentials(); + } + return; + } + + // Protocol V1 if (kpxc.databaseState === DatabaseState.UNLOCKED) { await kpxc.retrieveCredentials(); }