Skip to content

Commit

Permalink
Launch KeePassXC password generator popup from the extension
Browse files Browse the repository at this point in the history
  • Loading branch information
varjolintu committed Oct 11, 2021
1 parent b6716bd commit 06ba242
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 338 deletions.
26 changes: 11 additions & 15 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 @@ -315,25 +316,20 @@ 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();

browserService()->showPasswordGenerator(errorMessage);

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

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

QJsonArray arr;
arr.append(password);

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

QJsonObject message = buildMessage(newNonce);
message["entries"] = arr;
browserService()->sendPassword(buildResponse(action, message, newNonce));
});

return buildResponse(action, message, newNonce);
return QJsonObject();
}

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

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

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

connect(m_passwordGenerator.data(), &PasswordGeneratorWidget::appliedPassword, this, &BrowserService::passwordGenerated);
}

raiseWindow();
}

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);
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);

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 06ba242

Please sign in to comment.