From dc0e633e4668312a2b81f8eaea8caa0e512c31fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernhard=20Rosenkr=C3=A4nzer?= Date: Tue, 3 Sep 2024 21:52:55 +0200 Subject: [PATCH] Validate domain names, allow saving login data --- Configurator.cpp | 2 +- Configurator.h | 3 +++ dialogs/FederatedLogin.cpp | 20 +++++++++++++++++--- dialogs/FederatedLogin.h | 4 ++++ lib/CMakeLists.txt | 2 +- lib/DomainValidator.cpp | 4 ++++ lib/DomainValidator.h | 10 ++++++++++ 7 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 lib/DomainValidator.cpp create mode 100644 lib/DomainValidator.h diff --git a/Configurator.cpp b/Configurator.cpp index 05d6716..b650cba 100644 --- a/Configurator.cpp +++ b/Configurator.cpp @@ -4,7 +4,7 @@ #include #include -Configurator::Configurator(int &argc, char **&argv):QApplication(argc, argv),_nam(this) { +Configurator::Configurator(int &argc, char **&argv):QApplication(argc, argv),_nam(this),_settings("federated.computer", "configurator", this) { setQuitOnLastWindowClosed(false); _loginDialog = new FederatedLogin(0); _loginDialog->setModal(true); diff --git a/Configurator.h b/Configurator.h index a8fd769..9cdfe9e 100644 --- a/Configurator.h +++ b/Configurator.h @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -18,10 +19,12 @@ public: QByteArray download(QUrl const &url); void waitForDownload(QNetworkReply *r) const; Services const &services() const { return _services; } + QSettings &settings() { return _settings; } protected Q_SLOTS: void loginRequested(); private: QNetworkAccessManager _nam; + QSettings _settings; FederatedLogin * _loginDialog; Services _services; }; diff --git a/dialogs/FederatedLogin.cpp b/dialogs/FederatedLogin.cpp index 17f834d..db353f8 100644 --- a/dialogs/FederatedLogin.cpp +++ b/dialogs/FederatedLogin.cpp @@ -1,4 +1,6 @@ #include "FederatedLogin.h" +#include "DomainValidator.h" +#include FederatedLogin::FederatedLogin(QWidget *parent):QDialog(parent),_layout(this) { int y = 0; @@ -7,20 +9,23 @@ FederatedLogin::FederatedLogin(QWidget *parent):QDialog(parent),_layout(this) { _layout.addWidget(_message, y, 0, 1, 2); _domainLbl = new QLabel(tr("&Domain:"), this); _layout.addWidget(_domainLbl, ++y, 0); - _domain = new QLineEdit("f11890a1.fedcom.net", this); + _domain = new QLineEdit(app->settings().value("domain").toString(), this); + _domain->setValidator(new DomainValidator(_domain)); _domainLbl->setBuddy(_domain); _layout.addWidget(_domain, y, 1); _userLbl = new QLabel(tr("&User/Email:"), this); _layout.addWidget(_userLbl, ++y, 0); - _user = new QLineEdit(this); + _user = new QLineEdit(app->settings().value("user").toString(), this); _userLbl->setBuddy(_user); _layout.addWidget(_user, y, 1); _passwordLbl = new QLabel(tr("&Password:"), this); _layout.addWidget(_passwordLbl, ++y, 0); - _password = new QLineEdit(this); + _password = new QLineEdit(app->settings().value("password").toString(), this); _password->setEchoMode(QLineEdit::Password); _passwordLbl->setBuddy(_password); _layout.addWidget(_password, y, 1); + _storeLogin = new QCheckBox(tr("&Store authentication information for next login"), this); + _layout.addWidget(_storeLogin, ++y, 0, 1, 2); _buttons = new ButtonRow(this); _layout.addWidget(_buttons, ++y, 0, 1, 2); _ok = new QPushButton(tr("&OK"), _buttons); @@ -28,6 +33,7 @@ FederatedLogin::FederatedLogin(QWidget *parent):QDialog(parent),_layout(this) { _buttons->add(_ok); _cancel = new QPushButton(tr("&Cancel"), _buttons); _buttons->add(_cancel); + connect(_ok, &QPushButton::clicked, this, &FederatedLogin::storeLogin); connect(_ok, &QPushButton::clicked, this, &QDialog::accept); connect(_cancel, &QPushButton::clicked, this, &QDialog::reject); } @@ -36,3 +42,11 @@ void FederatedLogin::setMessage(QString const &m) { _message->setText(m); _message->setHidden(m.isEmpty()); } + +void FederatedLogin::storeLogin() { + if(_storeLogin->isChecked()) { + app->settings().setValue("domain", _domain->text()); + app->settings().setValue("user", _user->text()); + app->settings().setValue("password", _password->text()); + } +} diff --git a/dialogs/FederatedLogin.h b/dialogs/FederatedLogin.h index 53864ee..a31a2d3 100644 --- a/dialogs/FederatedLogin.h +++ b/dialogs/FederatedLogin.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "../lib/ButtonRow.h" @@ -16,6 +17,8 @@ public: QString user() const { return _user->text(); } QString password() const { return _password->text(); } void setMessage(QString const &m); +protected Q_SLOTS: + void storeLogin(); protected: QGridLayout _layout; QLabel * _message; @@ -25,6 +28,7 @@ protected: QLineEdit * _user; QLabel * _passwordLbl; QLineEdit * _password; + QCheckBox * _storeLogin; ButtonRow * _buttons; QPushButton * _ok; QPushButton * _cancel; diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index eb2da33..63fee78 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,4 +1,4 @@ include_directories(${CMAKE_SOURCE_DIR}) include_directories(${CMAKE_CURRENT_SOURCE_DIR}) -add_library(fed STATIC Service.cpp ButtonRow.cpp ServiceWidget.cpp) +add_library(fed STATIC Service.cpp ButtonRow.cpp ServiceWidget.cpp DomainValidator.cpp) target_link_libraries(fed Qt${QT_VERSION}::Core Qt${QT_VERSION}::Gui Qt${QT_VERSION}::Widgets Qt${QT_VERSION}::Network) diff --git a/lib/DomainValidator.cpp b/lib/DomainValidator.cpp new file mode 100644 index 0000000..462eb5b --- /dev/null +++ b/lib/DomainValidator.cpp @@ -0,0 +1,4 @@ +#include "DomainValidator.h" + +DomainValidator::DomainValidator(QObject * parent):QRegularExpressionValidator(QRegularExpression("^((?!-)[A-Za-z0-9-]{1,63}(? +#include + +class DomainValidator:public QRegularExpressionValidator { + Q_OBJECT +public: + DomainValidator(QObject * parent = nullptr); +};