From 735d508a29f2e67f1082ee922ee61ce9dec21c8a Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Thu, 1 Mar 2018 09:31:08 +0200 Subject: [PATCH] Handle empty matrix ID (regression from 9de1ec1) fixes #259 --- include/LoginPage.h | 2 +- src/LoginPage.cc | 61 ++++++++++++++++++--------------------------- src/MatrixClient.cc | 10 ++++---- 3 files changed, 30 insertions(+), 43 deletions(-) diff --git a/include/LoginPage.h b/include/LoginPage.h index 003f0ee2..b2b40537 100644 --- a/include/LoginPage.h +++ b/include/LoginPage.h @@ -48,7 +48,7 @@ protected: public slots: // Displays errors produced during the login. - void loginError(QString error_message); + void loginError(QString msg) { error_label_->setText(msg); } private slots: // Callback for the back button. diff --git a/src/LoginPage.cc b/src/LoginPage.cc index 6c01670a..f71a992e 100644 --- a/src/LoginPage.cc +++ b/src/LoginPage.cc @@ -151,40 +151,23 @@ LoginPage::LoginPage(QSharedPointer client, QWidget *parent) connect(serverInput_, SIGNAL(editingFinished()), this, SLOT(onServerAddressEntered())); } -void -LoginPage::loginError(QString error) -{ - error_label_->setText(error); -} - -bool -LoginPage::isMatrixIdValid() -{ - auto matrix_id = matrixid_input_->text(); - - try { - parse(matrix_id.toStdString()); - return true; - } catch (const std::exception &e) { - return false; - } - - return false; -} - void LoginPage::onMatrixIdEntered() { error_label_->setText(""); - if (!isMatrixIdValid()) { - loginError("You have entered an invalid Matrix ID e.g @joe:matrix.org"); - return; - } else if (password_input_->text().isEmpty()) { - loginError(tr("Empty password")); + User user; + + try { + user = parse(matrixid_input_->text().toStdString()); + } catch (const std::exception &e) { + return loginError("You have entered an invalid Matrix ID e.g @joe:matrix.org"); } - QString homeServer = matrixid_input_->text().split(":").at(1); + if (password_input_->text().isEmpty()) + return loginError(tr("Empty password")); + + QString homeServer = QString::fromStdString(user.hostname()); if (homeServer != inferredServerAddress_) { serverInput_->hide(); serverLayout_->removeWidget(errorIcon_); @@ -251,17 +234,21 @@ LoginPage::onLoginButtonClicked() { error_label_->setText(""); - if (!isMatrixIdValid()) { - loginError("You have entered an invalid Matrix ID e.g @joe:matrix.org"); - } else if (password_input_->text().isEmpty()) { - loginError("Empty password"); - } else { - QString user = matrixid_input_->text().split(":").at(0).split("@").at(1); - QString password = password_input_->text(); - client_->setServer(serverInput_->text()); - client_->login(user, password); - emit loggingIn(); + User user; + + try { + user = parse(matrixid_input_->text().toStdString()); + } catch (const std::exception &e) { + return loginError("You have entered an invalid Matrix ID e.g @joe:matrix.org"); } + + if (password_input_->text().isEmpty()) + return loginError(tr("Empty password")); + + client_->setServer(serverInput_->text()); + client_->login(QString::fromStdString(user.localpart()), password_input_->text()); + + emit loggingIn(); } void diff --git a/src/MatrixClient.cc b/src/MatrixClient.cc index bcf7a62b..544f58cb 100644 --- a/src/MatrixClient.cc +++ b/src/MatrixClient.cc @@ -123,11 +123,6 @@ MatrixClient::login(const QString &username, const QString &password) noexcept int status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - if (reply->error()) { - emit loginError(reply->errorString()); - return; - } - if (status_code == 403) { emit loginError(tr("Wrong username or password")); return; @@ -144,6 +139,11 @@ MatrixClient::login(const QString &username, const QString &password) noexcept return; } + if (reply->error()) { + emit loginError(reply->errorString()); + return; + } + try { mtx::responses::Login login = nlohmann::json::parse(reply->readAll().data());