Skip to content

Commit

Permalink
Show missing data to user
Browse files Browse the repository at this point in the history
  • Loading branch information
varjolintu committed Nov 10, 2023
1 parent f8aefdb commit 24bb139
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 13 deletions.
6 changes: 6 additions & 0 deletions share/translations/keepassxc_en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6084,6 +6084,12 @@ Do you want to overwrite it?
<source>Cannot import Passkey file &quot;%1&quot;. Private key is missing or malformed.</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Cannot import Passkey file &quot;%1&quot;.
The following data is missing:
%2</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>PasswordEditWidget</name>
Expand Down
14 changes: 14 additions & 0 deletions src/core/Tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "core/Global.h"

#include <QDateTime>
#include <QList>
#include <QProcessEnvironment>

class QIODevice;
Expand Down Expand Up @@ -100,6 +101,19 @@ namespace Tools
return version;
}

// Checks if all values are found inside the list. Returns a list of values not found.
template <typename T> QList<T> arrayContainsValues(const QList<T>& list, const QList<T>& required)
{
QList<T> missingValues;
for (const auto& r : required) {
if (!list.contains(r)) {
missingValues << r;
}
}

return missingValues;
}

QVariantMap qo2qvm(const QObject* object, const QStringList& ignoredProperties = {"objectName"});

QString substituteBackupFilePath(QString pattern, const QString& databasePath);
Expand Down
30 changes: 19 additions & 11 deletions src/gui/passkeys/PasskeyImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "browser/BrowserService.h"
#include "core/Entry.h"
#include "core/Group.h"
#include "core/Tools.h"
#include "gui/FileDialog.h"
#include "gui/MessageBox.h"
#include <QFileInfo>
Expand Down Expand Up @@ -61,25 +62,32 @@ void PasskeyImporter::importSelectedFile(QFile& file, QSharedPointer<Database>&
return;
}

const auto relyingParty = passkeyObject["relyingParty"].toString();
const auto url = passkeyObject["url"].toString();
const auto username = passkeyObject["username"].toString();
const auto credentialId = passkeyObject["credentialId"].toString();
const auto userHandle = passkeyObject["userHandle"].toString();
const auto privateKey = passkeyObject["privateKey"].toString();

if (relyingParty.isEmpty() || username.isEmpty() || credentialId.isEmpty() || userHandle.isEmpty()
|| privateKey.isEmpty()) {
const auto missingKeys = Tools::arrayContainsValues<QString>(passkeyObject.keys(),
QStringList() << "relyingParty"
<< "url"
<< "username"
<< "credentialId"
<< "userHandle"
<< "privateKey");

if (!missingKeys.isEmpty()) {
MessageBox::information(nullptr,
tr("Cannot import Passkey"),
tr("Cannot import Passkey file \"%1\". Data is missing.").arg(file.fileName()));
tr("Cannot import Passkey file \"%1\".\nThe following data is missing:\n%2")
.arg(file.fileName(), missingKeys.join(", ")));
} else if (!privateKey.startsWith("-----BEGIN PRIVATE KEY-----")
|| !privateKey.trimmed().endsWith("-----END PRIVATE KEY-----")) {
MessageBox::information(
nullptr,
tr("Cannot import Passkey"),
tr("Cannot import Passkey file \"%1\". Private key is missing or malformed.").arg(file.fileName()));
} else {
const auto relyingParty = passkeyObject["relyingParty"].toString();
const auto url = passkeyObject["url"].toString();
const auto username = passkeyObject["username"].toString();
const auto credentialId = passkeyObject["credentialId"].toString();
const auto userHandle = passkeyObject["userHandle"].toString();
showImportDialog(database, url, relyingParty, username, credentialId, userHandle, privateKey, entry);
}
}
Expand Down Expand Up @@ -109,7 +117,7 @@ void PasskeyImporter::showImportDialog(QSharedPointer<Database>& database,
// Store to entry if given directly
if (entry) {
browserService()->addPasskeyToEntry(
entry, relyingParty, relyingParty, username, userId, userHandle, privateKey);
entry, relyingParty, relyingParty, username, credentialId, userHandle, privateKey);
return;
}

Expand All @@ -122,7 +130,7 @@ void PasskeyImporter::showImportDialog(QSharedPointer<Database>& database,
auto selectedEntry = group->findEntryByUuid(passkeyImportDialog.getSelectedEntryUuid());
if (selectedEntry) {
browserService()->addPasskeyToEntry(
selectedEntry, relyingParty, relyingParty, username, userId, userHandle, privateKey);
selectedEntry, relyingParty, relyingParty, username, credentialId, userHandle, privateKey);
}
}

Expand Down
29 changes: 28 additions & 1 deletion tests/TestTools.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 KeePassXC Team <[email protected]>
* Copyright (C) 2023 KeePassXC Team <[email protected]>
*
* 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
Expand Down Expand Up @@ -239,3 +239,30 @@ void TestTools::testConvertToRegex_data()
<< input << static_cast<int>(Tools::RegexConvertOpts::WILDCARD_UNLIMITED_MATCH)
<< QString(R"(te\|st.*t\?\[5\]\^\(test\)\;\'\,\.)");
}

void TestTools::testArrayContainsValues()
{
const auto values = QStringList() << "first"
<< "second"
<< "third";

// One missing
const auto result1 = Tools::arrayContainsValues<QString>(values,
QStringList() << "first"
<< "second"
<< "none");
QCOMPARE(result1.length(), 1);
QCOMPARE(result1.first(), QString("none"));

// All found
const auto result2 = Tools::arrayContainsValues<QString>(values,
QStringList() << "first"
<< "second"
<< "third");
QCOMPARE(result2.length(), 0);

// None are found
const auto numberValues = {1, 2, 3, 4, 5};
const auto result3 = Tools::arrayContainsValues<int>(numberValues, {6, 7, 8});
QCOMPARE(result3.length(), 3);
}
3 changes: 2 additions & 1 deletion tests/TestTools.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 KeePassXC Team <[email protected]>
* Copyright (C) 2023 KeePassXC Team <[email protected]>
*
* 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
Expand Down Expand Up @@ -35,6 +35,7 @@ private slots:
void testEscapeRegex_data();
void testConvertToRegex();
void testConvertToRegex_data();
void testArrayContainsValues();
};

#endif // KEEPASSX_TESTTOOLS_H

0 comments on commit 24bb139

Please sign in to comment.