Skip to content

Commit

Permalink
Added button for KeePassHTTP attribute conversion to customData
Browse files Browse the repository at this point in the history
  • Loading branch information
varjolintu committed Mar 5, 2018
1 parent aad4cd9 commit 1c7ee40
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 35 deletions.
6 changes: 3 additions & 3 deletions src/browser/BrowserEntryConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "core/Entry.h"
#include "core/EntryAttributes.h"

static const char KEEPASSBROWSER_NAME[] = "KeePassXC-Browser Settings";
static const char KEEPASSXCBROWSER_NAME[] = "KeePassXC-Browser Settings";


BrowserEntryConfig::BrowserEntryConfig(QObject* parent) :
Expand Down Expand Up @@ -83,7 +83,7 @@ void BrowserEntryConfig::setRealm(const QString& realm)

bool BrowserEntryConfig::load(const Entry* entry)
{
QString s = entry->customData()->value(KEEPASSBROWSER_NAME);
QString s = entry->customData()->value(KEEPASSXCBROWSER_NAME);
if (s.isEmpty()) {
return false;
}
Expand All @@ -105,5 +105,5 @@ void BrowserEntryConfig::save(Entry* entry)
QVariantMap v = qo2qv(this);
QJsonObject o = QJsonObject::fromVariantMap(v);
QByteArray json = QJsonDocument(o).toJson(QJsonDocument::Compact);
entry->customData()->set(KEEPASSBROWSER_NAME, json);
entry->customData()->set(KEEPASSXCBROWSER_NAME, json);
}
2 changes: 1 addition & 1 deletion src/browser/BrowserOptionDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ BrowserOptionDialog::BrowserOptionDialog(DatabaseTabWidget* parent) :
connect(m_ui->customDataTable->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
SLOT(toggleRemoveButton(QItemSelection)));
connect(m_ui->removeCustomDataButton, SIGNAL(clicked()), SLOT(removeSelectedKey()));

connect(m_ui->convertToCustomData, SIGNAL(clicked()), this, SIGNAL(convertAttributesToCustomData()));
connect(m_ui->removeSharedEncryptionKeys, SIGNAL(clicked()), this, SIGNAL(removeSharedEncryptionKeys()));
connect(m_ui->removeStoredPermissions, SIGNAL(clicked()), this, SIGNAL(removeStoredPermissions()));

Expand Down
1 change: 1 addition & 0 deletions src/browser/BrowserOptionDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public slots:
signals:
void removeSharedEncryptionKeys();
void removeStoredPermissions();
void convertAttributesToCustomData();

private slots:
void showProxyLocationFileDialog();
Expand Down
17 changes: 17 additions & 0 deletions src/browser/BrowserOptionDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,23 @@
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QPushButton" name="convertToCustomData">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Move KeePassHTTP attributes to KeePassXC-Browser &amp;custom data</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
Expand Down
111 changes: 81 additions & 30 deletions src/browser/BrowserService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ static const char KEEPASSXCBROWSER_NAME[] = "KeePassXC-Browser Settings";
static const char ASSOCIATE_KEY_PREFIX[] = "Public Key: ";
static const char KEEPASSXCBROWSER_GROUP_NAME[] = "KeePassXC-Browser Passwords";
static int KEEPASSXCBROWSER_DEFAULT_ICON = 1;
// These are for the settings and password conversion
static const char KEEPASSHTTP_NAME[] = "KeePassHttp Settings";
static const char KEEPASSHTTP_GROUP_NAME[] = "KeePassHttp Passwords";

BrowserService::BrowserService(DatabaseTabWidget* parent) :
m_dbTabWidget(parent),
Expand Down Expand Up @@ -132,36 +135,6 @@ QString BrowserService::getDatabaseRecycleBinUuid()
return recycleBin->uuid().toHex();
}

Entry* BrowserService::getConfigEntry(bool create)
{
Entry* entry = nullptr;
Database* db = getDatabase();
if (!db) {
return nullptr;
}

entry = db->resolveEntry(KEEPASSXCBROWSER_UUID);
if (!entry && create) {
entry = new Entry();
entry->setTitle(QLatin1String(KEEPASSXCBROWSER_NAME));
entry->setUuid(KEEPASSXCBROWSER_UUID);
entry->setAutoTypeEnabled(false);
entry->setGroup(db->rootGroup());
return entry;
}

if (entry && entry->group() == db->metadata()->recycleBin()) {
if (!create) {
return nullptr;
} else {
entry->setGroup(db->rootGroup());
return entry;
}
}

return entry;
}

QString BrowserService::storeKey(const QString& key)
{
QString id;
Expand Down Expand Up @@ -500,6 +473,69 @@ void BrowserService::removeStoredPermissions()
}
}

