2017-04-27 20:24:53 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								// SPDX-License-Identifier: GPL-2.0
							 | 
						
					
						
							
								
									
										
										
										
											2016-01-26 12:24:27 -02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include "cloudstorage.h"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include "pref.h"
							 | 
						
					
						
							
								
									
										
										
										
											2018-06-03 22:15:19 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include "qthelper.h"
							 | 
						
					
						
							
								
									
										
										
										
											2018-09-08 19:46:11 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include "settings/qPrefCloudStorage.h"
							 | 
						
					
						
							
								
									
										
										
										
											2016-01-26 12:24:27 -02: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"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-01-26 12:28:38 -02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								QNetworkReply* CloudStorageAuthenticate::backend(const QString& email,const QString& password,const QString& pin,const QString& newpasswd)
							 | 
						
					
						
							
								
									
										
										
										
											2016-01-26 12:24:27 -02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							
								
									
										
										
										
											2016-01-26 12:28:38 -02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									QString payload(email + QChar(' ') + password);
							 | 
						
					
						
							
								
									
										
										
										
											2016-01-26 12:24:27 -02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									QUrl requestUrl;
							 | 
						
					
						
							
								
									
										
										
										
											2016-01-26 12:28:38 -02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									if (pin.isEmpty() && newpasswd.isEmpty()) {
							 | 
						
					
						
							
								
									
										
										
										
											2016-01-26 12:24:27 -02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										requestUrl = QUrl(CLOUDBACKENDSTORAGE);
							 | 
						
					
						
							
								
									
										
										
										
											2016-01-26 12:28:38 -02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									} else if (!newpasswd.isEmpty()) {
							 | 
						
					
						
							
								
									
										
										
										
											2016-01-26 12:24:27 -02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										requestUrl = QUrl(CLOUDBACKENDUPDATE);
							 | 
						
					
						
							
								
									
										
										
										
											2016-01-26 12:28:38 -02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										payload += QChar(' ') + newpasswd;
							 | 
						
					
						
							
								
									
										
										
										
											2018-09-10 06:49:14 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										cloudNewPassword = newpasswd;
							 | 
						
					
						
							
								
									
										
										
										
											2016-01-26 12:24:27 -02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									} else {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										requestUrl = QUrl(CLOUDBACKENDVERIFY);
							 | 
						
					
						
							
								
									
										
										
										
											2016-01-26 12:28:38 -02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										payload += QChar(' ') + pin;
							 | 
						
					
						
							
								
									
										
										
										
											2016-01-26 12:24:27 -02: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));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									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)));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									return reply;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								void CloudStorageAuthenticate::uploadFinished()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									static QString myLastError;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									QString cloudAuthReply(reply->readAll());
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									qDebug() << "Completed connection with cloud storage backend, response" << cloudAuthReply;
							 | 
						
					
						
							
								
									
										
										
										
											2018-07-14 16:52:25 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									qPrefCloudStorage csSettings(parent());
							 | 
						
					
						
							
								
									
										
										
										
											2016-07-23 12:53:37 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-01-26 12:28:38 -02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									if (cloudAuthReply == QLatin1String("[VERIFIED]") || cloudAuthReply == QLatin1String("[OK]")) {
							 | 
						
					
						
							
								
									
										
										
										
											2018-09-02 16:22:16 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										csSettings.set_cloud_verification_status(qPrefCloudStorage::CS_VERIFIED);
							 | 
						
					
						
							
								
									
										
										
										
											2016-01-26 12:24:27 -02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										/* TODO: Move this to a correct place
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										NotificationWidget *nw = MainWindow::instance()->getNotificationWidget();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										if (nw->getNotificationText() == myLastError)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											nw->hideNotification();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										*/
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										myLastError.clear();
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-26 09:17:22 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									} else if (cloudAuthReply == QLatin1String("[VERIFY]") ||
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										   cloudAuthReply == QLatin1String("Invalid PIN")) {
							 | 
						
					
						
							
								
									
										
										
										
											2018-09-02 16:22:16 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										csSettings.set_cloud_verification_status(qPrefCloudStorage::CS_NEED_TO_VERIFY);
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-26 15:57:03 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										report_error(qPrintable(tr("Cloud account verification required, enter PIN in preferences")));
							 | 
						
					
						
							
								
									
										
										
										
											2016-01-26 12:28:38 -02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									} else if (cloudAuthReply == QLatin1String("[PASSWDCHANGED]")) {
							 | 
						
					
						
							
								
									
										
										
										
											2018-09-10 10:38:42 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										csSettings.set_cloud_storage_password(cloudNewPassword);
							 | 
						
					
						
							
								
									
										
										
										
											2018-09-10 06:49:14 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										cloudNewPassword.clear();
							 | 
						
					
						
							
								
									
										
										
										
											2016-01-26 12:24:27 -02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										emit passwordChangeSuccessful();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										return;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									} else {
							 | 
						
					
						
							
								
									
										
										
										
											2018-09-02 16:22:16 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										csSettings.set_cloud_verification_status(qPrefCloudStorage::CS_INCORRECT_USER_PASSWD);
							 | 
						
					
						
							
								
									
										
										
										
											2016-01-26 12:24:27 -02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										myLastError = cloudAuthReply;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										report_error("%s", qPrintable(cloudAuthReply));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									emit finishedAuthenticate();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-03-06 19:38:17 -03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								void CloudStorageAuthenticate::uploadError(QNetworkReply::NetworkError)
							 | 
						
					
						
							
								
									
										
										
										
											2016-01-26 12:24:27 -02:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									qDebug() << "Received error response from cloud storage backend:" << reply->errorString();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								void CloudStorageAuthenticate::sslErrors(QList<QSslError> errorList)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									if (verbose) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										qDebug() << "Received error response trying to set up https connection with cloud storage backend:";
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										Q_FOREACH (QSslError err, errorList) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											qDebug() << err.errorString();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									QSslConfiguration conf = reply->sslConfiguration();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									QSslCertificate cert = conf.peerCertificate();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									QByteArray hexDigest = cert.digest().toHex();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									if (reply->url().toString().contains(prefs.cloud_base_url) &&
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									    hexDigest == "13ff44c62996cfa5cd69d6810675490e") {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										if (verbose)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											qDebug() << "Overriding SSL check as I recognize the certificate digest" << hexDigest;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										reply->ignoreSslErrors();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									} else {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										if (verbose)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											qDebug() << "got invalid SSL certificate with hex digest" << hexDigest;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								QNetworkAccessManager *manager()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									static QNetworkAccessManager *manager = new QNetworkAccessManager(qApp);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									return manager;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 |