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

Global TOTP dialog #10334

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ set(keepassx_SOURCES
gui/EditWidgetProperties.cpp
gui/FileDialog.cpp
gui/Font.cpp
gui/GlobalTotpDialog.cpp
gui/GuiTools.cpp
gui/HtmlExporter.cpp
gui/IconModels.cpp
Expand Down
145 changes: 145 additions & 0 deletions src/gui/GlobalTotpDialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* Copyright (C) 2024 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
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "GlobalTotpDialog.h"
#include "ui_GlobalTotpDialog.h"

#include "core/Clock.h"
#include "core/Totp.h"
#include "gui/Clipboard.h"
#include "gui/MainWindow.h"

#include <QPushButton>
#include <QShortcut>
#include <QToolButton>

GlobalTotpDialog::GlobalTotpDialog(QWidget* parent)
: QWidget(parent)
, m_ui(new Ui::GlobalTotpDialog())
{
setAttribute(Qt::WA_DeleteOnClose);

m_ui->setupUi(this);

connect(&m_totpUpdateTimer, SIGNAL(timeout()), this, SLOT(updateCounter()));
connect(&m_totpUpdateTimer, SIGNAL(timeout()), this, SLOT(updateTable()));

new QShortcut(QKeySequence(QKeySequence::Copy), this, SLOT(copyToClipboard()));

connect(m_ui->buttonBox, SIGNAL(rejected()), this, SIGNAL(closed()));
}

GlobalTotpDialog::~GlobalTotpDialog() = default;

void GlobalTotpDialog::setDatabaseWidget(DatabaseWidget* databaseWidget)
{
if (!databaseWidget) {
return;
}

m_databaseWidget = databaseWidget;
m_ui->totpTable->clear();

m_entries.clear();
const auto db = m_databaseWidget->database();
for (const auto& group : db->rootGroup()->groupsRecursive(true)) {
if (group == db->metadata()->recycleBin()) {
continue;
}

for (const auto& entry : group->entries()) {
if (entry->hasTotp()) {
m_entries.push_back(entry);
}
}
}

m_ui->totpTable->setRowCount(m_entries.size());
m_ui->totpTable->setColumnCount(5);
m_ui->totpTable->setHorizontalHeaderLabels(QStringList() << tr("Title") << tr("Username") << tr("TOTP")
<< tr("Expires in (s)") << tr("Copy TOTP"));
m_ui->totpTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
m_ui->totpTable->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
m_ui->totpTable->horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents);
m_ui->totpTable->horizontalHeader()->setSectionResizeMode(4, QHeaderView::ResizeToContents);

uint epoch = Clock::currentSecondsSinceEpoch() - 1;

auto i = 0;
for (const auto& entry : m_entries) {
const auto step = entry->totpSettings()->step;

m_ui->totpTable->setItem(i, 0, new QTableWidgetItem(entry->title()));
m_ui->totpTable->setItem(i, 1, new QTableWidgetItem(entry->username()));
m_ui->totpTable->setItem(i, 2, new QTableWidgetItem(entry->totp()));
m_ui->totpTable->setItem(i, 3, new QTableWidgetItem(QString::number(step - (epoch % step))));

auto button = new QToolButton();
button->setText(tr("Copy"));
button->setProperty("row", i);

connect(button, &QToolButton::clicked, this, [this]() {
auto btn = qobject_cast<QToolButton*>(sender());
auto totp = m_ui->totpTable->item(btn->property("row").toInt(), 2)->text();

clipboard()->setText(totp);
});

m_ui->totpTable->setCellWidget(i, 4, button);
++i;
}

m_totpUpdateTimer.start(1000);
}

void GlobalTotpDialog::copyToClipboard()
{
const auto selectedItems = m_ui->totpTable->selectedItems();
if (selectedItems.isEmpty()) {
return;
}

const auto selectedRow = selectedItems.first()->row();
auto totp = m_ui->totpTable->item(selectedRow, 2)->text();
clipboard()->setText(totp);
}

void GlobalTotpDialog::updateCounter()
{
if (!isVisible()) {
m_totpUpdateTimer.stop();
return;
}
}

void GlobalTotpDialog::updateTable()
{
uint epoch = Clock::currentSecondsSinceEpoch() - 1;

for (auto i = 0; i < m_ui->totpTable->rowCount(); ++i) {
const auto totpCounter = m_ui->totpTable->item(i, 3)->text().toInt();
const auto entry = m_entries.at(i);
const auto step = entry->totpSettings()->step;

if (totpCounter == 1) {
m_ui->totpTable->item(i, 2)->setText(entry->totp());
m_ui->totpTable->item(i, 3)->setText(QString::number(step));
} else {
m_ui->totpTable->item(i, 3)->setText(QString::number(step - (epoch % step)));
}
}
}
56 changes: 56 additions & 0 deletions src/gui/GlobalTotpDialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2024 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
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef KEEPASSXC_GLOBALTOTPDIALOG_H
#define KEEPASSXC_GLOBALTOTPDIALOG_H

#include "core/Database.h"
#include "core/Entry.h"
#include "gui/DatabaseWidget.h"

namespace Ui
{
class GlobalTotpDialog;
}

class GlobalTotpDialog : public QWidget
{
Q_OBJECT

public:
explicit GlobalTotpDialog(QWidget* parent = nullptr);
~GlobalTotpDialog() override;

void setDatabaseWidget(DatabaseWidget* databaseWidget);

signals:
void closed();

private Q_SLOTS:
void updateCounter();
void updateTable();
void copyToClipboard();

private:
QScopedPointer<Ui::GlobalTotpDialog> m_ui;

QPointer<DatabaseWidget> m_databaseWidget;
QList<Entry*> m_entries;
QTimer m_totpUpdateTimer;
};