void BrowserService::convertAttributesToCustomData()
{
if (!isDatabaseOpened()) {
QMessageBox::critical(0, tr("KeePassXC: Database locked!"),
tr("The active database is locked!\n"
"Please unlock the selected database or choose another one which is unlocked."),
QMessageBox::Ok);
return;
}

Database* db = m_dbTabWidget->currentDatabaseWidget()->database();
if (!db) {
return;
}

QList<Entry*> entries = db->rootGroup()->entriesRecursive();

QProgressDialog progress(tr("Converting attributes to custom data…"), tr("Abort"), 0, entries.count());
progress.setWindowModality(Qt::WindowModal);

uint counter = 0;
for (Entry* entry : entries) {
if (progress.wasCanceled()) {
return;
}

if (moveSettingsToCustomData(entry, KEEPASSHTTP_NAME)) {
++counter;
}
if (moveSettingsToCustomData(entry, KEEPASSXCBROWSER_NAME)) {
++counter;
}
progress.setValue(progress.value() + 1);
}
progress.reset();

if (counter > 0) {
QMessageBox::information(0, tr("KeePassXC: Converted KeePassHTTP attributes"),
tr("Successfully converted attributes from %n entry(s).", "", counter),
QMessageBox::Ok);
} else {
QMessageBox::information(0, tr("KeePassXC: No entry with KeePassHTTP attributes found!"),
tr("The active database does not contain an entry with KeePassHTTP attributes."),
QMessageBox::Ok);
}

// Rename password groupName
Group* rootGroup = db->rootGroup();
if (!rootGroup) {
return;
}

const QString keePassBrowserGroupName = QLatin1String(KEEPASSXCBROWSER_GROUP_NAME);
const QString keePassHttpGroupName = QLatin1String(KEEPASSHTTP_GROUP_NAME);

for (Group* g : rootGroup->groupsRecursive(true)) {
if (g->name() == keePassHttpGroupName) {
g->setName(keePassBrowserGroupName);
break;
}
}
}

QList<Entry*> BrowserService::sortEntries(QList<Entry*>& pwEntries, const QString& host, const QString& entryUrl)
{
QUrl url(entryUrl);
Expand Down Expand Up @@ -715,6 +751,21 @@ Database* BrowserService::getDatabase()
return nullptr;
}

bool BrowserService::moveSettingsToCustomData(Entry* entry, const QString& name) const
{
if (entry->attributes()->contains(name)) {
QString attr = entry->attributes()->value(name);
entry->beginUpdate();
if (!attr.isEmpty()) {
entry->customData()->set(KEEPASSXCBROWSER_NAME, attr);
}
entry->attributes()->remove(name);
entry->endUpdate();
return true;
}
return false;
}

void BrowserService::databaseLocked(DatabaseWidget* dbWidget)
{
if (dbWidget) {
Expand Down
3 changes: 2 additions & 1 deletion src/browser/BrowserService.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ class BrowserService : public QObject
bool openDatabase(bool triggerUnlock);
QString getDatabaseRootUuid();
QString getDatabaseRecycleBinUuid();
Entry* getConfigEntry(bool create = false);
QString getKey(const QString& id);
void addEntry(const QString& id, const QString& login, const QString& password, const QString& url, const QString& submitUrl, const QString& realm);
QList<Entry*> searchEntries(Database* db, const QString& hostname);
QList<Entry*> searchEntries(const QString& text);
void removeSharedEncryptionKeys();
void removeStoredPermissions();
void convertAttributesToCustomData();

public slots:
QJsonArray findMatchingEntries(const QString& id, const QString& url, const QString& submitUrl, const QString& realm);
Expand Down Expand Up @@ -73,6 +73,7 @@ public slots:
bool matchUrlScheme(const QString& url);
bool removeFirstDomain(QString& hostname);
Database* getDatabase();
bool moveSettingsToCustomData(Entry* entry, const QString& name) const;

private:
DatabaseTabWidget* const m_dbTabWidget;
Expand Down
6 changes: 6 additions & 0 deletions src/browser/NativeMessagingHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ void NativeMessagingHost::removeStoredPermissions()
m_browserService.removeStoredPermissions();
}

void NativeMessagingHost::convertAttributesToCustomData()
{
QMutexLocker locker(&m_mutex);
m_browserService.convertAttributesToCustomData();
}

void NativeMessagingHost::databaseLocked()
{
QJsonObject response;
Expand Down
1 change: 1 addition & 0 deletions src/browser/NativeMessagingHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class NativeMessagingHost : public NativeMessagingBase
public slots:
void removeSharedEncryptionKeys();
void removeStoredPermissions();
void convertAttributesToCustomData();

signals:
void quit();
Expand Down
1 change: 1 addition & 0 deletions src/gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class BrowserPlugin: public ISettingsPage
BrowserOptionDialog* dlg = new BrowserOptionDialog(m_dbTabWidget);
QObject::connect(dlg, SIGNAL(removeSharedEncryptionKeys()), m_nativeMessagingHost.data(), SLOT(removeSharedEncryptionKeys()));
QObject::connect(dlg, SIGNAL(removeStoredPermissions()), m_nativeMessagingHost.data(), SLOT(removeStoredPermissions()));
QObject::connect(dlg, SIGNAL(convertAttributesToCustomData()), m_nativeMessagingHost.data(), SLOT(convertAttributesToCustomData()));
return dlg;
}

Expand Down

0 comments on commit 1c7ee40

Please sign in to comment.