From 8bee3d937df19a7b5a0390a63142430331f2d6df Mon Sep 17 00:00:00 2001 From: "S. Razi Alavizadeh" Date: Mon, 26 May 2014 00:24:43 +0430 Subject: [PATCH] Improvement in asking of master-password. - Closes #1356 --- .../databaseencryptedpasswordbackend.cpp | 81 +++++++++++++------ .../databaseencryptedpasswordbackend.h | 20 +++++ 2 files changed, 76 insertions(+), 25 deletions(-) diff --git a/src/lib/autofill/passwordbackends/databaseencryptedpasswordbackend.cpp b/src/lib/autofill/passwordbackends/databaseencryptedpasswordbackend.cpp index 780673993..9711132ca 100644 --- a/src/lib/autofill/passwordbackends/databaseencryptedpasswordbackend.cpp +++ b/src/lib/autofill/passwordbackends/databaseencryptedpasswordbackend.cpp @@ -25,7 +25,6 @@ #include #include -#include #include #define INTERNAL_SERVER_ID QLatin1String("qupzilla.internal") @@ -281,32 +280,12 @@ bool DatabaseEncryptedPasswordBackend::hasPermission() m_askPasswordDialogVisible = true; - QInputDialog dialog; - dialog.setWindowModality(Qt::ApplicationModal); - dialog.setWindowTitle(AutoFill::tr("Enter Master Password")); - dialog.setLabelText(AutoFill::tr("Permission is required, please enter Master Password:")); - dialog.setTextEchoMode(QLineEdit::Password); + AskMasterPassword* dialog = new AskMasterPassword(this); - if (dialog.exec() == QDialog::Accepted && !dialog.textValue().isEmpty()) { - QByteArray enteredPassword = AesInterface::passwordToHash(dialog.textValue()); - if (!isPasswordVerified(enteredPassword)) { - QMessageBox::information(mApp->getWindow(), AutoFill::tr("Warning!"), AutoFill::tr("Entered password is wrong!")); - setAskMasterPasswordState(true); - - m_askPasswordDialogVisible = false; - return false; - } - else { - setAskMasterPasswordState(false); - //TODO: start timer for reset ask state to true - - m_askPasswordDialogVisible = false; - return true; - } - } + bool authorized = dialog->exec() == QDialog::Accepted; m_askPasswordDialogVisible = false; - return false; + return authorized; } bool DatabaseEncryptedPasswordBackend::isPasswordVerified(const QByteArray &password) @@ -517,7 +496,7 @@ void DatabaseEncryptedPasswordBackend::updateSampleData(const QByteArray &passwo #include MasterPasswordDialog::MasterPasswordDialog(DatabaseEncryptedPasswordBackend* backend, QWidget* parent) - : QDialog(parent, Qt::MSWindowsFixedSizeDialogHint) + : QDialog(parent, Qt::WindowStaysOnTopHint | Qt::MSWindowsFixedSizeDialogHint) , ui(new Ui::MasterPasswordDialog) , m_backend(backend) { @@ -670,3 +649,55 @@ bool MasterPasswordDialog::samePasswordEntry(const PasswordEntry &entry1, const } return true; } + + +AskMasterPassword::AskMasterPassword(DatabaseEncryptedPasswordBackend* backend, QWidget* parent) + : QDialog(parent, Qt::WindowStaysOnTopHint | Qt::MSWindowsFixedSizeDialogHint) + , m_backend(backend) +{ + setWindowModality(Qt::ApplicationModal); + setWindowTitle(AutoFill::tr("Enter Master Password")); + + QVBoxLayout* verticalLayout = new QVBoxLayout(this); + QLabel* label = new QLabel(this); + label->setText(AutoFill::tr("Permission is required, please enter Master Password:")); + m_lineEdit = new QLineEdit(this); + m_lineEdit->setEchoMode(QLineEdit::Password); + m_buttonBox = new QDialogButtonBox(this); + m_buttonBox->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Ok); + m_labelWarning = new QLabel(this); + m_labelWarning->setText(AutoFill::tr("Entered password is wrong!")); + QPalette pal = m_labelWarning->palette(); + pal.setBrush(QPalette::WindowText, Qt::red); + m_labelWarning->setPalette(pal); + m_labelWarning->hide(); + + verticalLayout->addWidget(label); + verticalLayout->addWidget(m_lineEdit); + verticalLayout->addWidget(m_labelWarning); + verticalLayout->addWidget(m_buttonBox); + setLayout(verticalLayout); + + connect(m_lineEdit, SIGNAL(returnPressed()), this, SLOT(verifyPassword())); + connect(m_buttonBox, SIGNAL(accepted()), this, SLOT(verifyPassword())); + connect(m_buttonBox, SIGNAL(rejected()), this, SLOT(reject())); + + setAttribute(Qt::WA_DeleteOnClose); +} + +void AskMasterPassword::verifyPassword() +{ + QByteArray enteredPassword = AesInterface::passwordToHash(m_lineEdit->text()); + if (!m_backend->isPasswordVerified(enteredPassword)) { + m_backend->setAskMasterPasswordState(true); + m_labelWarning->show(); + m_lineEdit->clear(); + m_lineEdit->setFocus(); + } + else { + m_backend->setAskMasterPasswordState(false); + //TODO: start timer for reset ask state to true + + accept(); + } +} diff --git a/src/lib/autofill/passwordbackends/databaseencryptedpasswordbackend.h b/src/lib/autofill/passwordbackends/databaseencryptedpasswordbackend.h index 6c70e5483..b14d3dac6 100644 --- a/src/lib/autofill/passwordbackends/databaseencryptedpasswordbackend.h +++ b/src/lib/autofill/passwordbackends/databaseencryptedpasswordbackend.h @@ -118,4 +118,24 @@ private: DatabaseEncryptedPasswordBackend* m_backend; }; +class QDialogButtonBox; +class QLineEdit; +class QLabel; + +class AskMasterPassword : public QDialog +{ + Q_OBJECT + +public: + explicit AskMasterPassword(DatabaseEncryptedPasswordBackend* backend, QWidget* parent = 0); + +private slots: + void verifyPassword(); + +private: + DatabaseEncryptedPasswordBackend* m_backend; + QDialogButtonBox* m_buttonBox; + QLineEdit* m_lineEdit; + QLabel* m_labelWarning; +}; #endif // DATABASEENCRYPTEDPASSWORDBACKEND_H