#endif // KEEPASSXC_GLOBALTOTPDIALOG_H
68 changes: 68 additions & 0 deletions src/gui/GlobalTotpDialog.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>GlobalTotpDialog</class>
<widget class="QWidget" name="GlobalTotpDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>264</width>
<height>194</height>
</rect>
</property>
<property name="windowTitle">
<string>Timed Passwords</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTableWidget" name="totpTable">
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
<property name="styleSheet">
<string notr="true">QTableView::item {padding: 3px;}</string>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="showDropIndicator" stdset="0">
<bool>false</bool>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
<property name="showGrid">
<bool>true</bool>
</property>
<property name="cornerButtonEnabled">
<bool>false</bool>
</property>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
<attribute name="horizontalHeaderVisible">
<bool>true</bool>
</attribute>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Close</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
24 changes: 24 additions & 0 deletions src/gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "core/Tools.h"
#include "gui/AboutDialog.h"
#include "gui/ActionCollection.h"
#include "gui/GlobalTotpDialog.h"
#include "gui/Icons.h"
#include "gui/MessageBox.h"
#include "gui/SearchWidget.h"
Expand Down Expand Up @@ -398,6 +399,7 @@ MainWindow::MainWindow()

m_ui->actionSettings->setIcon(icons()->icon("configure"));
m_ui->actionPasswordGenerator->setIcon(icons()->icon("password-generator"));
m_ui->actionGlobalTotp->setIcon(icons()->icon("totp"));

m_ui->actionAbout->setIcon(icons()->icon("help-about"));
m_ui->actionDonate->setIcon(icons()->icon("donate"));
Expand Down Expand Up @@ -529,6 +531,12 @@ MainWindow::MainWindow()
});
m_ui->passwordGeneratorWidget->setStandaloneMode(true);

connect(m_ui->actionGlobalTotp, SIGNAL(toggled(bool)), SLOT(toggleGlobalTotp(bool)));
connect(m_ui->globalTotpDialog, &GlobalTotpDialog::closed, this, [this] {
toggleGlobalTotp(false);
});
m_ui->passwordGeneratorWidget->setStandaloneMode(true);

connect(m_ui->welcomeWidget, SIGNAL(newDatabase()), SLOT(switchToNewDatabase()));
connect(m_ui->welcomeWidget, SIGNAL(openDatabase()), SLOT(switchToOpenDatabase()));
connect(m_ui->welcomeWidget, SIGNAL(openDatabaseFile(QString)), SLOT(switchToDatabaseFile(QString)));
Expand Down Expand Up @@ -958,6 +966,7 @@ void MainWindow::setMenuActionState(DatabaseWidget::Mode mode)
m_ui->actionExportHtml->setEnabled(true);
m_ui->actionExportXML->setEnabled(true);
m_ui->actionDatabaseMerge->setEnabled(m_ui->tabWidget->currentIndex() != -1);
m_ui->actionGlobalTotp->setEnabled(true);
#ifdef WITH_XC_BROWSER_PASSKEYS
m_ui->actionPasskeys->setEnabled(true);
m_ui->actionImportPasskey->setEnabled(true);
Expand Down Expand Up @@ -1041,6 +1050,7 @@ void MainWindow::setMenuActionState(DatabaseWidget::Mode mode)
#endif

m_searchWidgetAction->setEnabled(false);
m_ui->actionGlobalTotp->setEnabled(false);
break;
}
default:
Expand Down Expand Up @@ -1083,6 +1093,10 @@ void MainWindow::setMenuActionState(DatabaseWidget::Mode mode)
bool blocked = m_ui->actionPasswordGenerator->blockSignals(true);
m_ui->actionPasswordGenerator->toggle();
m_ui->actionPasswordGenerator->blockSignals(blocked);
} else if ((currentIndex == GlobalTotpScreen) != m_ui->actionGlobalTotp->isChecked()) {
bool blocked = m_ui->actionGlobalTotp->blockSignals(true);
m_ui->actionGlobalTotp->toggle();
m_ui->actionGlobalTotp->blockSignals(blocked);
} else if ((currentIndex == SettingsScreen) != m_ui->actionSettings->isChecked()) {
bool blocked = m_ui->actionSettings->blockSignals(true);
m_ui->actionSettings->toggle();
Expand Down Expand Up @@ -1273,6 +1287,16 @@ void MainWindow::togglePasswordGenerator(bool enabled)
}
}

void MainWindow::toggleGlobalTotp(bool enabled)
{
if (enabled) {
m_ui->globalTotpDialog->setDatabaseWidget(m_ui->tabWidget->currentDatabaseWidget());
m_ui->stackedWidget->setCurrentIndex(GlobalTotpScreen);
} else {
switchToDatabases();
}
}

void MainWindow::switchToNewDatabase()
{
m_ui->tabWidget->newDatabase();
Expand Down
4 changes: 3 additions & 1 deletion src/gui/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ class MainWindow : public QMainWindow
DatabaseTabScreen = 0,
SettingsScreen = 1,
WelcomeScreen = 2,
PasswordGeneratorScreen = 3
PasswordGeneratorScreen = 3,
GlobalTotpScreen = 4
};

signals:
Expand Down Expand Up @@ -121,6 +122,7 @@ private slots:
void switchToDatabases();
void switchToSettings(bool enabled);
void togglePasswordGenerator(bool enabled);
void toggleGlobalTotp(bool enabled);
void switchToNewDatabase();
void switchToOpenDatabase();
void switchToDatabaseFile(const QString& file);
Expand Down
Loading
Loading