Skip to content

Commit

Permalink
Use Passkey attributes instead of URL and username if Passkey is found.
Browse files Browse the repository at this point in the history
  • Loading branch information
varjolintu committed Nov 5, 2023
1 parent 81350ea commit df36beb
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 19 deletions.
8 changes: 6 additions & 2 deletions src/browser/BrowserService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ const QString BrowserService::OPTION_HIDE_ENTRY = QStringLiteral("BrowserHideEnt
const QString BrowserService::OPTION_ONLY_HTTP_AUTH = QStringLiteral("BrowserOnlyHttpAuth");
const QString BrowserService::OPTION_NOT_HTTP_AUTH = QStringLiteral("BrowserNotHttpAuth");
const QString BrowserService::OPTION_OMIT_WWW = QStringLiteral("BrowserOmitWww");
// Multiple URL's
const QString BrowserService::ADDITIONAL_URL = QStringLiteral("KP2A_URL");

Q_GLOBAL_STATIC(BrowserService, s_browserService);

Expand Down Expand Up @@ -1096,7 +1094,13 @@ void BrowserService::denyEntry(Entry* entry, const QString& siteHost, const QStr
QJsonObject BrowserService::prepareEntry(const Entry* entry)
{
QJsonObject res;
#ifdef WITH_XC_BROWSER_PASSKEYS
// Use Passkey's username instead if found
res["login"] = entry->hasPasskey() ? entry->attributes()->value(BrowserPasskeys::KPEX_PASSKEY_USERNAME)
: entry->resolveMultiplePlaceholders(entry->username());
#else
res["login"] = entry->resolveMultiplePlaceholders(entry->username());
#endif
res["password"] = entry->resolveMultiplePlaceholders(entry->password());
res["name"] = entry->resolveMultiplePlaceholders(entry->title());
res["uuid"] = entry->resolveMultiplePlaceholders(entry->uuidToHex());
Expand Down
1 change: 0 additions & 1 deletion src/browser/BrowserService.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ class BrowserService : public QObject
static const QString OPTION_ONLY_HTTP_AUTH;
static const QString OPTION_NOT_HTTP_AUTH;
static const QString OPTION_OMIT_WWW;
static const QString ADDITIONAL_URL;

signals:
void requestUnlock();
Expand Down
3 changes: 2 additions & 1 deletion src/core/Entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,8 @@ QStringList Entry::getAllUrls() const
}

for (const auto& key : m_attributes->keys()) {
if (key.startsWith("KP2A_URL")) {
if (key.startsWith(EntryAttributes::AdditionalUrlAttribute)
|| key == QString("%1_RELYING_PARTY").arg(EntryAttributes::PasskeyAttribute)) {
auto additionalUrl = m_attributes->value(key);
if (!additionalUrl.isEmpty()) {
urlList << resolveMultiplePlaceholders(additionalUrl);
Expand Down
1 change: 1 addition & 0 deletions src/core/EntryAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const QString EntryAttributes::SearchInGroupName = "SearchIn";
const QString EntryAttributes::SearchTextGroupName = "SearchText";

const QString EntryAttributes::RememberCmdExecAttr = "_EXEC_CMD";
const QString EntryAttributes::AdditionalUrlAttribute = "KP2A_URL";
const QString EntryAttributes::PasskeyAttribute = "KPEX_PASSKEY";

EntryAttributes::EntryAttributes(QObject* parent)
Expand Down
1 change: 1 addition & 0 deletions src/core/EntryAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class EntryAttributes : public ModifiableObject
static const QString NotesKey;
static const QStringList DefaultAttributes;
static const QString RememberCmdExecAttr;
static const QString AdditionalUrlAttribute;
static const QString PasskeyAttribute;
static bool isDefaultAttribute(const QString& key);
static bool isPasskeyAttribute(const QString& key);
Expand Down
5 changes: 3 additions & 2 deletions src/format/OpVaultReaderBandEntry.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 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 @@ -243,7 +243,8 @@ bool OpVaultReader::fillAttributes(Entry* entry, const QJsonObject& bandEntry)
auto newUrl = urlObj["u"].toString();
if (newUrl != url) {
// Add this url if it isn't the base one
entry->attributes()->set(QString("KP2A_URL_%1").arg(i), newUrl);
entry->attributes()->set(
QString("%1_%2").arg(EntryAttributes::AdditionalUrlAttribute, QString::number(i)), newUrl);
++i;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/gui/entry/EditEntryWidget.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2023 KeePassXC Team <[email protected]>
* Copyright (C) 2010 Felix Geyer <[email protected]>
* Copyright (C) 2021 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 @@ -329,11 +329,11 @@ void EditEntryWidget::insertURL()
{
Q_ASSERT(!m_history);

QString name(BrowserService::ADDITIONAL_URL);
QString name(EntryAttributes::AdditionalUrlAttribute);
int i = 1;

while (m_entryAttributes->keys().contains(name)) {
name = QString("%1_%2").arg(BrowserService::ADDITIONAL_URL, QString::number(i));
name = QString("%1_%2").arg(EntryAttributes::AdditionalUrlAttribute, QString::number(i));
i++;
}

Expand Down
4 changes: 2 additions & 2 deletions src/gui/entry/EntryURLModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ QVariant EntryURLModel::data(const QModelIndex& index, int role) const
const auto urlValid = urlTools()->isUrlValid(value);

// Check for duplicate URLs in the attribute list. Excludes the current key/value from the comparison.
auto customAttributeKeys = m_entryAttributes->customKeys().filter(BrowserService::ADDITIONAL_URL);
auto customAttributeKeys = m_entryAttributes->customKeys().filter(EntryAttributes::AdditionalUrlAttribute);
customAttributeKeys.removeOne(key);

const auto duplicateUrl =
Expand Down Expand Up @@ -148,7 +148,7 @@ void EntryURLModel::updateAttributes()

const auto attributesKeyList = m_entryAttributes->keys();
for (const auto& key : attributesKeyList) {
if (!EntryAttributes::isDefaultAttribute(key) && key.contains(BrowserService::ADDITIONAL_URL)) {
if (!EntryAttributes::isDefaultAttribute(key) && key.contains(EntryAttributes::AdditionalUrlAttribute)) {
const auto value = m_entryAttributes->value(key);
m_urls.append(qMakePair(key, value));

Expand Down
18 changes: 10 additions & 8 deletions tests/TestBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,8 @@ void TestBrowser::testSearchEntriesByReference()
auto secondEntryUuid = entries[1]->uuidToHex();
auto fullReference = QString("{REF:A@I:%1}").arg(firstEntryUuid);
auto partialReference = QString("https://subdomain.{REF:A@I:%1}").arg(secondEntryUuid);
entries[2]->attributes()->set(BrowserService::ADDITIONAL_URL, fullReference);
entries[3]->attributes()->set(BrowserService::ADDITIONAL_URL, partialReference);
entries[2]->attributes()->set(EntryAttributes::AdditionalUrlAttribute, fullReference);
entries[3]->attributes()->set(EntryAttributes::AdditionalUrlAttribute, partialReference);
entries[4]->setUrl(fullReference);
entries[5]->setUrl(partialReference);

Expand All @@ -351,11 +351,13 @@ void TestBrowser::testSearchEntriesByReference()
QCOMPARE(result[0]->url(), urls[0]);
QCOMPARE(result[1]->url(), urls[1]);
QCOMPARE(result[2]->url(), urls[2]);
QCOMPARE(result[2]->resolveMultiplePlaceholders(result[2]->attributes()->value(BrowserService::ADDITIONAL_URL)),
urls[0]);
QCOMPARE(
result[2]->resolveMultiplePlaceholders(result[2]->attributes()->value(EntryAttributes::AdditionalUrlAttribute)),
urls[0]);
QCOMPARE(result[3]->url(), urls[3]);
QCOMPARE(result[3]->resolveMultiplePlaceholders(result[3]->attributes()->value(BrowserService::ADDITIONAL_URL)),
urls[0]);
QCOMPARE(
result[3]->resolveMultiplePlaceholders(result[3]->attributes()->value(EntryAttributes::AdditionalUrlAttribute)),
urls[0]);
QCOMPARE(result[4]->url(), fullReference);
QCOMPARE(result[4]->resolveMultiplePlaceholders(result[4]->url()), urls[0]); // Should be resolved to the main entry
QCOMPARE(result[5]->url(), partialReference);
Expand Down Expand Up @@ -386,7 +388,7 @@ void TestBrowser::testSearchEntriesWithAdditionalURLs()
auto entries = createEntries(urls, root);

// Add an additional URL to the first entry
entries.first()->attributes()->set(BrowserService::ADDITIONAL_URL, "https://keepassxc.org");
entries.first()->attributes()->set(EntryAttributes::AdditionalUrlAttribute, "https://keepassxc.org");

auto result = m_browserService->searchEntries(db, "https://github.com", "https://github.com/session");
QCOMPARE(result.length(), 1);
Expand Down Expand Up @@ -663,7 +665,7 @@ void TestBrowser::testBestMatchingWithAdditionalURLs()
browserSettings()->setBestMatchOnly(true);

// Add an additional URL to the first entry
entries.first()->attributes()->set(BrowserService::ADDITIONAL_URL, "https://test.github.com/anotherpage");
entries.first()->attributes()->set(EntryAttributes::AdditionalUrlAttribute, "https://test.github.com/anotherpage");

// The first entry should be triggered
auto result = m_browserService->searchEntries(
Expand Down

0 comments on commit df36beb

Please sign in to comment.