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 May 16, 2021
1 parent 60adcac commit b2b4a0b
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 343 deletions.
27 changes: 11 additions & 16 deletions src/browser/BrowserAction.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 KeePassXC Team <[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 All @@ -21,6 +21,7 @@
#include "BrowserShared.h"
#include "config-keepassx.h"
#include "core/Global.h"
#include "gui/PasswordGeneratorWidget.h"

#include <QJsonDocument>
#include <QJsonParseError>
Expand Down Expand Up @@ -303,25 +304,19 @@ 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();
auto generator = 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);
connect(generator, &PasswordGeneratorWidget::appliedPassword, [=](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
7 changes: 5 additions & 2 deletions src/browser/BrowserAction.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 KeePassXC Team <[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 All @@ -19,10 +19,13 @@
#define BROWSERACTION_H

#include <QJsonObject>
#include <QObject>
#include <QString>

class BrowserAction
class BrowserAction : public QObject
{
Q_OBJECT

public:
explicit BrowserAction() = default;
~BrowserAction() = default;
Expand Down
23 changes: 22 additions & 1 deletion src/browser/BrowserService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include "core/EntrySearcher.h"
#include "core/Group.h"
#include "core/Metadata.h"
#include "core/PasswordGenerator.h"
#include "core/Tools.h"
#include "gui/MainWindow.h"
#include "gui/MessageBox.h"
Expand Down Expand Up @@ -313,6 +312,28 @@ QString BrowserService::getCurrentTotp(const QString& uuid)
return {};
}

PasswordGeneratorWidget* 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();
});
}

raiseWindow();
return m_passwordGenerator.data();
}

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

QString BrowserService::storeKey(const QString& key)
{
auto db = getDatabase();
Expand Down
6 changes: 5 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"
#include <QObject>
#include <QPointer>
#include <QSharedPointer>
Expand Down Expand Up @@ -59,6 +60,8 @@ class BrowserService : public QObject
QJsonObject getDatabaseGroups();
QJsonObject createNewGroup(const QString& groupName);
QString getCurrentTotp(const QString& uuid);
PasswordGeneratorWidget* showPasswordGenerator(const QJsonObject& errorMessage);
void sendPassword(const QJsonObject& message);

void addEntry(const QString& dbid,
const QString& login,
Expand Down Expand Up @@ -145,9 +148,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 +165,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 b2b4a0b

Please sign in to comment.