2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2017 Konstantinos Sideris <siderisk@auth.gr>
|
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2020-05-10 00:31:00 +03:00
|
|
|
#include <QDesktopServices>
|
2020-11-25 01:42:35 +03:00
|
|
|
#include <QFontMetrics>
|
2020-05-25 14:03:49 +03:00
|
|
|
#include <QLabel>
|
2020-01-31 08:12:02 +03:00
|
|
|
#include <QPainter>
|
2017-11-22 20:52:38 +03:00
|
|
|
#include <QStyleOption>
|
2020-11-25 01:42:35 +03:00
|
|
|
#include <QtMath>
|
2017-11-22 20:52:38 +03:00
|
|
|
|
2018-02-28 22:14:41 +03:00
|
|
|
#include <mtx/identifiers.hpp>
|
2020-05-10 00:31:00 +03:00
|
|
|
#include <mtx/requests.hpp>
|
2020-01-31 18:08:30 +03:00
|
|
|
#include <mtx/responses/login.hpp>
|
2018-02-28 22:14:41 +03:00
|
|
|
|
2017-07-15 17:11:46 +03:00
|
|
|
#include "Config.h"
|
2019-06-27 16:42:58 +03:00
|
|
|
#include "Logging.h"
|
2017-11-22 22:13:22 +03:00
|
|
|
#include "LoginPage.h"
|
2017-10-28 15:46:39 +03:00
|
|
|
#include "MatrixClient.h"
|
2020-05-10 00:31:00 +03:00
|
|
|
#include "SSOHandler.h"
|
2021-03-06 22:52:08 +03:00
|
|
|
#include "UserSettingsPage.h"
|
2018-07-17 16:37:25 +03:00
|
|
|
#include "ui/FlatButton.h"
|
|
|
|
#include "ui/LoadingIndicator.h"
|
|
|
|
#include "ui/OverlayModal.h"
|
|
|
|
#include "ui/RaisedButton.h"
|
|
|
|
#include "ui/TextField.h"
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2020-05-10 00:31:00 +03:00
|
|
|
Q_DECLARE_METATYPE(LoginPage::LoginMethod)
|
|
|
|
|
2018-02-28 22:14:41 +03:00
|
|
|
using namespace mtx::identifiers;
|
|
|
|
|
2018-05-08 18:43:56 +03:00
|
|
|
LoginPage::LoginPage(QWidget *parent)
|
2017-08-20 13:47:22 +03:00
|
|
|
: QWidget(parent)
|
|
|
|
, inferredServerAddress_()
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2020-05-10 00:31:00 +03:00
|
|
|
qRegisterMetaType<LoginPage::LoginMethod>("LoginPage::LoginMethod");
|
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
top_layout_ = new QVBoxLayout();
|
|
|
|
|
|
|
|
top_bar_layout_ = new QHBoxLayout();
|
|
|
|
top_bar_layout_->setSpacing(0);
|
|
|
|
top_bar_layout_->setMargin(0);
|
|
|
|
|
|
|
|
back_button_ = new FlatButton(this);
|
|
|
|
back_button_->setMinimumSize(QSize(30, 30));
|
|
|
|
|
|
|
|
top_bar_layout_->addWidget(back_button_, 0, Qt::AlignLeft | Qt::AlignVCenter);
|
|
|
|
top_bar_layout_->addStretch(1);
|
|
|
|
|
|
|
|
QIcon icon;
|
2017-10-15 22:08:51 +03:00
|
|
|
icon.addFile(":/icons/icons/ui/angle-pointing-to-left.png");
|
2017-09-10 12:58:00 +03:00
|
|
|
|
|
|
|
back_button_->setIcon(icon);
|
2017-10-15 22:08:51 +03:00
|
|
|
back_button_->setIconSize(QSize(32, 32));
|
2017-09-10 12:58:00 +03:00
|
|
|
|
2017-10-15 22:08:51 +03:00
|
|
|
QIcon logo;
|
|
|
|
logo.addFile(":/logos/login.png");
|
2017-09-10 12:58:00 +03:00
|
|
|
|
|
|
|
logo_ = new QLabel(this);
|
2017-10-15 22:08:51 +03:00
|
|
|
logo_->setPixmap(logo.pixmap(128));
|
2017-09-10 12:58:00 +03:00
|
|
|
|
|
|
|
logo_layout_ = new QHBoxLayout();
|
|
|
|
logo_layout_->setContentsMargins(0, 0, 0, 20);
|
|
|
|
logo_layout_->addWidget(logo_, 0, Qt::AlignHCenter);
|
|
|
|
|
|
|
|
form_wrapper_ = new QHBoxLayout();
|
|
|
|
form_widget_ = new QWidget();
|
|
|
|
form_widget_->setMinimumSize(QSize(350, 200));
|
|
|
|
|
|
|
|
form_layout_ = new QVBoxLayout();
|
|
|
|
form_layout_->setSpacing(20);
|
|
|
|
form_layout_->setContentsMargins(0, 0, 0, 30);
|
|
|
|
form_widget_->setLayout(form_layout_);
|
|
|
|
|
|
|
|
form_wrapper_->addStretch(1);
|
|
|
|
form_wrapper_->addWidget(form_widget_);
|
|
|
|
form_wrapper_->addStretch(1);
|
|
|
|
|
|
|
|
matrixid_input_ = new TextField(this);
|
|
|
|
matrixid_input_->setLabel(tr("Matrix ID"));
|
2020-12-01 01:30:33 +03:00
|
|
|
matrixid_input_->setRegexp(QRegularExpression("@.+?:.{3,}"));
|
2017-09-10 12:58:00 +03:00
|
|
|
matrixid_input_->setPlaceholderText(tr("e.g @joe:matrix.org"));
|
2020-05-09 14:06:47 +03:00
|
|
|
matrixid_input_->setToolTip(
|
|
|
|
tr("Your login name. A mxid should start with @ followed by the user id. After the user "
|
|
|
|
"id you need to include your server name after a :.\nYou can also put your homeserver "
|
|
|
|
"address there, if your server doesn't support .well-known lookup.\nExample: "
|
|
|
|
"@user:server.my\nIf Nheko fails to discover your homeserver, it will show you a "
|
|
|
|
"field to enter the server manually."));
|
2017-09-10 12:58:00 +03:00
|
|
|
|
|
|
|
spinner_ = new LoadingIndicator(this);
|
|
|
|
spinner_->setFixedHeight(40);
|
|
|
|
spinner_->setFixedWidth(40);
|
|
|
|
spinner_->hide();
|
|
|
|
|
|
|
|
errorIcon_ = new QLabel(this);
|
|
|
|
errorIcon_->setPixmap(QPixmap(":/icons/icons/error.png"));
|
|
|
|
errorIcon_->hide();
|
|
|
|
|
|
|
|
matrixidLayout_ = new QHBoxLayout();
|
|
|
|
matrixidLayout_->addWidget(matrixid_input_, 0, Qt::AlignVCenter);
|
|
|
|
|
2020-11-23 09:44:02 +03:00
|
|
|
QFont font;
|
|
|
|
|
|
|
|
error_matrixid_label_ = new QLabel(this);
|
|
|
|
error_matrixid_label_->setFont(font);
|
2020-11-24 00:24:26 +03:00
|
|
|
error_matrixid_label_->setWordWrap(true);
|
2020-11-23 09:44:02 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
password_input_ = new TextField(this);
|
|
|
|
password_input_->setLabel(tr("Password"));
|
|
|
|
password_input_->setEchoMode(QLineEdit::Password);
|
2021-01-27 21:24:06 +03:00
|
|
|
password_input_->setToolTip(tr("Your password."));
|
2017-09-10 12:58:00 +03:00
|
|
|
|
2018-07-22 18:41:15 +03:00
|
|
|
deviceName_ = new TextField(this);
|
|
|
|
deviceName_->setLabel(tr("Device name"));
|
2020-05-09 14:06:47 +03:00
|
|
|
deviceName_->setToolTip(
|
|
|
|
tr("A name for this device, which will be shown to others, when verifying your devices. "
|
2020-05-25 14:03:49 +03:00
|
|
|
"If none is provided a default is used."));
|
2018-07-22 18:41:15 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
serverInput_ = new TextField(this);
|
2021-01-27 21:24:06 +03:00
|
|
|
serverInput_->setLabel(tr("Homeserver address"));
|
|
|
|
serverInput_->setPlaceholderText(tr("server.my:8787"));
|
2020-05-09 14:06:47 +03:00
|
|
|
serverInput_->setToolTip(tr("The address that can be used to contact you homeservers "
|
|
|
|
"client API.\nExample: https://server.my:8787"));
|
2017-09-10 12:58:00 +03:00
|
|
|
serverInput_->hide();
|
|
|
|
|
|
|
|
serverLayout_ = new QHBoxLayout();
|
|
|
|
serverLayout_->addWidget(serverInput_, 0, Qt::AlignVCenter);
|
|
|
|
|
|
|
|
form_layout_->addLayout(matrixidLayout_);
|
2020-11-24 02:16:03 +03:00
|
|
|
form_layout_->addWidget(error_matrixid_label_, 0, Qt::AlignHCenter);
|
2018-07-22 18:41:15 +03:00
|
|
|
form_layout_->addWidget(password_input_);
|
2020-06-06 00:34:00 +03:00
|
|
|
form_layout_->addWidget(deviceName_, Qt::AlignHCenter);
|
2017-09-10 12:58:00 +03:00
|
|
|
form_layout_->addLayout(serverLayout_);
|
2020-11-24 00:10:43 +03:00
|
|
|
|
|
|
|
error_matrixid_label_->hide();
|
2017-09-10 12:58:00 +03:00
|
|
|
|
|
|
|
button_layout_ = new QHBoxLayout();
|
2021-02-19 17:48:43 +03:00
|
|
|
button_layout_->setSpacing(20);
|
2017-09-10 12:58:00 +03:00
|
|
|
button_layout_->setContentsMargins(0, 0, 0, 30);
|
|
|
|
|
|
|
|
login_button_ = new RaisedButton(tr("LOGIN"), this);
|
2021-02-19 17:48:43 +03:00
|
|
|
login_button_->setMinimumSize(150, 65);
|
2017-09-10 12:58:00 +03:00
|
|
|
login_button_->setFontSize(20);
|
|
|
|
login_button_->setCornerRadius(3);
|
|
|
|
|
2021-02-19 17:48:43 +03:00
|
|
|
sso_login_button_ = new RaisedButton(tr("SSO LOGIN"), this);
|
|
|
|
sso_login_button_->setMinimumSize(150, 65);
|
|
|
|
sso_login_button_->setFontSize(20);
|
|
|
|
sso_login_button_->setCornerRadius(3);
|
|
|
|
sso_login_button_->setVisible(false);
|
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
button_layout_->addStretch(1);
|
|
|
|
button_layout_->addWidget(login_button_);
|
2021-02-19 17:48:43 +03:00
|
|
|
button_layout_->addWidget(sso_login_button_);
|
2017-09-10 12:58:00 +03:00
|
|
|
button_layout_->addStretch(1);
|
|
|
|
|
|
|
|
error_label_ = new QLabel(this);
|
|
|
|
error_label_->setFont(font);
|
2020-11-23 09:44:02 +03:00
|
|
|
error_label_->setWordWrap(true);
|
2017-09-10 12:58:00 +03:00
|
|
|
|
|
|
|
top_layout_->addLayout(top_bar_layout_);
|
|
|
|
top_layout_->addStretch(1);
|
|
|
|
top_layout_->addLayout(logo_layout_);
|
|
|
|
top_layout_->addLayout(form_wrapper_);
|
|
|
|
top_layout_->addStretch(1);
|
|
|
|
top_layout_->addLayout(button_layout_);
|
2020-11-25 01:42:35 +03:00
|
|
|
top_layout_->addWidget(error_label_, 0, Qt::AlignHCenter);
|
2017-09-10 12:58:00 +03:00
|
|
|
top_layout_->addStretch(1);
|
|
|
|
|
|
|
|
setLayout(top_layout_);
|
|
|
|
|
2021-01-22 08:16:31 +03:00
|
|
|
connect(this, &LoginPage::versionOkCb, this, &LoginPage::versionOk, Qt::QueuedConnection);
|
|
|
|
connect(
|
|
|
|
this, &LoginPage::versionErrorCb, this, &LoginPage::versionError, Qt::QueuedConnection);
|
2018-06-09 16:03:14 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
connect(back_button_, SIGNAL(clicked()), this, SLOT(onBackButtonClicked()));
|
2021-02-19 17:48:43 +03:00
|
|
|
connect(login_button_, &RaisedButton::clicked, this, [this]() {
|
|
|
|
onLoginButtonClicked(passwordSupported ? LoginMethod::Password : LoginMethod::SSO);
|
|
|
|
});
|
|
|
|
connect(sso_login_button_, &RaisedButton::clicked, this, [this]() {
|
|
|
|
onLoginButtonClicked(LoginMethod::SSO);
|
|
|
|
});
|
2021-02-25 15:40:06 +03:00
|
|
|
connect(this,
|
|
|
|
&LoginPage::showErrorMessage,
|
|
|
|
this,
|
|
|
|
static_cast<void (LoginPage::*)(QLabel *, const QString &)>(&LoginPage::showError),
|
|
|
|
Qt::QueuedConnection);
|
2017-09-10 12:58:00 +03:00
|
|
|
connect(matrixid_input_, SIGNAL(returnPressed()), login_button_, SLOT(click()));
|
|
|
|
connect(password_input_, SIGNAL(returnPressed()), login_button_, SLOT(click()));
|
2018-07-22 18:41:15 +03:00
|
|
|
connect(deviceName_, SIGNAL(returnPressed()), login_button_, SLOT(click()));
|
2017-09-10 12:58:00 +03:00
|
|
|
connect(serverInput_, SIGNAL(returnPressed()), login_button_, SLOT(click()));
|
|
|
|
connect(matrixid_input_, SIGNAL(editingFinished()), this, SLOT(onMatrixIdEntered()));
|
|
|
|
connect(serverInput_, SIGNAL(editingFinished()), this, SLOT(onServerAddressEntered()));
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
2020-05-25 14:03:49 +03:00
|
|
|
void
|
2020-12-01 01:30:33 +03:00
|
|
|
LoginPage::showError(const QString &msg)
|
2020-05-25 14:03:49 +03:00
|
|
|
{
|
2020-11-25 02:03:25 +03:00
|
|
|
auto rect = QFontMetrics(font()).boundingRect(msg);
|
|
|
|
int width = rect.width();
|
|
|
|
int height = rect.height();
|
2020-12-01 01:30:33 +03:00
|
|
|
error_label_->setFixedHeight((int)qCeil(width / 200.0) * height);
|
2020-05-25 14:03:49 +03:00
|
|
|
error_label_->setText(msg);
|
|
|
|
}
|
|
|
|
|
2020-11-23 09:44:02 +03:00
|
|
|
void
|
2020-12-01 01:30:33 +03:00
|
|
|
LoginPage::showError(QLabel *label, const QString &msg)
|
2020-11-23 03:18:11 +03:00
|
|
|
{
|
2020-12-01 01:30:33 +03:00
|
|
|
auto rect = QFontMetrics(font()).boundingRect(msg);
|
|
|
|
int width = rect.width();
|
|
|
|
int height = rect.height();
|
|
|
|
label->setFixedHeight((int)qCeil(width / 200.0) * height);
|
|
|
|
label->setText(msg);
|
2020-11-23 03:18:11 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2018-03-01 10:31:08 +03:00
|
|
|
LoginPage::onMatrixIdEntered()
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2018-03-01 10:31:08 +03:00
|
|
|
error_label_->setText("");
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2018-03-01 10:31:08 +03:00
|
|
|
User user;
|
2017-08-21 20:55:35 +03:00
|
|
|
|
2020-12-01 01:30:33 +03:00
|
|
|
if (!matrixid_input_->isValid()) {
|
|
|
|
error_matrixid_label_->show();
|
|
|
|
showError(error_matrixid_label_,
|
2021-01-27 21:24:06 +03:00
|
|
|
tr("You have entered an invalid Matrix ID e.g @joe:matrix.org"));
|
2020-11-23 09:44:30 +03:00
|
|
|
return;
|
2020-11-23 09:44:02 +03:00
|
|
|
} else {
|
|
|
|
error_matrixid_label_->setText("");
|
2020-11-23 23:33:53 +03:00
|
|
|
error_matrixid_label_->hide();
|
2020-11-23 03:18:11 +03:00
|
|
|
}
|
|
|
|
|
2018-02-28 22:14:41 +03:00
|
|
|
try {
|
2018-03-01 10:31:08 +03:00
|
|
|
user = parse<User>(matrixid_input_->text().toStdString());
|
2020-12-25 03:08:06 +03:00
|
|
|
} catch (const std::exception &) {
|
2020-12-01 01:30:33 +03:00
|
|
|
showError(error_matrixid_label_,
|
2021-01-27 21:24:06 +03:00
|
|
|
tr("You have entered an invalid Matrix ID e.g @joe:matrix.org"));
|
2020-11-25 01:42:35 +03:00
|
|
|
return;
|
2018-02-28 22:14:41 +03:00
|
|
|
}
|
|
|
|
|
2018-03-01 10:31:08 +03:00
|
|
|
QString homeServer = QString::fromStdString(user.hostname());
|
2017-09-10 12:58:00 +03:00
|
|
|
if (homeServer != inferredServerAddress_) {
|
|
|
|
serverInput_->hide();
|
|
|
|
serverLayout_->removeWidget(errorIcon_);
|
|
|
|
errorIcon_->hide();
|
|
|
|
if (serverInput_->isVisible()) {
|
|
|
|
matrixidLayout_->removeWidget(spinner_);
|
|
|
|
serverLayout_->addWidget(spinner_, 0, Qt::AlignVCenter | Qt::AlignRight);
|
|
|
|
spinner_->start();
|
|
|
|
} else {
|
|
|
|
serverLayout_->removeWidget(spinner_);
|
|
|
|
matrixidLayout_->addWidget(spinner_, 0, Qt::AlignVCenter | Qt::AlignRight);
|
|
|
|
spinner_->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
inferredServerAddress_ = homeServer;
|
|
|
|
serverInput_->setText(homeServer);
|
2018-06-09 16:03:14 +03:00
|
|
|
|
2018-07-15 19:09:08 +03:00
|
|
|
http::client()->set_server(user.hostname());
|
2021-03-08 15:44:21 +03:00
|
|
|
http::client()->verify_certificates(
|
|
|
|
!UserSettings::instance()->disableCertificateValidation());
|
2021-03-06 22:52:08 +03:00
|
|
|
|
2019-06-27 16:42:58 +03:00
|
|
|
http::client()->well_known([this](const mtx::responses::WellKnown &res,
|
|
|
|
mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
|
|
|
using namespace boost::beast::http;
|
|
|
|
|
|
|
|
if (err->status_code == status::not_found) {
|
|
|
|
nhlog::net()->info("Autodiscovery: No .well-known.");
|
|
|
|
checkHomeserverVersion();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!err->parse_error.empty()) {
|
|
|
|
emit versionErrorCb(
|
|
|
|
tr("Autodiscovery failed. Received malformed response."));
|
|
|
|
nhlog::net()->error(
|
|
|
|
"Autodiscovery failed. Received malformed response.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit versionErrorCb(tr("Autodiscovery failed. Unknown error when "
|
|
|
|
"requesting .well-known."));
|
|
|
|
nhlog::net()->error("Autodiscovery failed. Unknown error when "
|
2020-05-10 00:31:00 +03:00
|
|
|
"requesting .well-known. {}",
|
2020-05-10 01:04:45 +03:00
|
|
|
err->error_code.message());
|
2019-06-27 16:42:58 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nhlog::net()->info("Autodiscovery: Discovered '" + res.homeserver.base_url +
|
|
|
|
"'");
|
|
|
|
http::client()->set_server(res.homeserver.base_url);
|
|
|
|
checkHomeserverVersion();
|
|
|
|
});
|
2017-09-10 12:58:00 +03:00
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2018-06-09 16:03:14 +03:00
|
|
|
void
|
|
|
|
LoginPage::checkHomeserverVersion()
|
|
|
|
{
|
2018-07-15 19:09:08 +03:00
|
|
|
http::client()->versions(
|
2018-06-09 16:03:14 +03:00
|
|
|
[this](const mtx::responses::Versions &, mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
|
|
|
using namespace boost::beast::http;
|
|
|
|
|
|
|
|
if (err->status_code == status::not_found) {
|
|
|
|
emit versionErrorCb(tr("The required endpoints were not found. "
|
|
|
|
"Possibly not a Matrix server."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!err->parse_error.empty()) {
|
|
|
|
emit versionErrorCb(tr("Received malformed response. Make sure "
|
|
|
|
"the homeserver domain is valid."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit versionErrorCb(tr(
|
|
|
|
"An unknown error occured. Make sure the homeserver domain is valid."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-10 00:31:00 +03:00
|
|
|
http::client()->get_login(
|
|
|
|
[this](mtx::responses::LoginFlows flows, mtx::http::RequestErr err) {
|
|
|
|
if (err || flows.flows.empty())
|
2021-02-19 17:48:43 +03:00
|
|
|
emit versionOkCb(true, false);
|
2020-05-10 00:31:00 +03:00
|
|
|
|
2021-02-19 17:48:43 +03:00
|
|
|
bool ssoSupported_ = false;
|
|
|
|
bool passwordSupported_ = false;
|
2021-01-01 06:14:34 +03:00
|
|
|
for (const auto &flow : flows.flows) {
|
|
|
|
if (flow.type == mtx::user_interactive::auth_types::sso) {
|
2021-02-19 17:48:43 +03:00
|
|
|
ssoSupported_ = true;
|
|
|
|
} else if (flow.type ==
|
|
|
|
mtx::user_interactive::auth_types::password) {
|
|
|
|
passwordSupported_ = true;
|
2021-01-01 06:14:34 +03:00
|
|
|
}
|
2020-12-31 18:09:42 +03:00
|
|
|
}
|
2021-02-19 17:48:43 +03:00
|
|
|
emit versionOkCb(passwordSupported_, ssoSupported_);
|
2020-05-10 00:31:00 +03:00
|
|
|
});
|
2018-06-09 16:03:14 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
LoginPage::onServerAddressEntered()
|
2017-05-09 13:25:43 +03:00
|
|
|
{
|
2017-09-10 12:58:00 +03:00
|
|
|
error_label_->setText("");
|
2021-03-08 15:44:21 +03:00
|
|
|
http::client()->verify_certificates(
|
|
|
|
!UserSettings::instance()->disableCertificateValidation());
|
2018-07-15 19:09:08 +03:00
|
|
|
http::client()->set_server(serverInput_->text().toStdString());
|
2018-06-09 16:03:14 +03:00
|
|
|
checkHomeserverVersion();
|
2017-09-10 12:58:00 +03:00
|
|
|
|
|
|
|
serverLayout_->removeWidget(errorIcon_);
|
|
|
|
errorIcon_->hide();
|
|
|
|
serverLayout_->addWidget(spinner_, 0, Qt::AlignVCenter | Qt::AlignRight);
|
|
|
|
spinner_->start();
|
2017-07-08 14:41:49 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2018-06-09 16:03:14 +03:00
|
|
|
LoginPage::versionError(const QString &error)
|
2017-07-08 14:41:49 +03:00
|
|
|
{
|
2020-12-01 01:30:33 +03:00
|
|
|
showError(error_label_, error);
|
2017-09-10 12:58:00 +03:00
|
|
|
serverInput_->show();
|
|
|
|
|
|
|
|
spinner_->stop();
|
|
|
|
serverLayout_->removeWidget(spinner_);
|
|
|
|
serverLayout_->addWidget(errorIcon_, 0, Qt::AlignVCenter | Qt::AlignRight);
|
|
|
|
errorIcon_->show();
|
|
|
|
matrixidLayout_->removeWidget(spinner_);
|
2017-07-08 14:41:49 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2021-02-19 17:48:43 +03:00
|
|
|
LoginPage::versionOk(bool passwordSupported_, bool ssoSupported_)
|
2017-07-08 14:41:49 +03:00
|
|
|
{
|
2021-02-19 17:48:43 +03:00
|
|
|
passwordSupported = passwordSupported_;
|
|
|
|
ssoSupported = ssoSupported_;
|
2020-05-10 00:31:00 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
serverLayout_->removeWidget(spinner_);
|
|
|
|
matrixidLayout_->removeWidget(spinner_);
|
|
|
|
spinner_->stop();
|
2017-07-08 14:41:49 +03:00
|
|
|
|
2021-02-19 17:48:43 +03:00
|
|
|
sso_login_button_->setVisible(ssoSupported);
|
|
|
|
login_button_->setVisible(passwordSupported);
|
2020-05-10 00:31:00 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
if (serverInput_->isVisible())
|
|
|
|
serverInput_->hide();
|
2017-05-09 13:25:43 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2021-02-19 17:48:43 +03:00
|
|
|
LoginPage::onLoginButtonClicked(LoginMethod loginMethod)
|
2017-05-09 13:25:43 +03:00
|
|
|
{
|
2017-09-10 12:58:00 +03:00
|
|
|
error_label_->setText("");
|
2018-03-01 10:31:08 +03:00
|
|
|
User user;
|
2020-11-23 09:44:02 +03:00
|
|
|
|
2020-12-01 01:30:33 +03:00
|
|
|
if (!matrixid_input_->isValid()) {
|
|
|
|
error_matrixid_label_->show();
|
|
|
|
showError(error_matrixid_label_,
|
2021-01-27 21:24:06 +03:00
|
|
|
tr("You have entered an invalid Matrix ID e.g @joe:matrix.org"));
|
2020-11-23 09:44:30 +03:00
|
|
|
return;
|
2020-11-23 09:44:02 +03:00
|
|
|
} else {
|
|
|
|
error_matrixid_label_->setText("");
|
2020-11-23 23:33:53 +03:00
|
|
|
error_matrixid_label_->hide();
|
2020-11-23 03:18:11 +03:00
|
|
|
}
|
2018-03-01 10:31:08 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
user = parse<User>(matrixid_input_->text().toStdString());
|
2020-12-25 03:08:06 +03:00
|
|
|
} catch (const std::exception &) {
|
2020-12-01 01:30:33 +03:00
|
|
|
showError(error_matrixid_label_,
|
2021-01-27 21:24:06 +03:00
|
|
|
tr("You have entered an invalid Matrix ID e.g @joe:matrix.org"));
|
2020-11-25 01:42:35 +03:00
|
|
|
return;
|
2017-09-10 12:58:00 +03:00
|
|
|
}
|
2018-03-01 10:31:08 +03:00
|
|
|
|
2020-05-10 00:31:00 +03:00
|
|
|
if (loginMethod == LoginMethod::Password) {
|
|
|
|
if (password_input_->text().isEmpty())
|
2020-12-01 01:30:33 +03:00
|
|
|
return showError(error_label_, tr("Empty password"));
|
2020-05-10 00:31:00 +03:00
|
|
|
|
|
|
|
http::client()->login(
|
|
|
|
user.localpart(),
|
|
|
|
password_input_->text().toStdString(),
|
|
|
|
deviceName_->text().trimmed().isEmpty() ? initialDeviceName()
|
|
|
|
: deviceName_->text().toStdString(),
|
|
|
|
[this](const mtx::responses::Login &res, mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
2021-02-25 15:40:06 +03:00
|
|
|
showErrorMessage(error_label_,
|
|
|
|
QString::fromStdString(err->matrix_error.error));
|
2020-05-10 00:31:00 +03:00
|
|
|
emit errorOccurred();
|
|
|
|
return;
|
|
|
|
}
|
2018-03-01 10:31:08 +03:00
|
|
|
|
2020-05-10 00:31:00 +03:00
|
|
|
if (res.well_known) {
|
|
|
|
http::client()->set_server(res.well_known->homeserver.base_url);
|
|
|
|
nhlog::net()->info("Login requested to user server: " +
|
|
|
|
res.well_known->homeserver.base_url);
|
|
|
|
}
|
2018-07-22 18:41:15 +03:00
|
|
|
|
2020-05-10 00:31:00 +03:00
|
|
|
emit loginOk(res);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
auto sso = new SSOHandler();
|
|
|
|
connect(sso, &SSOHandler::ssoSuccess, this, [this, sso](std::string token) {
|
|
|
|
mtx::requests::Login req{};
|
2020-05-10 02:00:20 +03:00
|
|
|
req.token = token;
|
|
|
|
req.type = mtx::user_interactive::auth_types::token;
|
2020-05-10 00:31:00 +03:00
|
|
|
req.device_id = deviceName_->text().trimmed().isEmpty()
|
|
|
|
? initialDeviceName()
|
|
|
|
: deviceName_->text().toStdString();
|
|
|
|
http::client()->login(
|
|
|
|
req, [this](const mtx::responses::Login &res, mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
2021-02-25 15:40:06 +03:00
|
|
|
showErrorMessage(
|
2020-12-01 01:30:33 +03:00
|
|
|
error_label_,
|
2020-05-10 00:31:00 +03:00
|
|
|
QString::fromStdString(err->matrix_error.error));
|
|
|
|
emit errorOccurred();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res.well_known) {
|
|
|
|
http::client()->set_server(
|
|
|
|
res.well_known->homeserver.base_url);
|
|
|
|
nhlog::net()->info("Login requested to user server: " +
|
|
|
|
res.well_known->homeserver.base_url);
|
|
|
|
}
|
|
|
|
|
|
|
|
emit loginOk(res);
|
|
|
|
});
|
|
|
|
sso->deleteLater();
|
|
|
|
});
|
|
|
|
connect(sso, &SSOHandler::ssoFailed, this, [this, sso]() {
|
2021-02-25 15:40:06 +03:00
|
|
|
showErrorMessage(error_label_, tr("SSO login failed"));
|
2020-05-10 00:31:00 +03:00
|
|
|
emit errorOccurred();
|
|
|
|
sso->deleteLater();
|
|
|
|
});
|
2019-06-27 16:55:04 +03:00
|
|
|
|
2020-05-10 00:31:00 +03:00
|
|
|
QDesktopServices::openUrl(
|
|
|
|
QString::fromStdString(http::client()->login_sso_redirect(sso->url())));
|
|
|
|
}
|
2018-03-01 10:31:08 +03:00
|
|
|
|
|
|
|
emit loggingIn();
|
2017-05-09 13:25:43 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
LoginPage::reset()
|
2017-04-09 02:32:48 +03:00
|
|
|
{
|
2017-09-10 12:58:00 +03:00
|
|
|
matrixid_input_->clear();
|
|
|
|
password_input_->clear();
|
2020-05-10 00:31:00 +03:00
|
|
|
password_input_->show();
|
2017-09-10 12:58:00 +03:00
|
|
|
serverInput_->clear();
|
2017-05-09 13:25:43 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
spinner_->stop();
|
|
|
|
errorIcon_->hide();
|
|
|
|
serverLayout_->removeWidget(spinner_);
|
|
|
|
serverLayout_->removeWidget(errorIcon_);
|
|
|
|
matrixidLayout_->removeWidget(spinner_);
|
2017-05-09 13:25:43 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
inferredServerAddress_.clear();
|
2017-04-09 02:32:48 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
LoginPage::onBackButtonClicked()
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-09-10 12:58:00 +03:00
|
|
|
emit backButtonClicked();
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-11-22 20:52:38 +03:00
|
|
|
void
|
|
|
|
LoginPage::paintEvent(QPaintEvent *)
|
|
|
|
{
|
|
|
|
QStyleOption opt;
|
|
|
|
opt.init(this);
|
|
|
|
QPainter p(this);
|
|
|
|
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
|
|
|
|
}
|