Store encrypted passwords with connections.
Closes #22 as encrypted password is now deleted as part of the connection record.
This commit is contained in:
parent
e5ae9663c4
commit
d489f11e52
11 changed files with 252 additions and 695 deletions
|
|
@ -25,10 +25,6 @@ ConnectionController::~ConnectionController()
|
|||
|
||||
void ConnectionController::init()
|
||||
{
|
||||
//std::string dbfilename = QDir::toNativeSeparators(GetUserConfigDatabaseName()).toUtf8().data();
|
||||
//m_userConfigDatabase = std::make_shared<Botan::Sqlite3_Database>(dbfilename);
|
||||
|
||||
|
||||
m_passwordManager = std::make_shared<PasswordManager>();
|
||||
|
||||
m_connectionTreeModel = new ConnectionTreeModel(this, m_masterController->userConfigDatabase());
|
||||
|
|
@ -36,7 +32,6 @@ void ConnectionController::init()
|
|||
|
||||
m_connectionManagerWindow = new ConnectionManagerWindow(m_masterController, nullptr);
|
||||
m_connectionManagerWindow->show();
|
||||
|
||||
}
|
||||
|
||||
void ConnectionController::showConnectionManager()
|
||||
|
|
@ -44,30 +39,9 @@ void ConnectionController::showConnectionManager()
|
|||
m_connectionManagerWindow->show();
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
ConnectionConfig* getConfigFromModelIndex(QModelIndex index)
|
||||
{
|
||||
if (!index.isValid())
|
||||
return nullptr;
|
||||
auto node = static_cast<ConnectionNode*>(index.internalPointer());
|
||||
return dynamic_cast<ConnectionConfig*>(node);
|
||||
}
|
||||
|
||||
ConnectionGroup* getGroupFromModelIndex(QModelIndex index)
|
||||
{
|
||||
if (!index.isValid())
|
||||
return nullptr;
|
||||
auto node = static_cast<ConnectionNode*>(index.internalPointer());
|
||||
return dynamic_cast<ConnectionGroup*>(node);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void ConnectionController::openSqlWindowForConnection(QModelIndex index)
|
||||
{
|
||||
auto config = getConfigFromModelIndex(index);
|
||||
auto config = ConnectionTreeModel::getConfigFromModelIndex(index);
|
||||
if (config) {
|
||||
|
||||
if (retrieveConnectionPassword(*config)) {
|
||||
|
|
@ -86,7 +60,7 @@ void ConnectionController::openSqlWindowForConnection(QModelIndex index)
|
|||
|
||||
void ConnectionController::openBackupDlgForConnection(QModelIndex index)
|
||||
{
|
||||
auto config = getConfigFromModelIndex(index);
|
||||
auto config = ConnectionTreeModel::getConfigFromModelIndex(index);
|
||||
if (config) {
|
||||
if (retrieveConnectionPassword(*config)) {
|
||||
m_connectionTreeModel->save(*config);
|
||||
|
|
@ -107,7 +81,7 @@ void ConnectionController::createConnection()
|
|||
|
||||
void ConnectionController::editConnection(QModelIndex index)
|
||||
{
|
||||
auto config = getConfigFromModelIndex(index);
|
||||
auto config = ConnectionTreeModel::getConfigFromModelIndex(index);
|
||||
if (config) {
|
||||
ConnectionConfigurationWidget::editExistingInWindow(this, *config);
|
||||
}
|
||||
|
|
@ -129,7 +103,7 @@ void ConnectionController::addGroup()
|
|||
|
||||
void ConnectionController::removeGroup(QModelIndex index)
|
||||
{
|
||||
auto group = getGroupFromModelIndex(index);
|
||||
auto group = ConnectionTreeModel::getGroupFromModelIndex(index);
|
||||
if (group) {
|
||||
auto btn = QMessageBox::question(nullptr, tr("Connection group"),
|
||||
tr("Remove the selected group and all connections contained in the group?"),
|
||||
|
|
@ -141,9 +115,14 @@ void ConnectionController::removeGroup(QModelIndex index)
|
|||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<PasswordManager> ConnectionController::passwordManager()
|
||||
{
|
||||
return m_passwordManager;
|
||||
}
|
||||
|
||||
void ConnectionController::openServerWindowForConnection(QModelIndex index)
|
||||
{
|
||||
auto config = getConfigFromModelIndex(index);
|
||||
auto config = ConnectionTreeModel::getConfigFromModelIndex(index);
|
||||
if (config) {
|
||||
if (retrieveConnectionPassword(*config)) {
|
||||
m_connectionTreeModel->save(*config);
|
||||
|
|
@ -155,16 +134,12 @@ void ConnectionController::openServerWindowForConnection(QModelIndex index)
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
bool ConnectionController::retrieveConnectionPassword(ConnectionConfig &cc)
|
||||
{
|
||||
auto pw_state = cc.passwordState();
|
||||
if (pw_state == PasswordState::NotNeeded) {
|
||||
return true;
|
||||
}
|
||||
else if (pw_state == PasswordState::SavedPasswordManager) {
|
||||
auto enc_pwd = cc.encodedPassword();
|
||||
if (!enc_pwd.empty()) {
|
||||
std::string pw;
|
||||
bool result = getPasswordFromPskdb(getPskId(cc), pw);
|
||||
bool result = decodePassword(getPskId(cc), cc.encodedPassword(), pw);// getPasswordFromPskdb(getPskId(cc), pw);
|
||||
if (result) {
|
||||
cc.setPassword(pw);
|
||||
return true;
|
||||
|
|
@ -182,30 +157,31 @@ bool ConnectionController::retrieveConnectionPassword(ConnectionConfig &cc)
|
|||
std::string password = dlg->password().toUtf8().data();
|
||||
cc.setPassword(password);
|
||||
if (dlg->saveChecked()) {
|
||||
storePasswordInPskdb(getPskId(cc), password);
|
||||
cc.setPasswordState(PasswordState::SavedPasswordManager);
|
||||
std::string encoded_pw;
|
||||
if (encodePassword(getPskId(cc), password, encoded_pw)) {
|
||||
cc.setEncodedPassword(encoded_pw);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool ConnectionController::getPasswordFromPskdb(const std::string &password_id, std::string &password)
|
||||
bool ConnectionController::decodePassword(const std::string &password_id, const std::string &enc_password, std::string &password)
|
||||
{
|
||||
if (!UnlockPasswordManagerIfNeeded())
|
||||
return false;
|
||||
|
||||
return m_passwordManager->get(password_id, password);
|
||||
password = m_passwordManager->decrypt(password_id, enc_password);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool ConnectionController::storePasswordInPskdb(const std::string &password_id, const std::string password)
|
||||
bool ConnectionController::encodePassword(const std::string &password_id, const std::string &password, std::string &enc_password)
|
||||
{
|
||||
if (!UnlockPasswordManagerIfNeeded())
|
||||
return false;
|
||||
|
||||
m_passwordManager->set(password_id, password);
|
||||
enc_password = m_passwordManager->encrypt(password_id, password);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue