diff --git a/GPClient/CMakeLists.txt b/GPClient/CMakeLists.txt index cf1c751..578e1fd 100644 --- a/GPClient/CMakeLists.txt +++ b/GPClient/CMakeLists.txt @@ -23,7 +23,7 @@ add_executable(gpclient gphelper.cpp loginparams.cpp main.cpp - normalloginwindow.cpp + standardloginwindow.cpp portalauthenticator.cpp portalconfigresponse.cpp preloginresponse.cpp @@ -31,7 +31,7 @@ add_executable(gpclient gpclient.cpp settingsdialog.cpp gpclient.ui - normalloginwindow.ui + standardloginwindow.ui settingsdialog.ui challengedialog.h challengedialog.cpp diff --git a/GPClient/enhancedwebview.cpp b/GPClient/enhancedwebview.cpp index 8ac3ac1..8415609 100644 --- a/GPClient/enhancedwebview.cpp +++ b/GPClient/enhancedwebview.cpp @@ -8,18 +8,13 @@ EnhancedWebView::EnhancedWebView(QWidget *parent) : QWebEngineView(parent) , cdp(new CDPCommandManager) { - QObject::connect(cdp, &CDPCommandManager::ready, this, &EnhancedWebView::onCDPReady); - QObject::connect(cdp, &CDPCommandManager::eventReceived, this, &EnhancedWebView::onEventReceived); -} - -EnhancedWebView::~EnhancedWebView() -{ - delete cdp; + QObject::connect(cdp, &CDPCommandManager::ready, this, &EnhancedWebView::onCDPReady); + QObject::connect(cdp, &CDPCommandManager::eventReceived, this, &EnhancedWebView::onEventReceived); } void EnhancedWebView::initialize() { - QString port = QProcessEnvironment::systemEnvironment().value(ENV_CDP_PORT); + auto port = QProcessEnvironment::systemEnvironment().value(ENV_CDP_PORT); cdp->initialize("http://127.0.0.1:" + port + "/json"); } diff --git a/GPClient/enhancedwebview.h b/GPClient/enhancedwebview.h index 163768e..d402333 100644 --- a/GPClient/enhancedwebview.h +++ b/GPClient/enhancedwebview.h @@ -12,7 +12,6 @@ class EnhancedWebView : public QWebEngineView Q_OBJECT public: explicit EnhancedWebView(QWidget *parent = nullptr); - ~EnhancedWebView(); void initialize(); @@ -24,7 +23,7 @@ private slots: void onEventReceived(QString eventName, QJsonObject params); private: - CDPCommandManager *cdp; + CDPCommandManager *cdp { nullptr }; }; #endif // ENHANCEDWEBVIEW_H diff --git a/GPClient/gatewayauthenticator.cpp b/GPClient/gatewayauthenticator.cpp index b900b79..0d429a5 100644 --- a/GPClient/gatewayauthenticator.cpp +++ b/GPClient/gatewayauthenticator.cpp @@ -38,7 +38,7 @@ void GatewayAuthenticator::authenticate() void GatewayAuthenticator::login(const LoginParams &loginParams) { - LOGI << QString("Trying to login the gateway at %1, with %2").arg(loginUrl).arg(QString::fromUtf8(loginParams.toUtf8())); + LOGI << QString("Trying to login the gateway at %1, with %2").arg(loginUrl).arg(QString(loginParams.toUtf8())); auto *reply = createRequest(loginUrl, loginParams.toUtf8()); connect(reply, &QNetworkReply::finished, this, &GatewayAuthenticator::onLoginFinished); @@ -52,8 +52,8 @@ void GatewayAuthenticator::onLoginFinished() if (reply->error() || response.contains("Authentication failure")) { LOGE << QString("Failed to login the gateway at %1, %2").arg(loginUrl, reply->errorString()); - if (normalLoginWindow) { - normalLoginWindow->setProcessing(false); + if (standardLoginWindow) { + standardLoginWindow->setProcessing(false); openMessageBox("Gateway login failed.", "Please check your credentials and try again."); } else { doAuth(); @@ -68,8 +68,8 @@ void GatewayAuthenticator::onLoginFinished() return; } - if (normalLoginWindow) { - normalLoginWindow->close(); + if (standardLoginWindow) { + standardLoginWindow->close(); } const auto params = gpclient::helper::parseGatewayResponse(response); @@ -115,28 +115,24 @@ void GatewayAuthenticator::normalAuth(QString labelUsername, QString labelPasswo { LOGI << QString("Trying to perform the normal login with %1 / %2 credentials").arg(labelUsername, labelPassword); - normalLoginWindow = new NormalLoginWindow; - normalLoginWindow->setPortalAddress(gateway); - normalLoginWindow->setAuthMessage(authMessage); - normalLoginWindow->setUsernameLabel(labelUsername); - normalLoginWindow->setPasswordLabel(labelPassword); + standardLoginWindow = new StandardLoginWindow {gateway, labelUsername, labelPassword, authMessage}; // Do login - connect(normalLoginWindow, &NormalLoginWindow::performLogin, this, &GatewayAuthenticator::onPerformNormalLogin); - connect(normalLoginWindow, &NormalLoginWindow::rejected, this, &GatewayAuthenticator::onLoginWindowRejected); - connect(normalLoginWindow, &NormalLoginWindow::finished, this, &GatewayAuthenticator::onLoginWindowFinished); + connect(standardLoginWindow, &StandardLoginWindow::performLogin, this, &GatewayAuthenticator::onPerformStandardLogin); + connect(standardLoginWindow, &StandardLoginWindow::rejected, this, &GatewayAuthenticator::onLoginWindowRejected); + connect(standardLoginWindow, &StandardLoginWindow::finished, this, &GatewayAuthenticator::onLoginWindowFinished); - normalLoginWindow->show(); + standardLoginWindow->show(); } -void GatewayAuthenticator::onPerformNormalLogin(const QString &username, const QString &password) +void GatewayAuthenticator::onPerformStandardLogin(const QString &username, const QString &password) { LOGI << "Start to perform normal login..."; - normalLoginWindow->setProcessing(true); + standardLoginWindow->setProcessing(true); params.setUsername(username); params.setPassword(password); - + authenticate(); } @@ -147,15 +143,15 @@ void GatewayAuthenticator::onLoginWindowRejected() void GatewayAuthenticator::onLoginWindowFinished() { - delete normalLoginWindow; - normalLoginWindow = nullptr; + delete standardLoginWindow; + standardLoginWindow = nullptr; } void GatewayAuthenticator::samlAuth(QString samlMethod, QString samlRequest, QString preloginUrl) { LOGI << "Trying to perform SAML login with saml-method " << samlMethod; - SAMLLoginWindow *loginWindow = new SAMLLoginWindow; + auto *loginWindow = new SAMLLoginWindow; connect(loginWindow, &SAMLLoginWindow::success, [this, loginWindow](const QMap &samlResult) { this->onSAMLLoginSuccess(samlResult); @@ -215,8 +211,8 @@ void GatewayAuthenticator::showChallenge(const QString &responseText) }); connect(challengeDialog, &ChallengeDialog::rejected, this, [this] { - if (normalLoginWindow) { - normalLoginWindow->close(); + if (standardLoginWindow) { + standardLoginWindow->close(); } emit fail(); }); diff --git a/GPClient/gatewayauthenticator.h b/GPClient/gatewayauthenticator.h index 3494dff..e3fba4e 100644 --- a/GPClient/gatewayauthenticator.h +++ b/GPClient/gatewayauthenticator.h @@ -3,7 +3,7 @@ #include -#include "normalloginwindow.h" +#include "standardloginwindow.h" #include "challengedialog.h" #include "loginparams.h" #include "gatewayauthenticatorparams.h" @@ -17,13 +17,13 @@ public: void authenticate(); signals: - void success(const QString& authCookie); - void fail(const QString& msg = ""); + void success(const QString &authCookie); + void fail(const QString &msg = ""); private slots: void onLoginFinished(); void onPreloginFinished(); - void onPerformNormalLogin(const QString &username, const QString &password); + void onPerformStandardLogin(const QString &username, const QString &password); void onLoginWindowRejected(); void onLoginWindowFinished(); void onSAMLLoginSuccess(const QMap &samlResult); @@ -35,8 +35,8 @@ private: QString preloginUrl; QString loginUrl; - NormalLoginWindow *normalLoginWindow{ nullptr }; - ChallengeDialog *challengeDialog{ nullptr }; + StandardLoginWindow *standardLoginWindow { nullptr }; + ChallengeDialog *challengeDialog { nullptr }; void login(const LoginParams& loginParams); void doAuth(); diff --git a/GPClient/normalloginwindow.cpp b/GPClient/normalloginwindow.cpp deleted file mode 100644 index 1626a09..0000000 --- a/GPClient/normalloginwindow.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include - -#include "normalloginwindow.h" -#include "ui_normalloginwindow.h" - -NormalLoginWindow::NormalLoginWindow(QWidget *parent) : - QDialog(parent), - ui(new Ui::NormalLoginWindow) -{ - ui->setupUi(this); - setWindowTitle("GlobalProtect Login"); - setFixedSize(width(), height()); - setModal(true); -} - -NormalLoginWindow::~NormalLoginWindow() -{ - delete ui; -} - -void NormalLoginWindow::setAuthMessage(QString message) -{ - ui->authMessage->setText(message); -} - -void NormalLoginWindow::setUsernameLabel(QString label) -{ - ui->username->setPlaceholderText(label); -} - -void NormalLoginWindow::setPasswordLabel(QString label) -{ - ui->password->setPlaceholderText(label); -} - -void NormalLoginWindow::setPortalAddress(QString portal) -{ - ui->portalAddress->setText(portal); -} - -void NormalLoginWindow::setProcessing(bool isProcessing) -{ - ui->username->setReadOnly(isProcessing); - ui->password->setReadOnly(isProcessing); - ui->loginButton->setDisabled(isProcessing); -} - -void NormalLoginWindow::on_loginButton_clicked() -{ - const QString username = ui->username->text().trimmed(); - const QString password = ui->password->text().trimmed(); - - if (username.isEmpty() || password.isEmpty()) { - return; - } - - emit performLogin(username, password); -} - -void NormalLoginWindow::closeEvent(QCloseEvent *event) -{ - event->accept(); - reject(); -} diff --git a/GPClient/normalloginwindow.h b/GPClient/normalloginwindow.h deleted file mode 100644 index a2327bb..0000000 --- a/GPClient/normalloginwindow.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef PORTALAUTHWINDOW_H -#define PORTALAUTHWINDOW_H - -#include - -namespace Ui { -class NormalLoginWindow; -} - -class NormalLoginWindow : public QDialog -{ - Q_OBJECT - -public: - explicit NormalLoginWindow(QWidget *parent = nullptr); - ~NormalLoginWindow(); - - void setAuthMessage(QString); - void setUsernameLabel(QString); - void setPasswordLabel(QString); - void setPortalAddress(QString); - - void setProcessing(bool isProcessing); - -private slots: - void on_loginButton_clicked(); - -signals: - void performLogin(QString username, QString password); - -private: - Ui::NormalLoginWindow *ui; - - void closeEvent(QCloseEvent *event); -}; - -#endif // PORTALAUTHWINDOW_H diff --git a/GPClient/portalauthenticator.cpp b/GPClient/portalauthenticator.cpp index a1435ef..b87f4ae 100644 --- a/GPClient/portalauthenticator.cpp +++ b/GPClient/portalauthenticator.cpp @@ -3,7 +3,7 @@ #include "portalauthenticator.h" #include "gphelper.h" -#include "normalloginwindow.h" +#include "standardloginwindow.h" #include "samlloginwindow.h" #include "loginparams.h" #include "preloginresponse.h" @@ -25,7 +25,7 @@ PortalAuthenticator::PortalAuthenticator(const QString& portal, const QString& c PortalAuthenticator::~PortalAuthenticator() { - delete normalLoginWindow; + delete standardLoginWindow; } void PortalAuthenticator::authenticate() @@ -87,23 +87,19 @@ void PortalAuthenticator::normalAuth() { LOGI << "Trying to launch the normal login window..."; - normalLoginWindow = new NormalLoginWindow; - normalLoginWindow->setPortalAddress(portal); - normalLoginWindow->setAuthMessage(preloginResponse.authMessage()); - normalLoginWindow->setUsernameLabel(preloginResponse.labelUsername()); - normalLoginWindow->setPasswordLabel(preloginResponse.labelPassword()); + standardLoginWindow = new StandardLoginWindow {portal, preloginResponse.labelUsername(), preloginResponse.labelPassword(), preloginResponse.authMessage() }; // Do login - connect(normalLoginWindow, &NormalLoginWindow::performLogin, this, &PortalAuthenticator::onPerformNormalLogin); - connect(normalLoginWindow, &NormalLoginWindow::rejected, this, &PortalAuthenticator::onLoginWindowRejected); - connect(normalLoginWindow, &NormalLoginWindow::finished, this, &PortalAuthenticator::onLoginWindowFinished); + connect(standardLoginWindow, &StandardLoginWindow::performLogin, this, &PortalAuthenticator::onPerformNormalLogin); + connect(standardLoginWindow, &StandardLoginWindow::rejected, this, &PortalAuthenticator::onLoginWindowRejected); + connect(standardLoginWindow, &StandardLoginWindow::finished, this, &PortalAuthenticator::onLoginWindowFinished); - normalLoginWindow->show(); + standardLoginWindow->show(); } void PortalAuthenticator::onPerformNormalLogin(const QString &username, const QString &password) { - normalLoginWindow->setProcessing(true); + standardLoginWindow->setProcessing(true); fetchConfig(username, password); } @@ -114,15 +110,15 @@ void PortalAuthenticator::onLoginWindowRejected() void PortalAuthenticator::onLoginWindowFinished() { - delete normalLoginWindow; - normalLoginWindow = nullptr; + delete standardLoginWindow; + standardLoginWindow = nullptr; } void PortalAuthenticator::samlAuth() { LOGI << "Trying to perform SAML login with saml-method " << preloginResponse.samlMethod(); - SAMLLoginWindow *loginWindow = new SAMLLoginWindow; + auto *loginWindow = new SAMLLoginWindow; connect(loginWindow, &SAMLLoginWindow::success, [this, loginWindow](const QMap samlResult) { this->onSAMLLoginSuccess(samlResult); @@ -188,8 +184,8 @@ void PortalAuthenticator::onFetchConfigFinished() LOGE << QString("Failed to fetch the portal config from %1, %2").arg(configUrl).arg(reply->errorString()); // Login failed, enable the fields of the normal login window - if (normalLoginWindow) { - normalLoginWindow->setProcessing(false); + if (standardLoginWindow) { + standardLoginWindow->setProcessing(false); openMessageBox("Portal login failed.", "Please check your credentials and try again."); } else if (isAutoLogin) { isAutoLogin = false; @@ -208,10 +204,10 @@ void PortalAuthenticator::onFetchConfigFinished() response.setPassword(password); // Close the login window - if (normalLoginWindow) { - LOGI << "Closing the NormalLoginWindow..."; + if (standardLoginWindow) { + LOGI << "Closing the StandardLoginWindow..."; - normalLoginWindow->close(); + standardLoginWindow->close(); } emit success(response, preloginResponse.region()); diff --git a/GPClient/portalauthenticator.h b/GPClient/portalauthenticator.h index 7c4b823..b0dadf8 100644 --- a/GPClient/portalauthenticator.h +++ b/GPClient/portalauthenticator.h @@ -4,7 +4,7 @@ #include #include "portalconfigresponse.h" -#include "normalloginwindow.h" +#include "standardloginwindow.h" #include "samlloginwindow.h" #include "preloginresponse.h" @@ -48,7 +48,7 @@ private: bool isAutoLogin{ false }; - NormalLoginWindow *normalLoginWindow{ nullptr }; + StandardLoginWindow *standardLoginWindow { nullptr }; void tryAutoLogin(); void normalAuth(); diff --git a/GPClient/samlloginwindow.cpp b/GPClient/samlloginwindow.cpp index 41bbd6b..d7b8d16 100644 --- a/GPClient/samlloginwindow.cpp +++ b/GPClient/samlloginwindow.cpp @@ -33,23 +33,18 @@ SAMLLoginWindow::SAMLLoginWindow(QWidget *parent) }); } -SAMLLoginWindow::~SAMLLoginWindow() -{ - delete webView; -} - void SAMLLoginWindow::closeEvent(QCloseEvent *event) { event->accept(); reject(); } -void SAMLLoginWindow::login(const QString samlMethod, const QString samlRequest, const QString preloingUrl) +void SAMLLoginWindow::login(const QString samlMethod, const QString samlRequest, const QString preloginUrl) { webView->page()->profile()->cookieStore()->deleteSessionCookies(); if (samlMethod == "POST") { - webView->setHtml(samlRequest, preloingUrl); + webView->setHtml(samlRequest, preloginUrl); } else if (samlMethod == "REDIRECT") { LOGI << "Redirect to " << samlRequest; webView->load(samlRequest); diff --git a/GPClient/samlloginwindow.h b/GPClient/samlloginwindow.h index 7bbf700..dc5bf3e 100644 --- a/GPClient/samlloginwindow.h +++ b/GPClient/samlloginwindow.h @@ -13,9 +13,8 @@ class SAMLLoginWindow : public QDialog public: explicit SAMLLoginWindow(QWidget *parent = nullptr); - ~SAMLLoginWindow(); - void login(const QString samlMethod, const QString samlRequest, const QString preloingUrl); + void login(const QString samlMethod, const QString samlRequest, const QString preloginUrl); signals: void success(QMap samlResult); @@ -30,7 +29,7 @@ private: static const auto MAX_WAIT_TIME { 10 * 1000 }; bool failed { false }; - EnhancedWebView *webView; + QWebEngineView *webView { nullptr }; QMap samlResult; void closeEvent(QCloseEvent *event); diff --git a/GPClient/standardloginwindow.cpp b/GPClient/standardloginwindow.cpp new file mode 100644 index 0000000..bdd9aa4 --- /dev/null +++ b/GPClient/standardloginwindow.cpp @@ -0,0 +1,41 @@ +#include + +#include "standardloginwindow.h" +#include "ui_standardloginwindow.h" + +StandardLoginWindow::StandardLoginWindow(const QString &portalAddress, const QString &labelUsername, + const QString &labelPassword, const QString &authMessage) : + QDialog(nullptr), + ui(new Ui::StandardLoginWindow) { + ui->setupUi(this); + ui->portalAddress->setText(portalAddress); + ui->username->setPlaceholderText(labelUsername); + ui->password->setPlaceholderText(labelPassword); + ui->authMessage->setText(authMessage); + + setWindowTitle("GlobalProtect Login"); + setFixedSize(width(), height()); + setModal(true); +} + +void StandardLoginWindow::setProcessing(bool isProcessing) { + ui->username->setReadOnly(isProcessing); + ui->password->setReadOnly(isProcessing); + ui->loginButton->setDisabled(isProcessing); +} + +void StandardLoginWindow::on_loginButton_clicked() { + const QString username = ui->username->text().trimmed(); + const QString password = ui->password->text().trimmed(); + + if (username.isEmpty() || password.isEmpty()) { + return; + } + + emit performLogin(username, password); +} + +void StandardLoginWindow::closeEvent(QCloseEvent *event) { + event->accept(); + reject(); +} diff --git a/GPClient/standardloginwindow.h b/GPClient/standardloginwindow.h new file mode 100644 index 0000000..92f5f5d --- /dev/null +++ b/GPClient/standardloginwindow.h @@ -0,0 +1,33 @@ +#ifndef STANDARDLOGINWINDOW_H +#define STANDARDLOGINWINDOW_H + +#include + +namespace Ui { + class StandardLoginWindow; +} + +class StandardLoginWindow : public QDialog { +Q_OBJECT + +public: + explicit StandardLoginWindow(const QString &portalAddress, const QString &labelUsername, + const QString &labelPassword, const QString &authMessage); + + void setProcessing(bool isProcessing); + +private slots: + + void on_loginButton_clicked(); + +signals: + + void performLogin(QString username, QString password); + +private: + Ui::StandardLoginWindow *ui; + + void closeEvent(QCloseEvent *event); +}; + +#endif // STANDARDLOGINWINDOW_H diff --git a/GPClient/normalloginwindow.ui b/GPClient/standardloginwindow.ui similarity index 97% rename from GPClient/normalloginwindow.ui rename to GPClient/standardloginwindow.ui index 4183116..1190534 100644 --- a/GPClient/normalloginwindow.ui +++ b/GPClient/standardloginwindow.ui @@ -1,7 +1,7 @@ - NormalLoginWindow - + StandardLoginWindow + 0