2013-05-14 17:25:55 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
|
|
|
* Copyright (C) 2013 David Rosca <nowrep@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
|
|
|
#include "passwordmanager.h"
|
2013-05-15 14:58:55 +02:00
|
|
|
#include "passwordbackends/passwordbackend.h"
|
|
|
|
#include "passwordbackends/databasepasswordbackend.h"
|
2013-05-14 17:25:55 +02:00
|
|
|
#include "settings.h"
|
|
|
|
|
|
|
|
#include <QVector>
|
|
|
|
|
2013-05-20 11:05:54 +02:00
|
|
|
static const int passwordEntryVersion = 2;
|
2013-05-15 20:05:45 +02:00
|
|
|
|
2013-05-20 11:05:54 +02:00
|
|
|
QDataStream &operator <<(QDataStream &stream, const PasswordEntry &entry)
|
2013-05-15 20:05:45 +02:00
|
|
|
{
|
|
|
|
stream << passwordEntryVersion;
|
2013-05-20 11:05:54 +02:00
|
|
|
stream << entry.host;
|
|
|
|
stream << entry.id;
|
|
|
|
stream << entry.username;
|
|
|
|
stream << entry.password;
|
|
|
|
stream << entry.data;
|
|
|
|
stream << entry.updated;
|
2013-05-15 20:05:45 +02:00
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2013-05-20 11:05:54 +02:00
|
|
|
QDataStream &operator >>(QDataStream &stream, PasswordEntry &entry)
|
2013-05-15 20:05:45 +02:00
|
|
|
{
|
|
|
|
int version;
|
|
|
|
stream >> version;
|
|
|
|
|
|
|
|
if (version != passwordEntryVersion) {
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2013-05-20 11:05:54 +02:00
|
|
|
stream >> entry.host;
|
|
|
|
stream >> entry.id;
|
|
|
|
stream >> entry.username;
|
|
|
|
stream >> entry.password;
|
|
|
|
stream >> entry.data;
|
|
|
|
stream >> entry.updated;
|
2013-05-15 20:05:45 +02:00
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2013-05-14 17:25:55 +02:00
|
|
|
PasswordManager::PasswordManager(QObject* parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_loaded(false)
|
2013-05-15 20:05:45 +02:00
|
|
|
, m_backend(0)
|
2013-05-14 17:25:55 +02:00
|
|
|
, m_databaseBackend(new DatabasePasswordBackend)
|
|
|
|
{
|
|
|
|
m_backends["database"] = m_databaseBackend;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PasswordManager::loadSettings()
|
|
|
|
{
|
|
|
|
Settings settings;
|
|
|
|
settings.beginGroup("PasswordManager");
|
|
|
|
QString backendId = settings.value("Backend", "database").toString();
|
|
|
|
settings.endGroup();
|
|
|
|
|
|
|
|
m_backend = m_backends[m_backends.contains(backendId) ? backendId : "database"];
|
2013-05-15 15:04:48 +02:00
|
|
|
m_backend->setActive(true);
|
2013-05-14 17:25:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QVector<PasswordEntry> PasswordManager::getEntries(const QUrl &url)
|
|
|
|
{
|
|
|
|
ensureLoaded();
|
|
|
|
return m_backend->getEntries(url);
|
|
|
|
}
|
|
|
|
|
2013-05-14 22:45:20 +02:00
|
|
|
QVector<PasswordEntry> PasswordManager::getAllEntries()
|
|
|
|
{
|
|
|
|
ensureLoaded();
|
|
|
|
return m_backend->getAllEntries();
|
|
|
|
}
|
|
|
|
|
2013-05-14 17:25:55 +02:00
|
|
|
void PasswordManager::addEntry(const PasswordEntry &entry)
|
|
|
|
{
|
|
|
|
ensureLoaded();
|
|
|
|
m_backend->addEntry(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PasswordManager::updateEntry(const PasswordEntry &entry)
|
|
|
|
{
|
|
|
|
ensureLoaded();
|
|
|
|
m_backend->updateEntry(entry);
|
|
|
|
}
|
|
|
|
|
2013-05-20 11:05:54 +02:00
|
|
|
void PasswordManager::updateLastUsed(PasswordEntry &entry)
|
2013-05-14 17:25:55 +02:00
|
|
|
{
|
|
|
|
ensureLoaded();
|
|
|
|
m_backend->updateLastUsed(entry);
|
|
|
|
}
|
|
|
|
|
2013-05-14 22:45:20 +02:00
|
|
|
void PasswordManager::removeEntry(const PasswordEntry &entry)
|
|
|
|
{
|
|
|
|
ensureLoaded();
|
|
|
|
m_backend->removeEntry(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PasswordManager::removeAllEntries()
|
|
|
|
{
|
|
|
|
ensureLoaded();
|
|
|
|
m_backend->removeAll();
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:42:06 +02:00
|
|
|
QHash<QString, PasswordBackend*> PasswordManager::availableBackends() const
|
|
|
|
{
|
|
|
|
return m_backends;
|
|
|
|
}
|
|
|
|
|
|
|
|
PasswordBackend* PasswordManager::activeBackend() const
|
|
|
|
{
|
|
|
|
return m_backend;
|
|
|
|
}
|
|
|
|
|
2013-05-18 13:29:11 +02:00
|
|
|
void PasswordManager::switchBackend(PasswordBackend* backend)
|
|
|
|
{
|
|
|
|
if (!backend) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_backend->setActive(false);
|
|
|
|
m_backend = backend;
|
|
|
|
m_backend->setActive(true);
|
|
|
|
}
|
|
|
|
|
2013-05-14 17:25:55 +02:00
|
|
|
bool PasswordManager::registerBackend(const QString &id, PasswordBackend* backend)
|
|
|
|
{
|
|
|
|
if (m_backends.contains(id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_backends[id] = backend;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PasswordManager::unregisterBackend(PasswordBackend* backend)
|
|
|
|
{
|
|
|
|
const QString &key = m_backends.key(backend);
|
|
|
|
m_backends.remove(key);
|
2013-05-15 21:30:20 +02:00
|
|
|
|
|
|
|
if (m_backend == backend) {
|
|
|
|
m_backend = m_databaseBackend;
|
|
|
|
}
|
2013-05-14 17:25:55 +02:00
|
|
|
}
|
|
|
|
|
2013-05-14 22:45:20 +02:00
|
|
|
QString PasswordManager::createHost(const QUrl &url)
|
|
|
|
{
|
|
|
|
QString host = url.host();
|
|
|
|
|
|
|
|
if (host.isEmpty()) {
|
|
|
|
host = url.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return host;
|
|
|
|
}
|
|
|
|
|
2013-05-20 13:33:13 +02:00
|
|
|
QByteArray PasswordManager::urlEncodePassword(const QString &password)
|
|
|
|
{
|
|
|
|
// Exclude space to properly decode to +
|
|
|
|
QByteArray encodedPass = QUrl::toPercentEncoding(password, " ");
|
2013-05-21 16:42:12 +02:00
|
|
|
encodedPass.replace(' ', '+'); // space has to be encoded to +
|
|
|
|
encodedPass.replace('~', "%7E"); // ~ is unreserved char, needs to be manually encoded
|
2013-05-20 13:33:13 +02:00
|
|
|
return encodedPass;
|
|
|
|
}
|
|
|
|
|
2013-05-14 17:25:55 +02:00
|
|
|
void PasswordManager::ensureLoaded()
|
|
|
|
{
|
|
|
|
if (!m_loaded) {
|
|
|
|
loadSettings();
|
|
|
|
m_loaded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PasswordManager::~PasswordManager()
|
|
|
|
{
|
|
|
|
delete m_databaseBackend;
|
|
|
|
}
|