2017-04-27 18:24:53 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-01-26 14:24:27 +00:00
|
|
|
#include "cloudstorage.h"
|
|
|
|
#include "pref.h"
|
2018-06-03 20:15:19 +00:00
|
|
|
#include "qthelper.h"
|
2019-08-05 17:41:15 +00:00
|
|
|
#include "errorhelper.h"
|
2018-09-08 17:46:11 +00:00
|
|
|
#include "settings/qPrefCloudStorage.h"
|
2016-01-26 14:24:27 +00:00
|
|
|
#include <QApplication>
|
|
|
|
|
|
|
|
CloudStorageAuthenticate::CloudStorageAuthenticate(QObject *parent) :
|
|
|
|
QObject(parent),
|
|
|
|
reply(NULL)
|
|
|
|
{
|
|
|
|
userAgent = getUserAgent();
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CLOUDURL QString(prefs.cloud_base_url)
|
|
|
|
#define CLOUDBACKENDSTORAGE CLOUDURL + "/storage"
|
|
|
|
#define CLOUDBACKENDVERIFY CLOUDURL + "/verify"
|
|
|
|
#define CLOUDBACKENDUPDATE CLOUDURL + "/update"
|
2022-08-08 00:11:05 +00:00
|
|
|
#define CLOUDBACKENDDELETE CLOUDURL + "/delete-account"
|
2016-01-26 14:24:27 +00:00
|
|
|
|
2016-01-26 14:28:38 +00:00
|
|
|
QNetworkReply* CloudStorageAuthenticate::backend(const QString& email,const QString& password,const QString& pin,const QString& newpasswd)
|
2016-01-26 14:24:27 +00:00
|
|
|
{
|
2016-01-26 14:28:38 +00:00
|
|
|
QString payload(email + QChar(' ') + password);
|
2016-01-26 14:24:27 +00:00
|
|
|
QUrl requestUrl;
|
2016-01-26 14:28:38 +00:00
|
|
|
if (pin.isEmpty() && newpasswd.isEmpty()) {
|
2016-01-26 14:24:27 +00:00
|
|
|
requestUrl = QUrl(CLOUDBACKENDSTORAGE);
|
2016-01-26 14:28:38 +00:00
|
|
|
} else if (!newpasswd.isEmpty()) {
|
2016-01-26 14:24:27 +00:00
|
|
|
requestUrl = QUrl(CLOUDBACKENDUPDATE);
|
2016-01-26 14:28:38 +00:00
|
|
|
payload += QChar(' ') + newpasswd;
|
2018-09-10 13:49:14 +00:00
|
|
|
cloudNewPassword = newpasswd;
|
2016-01-26 14:24:27 +00:00
|
|
|
} else {
|
|
|
|
requestUrl = QUrl(CLOUDBACKENDVERIFY);
|
2016-01-26 14:28:38 +00:00
|
|
|
payload += QChar(' ') + pin;
|
2016-01-26 14:24:27 +00:00
|
|
|
}
|
|
|
|
QNetworkRequest *request = new QNetworkRequest(requestUrl);
|
|
|
|
request->setRawHeader("Accept", "text/xml, text/plain");
|
|
|
|
request->setRawHeader("User-Agent", userAgent.toUtf8());
|
|
|
|
request->setHeader(QNetworkRequest::ContentTypeHeader, "text/plain");
|
|
|
|
reply = manager()->post(*request, qPrintable(payload));
|
2022-04-11 04:49:11 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
connect(reply, &QNetworkReply::finished, this, &CloudStorageAuthenticate::uploadFinished);
|
|
|
|
connect(reply, &QNetworkReply::sslErrors, this, &CloudStorageAuthenticate::sslErrors);
|
|
|
|
connect(reply, &QNetworkReply::errorOccurred, this, &CloudStorageAuthenticate::uploadError);
|
|
|
|
#else
|
2016-01-26 14:24:27 +00:00
|
|
|
connect(reply, SIGNAL(finished()), this, SLOT(uploadFinished()));
|
|
|
|
connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrors(QList<QSslError>)));
|
|
|
|
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this,
|
|
|
|
SLOT(uploadError(QNetworkReply::NetworkError)));
|
2022-04-11 04:49:11 +00:00
|
|
|
#endif
|
2016-01-26 14:24:27 +00:00
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
2022-08-08 00:11:05 +00:00
|
|
|
QNetworkReply* CloudStorageAuthenticate::deleteAccount(const QString& email, const QString& password)
|
|
|
|
{
|
|
|
|
QString payload(email + QChar(' ') + password);
|
|
|
|
QNetworkRequest *request = new QNetworkRequest(QUrl(CLOUDBACKENDDELETE));
|
|
|
|
request->setRawHeader("Accept", "text/xml, text/plain");
|
|
|
|
request->setRawHeader("User-Agent", userAgent.toUtf8());
|
|
|
|
request->setHeader(QNetworkRequest::ContentTypeHeader, "text/plain");
|
|
|
|
reply = manager()->post(*request, qPrintable(payload));
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
connect(reply, &QNetworkReply::finished, this, &CloudStorageAuthenticate::deleteFinished);
|
|
|
|
connect(reply, &QNetworkReply::sslErrors, this, &CloudStorageAuthenticate::sslErrors);
|
|
|
|
connect(reply, &QNetworkReply::errorOccurred, this, &CloudStorageAuthenticate::uploadError);
|
|
|
|
#else
|
|
|
|
connect(reply, SIGNAL(finished()), this, SLOT(deleteFinished()));
|
|
|
|
connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrors(QList<QSslError>)));
|
|
|
|
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this,
|
|
|
|
SLOT(uploadError(QNetworkReply::NetworkError)));
|
|
|
|
#endif
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloudStorageAuthenticate::deleteFinished()
|
|
|
|
{
|
2024-03-26 14:49:55 +00:00
|
|
|
std::string cloudAuthReply = reply->readAll().toStdString();
|
|
|
|
report_info("Completed connection with cloud storage backend, response %s", cloudAuthReply.c_str());
|
2022-08-08 00:11:05 +00:00
|
|
|
emit finishedDelete();
|
|
|
|
}
|
|
|
|
|
2016-01-26 14:24:27 +00:00
|
|
|
void CloudStorageAuthenticate::uploadFinished()
|
|
|
|
{
|
2024-03-26 14:49:55 +00:00
|
|
|
static std::string myLastError;
|
2016-01-26 14:24:27 +00:00
|
|
|
|
2024-03-26 14:49:55 +00:00
|
|
|
std::string cloudAuthReply = reply->readAll().toStdString();
|
|
|
|
report_info("Completed connection with cloud storage backend, response %s", cloudAuthReply.c_str());
|
2016-07-23 03:53:37 +00:00
|
|
|
|
2024-03-26 14:49:55 +00:00
|
|
|
if (cloudAuthReply == "[VERIFIED]" || cloudAuthReply == "[OK]") {
|
2019-11-23 16:34:13 +00:00
|
|
|
qPrefCloudStorage::set_cloud_verification_status(qPrefCloudStorage::CS_VERIFIED);
|
2016-01-26 14:24:27 +00:00
|
|
|
/* TODO: Move this to a correct place
|
|
|
|
NotificationWidget *nw = MainWindow::instance()->getNotificationWidget();
|
2024-03-26 14:49:55 +00:00
|
|
|
if (nw->getNotificationText().toStdString() == myLastError)
|
2016-01-26 14:24:27 +00:00
|
|
|
nw->hideNotification();
|
|
|
|
*/
|
|
|
|
myLastError.clear();
|
2024-03-26 14:49:55 +00:00
|
|
|
} else if (cloudAuthReply == "[VERIFY]" || cloudAuthReply == "Invalid PIN") {
|
2019-11-23 16:34:13 +00:00
|
|
|
qPrefCloudStorage::set_cloud_verification_status(qPrefCloudStorage::CS_NEED_TO_VERIFY);
|
2024-03-12 08:17:50 +00:00
|
|
|
report_error("%s", qPrintable(tr("Cloud account verification required, enter PIN in preferences")));
|
2024-03-26 14:49:55 +00:00
|
|
|
} else if (cloudAuthReply == "[PASSWDCHANGED]") {
|
2019-11-23 16:34:13 +00:00
|
|
|
qPrefCloudStorage::set_cloud_storage_password(cloudNewPassword);
|
2018-09-10 13:49:14 +00:00
|
|
|
cloudNewPassword.clear();
|
2016-01-26 14:24:27 +00:00
|
|
|
emit passwordChangeSuccessful();
|
|
|
|
return;
|
|
|
|
} else {
|
2019-11-23 16:34:13 +00:00
|
|
|
qPrefCloudStorage::set_cloud_verification_status(qPrefCloudStorage::CS_INCORRECT_USER_PASSWD);
|
2016-01-26 14:24:27 +00:00
|
|
|
myLastError = cloudAuthReply;
|
2024-03-26 14:49:55 +00:00
|
|
|
report_error("%s", cloudAuthReply.c_str());
|
2016-01-26 14:24:27 +00:00
|
|
|
}
|
|
|
|
emit finishedAuthenticate();
|
|
|
|
}
|
|
|
|
|
2016-03-06 22:38:17 +00:00
|
|
|
void CloudStorageAuthenticate::uploadError(QNetworkReply::NetworkError)
|
2016-01-26 14:24:27 +00:00
|
|
|
{
|
2024-03-26 14:49:55 +00:00
|
|
|
report_info("Received error response from cloud storage backend: %s", qPrintable(reply->errorString()));
|
2016-01-26 14:24:27 +00:00
|
|
|
}
|
|
|
|
|
2019-04-01 20:15:19 +00:00
|
|
|
void CloudStorageAuthenticate::sslErrors(const QList<QSslError> &errorList)
|
2016-01-26 14:24:27 +00:00
|
|
|
{
|
|
|
|
if (verbose) {
|
2024-03-26 14:49:55 +00:00
|
|
|
report_info("Received error response trying to set up https connection with cloud storage backend:");
|
2019-04-01 20:15:19 +00:00
|
|
|
for (QSslError err: errorList) {
|
2024-03-26 14:49:55 +00:00
|
|
|
report_info("%s", qPrintable(err.errorString()));
|
2016-01-26 14:24:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
QSslConfiguration conf = reply->sslConfiguration();
|
|
|
|
QSslCertificate cert = conf.peerCertificate();
|
|
|
|
QByteArray hexDigest = cert.digest().toHex();
|
2024-03-26 14:49:55 +00:00
|
|
|
report_info("got invalid SSL certificate with hex digest %s", qPrintable(hexDigest));
|
2016-01-26 14:24:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QNetworkAccessManager *manager()
|
|
|
|
{
|
|
|
|
static QNetworkAccessManager *manager = new QNetworkAccessManager(qApp);
|
|
|
|
return manager;
|
|
|
|
}
|