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>
This commit is contained in:
Dirk Hohndel 2015-06-14 13:50:13 -07:00
parent 8f7a4a1a97
commit b5eb66545b
5 changed files with 43 additions and 27 deletions

9
pref.h
View file

@ -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)

View file

@ -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();
}

View file

@ -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();

View file

@ -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);
}
}

View file

@ -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);