From 318bf5cccc9a8ac2c8ee18939e3c6c4a4e7a0fb3 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Thu, 4 Jun 2015 17:26:30 -0700 Subject: Cloud storage: first stab at creating an account on the backend This triggers when the email address / password is changed in the preferences. It opens an https connection with the backend server (the URL is hardcoded) which should create an account with these credentials. Signed-off-by: Dirk Hohndel --- qt-ui/preferences.cpp | 17 +++++++++++++++-- qt-ui/subsurfacewebservices.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ qt-ui/subsurfacewebservices.h | 15 +++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/qt-ui/preferences.cpp b/qt-ui/preferences.cpp index f0f1212a2..8342dff78 100644 --- a/qt-ui/preferences.cpp +++ b/qt-ui/preferences.cpp @@ -9,6 +9,8 @@ #include #include +#include "subsurfacewebservices.h" + #if defined(FBSUPPORT) #include "socialnetworks.h" #endif @@ -360,10 +362,21 @@ void PreferencesDialog::syncSettings() s.endGroup(); s.beginGroup("CloudStorage"); - SAVE_OR_REMOVE("email", default_prefs.cloud_storage_email, ui.cloud_storage_email->text()); + QString email = ui.cloud_storage_email->text(); + QString password = ui.cloud_storage_password->text(); + if (email != prefs.cloud_storage_email || password != prefs.cloud_storage_password) { + // connect to backend server to check / create credentials + QRegularExpression reg("^[a-zA-Z0-9@.+_-]+$"); + if (!reg.match(email).hasMatch() || !reg.match(password).hasMatch()) { + report_error(qPrintable(tr("Cloud storage email and password can only consist of letters, numbers, and '.', '-', '_', and '+'."))); + } + CloudStorageAuthenticate *cloudAuth = new CloudStorageAuthenticate(this); + QNetworkReply *reply = cloudAuth->authenticate(email, password); + } + SAVE_OR_REMOVE("email", default_prefs.cloud_storage_email, email); SAVE_OR_REMOVE("save_password_local", default_prefs.save_password_local, ui.save_password_local->isChecked()); if (ui.save_password_local->isChecked()) - SAVE_OR_REMOVE("password", default_prefs.cloud_storage_password, ui.cloud_storage_password->text()); + SAVE_OR_REMOVE("password", default_prefs.cloud_storage_password, password); else s.remove("password"); s.endGroup(); diff --git a/qt-ui/subsurfacewebservices.cpp b/qt-ui/subsurfacewebservices.cpp index 0159977a7..5681974ec 100644 --- a/qt-ui/subsurfacewebservices.cpp +++ b/qt-ui/subsurfacewebservices.cpp @@ -928,3 +928,43 @@ QNetworkReply* UserSurveyServices::sendSurvey(QString values) reply = manager()->get(request); return reply; } + +CloudStorageAuthenticate::CloudStorageAuthenticate(QObject *parent) : QObject(parent) +{ + userAgent = getUserAgent(); + +} + +#define CLOUDBACKEND "https://cloud.subsurface-divelog.org/storage" + +QNetworkReply* CloudStorageAuthenticate::authenticate(QString email, QString password) +{ + QNetworkRequest *request = new QNetworkRequest(QUrl(CLOUDBACKEND)); + request->setRawHeader("Accept", "text/xml, text/plain"); + request->setRawHeader("User-Agent", userAgent.toUtf8()); + request->setHeader(QNetworkRequest::ContentTypeHeader, "text/plain"); + reply = WebServices::manager()->post(*request, qPrintable(QString(email + " " + password))); + connect(reply, SIGNAL(finished()), this, SLOT(uploadFinished())); + connect(reply, SIGNAL(sslErrors(QList)), this, SLOT(sslErrors(QList))); + connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, + SLOT(uploadError(QNetworkReply::NetworkError))); + return reply; +} + +void CloudStorageAuthenticate::uploadFinished() +{ + qDebug() << "Completed connection with cloud storage backend, response" << reply->readAll(); +} + +void CloudStorageAuthenticate::uploadError(QNetworkReply::NetworkError error) +{ + qDebug() << "Received error response from cloud storage backend:" << reply->errorString(); +} + +void CloudStorageAuthenticate::sslErrors(QList errorList) +{ + qDebug() << "Received error response trying to set up https connection with cloud storage backend:"; + Q_FOREACH (QSslError err, errorList) { + qDebug() << err.errorString(); + } +} diff --git a/qt-ui/subsurfacewebservices.h b/qt-ui/subsurfacewebservices.h index a9735382c..48f93da92 100644 --- a/qt-ui/subsurfacewebservices.h +++ b/qt-ui/subsurfacewebservices.h @@ -109,6 +109,21 @@ slots: virtual void startDownload() { } virtual void startUpload() { } virtual void buttonClicked(QAbstractButton *button) { } +}; + +class CloudStorageAuthenticate : QObject { + Q_OBJECT +public: + QNetworkReply* authenticate(QString email, QString password); + explicit CloudStorageAuthenticate(QObject *parent); +private +slots: + void uploadError(QNetworkReply::NetworkError error); + void sslErrors(QList errorList); + void uploadFinished(); +private: + QNetworkReply *reply; + QString userAgent; }; -- cgit v1.2.3-70-g09d2 From a07376b5345fdb398510ac29835493b7ca75ce77 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Sun, 7 Jun 2015 09:54:28 -0700 Subject: Cloud storage: initial support for confirming the email PIN Signed-off-by: Dirk Hohndel --- pref.h | 1 + qt-ui/preferences.cpp | 12 +++++++++++- qt-ui/preferences.h | 1 + qt-ui/preferences.ui | 14 ++++++++++++++ qt-ui/subsurfacewebservices.cpp | 5 ++++- qt-ui/subsurfacewebservices.h | 4 +++- 6 files changed, 34 insertions(+), 3 deletions(-) diff --git a/pref.h b/pref.h index 6fb72bd34..e8b9571cd 100644 --- a/pref.h +++ b/pref.h @@ -94,6 +94,7 @@ struct preferences { char *cloud_storage_email; char *cloud_storage_email_encoded; bool save_password_local; + bool show_cloud_pin; }; enum unit_system_values { METRIC, diff --git a/qt-ui/preferences.cpp b/qt-ui/preferences.cpp index 8342dff78..89402c87a 100644 --- a/qt-ui/preferences.cpp +++ b/qt-ui/preferences.cpp @@ -59,7 +59,6 @@ PreferencesDialog::PreferencesDialog(QWidget *parent, Qt::WindowFlags f) : QDial connect(ui.btnDisconnectFacebook, &QPushButton::clicked, fb, &FacebookManager::logout); connect(fb, &FacebookManager::justLoggedOut, this, &PreferencesDialog::facebookDisconnect); #endif - connect(ui.proxyType, SIGNAL(currentIndexChanged(int)), this, SLOT(proxyType_changed(int))); connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(buttonClicked(QAbstractButton *))); connect(ui.gflow, SIGNAL(valueChanged(int)), this, SLOT(gflowChanged(int))); @@ -104,6 +103,12 @@ void PreferencesDialog::facebookDisconnect() #endif } +void PreferencesDialog::cloudPinNeeded(bool toggle) +{ + ui.cloud_storage_pin->setEnabled(toggle); + ui.cloud_storage_pin->setVisible(toggle); +} + #define DANGER_GF (gf > 100) ? "* { color: red; }" : "" void PreferencesDialog::gflowChanged(int gf) { @@ -210,6 +215,7 @@ void PreferencesDialog::setUiFromPrefs() ui.cloud_storage_email->setText(prefs.cloud_storage_email); ui.cloud_storage_password->setText(prefs.cloud_storage_password); ui.save_password_local->setChecked(prefs.save_password_local); + ui.cloud_storage_pin->setVisible(prefs.show_cloud_pin); } void PreferencesDialog::restorePrefs() @@ -364,6 +370,7 @@ void PreferencesDialog::syncSettings() s.beginGroup("CloudStorage"); QString email = ui.cloud_storage_email->text(); QString password = ui.cloud_storage_password->text(); + QString pin = ui.cloud_storage_pin->text(); if (email != prefs.cloud_storage_email || password != prefs.cloud_storage_password) { // connect to backend server to check / create credentials QRegularExpression reg("^[a-zA-Z0-9@.+_-]+$"); @@ -371,6 +378,7 @@ void PreferencesDialog::syncSettings() report_error(qPrintable(tr("Cloud storage email and password can only consist of letters, numbers, and '.', '-', '_', and '+'."))); } CloudStorageAuthenticate *cloudAuth = new CloudStorageAuthenticate(this); + connect(cloudAuth, SIGNAL(finishedAuthenticate(bool)), this, SLOT(cloudPinNeeded(bool))); QNetworkReply *reply = cloudAuth->authenticate(email, password); } SAVE_OR_REMOVE("email", default_prefs.cloud_storage_email, email); @@ -379,6 +387,7 @@ void PreferencesDialog::syncSettings() SAVE_OR_REMOVE("password", default_prefs.cloud_storage_password, password); else s.remove("password"); + SAVE_OR_REMOVE("show_cloud_pin", default_prefs.show_cloud_pin, prefs.show_cloud_pin); s.endGroup(); loadSettings(); emit settingsChanged(); @@ -495,6 +504,7 @@ void PreferencesDialog::loadSettings() GET_TXT("password", cloud_storage_password); GET_TXT("email", cloud_storage_email); GET_BOOL("save_password_local", save_password_local); + GET_BOOL("show_cloud_pin", show_cloud_pin); s.endGroup(); } diff --git a/qt-ui/preferences.h b/qt-ui/preferences.h index 842597732..6553cba18 100644 --- a/qt-ui/preferences.h +++ b/qt-ui/preferences.h @@ -36,6 +36,7 @@ slots: void on_btnUseDefaultFile_toggled(bool toggle); void facebookLoggedIn(); void facebookDisconnect(); + void cloudPinNeeded(bool toggle); private: explicit PreferencesDialog(QWidget *parent = 0, Qt::WindowFlags f = 0); void setUiFromPrefs(); diff --git a/qt-ui/preferences.ui b/qt-ui/preferences.ui index 1692ab069..069255643 100644 --- a/qt-ui/preferences.ui +++ b/qt-ui/preferences.ui @@ -451,6 +451,20 @@ + + + + Verification PIN + + + + + + + + + + diff --git a/qt-ui/subsurfacewebservices.cpp b/qt-ui/subsurfacewebservices.cpp index 5681974ec..f0208db53 100644 --- a/qt-ui/subsurfacewebservices.cpp +++ b/qt-ui/subsurfacewebservices.cpp @@ -953,7 +953,10 @@ QNetworkReply* CloudStorageAuthenticate::authenticate(QString email, QString pas void CloudStorageAuthenticate::uploadFinished() { - qDebug() << "Completed connection with cloud storage backend, response" << reply->readAll(); + QString cloudAuthReply(reply->readAll()); + qDebug() << "Completed connection with cloud storage backend, response" << cloudAuthReply; + prefs.show_cloud_pin = (cloudAuthReply == "[VERIFY]"); + emit finishedAuthenticate(prefs.show_cloud_pin); } void CloudStorageAuthenticate::uploadError(QNetworkReply::NetworkError error) diff --git a/qt-ui/subsurfacewebservices.h b/qt-ui/subsurfacewebservices.h index 48f93da92..352c69459 100644 --- a/qt-ui/subsurfacewebservices.h +++ b/qt-ui/subsurfacewebservices.h @@ -111,11 +111,13 @@ slots: virtual void buttonClicked(QAbstractButton *button) { } }; -class CloudStorageAuthenticate : QObject { +class CloudStorageAuthenticate : public QObject { Q_OBJECT public: QNetworkReply* authenticate(QString email, QString password); explicit CloudStorageAuthenticate(QObject *parent); +signals: + void finishedAuthenticate(bool toggle); private slots: void uploadError(QNetworkReply::NetworkError error); -- cgit v1.2.3-70-g09d2 From a04f1fd13317034c471ad7eb8dbf55375566f947 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Tue, 9 Jun 2015 11:21:03 -0700 Subject: Cloud storage: deal with visibility of PIN entry Signed-off-by: Dirk Hohndel --- qt-ui/preferences.cpp | 17 +++++++++++++++-- qt-ui/preferences.ui | 2 +- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/qt-ui/preferences.cpp b/qt-ui/preferences.cpp index 89402c87a..e3872ee4d 100644 --- a/qt-ui/preferences.cpp +++ b/qt-ui/preferences.cpp @@ -107,6 +107,8 @@ void PreferencesDialog::cloudPinNeeded(bool toggle) { ui.cloud_storage_pin->setEnabled(toggle); ui.cloud_storage_pin->setVisible(toggle); + ui.cloud_storage_pin_label->setEnabled(toggle); + ui.cloud_storage_pin_label->setVisible(toggle); } #define DANGER_GF (gf > 100) ? "* { color: red; }" : "" @@ -216,6 +218,7 @@ void PreferencesDialog::setUiFromPrefs() ui.cloud_storage_password->setText(prefs.cloud_storage_password); ui.save_password_local->setChecked(prefs.save_password_local); ui.cloud_storage_pin->setVisible(prefs.show_cloud_pin); + ui.cloud_storage_pin_label->setVisible(prefs.show_cloud_pin); } void PreferencesDialog::restorePrefs() @@ -370,8 +373,18 @@ void PreferencesDialog::syncSettings() s.beginGroup("CloudStorage"); QString email = ui.cloud_storage_email->text(); QString password = ui.cloud_storage_password->text(); - QString pin = ui.cloud_storage_pin->text(); - if (email != prefs.cloud_storage_email || password != prefs.cloud_storage_password) { + if (ui.cloud_storage_pin->isVisible()) { + QString pin = ui.cloud_storage_pin->text(); + if (!pin.isEmpty()) { + // connect to backend server to check / create credentials + QRegularExpression reg("^[a-zA-Z0-9@.+_-]+$"); + if (!reg.match(email).hasMatch() || !reg.match(password).hasMatch()) { + report_error(qPrintable(tr("Cloud storage email and password can only consist of letters, numbers, and '.', '-', '_', and '+'."))); + } + CloudStorageAuthenticate *cloudAuth = new CloudStorageAuthenticate(this); + QNetworkReply *reply = cloudAuth->authenticate(email, password, pin); + } + } else if (email != prefs.cloud_storage_email || password != prefs.cloud_storage_password) { // connect to backend server to check / create credentials QRegularExpression reg("^[a-zA-Z0-9@.+_-]+$"); if (!reg.match(email).hasMatch() || !reg.match(password).hasMatch()) { diff --git a/qt-ui/preferences.ui b/qt-ui/preferences.ui index 069255643..54ffb41d0 100644 --- a/qt-ui/preferences.ui +++ b/qt-ui/preferences.ui @@ -452,7 +452,7 @@ - + Verification PIN -- cgit v1.2.3-70-g09d2 From 57507cfb93984e520fdd9545c84ea3b921c2914e Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Tue, 9 Jun 2015 11:21:46 -0700 Subject: Cloud storage: implement confirmation of email address via PIN Signed-off-by: Dirk Hohndel --- qt-ui/subsurfacewebservices.cpp | 27 +++++++++++++++++++++------ qt-ui/subsurfacewebservices.h | 2 +- qthelper.cpp | 3 +-- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/qt-ui/subsurfacewebservices.cpp b/qt-ui/subsurfacewebservices.cpp index f0208db53..6aadc2de7 100644 --- a/qt-ui/subsurfacewebservices.cpp +++ b/qt-ui/subsurfacewebservices.cpp @@ -935,15 +935,25 @@ CloudStorageAuthenticate::CloudStorageAuthenticate(QObject *parent) : QObject(pa } -#define CLOUDBACKEND "https://cloud.subsurface-divelog.org/storage" +#define CLOUDURL "https://cloud.subsurface-divelog.org/" +#define CLOUDBACKENDSTORAGE CLOUDURL "storage" +#define CLOUDBACKENDVERIFY CLOUDURL "verify" -QNetworkReply* CloudStorageAuthenticate::authenticate(QString email, QString password) +QNetworkReply* CloudStorageAuthenticate::authenticate(QString email, QString password, QString pin) { - QNetworkRequest *request = new QNetworkRequest(QUrl(CLOUDBACKEND)); + QString payload(email + " " + password); + QUrl requestUrl; + if (pin == "") { + requestUrl = QUrl(CLOUDBACKENDSTORAGE); + } else { + requestUrl = QUrl(CLOUDBACKENDVERIFY); + payload += " " + pin; + } + 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 = WebServices::manager()->post(*request, qPrintable(QString(email + " " + password))); + reply = WebServices::manager()->post(*request, qPrintable(payload)); connect(reply, SIGNAL(finished()), this, SLOT(uploadFinished())); connect(reply, SIGNAL(sslErrors(QList)), this, SLOT(sslErrors(QList))); connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, @@ -955,8 +965,13 @@ void CloudStorageAuthenticate::uploadFinished() { QString cloudAuthReply(reply->readAll()); qDebug() << "Completed connection with cloud storage backend, response" << cloudAuthReply; - prefs.show_cloud_pin = (cloudAuthReply == "[VERIFY]"); - emit finishedAuthenticate(prefs.show_cloud_pin); + if (cloudAuthReply == "[VERIFIED]") { + prefs.show_cloud_pin = false; + emit finishedAuthenticate(prefs.show_cloud_pin); + } else if (cloudAuthReply == "[VERIFY]") { + prefs.show_cloud_pin = true; + emit finishedAuthenticate(prefs.show_cloud_pin); + } } void CloudStorageAuthenticate::uploadError(QNetworkReply::NetworkError error) diff --git a/qt-ui/subsurfacewebservices.h b/qt-ui/subsurfacewebservices.h index 352c69459..6f8d6279a 100644 --- a/qt-ui/subsurfacewebservices.h +++ b/qt-ui/subsurfacewebservices.h @@ -114,7 +114,7 @@ slots: class CloudStorageAuthenticate : public QObject { Q_OBJECT public: - QNetworkReply* authenticate(QString email, QString password); + QNetworkReply* authenticate(QString email, QString password, QString pin = ""); explicit CloudStorageAuthenticate(QObject *parent); signals: void finishedAuthenticate(bool toggle); diff --git a/qthelper.cpp b/qthelper.cpp index a85be003e..fb688746e 100644 --- a/qthelper.cpp +++ b/qthelper.cpp @@ -1024,8 +1024,7 @@ fraction_t string_to_fraction(const char *str) int getCloudURL(QString &filename) { QString email = QString(prefs.cloud_storage_email); - email.replace("@", "_at_"); - email.replace(QRegularExpression("[^a-zA-Z0-9._+-]"), ""); + email.replace(QRegularExpression("[^a-zA-Z0-9@._+-]"), ""); if (email.isEmpty() || same_string(prefs.cloud_storage_password, "")) return report_error("Please configure Cloud storage email and password in the preferences"); if (email != prefs.cloud_storage_email_encoded) { -- cgit v1.2.3-70-g09d2