keepassxc

Форк
0
/
TotpSetupDialog.cpp 
131 строка · 4.7 Кб
1
/*
2
 *  Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
3
 *
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.
8
 *
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.
13
 *
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/>.
16
 */
17

18
#include "TotpSetupDialog.h"
19
#include "ui_TotpSetupDialog.h"
20

21
#include "core/Base32.h"
22
#include "core/Totp.h"
23
#include "gui/MessageBox.h"
24

25
TotpSetupDialog::TotpSetupDialog(QWidget* parent, Entry* entry)
26
    : QDialog(parent)
27
    , m_ui(new Ui::TotpSetupDialog())
28
    , m_entry(entry)
29
{
30
    m_ui->setupUi(this);
31
    setAttribute(Qt::WA_DeleteOnClose);
32
    setWindowFlag(Qt::WindowContextHelpButtonHint, false);
33
    setFixedSize(sizeHint());
34

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)));
38

39
    init();
40
}
41

42
TotpSetupDialog::~TotpSetupDialog() = default;
43

44
void TotpSetupDialog::saveSettings()
45
{
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"));
56
        return;
57
    }
58

59
    QString encShortName;
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;
64

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();
72
    }
73

74
    auto settings = m_entry->totpSettings();
75
    if (settings) {
76
        if (key.isEmpty()) {
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) {
82
                return;
83
            }
84
        }
85

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;
90
        }
91
    }
92

93
    m_entry->setTotp(Totp::createSettings(key, digits, step, format, encShortName, algorithm));
94
    emit totpUpdated();
95
    close();
96
}
97

98
void TotpSetupDialog::toggleCustom(bool status)
99
{
100
    m_ui->customSettingsGroup->setEnabled(status);
101
}
102

103
void TotpSetupDialog::init()
104
{
105
    // Add algorithm choices
106
    auto algorithms = Totp::supportedAlgorithms();
107
    for (const auto& item : algorithms) {
108
        m_ui->algorithmComboBox->addItem(item.first, item.second);
109
    }
110
    m_ui->algorithmComboBox->setCurrentIndex(0);
111

112
    // Read entry totp settings
113
    auto settings = m_entry->totpSettings();
114
    if (settings) {
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);
119

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);
126
            if (index != -1) {
127
                m_ui->algorithmComboBox->setCurrentIndex(index);
128
            }
129
        }
130
    }
131
}
132

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.