File indexing completed on 2024-12-22 04:40:56

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2013-2014  S. Razi Alavizadeh <s.r.alavizadeh@gmail.com>
0004 * Copyright (C) 2013-2018  David Rosca <nowrep@gmail.com>
0005 *
0006 * This program is free software: you can redistribute it and/or modify
0007 * it under the terms of the GNU General Public License as published by
0008 * the Free Software Foundation, either version 3 of the License, or
0009 * (at your option) any later version.
0010 *
0011 * This program is distributed in the hope that it will be useful,
0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 * GNU General Public License for more details.
0015 *
0016 * You should have received a copy of the GNU General Public License
0017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0018 * ============================================================ */
0019 #ifndef DATABASEENCRYPTEDPASSWORDBACKEND_H
0020 #define DATABASEENCRYPTEDPASSWORDBACKEND_H
0021 
0022 #include "passwordbackend.h"
0023 #include "qzcommon.h"
0024 
0025 #include <QDialog>
0026 
0027 class AesInterface;
0028 class MasterPasswordDialog;
0029 
0030 class FALKON_EXPORT DatabaseEncryptedPasswordBackend : public PasswordBackend
0031 {
0032 public:
0033     enum MasterPasswordState {
0034         PasswordIsSetted,
0035         PasswordIsNotSetted,
0036         UnKnownState = -1
0037     };
0038 
0039     explicit DatabaseEncryptedPasswordBackend();
0040 
0041     ~DatabaseEncryptedPasswordBackend() override;
0042 
0043     QStringList getUsernames(const QUrl &url) override;
0044     QVector<PasswordEntry> getEntries(const QUrl &url) override;
0045     QVector<PasswordEntry> getAllEntries() override;
0046 
0047     void setActive(bool active) override;
0048 
0049     void addEntry(const PasswordEntry &entry) override;
0050     bool updateEntry(const PasswordEntry &entry) override;
0051     void updateLastUsed(PasswordEntry &entry) override;
0052 
0053     void removeEntry(const PasswordEntry &entry) override;
0054     void removeAll() override;
0055 
0056     QString name() const override;
0057 
0058     bool hasSettings() const override;
0059     void showSettings(QWidget* parent) override;
0060 
0061     bool isMasterPasswordSetted();
0062 
0063     QByteArray masterPassword() const;
0064 
0065     bool hasPermission();
0066     bool isPasswordVerified(const QByteArray &password);
0067 
0068     bool decryptPasswordEntry(PasswordEntry &entry, AesInterface* aesInterface);
0069     bool encryptPasswordEntry(PasswordEntry &entry, AesInterface* aesInterface);
0070 
0071     void tryToChangeMasterPassword(const QByteArray &newPassword);
0072     void removeMasterPassword();
0073 
0074     void setAskMasterPasswordState(bool ask);
0075 
0076     void encryptDataBaseTableOnFly(const QByteArray &decryptorPassword,
0077                                    const QByteArray &encryptorPassword);
0078 
0079     void updateSampleData(const QByteArray &password);
0080 
0081     void showMasterPasswordDialog();
0082 
0083 private:
0084     QByteArray someDataFromDatabase();
0085 
0086     MasterPasswordState m_stateOfMasterPassword;
0087     QByteArray m_someDataStoredOnDataBase;
0088 
0089     bool m_askPasswordDialogVisible;
0090     bool m_askMasterPassword;
0091     QByteArray m_masterPassword;
0092 };
0093 
0094 namespace Ui
0095 {
0096 class MasterPasswordDialog;
0097 }
0098 
0099 class MasterPasswordDialog : public QDialog
0100 {
0101     Q_OBJECT
0102 
0103 public:
0104     explicit MasterPasswordDialog(DatabaseEncryptedPasswordBackend* backend, QWidget* parent = nullptr);
0105     ~MasterPasswordDialog() override;
0106 
0107     void delayedExec();
0108 
0109 public Q_SLOTS:
0110     void accept() override;
0111     void reject() override;
0112     void showSettingPage();
0113     void showSetMasterPasswordPage();
0114     void clearMasterPasswordAndConvert(bool forcedAskPass = true);
0115     bool samePasswordEntry(const PasswordEntry &entry1, const PasswordEntry &entry2);
0116 
0117 private:
0118     Ui::MasterPasswordDialog* ui;
0119     DatabaseEncryptedPasswordBackend* m_backend;
0120 };
0121 
0122 class QDialogButtonBox;
0123 class QLineEdit;
0124 class QLabel;
0125 
0126 class AskMasterPassword : public QDialog
0127 {
0128     Q_OBJECT
0129 
0130 public:
0131     explicit AskMasterPassword(DatabaseEncryptedPasswordBackend* backend, QWidget* parent = nullptr);
0132 
0133 private Q_SLOTS:
0134     void verifyPassword();
0135 
0136 private:
0137     DatabaseEncryptedPasswordBackend* m_backend;
0138     QDialogButtonBox* m_buttonBox;
0139     QLineEdit* m_lineEdit;
0140     QLabel* m_labelWarning;
0141 };
0142 #endif // DATABASEENCRYPTEDPASSWORDBACKEND_H