1 /* ============================================================
2 * Falkon - Qt web browser
3 * Copyright (C) 2013-2018 David Rosca <nowrep@gmail.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 * ============================================================ */
18 #ifndef PASSWORDMANAGER_H
19 #define PASSWORDMANAGER_H
20 
21 #include <QObject>
22 #include <QUrl>
23 #include <QVariant>
24 
25 #include "qzcommon.h"
26 
27 class PasswordBackend;
28 class DatabasePasswordBackend;
29 class DatabaseEncryptedPasswordBackend;
30 
31 struct FALKON_EXPORT PasswordEntry {
32     QVariant id;
33     QString host;
34     QString username;
35     QString password;
36     QByteArray data;
37     int updated;
38 
PasswordEntryPasswordEntry39     PasswordEntry() : updated(-1) { }
40 
isValidPasswordEntry41     bool isValid() const {
42         return !password.isEmpty() && !host.isEmpty();
43     }
44 
45     bool operator==(const PasswordEntry &other) const {
46         return id == other.id;
47     }
48 
49     bool operator<(const PasswordEntry &other) const {
50         return updated > other.updated;
51     }
52 
53     friend FALKON_EXPORT QDataStream &operator<<(QDataStream &stream, const PasswordEntry &entry);
54     friend FALKON_EXPORT QDataStream &operator>>(QDataStream &stream, PasswordEntry &entry);
55 };
56 
57 class FALKON_EXPORT PasswordManager : public QObject
58 {
59     Q_OBJECT
60 public:
61     explicit PasswordManager(QObject* parent = nullptr);
62     ~PasswordManager();
63 
64     void loadSettings();
65 
66     QStringList getUsernames(const QUrl &url);
67     QVector<PasswordEntry> getEntries(const QUrl &url);
68     QVector<PasswordEntry> getAllEntries();
69 
70     void addEntry(const PasswordEntry &entry);
71     bool updateEntry(const PasswordEntry &entry);
72     void updateLastUsed(PasswordEntry &entry);
73 
74     void removeEntry(const PasswordEntry &entry);
75     void removeAllEntries();
76 
77     QHash<QString, PasswordBackend*> availableBackends();
78     PasswordBackend* activeBackend();
79     void switchBackend(const QString &backendID);
80 
81     bool registerBackend(const QString &id, PasswordBackend* backend);
82     void unregisterBackend(PasswordBackend* backend);
83 
84     static QString createHost(const QUrl &url);
85     static QByteArray urlEncodePassword(const QString &password);
86 
87 private:
88     void ensureLoaded();
89 
90     bool m_loaded;
91 
92     PasswordBackend* m_backend;
93     DatabasePasswordBackend* m_databaseBackend;
94     DatabaseEncryptedPasswordBackend* m_databaseEncryptedBackend;
95 
96     QHash<QString, PasswordBackend*> m_backends;
97 
98 Q_SIGNALS:
99     void passwordBackendChanged();
100 };
101 
102 // Hint to QVector to use std::realloc on item moving
103 Q_DECLARE_TYPEINFO(PasswordEntry, Q_MOVABLE_TYPE);
104 
105 Q_DECLARE_METATYPE(PasswordEntry)
106 
107 #endif // PASSWORDMANAGER_H
108