File indexing completed on 2024-05-19 04:58:38

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2013-2018 David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #ifndef PASSWORDMANAGER_H
0019 #define PASSWORDMANAGER_H
0020 
0021 #include <QObject>
0022 #include <QUrl>
0023 #include <QVariant>
0024 
0025 #include "qzcommon.h"
0026 
0027 class PasswordBackend;
0028 class DatabasePasswordBackend;
0029 class DatabaseEncryptedPasswordBackend;
0030 
0031 struct FALKON_EXPORT PasswordEntry {
0032     QVariant id;
0033     QString host;
0034     QString username;
0035     QString password;
0036     QByteArray data;
0037     int updated;
0038 
0039     PasswordEntry() : updated(-1) { }
0040 
0041     bool isValid() const {
0042         return !password.isEmpty() && !host.isEmpty();
0043     }
0044 
0045     bool operator==(const PasswordEntry &other) const {
0046         return id == other.id;
0047     }
0048 
0049     bool operator<(const PasswordEntry &other) const {
0050         return updated > other.updated;
0051     }
0052 
0053     friend FALKON_EXPORT QDataStream &operator<<(QDataStream &stream, const PasswordEntry &entry);
0054     friend FALKON_EXPORT QDataStream &operator>>(QDataStream &stream, PasswordEntry &entry);
0055 };
0056 
0057 class FALKON_EXPORT PasswordManager : public QObject
0058 {
0059     Q_OBJECT
0060 public:
0061     explicit PasswordManager(QObject* parent = nullptr);
0062     ~PasswordManager();
0063 
0064     void loadSettings();
0065 
0066     QStringList getUsernames(const QUrl &url);
0067     QVector<PasswordEntry> getEntries(const QUrl &url);
0068     QVector<PasswordEntry> getAllEntries();
0069 
0070     void addEntry(const PasswordEntry &entry);
0071     bool updateEntry(const PasswordEntry &entry);
0072     void updateLastUsed(PasswordEntry &entry);
0073 
0074     void removeEntry(const PasswordEntry &entry);
0075     void removeAllEntries();
0076 
0077     QHash<QString, PasswordBackend*> availableBackends();
0078     PasswordBackend* activeBackend();
0079     void switchBackend(const QString &backendID);
0080 
0081     bool registerBackend(const QString &id, PasswordBackend* backend);
0082     void unregisterBackend(PasswordBackend* backend);
0083 
0084     static QString createHost(const QUrl &url);
0085     static QByteArray urlEncodePassword(const QString &password);
0086 
0087 private:
0088     void ensureLoaded();
0089 
0090     bool m_loaded;
0091 
0092     PasswordBackend* m_backend;
0093     DatabasePasswordBackend* m_databaseBackend;
0094     DatabaseEncryptedPasswordBackend* m_databaseEncryptedBackend;
0095 
0096     QHash<QString, PasswordBackend*> m_backends;
0097 
0098 Q_SIGNALS:
0099     void passwordBackendChanged();
0100 };
0101 
0102 // Hint to QVector to use std::realloc on item moving
0103 Q_DECLARE_TYPEINFO(PasswordEntry, Q_MOVABLE_TYPE);
0104 
0105 Q_DECLARE_METATYPE(PasswordEntry)
0106 
0107 #endif // PASSWORDMANAGER_H