From 0ad4475fa827181fc8d430c5d09dbdb29a47387b Mon Sep 17 00:00:00 2001 From: varjolintu Date: Mon, 6 Aug 2018 09:10:25 +0300 Subject: [PATCH] Fixed updating credentials from the context menu --- keepassxc-browser/popups/popup_remember.js | 49 ++++++++++++++++++---- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/keepassxc-browser/popups/popup_remember.js b/keepassxc-browser/popups/popup_remember.js index 13c04ae..fd1b9ca 100644 --- a/keepassxc-browser/popups/popup_remember.js +++ b/keepassxc-browser/popups/popup_remember.js @@ -5,17 +5,22 @@ var _tab; function _initialize(tab) { _tab = tab; - // no credentials set or credentials already cleared - if (!_tab.credentials.username) { + // No credentials set or credentials already cleared + if (!_tab.credentials.username && !_tab.credentials.password) { _close(); return; } - // no existing credentials to update --> disable update-button + // No existing credentials to update --> disable Update button if (_tab.credentials.list.length === 0) { $('#btn-update').attr('disabled', true).removeClass('btn-warning'); } + // No username available. This might be because of trigger from context menu --> disable New button + if (!_tab.credentials.username && _tab.credentials.password) { + $('#btn-new').attr('disabled', true).removeClass('btn-success'); + } + let url = _tab.credentials.url; url = (url.length > 50) ? url.substring(0, 50) + '...' : url; $('.information-url:first span:first').text(url); @@ -31,8 +36,13 @@ function _initialize(tab) { $('#btn-update').click(function(e) { e.preventDefault(); - // only one entry which could be updated + // Only one entry which could be updated if(_tab.credentials.list.length === 1) { + // Use the current username if it's empty + if (!_tab.credentials.username) { + _tab.credentials.username = _tab.credentials.list[0].login; + } + browser.runtime.sendMessage({ action: 'update_credentials', args: [_tab.credentials.list[0].uuid, _tab.credentials.username, _tab.credentials.password, _tab.credentials.url] @@ -58,10 +68,35 @@ function _initialize(tab) { .data('entryId', i) .click(function(e) { e.preventDefault(); + const entryId = $(this).data('entryId'); + + // Use the current username if it's empty + if (!_tab.credentials.username) { + _tab.credentials.username = _tab.credentials.list[entryId].login; + } + + // Check if the password has changed for the updated credentials browser.runtime.sendMessage({ - action: 'update_credentials', - args: [_tab.credentials.list[$(this).data('entryId')].uuid, _tab.credentials.username, _tab.credentials.password, _tab.credentials.url] - }).then(_verifyResult); + action: 'retrieve_credentials', + args: [ url, '', false, true ] + }).then((credentials) => { + if (!credentials || credentials.length !== _tab.credentials.list.length) { + _verifyResult('error'); + return; + } + + // Show a notification if the user tries to update credentials using the old password + if (credentials[entryId].password === _tab.credentials.password) { + showNotification('Error: Credentials not updated. The password has not been changed.'); + _close(); + return; + } + + browser.runtime.sendMessage({ + action: 'update_credentials', + args: [_tab.credentials.list[entryId].uuid, _tab.credentials.username, _tab.credentials.password, _tab.credentials.url] + }).then(_verifyResult); + }); }); if (_tab.credentials.usernameExists && _tab.credentials.username === _tab.credentials.list[i].login) {