From f88bb005cc0a981ff2f59b6c2389c1040f1762be Mon Sep 17 00:00:00 2001 From: eelke Date: Sun, 4 Jul 2021 20:07:20 +0200 Subject: [PATCH] cleanup ConnectionController --- pglab/ConnectionController.cpp | 90 ++++++++++++++++++++++------------ pglab/ConnectionController.h | 14 ++++++ 2 files changed, 73 insertions(+), 31 deletions(-) diff --git a/pglab/ConnectionController.cpp b/pglab/ConnectionController.cpp index 3fbd619..7824d3d 100644 --- a/pglab/ConnectionController.cpp +++ b/pglab/ConnectionController.cpp @@ -83,14 +83,14 @@ void ConnectionController::createConnection() { ConnectionConfig cc; cc.setUuid(QUuid::createUuid()); - ConnectionConfigurationWidget::editExistingInWindow(this, cc, [this] (ConnectionConfigurationWidget &w) -> void { saveConnection(w); }); + editConfig(cc); } void ConnectionController::editConnection(QModelIndex index) { auto config = ConnectionTreeModel::getConfigFromModelIndex(index); if (config) { - ConnectionConfigurationWidget::editExistingInWindow(this, *config, [this] (ConnectionConfigurationWidget &w) -> void { saveConnection(w); }); + editConfig(*config); } } @@ -100,10 +100,16 @@ void ConnectionController::editCopy(QModelIndex index) if (config) { auto cc = *config; cc.setUuid(QUuid::createUuid()); - ConnectionConfigurationWidget::editExistingInWindow(this, cc, [this] (ConnectionConfigurationWidget &w) -> void { saveConnection(w); }); + editConfig(cc); } } +void ConnectionController::editConfig(ConnectionConfig &cc) +{ + ConnectionConfigurationWidget::editExistingInWindow(this, cc, + [this] (ConnectionConfigurationWidget &w) -> void { saveConnection(w); }); +} + void ConnectionController::saveConnection(ConnectionConfigurationWidget &w) { auto cc = w.data(); @@ -219,45 +225,67 @@ bool ConnectionController::UnlockPasswordManagerIfNeeded() while (true) { // ask user for passphrase - auto dlg = std::make_unique(PasswordPromptDialog::RememberPassword, nullptr); - dlg->setCaption(tr("Unlock password manager")); - dlg->setDescription(tr("Enter password for password manager")); - int exec_result = dlg->exec(); - bool ok = (exec_result == QDialog::Accepted); + PassphraseResult pp_result = PassphrasePrompt(); + if (!pp_result.success) + break; // leave this retry loop - if (!ok) { - // leave this retry loop - break; - } // user gave OK, if succeeds return true otherwise loop a prompt for password again. - if (m_passwordManager->openDatabase(user_cfg_db, dlg->password())) { - int rem = dlg->remember(); - if (rem >= 0) { - int timeout = rem * 60 * 1000; /// rem is in minutes, timeout in millisec - m_relockTimer->start(timeout); - } + if (m_passwordManager->openDatabase(user_cfg_db, pp_result.passphrase)) { + setRelockTimer(pp_result.rememberForMinutes); return true; } } } else { - // Ask user for passphrase + confirmation, clearly instruct this is first setup - // create - auto dlg = std::make_unique(PasswordPromptDialog::ConfirmPassword, nullptr); - dlg->setCaption(tr("Password manager setup")); - dlg->setDescription(tr("Enter a strong password for password manager initialization. A strong key will be " - "derived from your password and it will be impossible to recover anything from the " - "password manager without the password you enter here.")); - int exec_result = dlg->exec(); - if (exec_result == QDialog::Accepted) { - QString passphrase = dlg->password(); - if (m_passwordManager->createDatabase(user_cfg_db, passphrase)) - return true; - } + InitializePasswordManager(); } return false; } +void ConnectionController::setRelockTimer(int rem_minutes) +{ + if (rem_minutes >= 0) { + int timeout = rem_minutes * 60 * 1000; /// rem is in minutes, timeout in millisec + m_relockTimer->start(timeout); + } +} + +PassphraseResult ConnectionController::PassphrasePrompt() +{ + auto dlg = std::make_unique(PasswordPromptDialog::RememberPassword, nullptr); + dlg->setCaption(tr("Unlock password manager")); + dlg->setDescription(tr("Enter password for password manager")); + int exec_result = dlg->exec(); + bool ok = (exec_result == QDialog::Accepted); + + PassphraseResult result; + result.success = ok; + if (ok) { + result.passphrase = dlg->password(); + result.rememberForMinutes = dlg->remember(); + } + return result; +} + +bool ConnectionController::InitializePasswordManager() +{ + // Ask user for passphrase + confirmation, clearly instruct this is first setup + // create + auto dlg = std::make_unique(PasswordPromptDialog::ConfirmPassword, nullptr); + dlg->setCaption(tr("Password manager setup")); + dlg->setDescription(tr("Enter a strong password for password manager initialization. A strong key will be " + "derived from your password and it will be impossible to recover anything from the " + "password manager without the password you enter here.")); + int exec_result = dlg->exec(); + if (exec_result == QDialog::Accepted) { + QString passphrase = dlg->password(); + auto&& user_cfg_db = m_masterController->userConfigDatabase(); + if (m_passwordManager->createDatabase(user_cfg_db, passphrase)) + return true; + } + return false; +} + std::string ConnectionController::getPskId(QUuid connectionid) { std::string id = "dbpw/"; diff --git a/pglab/ConnectionController.h b/pglab/ConnectionController.h index d19a93c..71930a5 100644 --- a/pglab/ConnectionController.h +++ b/pglab/ConnectionController.h @@ -13,6 +13,15 @@ class ConnectionManagerWindow; class PasswordManager; class QTimer; + +class PassphraseResult { +public: + bool success; + QString passphrase; + int rememberForMinutes; +}; + + class ConnectionController : public QObject { Q_OBJECT public: @@ -69,7 +78,12 @@ private: static std::string getPskId(QUuid connectionid); + void editConfig(ConnectionConfig &cc); void saveConnection(ConnectionConfigurationWidget &w); + void setRelockTimer(int rem_minutes); + PassphraseResult PassphrasePrompt(); + /// Asks user for new passphares and initialize the password manager. + bool InitializePasswordManager(); private slots: void relock(); };