Skip to content

Commit

Permalink
Launch KeePassXC password generator popup from the extension
Browse files Browse the repository at this point in the history
* Closes #6473
  • Loading branch information
varjolintu authored and droidmonkey committed Oct 24, 2021
1 parent 2a9d92f commit dd41f09
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 341 deletions.
35 changes: 18 additions & 17 deletions src/browser/BrowserAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "config-keepassx.h"
#include "core/Global.h"
#include "core/Tools.h"
#include "gui/PasswordGeneratorWidget.h"

#include <QJsonArray>
#include <QJsonDocument>
Expand Down Expand Up @@ -59,6 +60,20 @@ namespace

const int BrowserAction::MaxUrlLength = 256;

BrowserAction::BrowserAction()
{
QObject::connect(browserService(),
&BrowserService::passwordGenerated,
browserService(),
[=](const QString& password, const QString& nonce) {
auto newNonce = incrementNonce(nonce);
QJsonObject message = buildMessage(newNonce);
message["password"] = password;

browserService()->sendPassword(buildResponse("generate-password", message, newNonce));
});
}

QJsonObject BrowserAction::processClientMessage(const QJsonObject& json)
{
if (json.isEmpty()) {
Expand Down Expand Up @@ -315,25 +330,11 @@ QJsonObject BrowserAction::handleGetLogins(const QJsonObject& json, const QStrin

QJsonObject BrowserAction::handleGeneratePassword(const QJsonObject& json, const QString& action)
{
auto errorMessage = getErrorReply(action, ERROR_KEEPASS_ACTION_CANCELLED_OR_DENIED);
auto nonce = json.value("nonce").toString();
auto password = browserSettings()->generatePassword();

if (nonce.isEmpty() || password.isEmpty()) {
return QJsonObject();
}

// For backwards compatibility
password["login"] = password["entropy"];

QJsonArray arr;
arr.append(password);

const QString newNonce = incrementNonce(nonce);

QJsonObject message = buildMessage(newNonce);
message["entries"] = arr;

return buildResponse(action, message, newNonce);
browserService()->showPasswordGenerator(errorMessage, nonce);
return QJsonObject();
}

QJsonObject BrowserAction::handleSetLogin(const QJsonObject& json, const QString& action)
Expand Down
2 changes: 1 addition & 1 deletion src/browser/BrowserAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class QJsonObject;
class BrowserAction
{
public:
explicit BrowserAction() = default;
explicit BrowserAction();
~BrowserAction() = default;

QJsonObject processClientMessage(const QJsonObject& json);
Expand Down
28 changes: 28 additions & 0 deletions src/browser/BrowserService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,34 @@ QString BrowserService::getCurrentTotp(const QString& uuid)
return {};
}

void BrowserService::showPasswordGenerator(const QJsonObject& errorMessage, const QString& nonce)
{
if (!m_passwordGenerator) {
m_passwordGenerator.reset(PasswordGeneratorWidget::popupGenerator());

connect(m_passwordGenerator.data(), &PasswordGeneratorWidget::closed, m_passwordGenerator.data(), [=] {
m_passwordGenerator.reset();
m_browserHost->sendClientMessage(errorMessage);
hideWindow();
});

connect(m_passwordGenerator.data(),
&PasswordGeneratorWidget::appliedPassword,
m_passwordGenerator.data(),
[=](const QString& password) { emit passwordGenerated(password, nonce); });
}

raiseWindow();
m_passwordGenerator->raise();
m_passwordGenerator->activateWindow();
}

void BrowserService::sendPassword(const QJsonObject& message)
{
m_browserHost->sendClientMessage(message);
hideWindow();
}

QString BrowserService::storeKey(const QString& key)
{
auto db = getDatabase();
Expand Down
7 changes: 6 additions & 1 deletion src/browser/BrowserService.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define BROWSERSERVICE_H

#include "core/Entry.h"
#include "gui/PasswordGeneratorWidget.h"

typedef QPair<QString, QString> StringPair;
typedef QList<StringPair> StringPairList;
Expand Down Expand Up @@ -55,6 +56,8 @@ class BrowserService : public QObject
QJsonObject getDatabaseGroups();
QJsonObject createNewGroup(const QString& groupName);
QString getCurrentTotp(const QString& uuid);
void showPasswordGenerator(const QJsonObject& errorMessage, const QString& nonce);
void sendPassword(const QJsonObject& message);

void addEntry(const QString& dbid,
const QString& login,
Expand Down Expand Up @@ -93,6 +96,7 @@ class BrowserService : public QObject

signals:
void requestUnlock();
void passwordGenerated(const QString& password, const QString& nonce);

public slots:
void databaseLocked(DatabaseWidget* dbWidget);
Expand Down Expand Up @@ -145,9 +149,9 @@ private slots:
QString getDatabaseRecycleBinUuid();
bool checkLegacySettings(QSharedPointer<Database> db);
QStringList getEntryURLs(const Entry* entry);

void hideWindow() const;
void raiseWindow(const bool force = false);

void updateWindowState();

static bool moveSettingsToCustomData(Entry* entry, const QString& name);
Expand All @@ -162,6 +166,7 @@ private slots:
QUuid m_keepassBrowserUUID;

QPointer<DatabaseWidget> m_currentDatabaseWidget;
QScopedPointer<PasswordGeneratorWidget> m_passwordGenerator;

Q_DISABLE_COPY(BrowserService);

Expand Down
Loading

0 comments on commit dd41f09

Please sign in to comment.