Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Launch KeePassXC password generator popup from the extension #6529

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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