1 /* ============================================================
2 * QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader
3 * Copyright (C) 2011-2020 QuiteRSS Team <quiterssteam@gmail.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 * ============================================================ */
18 #include "authenticationdialog.h"
19 
20 #include <QSqlQuery>
21 
AuthenticationDialog(const QUrl & url,QAuthenticator * auth,QWidget * parent)22 AuthenticationDialog::AuthenticationDialog(const QUrl &url,
23                                            QAuthenticator *auth,
24                                            QWidget *parent)
25   : Dialog(parent, Qt::MSWindowsFixedSizeDialogHint),
26     auth_(auth)
27 {
28   save_ = new QCheckBox(tr("Save password"), this);
29   save_->setChecked(false);
30 
31   server_ = url.host();
32   if (server_.isEmpty()) {
33     server_ = url.toString();
34   }
35 
36   QSqlQuery q;
37   q.prepare("SELECT username, password FROM passwords WHERE server=?");
38   q.addBindValue(server_);
39   q.exec();
40   if (q.next()) {
41     auth_->setUser(q.value(0).toString());
42     auth_->setPassword(QString::fromUtf8(QByteArray::fromBase64(q.value(1).toByteArray())));
43     save_->setChecked(true);
44   } else {
45     setWindowFlags (windowFlags() & ~Qt::WindowContextHelpButtonHint);
46     setWindowTitle(tr("Authorization required"));
47     setMinimumWidth(400);
48 
49     user_ = new LineEdit(this);
50     user_->setText(auth->user());
51     pass_ = new LineEdit(this);
52     pass_->setEchoMode(QLineEdit::Password);
53 
54     QGridLayout *layout = new QGridLayout();
55     layout->addWidget(new QLabel(tr("Server:")), 0, 0);
56     layout->addWidget(new QLabel(server_), 0, 1);
57     layout->addWidget(new QLabel(tr("Message:")), 1, 0);
58     layout->addWidget(new QLabel(auth->realm()), 1, 1);
59     layout->addWidget(new QLabel(tr("Username:")), 2, 0);
60     layout->addWidget(user_, 2, 1);
61     layout->addWidget(new QLabel(tr("Password:")), 3, 0);
62     layout->addWidget(pass_, 3, 1);
63 
64     pageLayout->addLayout(layout);
65     pageLayout->addSpacing(10);
66     pageLayout->addWidget(save_);
67 
68     buttonBox->addButton(QDialogButtonBox::Ok);
69     buttonBox->addButton(QDialogButtonBox::Cancel);
70     connect(buttonBox, SIGNAL(accepted()), this, SLOT(acceptDialog()));
71   }
72 }
73 
acceptDialog()74 void AuthenticationDialog::acceptDialog()
75 {
76   auth_->setUser(user_->text());
77   auth_->setPassword(pass_->text());
78 
79   if (save_->isChecked()) {
80     QSqlQuery q;
81     q.prepare("INSERT INTO passwords (server, username, password) "
82               "VALUES (:server, :username, :password)");
83     q.bindValue(":server", server_);
84     q.bindValue(":username", user_->text());
85     q.bindValue(":password", pass_->text().toUtf8().toBase64());
86     q.exec();
87   }
88 
89   accept();
90 }
91