2
* Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, either version 2 or (at your option)
7
* version 3 of the License.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18
#include "TotpSetupDialog.h"
19
#include "ui_TotpSetupDialog.h"
21
#include "core/Base32.h"
23
#include "gui/MessageBox.h"
25
TotpSetupDialog::TotpSetupDialog(QWidget* parent, Entry* entry)
27
, m_ui(new Ui::TotpSetupDialog())
31
setAttribute(Qt::WA_DeleteOnClose);
32
setWindowFlag(Qt::WindowContextHelpButtonHint, false);
33
setFixedSize(sizeHint());
35
connect(m_ui->buttonBox, SIGNAL(rejected()), SLOT(close()));
36
connect(m_ui->buttonBox, SIGNAL(accepted()), SLOT(saveSettings()));
37
connect(m_ui->radioCustom, SIGNAL(toggled(bool)), SLOT(toggleCustom(bool)));
42
TotpSetupDialog::~TotpSetupDialog() = default;
44
void TotpSetupDialog::saveSettings()
46
// Secret key sanity check
47
// Convert user input to all uppercase and remove '='
48
auto key = m_ui->seedEdit->text().toUpper().remove(" ").remove("=").trimmed().toLatin1();
49
auto sanitizedKey = Base32::sanitizeInput(key);
50
// Use startsWith to ignore added '=' for padding at the end
51
if (!sanitizedKey.startsWith(key)) {
52
MessageBox::information(this,
53
tr("Invalid TOTP Secret"),
54
tr("You have entered an invalid secret key. The key must be in Base32 format.\n"
55
"Example: JBSWY3DPEHPK3PXP"));
60
uint digits = Totp::DEFAULT_DIGITS;
61
uint step = Totp::DEFAULT_STEP;
62
Totp::Algorithm algorithm = Totp::DEFAULT_ALGORITHM;
63
Totp::StorageFormat format = Totp::DEFAULT_FORMAT;
65
if (m_ui->radioSteam->isChecked()) {
66
digits = Totp::STEAM_DIGITS;
67
encShortName = Totp::STEAM_SHORTNAME;
68
} else if (m_ui->radioCustom->isChecked()) {
69
algorithm = static_cast<Totp::Algorithm>(m_ui->algorithmComboBox->currentData().toInt());
70
step = m_ui->stepSpinBox->value();
71
digits = m_ui->digitsSpinBox->value();
74
auto settings = m_entry->totpSettings();
77
auto answer = MessageBox::question(this,
78
tr("Confirm Remove TOTP Settings"),
79
tr("Are you sure you want to delete TOTP settings for this entry?"),
80
MessageBox::Delete | MessageBox::Cancel);
81
if (answer != MessageBox::Delete) {
86
format = settings->format;
87
if (format == Totp::StorageFormat::LEGACY && m_ui->radioCustom->isChecked()) {
88
// Implicitly upgrade to the OTPURL format to allow for custom settings
89
format = Totp::DEFAULT_FORMAT;
93
m_entry->setTotp(Totp::createSettings(key, digits, step, format, encShortName, algorithm));
98
void TotpSetupDialog::toggleCustom(bool status)
100
m_ui->customSettingsGroup->setEnabled(status);
103
void TotpSetupDialog::init()
105
// Add algorithm choices
106
auto algorithms = Totp::supportedAlgorithms();
107
for (const auto& item : algorithms) {
108
m_ui->algorithmComboBox->addItem(item.first, item.second);
110
m_ui->algorithmComboBox->setCurrentIndex(0);
112
// Read entry totp settings
113
auto settings = m_entry->totpSettings();
115
auto key = settings->key;
116
m_ui->seedEdit->setText(key.remove("="));
117
m_ui->seedEdit->setCursorPosition(0);
118
m_ui->stepSpinBox->setValue(settings->step);
120
if (settings->encoder.shortName == Totp::STEAM_SHORTNAME) {
121
m_ui->radioSteam->setChecked(true);
122
} else if (settings->custom) {
123
m_ui->radioCustom->setChecked(true);
124
m_ui->digitsSpinBox->setValue(settings->digits);
125
int index = m_ui->algorithmComboBox->findData(settings->algorithm);
127
m_ui->algorithmComboBox->setCurrentIndex(index);