2018-09-05 16:20:57 -04:00
|
|
|
/*
|
2019-11-08 18:06:13 -05:00
|
|
|
* Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
|
2018-09-05 16:20:57 -04:00
|
|
|
*
|
|
|
|
* 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 "TotpSetupDialog.h"
|
|
|
|
#include "ui_TotpSetupDialog.h"
|
|
|
|
|
2019-10-14 09:25:45 -04:00
|
|
|
#include "core/Base32.h"
|
|
|
|
#include "gui/MessageBox.h"
|
2018-10-31 23:27:38 -04:00
|
|
|
#include "totp/totp.h"
|
|
|
|
|
2018-09-05 16:20:57 -04:00
|
|
|
TotpSetupDialog::TotpSetupDialog(QWidget* parent, Entry* entry)
|
|
|
|
: QDialog(parent)
|
|
|
|
, m_ui(new Ui::TotpSetupDialog())
|
|
|
|
, m_entry(entry)
|
|
|
|
{
|
|
|
|
m_ui->setupUi(this);
|
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
setFixedSize(sizeHint());
|
|
|
|
|
|
|
|
connect(m_ui->buttonBox, SIGNAL(rejected()), SLOT(close()));
|
|
|
|
connect(m_ui->buttonBox, SIGNAL(accepted()), SLOT(saveSettings()));
|
|
|
|
connect(m_ui->radioCustom, SIGNAL(toggled(bool)), SLOT(toggleCustom(bool)));
|
|
|
|
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
TotpSetupDialog::~TotpSetupDialog()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void TotpSetupDialog::saveSettings()
|
|
|
|
{
|
2019-10-14 09:25:45 -04:00
|
|
|
// Secret key sanity check
|
2019-11-08 18:06:13 -05:00
|
|
|
// Convert user input to all uppercase and remove '='
|
2021-06-08 19:03:07 -04:00
|
|
|
auto key = m_ui->seedEdit->text().toUpper().remove(" ").remove("=").trimmed().toLatin1();
|
2019-10-14 09:25:45 -04:00
|
|
|
auto sanitizedKey = Base32::sanitizeInput(key);
|
2019-11-08 18:06:13 -05:00
|
|
|
// Use startsWith to ignore added '=' for padding at the end
|
|
|
|
if (!sanitizedKey.startsWith(key)) {
|
2019-10-14 09:25:45 -04:00
|
|
|
MessageBox::information(this,
|
|
|
|
tr("Invalid TOTP Secret"),
|
|
|
|
tr("You have entered an invalid secret key. The key must be in Base32 format.\n"
|
|
|
|
"Example: JBSWY3DPEHPK3PXP"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-05 16:20:57 -04:00
|
|
|
QString encShortName;
|
|
|
|
uint digits = Totp::DEFAULT_DIGITS;
|
2019-02-01 21:01:51 -05:00
|
|
|
uint step = Totp::DEFAULT_STEP;
|
2019-10-14 09:25:45 -04:00
|
|
|
Totp::Algorithm algorithm = Totp::DEFAULT_ALGORITHM;
|
|
|
|
Totp::StorageFormat format = Totp::DEFAULT_FORMAT;
|
2019-02-01 21:01:51 -05:00
|
|
|
|
|
|
|
if (m_ui->radioSteam->isChecked()) {
|
2018-09-05 16:20:57 -04:00
|
|
|
digits = Totp::STEAM_DIGITS;
|
|
|
|
encShortName = Totp::STEAM_SHORTNAME;
|
2019-02-01 21:01:51 -05:00
|
|
|
} else if (m_ui->radioCustom->isChecked()) {
|
2019-10-14 09:25:45 -04:00
|
|
|
algorithm = static_cast<Totp::Algorithm>(m_ui->algorithmComboBox->currentData().toInt());
|
2019-02-01 21:01:51 -05:00
|
|
|
step = m_ui->stepSpinBox->value();
|
2019-10-14 09:25:45 -04:00
|
|
|
digits = m_ui->digitsSpinBox->value();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto settings = m_entry->totpSettings();
|
|
|
|
if (settings) {
|
|
|
|
if (key.isEmpty()) {
|
|
|
|
auto answer = MessageBox::question(this,
|
|
|
|
tr("Confirm Remove TOTP Settings"),
|
|
|
|
tr("Are you sure you want to delete TOTP settings for this entry?"),
|
|
|
|
MessageBox::Delete | MessageBox::Cancel);
|
|
|
|
if (answer != MessageBox::Delete) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
format = settings->format;
|
|
|
|
if (format == Totp::StorageFormat::LEGACY && m_ui->radioCustom->isChecked()) {
|
|
|
|
// Implicitly upgrade to the OTPURL format to allow for custom settings
|
|
|
|
format = Totp::DEFAULT_FORMAT;
|
2019-02-01 21:01:51 -05:00
|
|
|
}
|
2018-09-05 16:20:57 -04:00
|
|
|
}
|
|
|
|
|
2019-10-14 09:25:45 -04:00
|
|
|
m_entry->setTotp(Totp::createSettings(key, digits, step, format, encShortName, algorithm));
|
2018-09-05 16:20:57 -04:00
|
|
|
emit totpUpdated();
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TotpSetupDialog::toggleCustom(bool status)
|
|
|
|
{
|
2019-10-14 09:25:45 -04:00
|
|
|
m_ui->customSettingsGroup->setEnabled(status);
|
2018-09-05 16:20:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TotpSetupDialog::init()
|
|
|
|
{
|
2019-10-14 09:25:45 -04:00
|
|
|
// Add algorithm choices
|
|
|
|
auto algorithms = Totp::supportedAlgorithms();
|
|
|
|
for (const auto& item : algorithms) {
|
|
|
|
m_ui->algorithmComboBox->addItem(item.first, item.second);
|
|
|
|
}
|
|
|
|
m_ui->algorithmComboBox->setCurrentIndex(0);
|
|
|
|
|
|
|
|
// Read entry totp settings
|
2018-09-05 16:20:57 -04:00
|
|
|
auto settings = m_entry->totpSettings();
|
2019-10-14 09:25:45 -04:00
|
|
|
if (settings) {
|
2019-11-08 18:06:13 -05:00
|
|
|
auto key = settings->key;
|
|
|
|
m_ui->seedEdit->setText(key.remove("="));
|
|
|
|
m_ui->seedEdit->setCursorPosition(0);
|
2018-09-05 16:20:57 -04:00
|
|
|
m_ui->stepSpinBox->setValue(settings->step);
|
|
|
|
|
|
|
|
if (settings->encoder.shortName == Totp::STEAM_SHORTNAME) {
|
|
|
|
m_ui->radioSteam->setChecked(true);
|
|
|
|
} else if (settings->custom) {
|
|
|
|
m_ui->radioCustom->setChecked(true);
|
2019-10-14 09:25:45 -04:00
|
|
|
m_ui->digitsSpinBox->setValue(settings->digits);
|
|
|
|
int index = m_ui->algorithmComboBox->findData(settings->algorithm);
|
|
|
|
if (index != -1) {
|
|
|
|
m_ui->algorithmComboBox->setCurrentIndex(index);
|
2018-09-05 16:20:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|