diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2015-06-14 13:50:13 -0700 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2015-06-14 14:06:28 -0700 |
commit | b5eb66545b4c28809610d0778b9a797da9dda002 (patch) | |
tree | efd7b4c175a7dc5f3454078fe668b85baecf1d6e | |
parent | 8f7a4a1a97629f29e054a9a2204bfffa558e1121 (diff) | |
download | subsurface-b5eb66545b4c28809610d0778b9a797da9dda002.tar.gz |
Cloud storage: clean up handling of cloud storage account
Correctly tracking the status of our authentication with the cloud service
is non-trivial, especially since the user may quit Subsurface between
registering and verifying an account, they might even register on one
machine and verify on another.
This tries to make sure that when in doubt we check with the cloud service
backend. And we show errors in the UI.
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
-rw-r--r-- | pref.h | 9 | ||||
-rw-r--r-- | qt-ui/preferences.cpp | 43 | ||||
-rw-r--r-- | qt-ui/preferences.h | 2 | ||||
-rw-r--r-- | qt-ui/subsurfacewebservices.cpp | 14 | ||||
-rw-r--r-- | qt-ui/subsurfacewebservices.h | 2 |
5 files changed, 43 insertions, 27 deletions
@@ -95,7 +95,7 @@ struct preferences { char *cloud_storage_email; char *cloud_storage_email_encoded; bool save_password_local; - bool show_cloud_pin; + short cloud_verification_status; bool cloud_background_sync; }; enum unit_system_values { @@ -111,6 +111,13 @@ enum def_file_behavior { CLOUD_DEFAULT_FILE }; +enum cloud_status { + CS_UNKNOWN, + CS_INCORRECT_USER_PASSWD, + CS_NEED_TO_VERIFY, + CS_VERIFIED +}; + extern struct preferences prefs, default_prefs; #define PP_GRAPHS_ENABLED (prefs.pp_graphs.po2 || prefs.pp_graphs.pn2 || prefs.pp_graphs.phe) diff --git a/qt-ui/preferences.cpp b/qt-ui/preferences.cpp index 150ab50f6..1bc776e01 100644 --- a/qt-ui/preferences.cpp +++ b/qt-ui/preferences.cpp @@ -103,12 +103,12 @@ void PreferencesDialog::facebookDisconnect() #endif } -void PreferencesDialog::cloudPinNeeded(bool toggle) +void PreferencesDialog::cloudPinNeeded() { - 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); + ui.cloud_storage_pin->setEnabled(prefs.cloud_verification_status == CS_NEED_TO_VERIFY); + ui.cloud_storage_pin->setVisible(prefs.cloud_verification_status == CS_NEED_TO_VERIFY); + ui.cloud_storage_pin_label->setEnabled(prefs.cloud_verification_status == CS_NEED_TO_VERIFY); + ui.cloud_storage_pin_label->setVisible(prefs.cloud_verification_status == CS_NEED_TO_VERIFY); } #define DANGER_GF (gf > 100) ? "* { color: red; }" : "" @@ -220,8 +220,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); - ui.cloud_storage_pin_label->setVisible(prefs.show_cloud_pin); + cloudPinNeeded(); ui.cloud_background_sync->setChecked(prefs.cloud_background_sync); } @@ -383,7 +382,22 @@ void PreferencesDialog::syncSettings() s.beginGroup("CloudStorage"); QString email = ui.cloud_storage_email->text(); QString password = ui.cloud_storage_password->text(); - if (ui.cloud_storage_pin->isVisible()) { + if (prefs.cloud_verification_status == CS_UNKNOWN || + prefs.cloud_verification_status == CS_INCORRECT_USER_PASSWD || + email != prefs.cloud_storage_email || + password != prefs.cloud_storage_password) { + // different credentials - reset verification status + prefs.cloud_verification_status = CS_UNKNOWN; + + // 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); + connect(cloudAuth, SIGNAL(finishedAuthenticate()), this, SLOT(cloudPinNeeded())); + QNetworkReply *reply = cloudAuth->authenticate(email, password); + } else if (prefs.cloud_verification_status == CS_NEED_TO_VERIFY) { QString pin = ui.cloud_storage_pin->text(); if (!pin.isEmpty()) { // connect to backend server to check / create credentials @@ -394,15 +408,6 @@ void PreferencesDialog::syncSettings() 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()) { - 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); SAVE_OR_REMOVE("save_password_local", default_prefs.save_password_local, ui.save_password_local->isChecked()); @@ -413,7 +418,7 @@ void PreferencesDialog::syncSettings() free(prefs.cloud_storage_password); prefs.cloud_storage_password = strdup(qPrintable(password)); } - SAVE_OR_REMOVE("show_cloud_pin", default_prefs.show_cloud_pin, prefs.show_cloud_pin); + SAVE_OR_REMOVE("cloud_verification_status", default_prefs.cloud_verification_status, prefs.cloud_verification_status); SAVE_OR_REMOVE("cloud_background_sync", default_prefs.cloud_background_sync, ui.cloud_background_sync->isChecked()); s.endGroup(); @@ -546,7 +551,7 @@ void PreferencesDialog::loadSettings() if (prefs.save_password_local) { // GET_TEXT macro is not a single statement GET_TXT("password", cloud_storage_password); } - GET_BOOL("show_cloud_pin", show_cloud_pin); + GET_INT("cloud_verification_status", cloud_verification_status); GET_BOOL("cloud_background_sync", cloud_background_sync); s.endGroup(); } diff --git a/qt-ui/preferences.h b/qt-ui/preferences.h index 9e2e25da3..2402a7964 100644 --- a/qt-ui/preferences.h +++ b/qt-ui/preferences.h @@ -39,7 +39,7 @@ slots: void on_cloudDefaultFile_toggled(bool toggle); void facebookLoggedIn(); void facebookDisconnect(); - void cloudPinNeeded(bool toggle); + void cloudPinNeeded(); private: explicit PreferencesDialog(QWidget *parent = 0, Qt::WindowFlags f = 0); void setUiFromPrefs(); diff --git a/qt-ui/subsurfacewebservices.cpp b/qt-ui/subsurfacewebservices.cpp index 16faac61d..06e4d1ec2 100644 --- a/qt-ui/subsurfacewebservices.cpp +++ b/qt-ui/subsurfacewebservices.cpp @@ -976,12 +976,16 @@ void CloudStorageAuthenticate::uploadFinished() { QString cloudAuthReply(reply->readAll()); qDebug() << "Completed connection with cloud storage backend, response" << cloudAuthReply; - if (cloudAuthReply == "[VERIFIED]") { - prefs.show_cloud_pin = false; - emit finishedAuthenticate(prefs.show_cloud_pin); + if (cloudAuthReply == "[VERIFIED]" || cloudAuthReply == "[OK]") { + prefs.cloud_verification_status = CS_VERIFIED; + emit finishedAuthenticate(); } else if (cloudAuthReply == "[VERIFY]") { - prefs.show_cloud_pin = true; - emit finishedAuthenticate(prefs.show_cloud_pin); + prefs.cloud_verification_status = CS_NEED_TO_VERIFY; + emit finishedAuthenticate(); + } else { + prefs.cloud_verification_status = CS_INCORRECT_USER_PASSWD; + report_error("%s", qPrintable(cloudAuthReply)); + MainWindow::instance()->getNotificationWidget()->showNotification(get_error_string(), KMessageWidget::Error); } } diff --git a/qt-ui/subsurfacewebservices.h b/qt-ui/subsurfacewebservices.h index d08f64aa8..43d1a8add 100644 --- a/qt-ui/subsurfacewebservices.h +++ b/qt-ui/subsurfacewebservices.h @@ -117,7 +117,7 @@ public: QNetworkReply* authenticate(QString email, QString password, QString pin = ""); explicit CloudStorageAuthenticate(QObject *parent); signals: - void finishedAuthenticate(bool toggle); + void finishedAuthenticate(); private slots: void uploadError(QNetworkReply::NetworkError error); |