cleanup ConnectionController
This commit is contained in:
parent
87cfb84997
commit
f88bb005cc
2 changed files with 73 additions and 31 deletions
|
|
@ -83,14 +83,14 @@ void ConnectionController::createConnection()
|
||||||
{
|
{
|
||||||
ConnectionConfig cc;
|
ConnectionConfig cc;
|
||||||
cc.setUuid(QUuid::createUuid());
|
cc.setUuid(QUuid::createUuid());
|
||||||
ConnectionConfigurationWidget::editExistingInWindow(this, cc, [this] (ConnectionConfigurationWidget &w) -> void { saveConnection(w); });
|
editConfig(cc);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConnectionController::editConnection(QModelIndex index)
|
void ConnectionController::editConnection(QModelIndex index)
|
||||||
{
|
{
|
||||||
auto config = ConnectionTreeModel::getConfigFromModelIndex(index);
|
auto config = ConnectionTreeModel::getConfigFromModelIndex(index);
|
||||||
if (config) {
|
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) {
|
if (config) {
|
||||||
auto cc = *config;
|
auto cc = *config;
|
||||||
cc.setUuid(QUuid::createUuid());
|
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)
|
void ConnectionController::saveConnection(ConnectionConfigurationWidget &w)
|
||||||
{
|
{
|
||||||
auto cc = w.data();
|
auto cc = w.data();
|
||||||
|
|
@ -219,45 +225,67 @@ bool ConnectionController::UnlockPasswordManagerIfNeeded()
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
// ask user for passphrase
|
// ask user for passphrase
|
||||||
auto dlg = std::make_unique<PasswordPromptDialog>(PasswordPromptDialog::RememberPassword, nullptr);
|
PassphraseResult pp_result = PassphrasePrompt();
|
||||||
dlg->setCaption(tr("Unlock password manager"));
|
if (!pp_result.success)
|
||||||
dlg->setDescription(tr("Enter password for password manager"));
|
break; // leave this retry loop
|
||||||
int exec_result = dlg->exec();
|
|
||||||
bool ok = (exec_result == QDialog::Accepted);
|
|
||||||
|
|
||||||
if (!ok) {
|
|
||||||
// leave this retry loop
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// user gave OK, if succeeds return true otherwise loop a prompt for password again.
|
// user gave OK, if succeeds return true otherwise loop a prompt for password again.
|
||||||
if (m_passwordManager->openDatabase(user_cfg_db, dlg->password())) {
|
if (m_passwordManager->openDatabase(user_cfg_db, pp_result.passphrase)) {
|
||||||
int rem = dlg->remember();
|
setRelockTimer(pp_result.rememberForMinutes);
|
||||||
if (rem >= 0) {
|
|
||||||
int timeout = rem * 60 * 1000; /// rem is in minutes, timeout in millisec
|
|
||||||
m_relockTimer->start(timeout);
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Ask user for passphrase + confirmation, clearly instruct this is first setup
|
InitializePasswordManager();
|
||||||
// create
|
|
||||||
auto dlg = std::make_unique<PasswordPromptDialog>(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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return false;
|
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>(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>(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 ConnectionController::getPskId(QUuid connectionid)
|
||||||
{
|
{
|
||||||
std::string id = "dbpw/";
|
std::string id = "dbpw/";
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,15 @@ class ConnectionManagerWindow;
|
||||||
class PasswordManager;
|
class PasswordManager;
|
||||||
class QTimer;
|
class QTimer;
|
||||||
|
|
||||||
|
|
||||||
|
class PassphraseResult {
|
||||||
|
public:
|
||||||
|
bool success;
|
||||||
|
QString passphrase;
|
||||||
|
int rememberForMinutes;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
class ConnectionController : public QObject {
|
class ConnectionController : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
|
|
@ -69,7 +78,12 @@ private:
|
||||||
|
|
||||||
static std::string getPskId(QUuid connectionid);
|
static std::string getPskId(QUuid connectionid);
|
||||||
|
|
||||||
|
void editConfig(ConnectionConfig &cc);
|
||||||
void saveConnection(ConnectionConfigurationWidget &w);
|
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:
|
private slots:
|
||||||
void relock();
|
void relock();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue