From ea2e36c67666c387a5f3d9d8bbcd1e4f3773338d Mon Sep 17 00:00:00 2001 From: varjolintu Date: Sat, 14 Sep 2024 17:58:26 +0300 Subject: [PATCH] Add support for group selection when creating a passkey --- src/browser/BrowserAction.cpp | 3 ++- src/browser/BrowserService.cpp | 22 +++++++++++++++++++--- src/browser/BrowserService.h | 3 ++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/browser/BrowserAction.cpp b/src/browser/BrowserAction.cpp index 35a7acc19..67cf7f0df 100644 --- a/src/browser/BrowserAction.cpp +++ b/src/browser/BrowserAction.cpp @@ -579,8 +579,9 @@ QJsonObject BrowserAction::handlePasskeysRegister(const QJsonObject& json, const return getErrorReply(action, ERROR_PASSKEYS_INVALID_URL_PROVIDED); } + const auto groupName = browserRequest.getString("groupName"); const auto keyList = getConnectionKeys(browserRequest); - const auto response = browserService()->showPasskeysRegisterPrompt(publicKey, origin, keyList); + const auto response = browserService()->showPasskeysRegisterPrompt(publicKey, origin, groupName, keyList); const Parameters params{{"response", response}}; return buildResponse(action, browserRequest.incrementedNonce, params); diff --git a/src/browser/BrowserService.cpp b/src/browser/BrowserService.cpp index 064b5fa50..d60a55109 100644 --- a/src/browser/BrowserService.cpp +++ b/src/browser/BrowserService.cpp @@ -58,6 +58,7 @@ static const QString KEEPASSXCBROWSER_GROUP_NAME = QStringLiteral("KeePassXC-Bro static int KEEPASSXCBROWSER_DEFAULT_ICON = 1; #ifdef WITH_XC_BROWSER_PASSKEYS static int KEEPASSXCBROWSER_PASSKEY_ICON = 13; +static const QString PASSKEYS_DEFAULT_GROUP_NAME = QStringLiteral("KeePassXC-Browser Passkeys"); #endif // These are for the settings and password conversion static const QString KEEPASSHTTP_NAME = QStringLiteral("KeePassHttp Settings"); @@ -258,8 +259,12 @@ QJsonArray BrowserService::getDatabaseEntries() return entries; } -QJsonObject BrowserService::createNewGroup(const QString& groupName) +QJsonObject BrowserService::createNewGroup(const QString& groupName, bool isPasskeysGroup) { + if (groupName.isEmpty()) { + return {}; + } + auto db = getDatabase(); if (!db) { return {}; @@ -309,10 +314,15 @@ QJsonObject BrowserService::createNewGroup(const QString& groupName) QString gName = getGroupName(i); auto tempGroup = rootGroup->findGroupByPath(gName); if (!tempGroup) { - Group* newGroup = new Group(); + auto newGroup = new Group(); newGroup->setName(groups[i]); newGroup->setUuid(QUuid::createUuid()); newGroup->setParent(previousGroup); +#ifdef WITH_XC_BROWSER_PASSKEYS + if (isPasskeysGroup && i == groups.length() - 1) { + newGroup->setIcon(KEEPASSXCBROWSER_PASSKEY_ICON); + } +#endif name = newGroup->name(); uuid = Tools::uuidToHex(newGroup->uuid()); previousGroup = newGroup; @@ -620,6 +630,7 @@ QString BrowserService::getKey(const QString& id) // Passkey registration QJsonObject BrowserService::showPasskeysRegisterPrompt(const QJsonObject& publicKeyOptions, const QString& origin, + const QString& groupName, const StringPairList& keyList) { auto db = selectedDatabase(); @@ -691,8 +702,13 @@ QJsonObject BrowserService::showPasskeysRegisterPrompt(const QJsonObject& public publicKeyCredentials.key); } } else { + // Handle new/existing group + const auto createResponse = + createNewGroup(groupName.isEmpty() ? PASSKEYS_DEFAULT_GROUP_NAME : groupName, true); + const auto group = db->rootGroup()->findGroupByUuid(Tools::hexToUuid(createResponse["uuid"].toString())); + addPasskeyToGroup(db, - nullptr, + group, origin, rpId, rpName, diff --git a/src/browser/BrowserService.h b/src/browser/BrowserService.h index 801fd2ed3..1c7ec5a37 100644 --- a/src/browser/BrowserService.h +++ b/src/browser/BrowserService.h @@ -80,7 +80,7 @@ public: QJsonObject getDatabaseGroups(); QJsonArray getDatabaseEntries(); - QJsonObject createNewGroup(const QString& groupName); + QJsonObject createNewGroup(const QString& groupName, bool isPasskeysGroup = false); QString getCurrentTotp(const QString& uuid); void showPasswordGenerator(const KeyPairMessage& keyPairMessage); bool isPasswordGeneratorRequested() const; @@ -90,6 +90,7 @@ public: #ifdef WITH_XC_BROWSER_PASSKEYS QJsonObject showPasskeysRegisterPrompt(const QJsonObject& publicKeyOptions, const QString& origin, + const QString& groupName, const StringPairList& keyList); QJsonObject showPasskeysAuthenticationPrompt(const QJsonObject& publicKeyOptions, const QString& origin,