From b56a1be0bd3085680fd572bb31f17007bce53494 Mon Sep 17 00:00:00 2001 From: Callum Brown Date: Wed, 11 Aug 2021 15:07:06 +0100 Subject: [PATCH 1/4] Support token authenticated registration Using a dialog after username and password have been provided. --- CMakeLists.txt | 2 + src/RegisterPage.cpp | 18 +++++++++ src/dialogs/TokenRegistration.cpp | 62 +++++++++++++++++++++++++++++++ src/dialogs/TokenRegistration.h | 31 ++++++++++++++++ 4 files changed, 113 insertions(+) create mode 100644 src/dialogs/TokenRegistration.cpp create mode 100644 src/dialogs/TokenRegistration.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 049ed8a3..a146931e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -286,6 +286,7 @@ set(SRC_FILES src/dialogs/Logout.cpp src/dialogs/PreviewUploadOverlay.cpp src/dialogs/ReCaptcha.cpp + src/dialogs/TokenRegistration.cpp # Emoji src/emoji/EmojiModel.cpp @@ -497,6 +498,7 @@ qt5_wrap_cpp(MOC_HEADERS src/dialogs/Logout.h src/dialogs/PreviewUploadOverlay.h src/dialogs/ReCaptcha.h + src/dialogs/TokenRegistration.h # Emoji src/emoji/EmojiModel.h diff --git a/src/RegisterPage.cpp b/src/RegisterPage.cpp index bae24df0..1eac85fd 100644 --- a/src/RegisterPage.cpp +++ b/src/RegisterPage.cpp @@ -25,6 +25,7 @@ #include "dialogs/FallbackAuth.h" #include "dialogs/ReCaptcha.h" +#include "dialogs/TokenRegistration.h" Q_DECLARE_METATYPE(mtx::user_interactive::Unauthorized) Q_DECLARE_METATYPE(mtx::user_interactive::Auth) @@ -481,6 +482,23 @@ RegisterPage::doUIA(const mtx::user_interactive::Unauthorized &unauthorized) doRegistrationWithAuth( mtx::user_interactive::Auth{session, mtx::user_interactive::auth::Dummy{}}); + } else if (current_stage == mtx::user_interactive::auth_types::registration_token) { + auto dialog = new dialogs::TokenRegistration(this); + + connect(dialog, + &dialogs::TokenRegistration::confirmation, + this, + [this, session, dialog](std::string token) { + dialog->close(); + dialog->deleteLater(); + emit registrationWithAuth(mtx::user_interactive::Auth{ + session, mtx::user_interactive::auth::RegistrationToken{token}}); + }); + + connect( + dialog, &dialogs::TokenRegistration::cancel, this, &RegisterPage::errorOccurred); + + dialog->show(); } else { // use fallback auto dialog = new dialogs::FallbackAuth( diff --git a/src/dialogs/TokenRegistration.cpp b/src/dialogs/TokenRegistration.cpp new file mode 100644 index 00000000..2333dcb1 --- /dev/null +++ b/src/dialogs/TokenRegistration.cpp @@ -0,0 +1,62 @@ +// SPDX-FileCopyrightText: 2021 Nheko Contributors +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#include +#include +#include + +#include "dialogs/TokenRegistration.h" + +#include "Config.h" +#include "MatrixClient.h" +#include "ui/TextField.h" + +using namespace dialogs; + +TokenRegistration::TokenRegistration(QWidget *parent) + : QWidget(parent) +{ + setAutoFillBackground(true); + setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint); + setWindowModality(Qt::WindowModal); + setAttribute(Qt::WA_DeleteOnClose, true); + + auto layout = new QVBoxLayout(this); + layout->setSpacing(conf::modals::WIDGET_SPACING); + layout->setMargin(conf::modals::WIDGET_MARGIN); + + auto buttonLayout = new QHBoxLayout(); + buttonLayout->setSpacing(8); + buttonLayout->setMargin(0); + + cancelBtn_ = new QPushButton(tr("Cancel"), this); + confirmBtn_ = new QPushButton(tr("Confirm"), this); + confirmBtn_->setDefault(true); + + buttonLayout->addStretch(1); + buttonLayout->addWidget(cancelBtn_); + buttonLayout->addWidget(confirmBtn_); + + tokenInput_ = new TextField(this); + tokenInput_->setLabel(tr("Registration token")); + + QFont font; + font.setPointSizeF(font.pointSizeF() * conf::modals::LABEL_MEDIUM_SIZE_RATIO); + + auto label = new QLabel(tr("Please enter a valid registration token."), this); + label->setFont(font); + + layout->addWidget(label); + layout->addWidget(tokenInput_); + layout->addLayout(buttonLayout); + + connect(confirmBtn_, &QPushButton::clicked, this, [this]() { + emit confirmation(tokenInput_->text().toStdString()); + emit close(); + }); + connect(cancelBtn_, &QPushButton::clicked, this, [this]() { + emit cancel(); + emit close(); + }); +} diff --git a/src/dialogs/TokenRegistration.h b/src/dialogs/TokenRegistration.h new file mode 100644 index 00000000..562dcb7b --- /dev/null +++ b/src/dialogs/TokenRegistration.h @@ -0,0 +1,31 @@ +// SPDX-FileCopyrightText: 2021 Nheko Contributors +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#pragma once + +#include + +class QPushButton; +class QLabel; +class TextField; + +namespace dialogs { + +class TokenRegistration : public QWidget +{ + Q_OBJECT + +public: + TokenRegistration(QWidget *parent = nullptr); + +signals: + void confirmation(std::string token); + void cancel(); + +private: + QPushButton *confirmBtn_; + QPushButton *cancelBtn_; + TextField *tokenInput_; +}; +} // dialogs From 7d484a8228886f942f62ca9cf23920b7fab2c602 Mon Sep 17 00:00:00 2001 From: Callum Brown Date: Thu, 12 Aug 2021 16:41:29 +0100 Subject: [PATCH 2/4] Use QInputDialog to get registration token --- CMakeLists.txt | 2 - src/RegisterPage.cpp | 22 +++-------- src/dialogs/TokenRegistration.cpp | 62 ------------------------------- src/dialogs/TokenRegistration.h | 31 ---------------- 4 files changed, 5 insertions(+), 112 deletions(-) delete mode 100644 src/dialogs/TokenRegistration.cpp delete mode 100644 src/dialogs/TokenRegistration.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a146931e..049ed8a3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -286,7 +286,6 @@ set(SRC_FILES src/dialogs/Logout.cpp src/dialogs/PreviewUploadOverlay.cpp src/dialogs/ReCaptcha.cpp - src/dialogs/TokenRegistration.cpp # Emoji src/emoji/EmojiModel.cpp @@ -498,7 +497,6 @@ qt5_wrap_cpp(MOC_HEADERS src/dialogs/Logout.h src/dialogs/PreviewUploadOverlay.h src/dialogs/ReCaptcha.h - src/dialogs/TokenRegistration.h # Emoji src/emoji/EmojiModel.h diff --git a/src/RegisterPage.cpp b/src/RegisterPage.cpp index 1eac85fd..acee6f78 100644 --- a/src/RegisterPage.cpp +++ b/src/RegisterPage.cpp @@ -3,6 +3,7 @@ // // SPDX-License-Identifier: GPL-3.0-or-later +#include #include #include #include @@ -25,7 +26,6 @@ #include "dialogs/FallbackAuth.h" #include "dialogs/ReCaptcha.h" -#include "dialogs/TokenRegistration.h" Q_DECLARE_METATYPE(mtx::user_interactive::Unauthorized) Q_DECLARE_METATYPE(mtx::user_interactive::Auth) @@ -483,22 +483,10 @@ RegisterPage::doUIA(const mtx::user_interactive::Unauthorized &unauthorized) mtx::user_interactive::Auth{session, mtx::user_interactive::auth::Dummy{}}); } else if (current_stage == mtx::user_interactive::auth_types::registration_token) { - auto dialog = new dialogs::TokenRegistration(this); - - connect(dialog, - &dialogs::TokenRegistration::confirmation, - this, - [this, session, dialog](std::string token) { - dialog->close(); - dialog->deleteLater(); - emit registrationWithAuth(mtx::user_interactive::Auth{ - session, mtx::user_interactive::auth::RegistrationToken{token}}); - }); - - connect( - dialog, &dialogs::TokenRegistration::cancel, this, &RegisterPage::errorOccurred); - - dialog->show(); + QString token = QInputDialog::getText( + this, tr("Registration token"), tr("Please enter a valid registration token.")); + emit registrationWithAuth(mtx::user_interactive::Auth{ + session, mtx::user_interactive::auth::RegistrationToken{token.toStdString()}}); } else { // use fallback auto dialog = new dialogs::FallbackAuth( diff --git a/src/dialogs/TokenRegistration.cpp b/src/dialogs/TokenRegistration.cpp deleted file mode 100644 index 2333dcb1..00000000 --- a/src/dialogs/TokenRegistration.cpp +++ /dev/null @@ -1,62 +0,0 @@ -// SPDX-FileCopyrightText: 2021 Nheko Contributors -// -// SPDX-License-Identifier: GPL-3.0-or-later - -#include -#include -#include - -#include "dialogs/TokenRegistration.h" - -#include "Config.h" -#include "MatrixClient.h" -#include "ui/TextField.h" - -using namespace dialogs; - -TokenRegistration::TokenRegistration(QWidget *parent) - : QWidget(parent) -{ - setAutoFillBackground(true); - setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint); - setWindowModality(Qt::WindowModal); - setAttribute(Qt::WA_DeleteOnClose, true); - - auto layout = new QVBoxLayout(this); - layout->setSpacing(conf::modals::WIDGET_SPACING); - layout->setMargin(conf::modals::WIDGET_MARGIN); - - auto buttonLayout = new QHBoxLayout(); - buttonLayout->setSpacing(8); - buttonLayout->setMargin(0); - - cancelBtn_ = new QPushButton(tr("Cancel"), this); - confirmBtn_ = new QPushButton(tr("Confirm"), this); - confirmBtn_->setDefault(true); - - buttonLayout->addStretch(1); - buttonLayout->addWidget(cancelBtn_); - buttonLayout->addWidget(confirmBtn_); - - tokenInput_ = new TextField(this); - tokenInput_->setLabel(tr("Registration token")); - - QFont font; - font.setPointSizeF(font.pointSizeF() * conf::modals::LABEL_MEDIUM_SIZE_RATIO); - - auto label = new QLabel(tr("Please enter a valid registration token."), this); - label->setFont(font); - - layout->addWidget(label); - layout->addWidget(tokenInput_); - layout->addLayout(buttonLayout); - - connect(confirmBtn_, &QPushButton::clicked, this, [this]() { - emit confirmation(tokenInput_->text().toStdString()); - emit close(); - }); - connect(cancelBtn_, &QPushButton::clicked, this, [this]() { - emit cancel(); - emit close(); - }); -} diff --git a/src/dialogs/TokenRegistration.h b/src/dialogs/TokenRegistration.h deleted file mode 100644 index 562dcb7b..00000000 --- a/src/dialogs/TokenRegistration.h +++ /dev/null @@ -1,31 +0,0 @@ -// SPDX-FileCopyrightText: 2021 Nheko Contributors -// -// SPDX-License-Identifier: GPL-3.0-or-later - -#pragma once - -#include - -class QPushButton; -class QLabel; -class TextField; - -namespace dialogs { - -class TokenRegistration : public QWidget -{ - Q_OBJECT - -public: - TokenRegistration(QWidget *parent = nullptr); - -signals: - void confirmation(std::string token); - void cancel(); - -private: - QPushButton *confirmBtn_; - QPushButton *cancelBtn_; - TextField *tokenInput_; -}; -} // dialogs From 4fa644f2b7d0e95f8f917dc6d4d030153c0da73e Mon Sep 17 00:00:00 2001 From: Callum Brown Date: Thu, 12 Aug 2021 17:57:07 +0100 Subject: [PATCH 3/4] Fix cancel button on registration token dialog --- src/RegisterPage.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/RegisterPage.cpp b/src/RegisterPage.cpp index acee6f78..ddd4d47d 100644 --- a/src/RegisterPage.cpp +++ b/src/RegisterPage.cpp @@ -483,10 +483,21 @@ RegisterPage::doUIA(const mtx::user_interactive::Unauthorized &unauthorized) mtx::user_interactive::Auth{session, mtx::user_interactive::auth::Dummy{}}); } else if (current_stage == mtx::user_interactive::auth_types::registration_token) { - QString token = QInputDialog::getText( - this, tr("Registration token"), tr("Please enter a valid registration token.")); - emit registrationWithAuth(mtx::user_interactive::Auth{ - session, mtx::user_interactive::auth::RegistrationToken{token.toStdString()}}); + bool ok; + QString token = + QInputDialog::getText(this, + tr("Registration token"), + tr("Please enter a valid registration token."), + QLineEdit::Normal, + QString(), + &ok); + + if (ok) { + emit registrationWithAuth(mtx::user_interactive::Auth{ + session, mtx::user_interactive::auth::RegistrationToken{token.toStdString()}}); + } else { + emit errorOccurred(); + } } else { // use fallback auto dialog = new dialogs::FallbackAuth( From 0da58c476c05c3ec52fe69609b741d06938c9521 Mon Sep 17 00:00:00 2001 From: Callum Brown Date: Sun, 15 Aug 2021 15:51:10 +0100 Subject: [PATCH 4/4] Run linter --- src/RegisterPage.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/RegisterPage.cpp b/src/RegisterPage.cpp index ddd4d47d..fb6a1b97 100644 --- a/src/RegisterPage.cpp +++ b/src/RegisterPage.cpp @@ -492,12 +492,13 @@ RegisterPage::doUIA(const mtx::user_interactive::Unauthorized &unauthorized) QString(), &ok); - if (ok) { - emit registrationWithAuth(mtx::user_interactive::Auth{ - session, mtx::user_interactive::auth::RegistrationToken{token.toStdString()}}); - } else { - emit errorOccurred(); - } + if (ok) { + emit registrationWithAuth(mtx::user_interactive::Auth{ + session, + mtx::user_interactive::auth::RegistrationToken{token.toStdString()}}); + } else { + emit errorOccurred(); + } } else { // use fallback auto dialog = new dialogs::FallbackAuth(