refactor: simplify the code

This commit is contained in:
Kevin Yue 2022-06-12 17:34:23 +08:00
parent 5ebfe9b0f4
commit cb457c4b09
14 changed files with 128 additions and 175 deletions

View File

@ -23,7 +23,7 @@ add_executable(gpclient
gphelper.cpp gphelper.cpp
loginparams.cpp loginparams.cpp
main.cpp main.cpp
normalloginwindow.cpp standardloginwindow.cpp
portalauthenticator.cpp portalauthenticator.cpp
portalconfigresponse.cpp portalconfigresponse.cpp
preloginresponse.cpp preloginresponse.cpp
@ -31,7 +31,7 @@ add_executable(gpclient
gpclient.cpp gpclient.cpp
settingsdialog.cpp settingsdialog.cpp
gpclient.ui gpclient.ui
normalloginwindow.ui standardloginwindow.ui
settingsdialog.ui settingsdialog.ui
challengedialog.h challengedialog.h
challengedialog.cpp challengedialog.cpp

View File

@ -8,18 +8,13 @@ EnhancedWebView::EnhancedWebView(QWidget *parent)
: QWebEngineView(parent) : QWebEngineView(parent)
, cdp(new CDPCommandManager) , cdp(new CDPCommandManager)
{ {
QObject::connect(cdp, &CDPCommandManager::ready, this, &EnhancedWebView::onCDPReady); QObject::connect(cdp, &CDPCommandManager::ready, this, &EnhancedWebView::onCDPReady);
QObject::connect(cdp, &CDPCommandManager::eventReceived, this, &EnhancedWebView::onEventReceived); QObject::connect(cdp, &CDPCommandManager::eventReceived, this, &EnhancedWebView::onEventReceived);
}
EnhancedWebView::~EnhancedWebView()
{
delete cdp;
} }
void EnhancedWebView::initialize() 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"); cdp->initialize("http://127.0.0.1:" + port + "/json");
} }

View File

@ -12,7 +12,6 @@ class EnhancedWebView : public QWebEngineView
Q_OBJECT Q_OBJECT
public: public:
explicit EnhancedWebView(QWidget *parent = nullptr); explicit EnhancedWebView(QWidget *parent = nullptr);
~EnhancedWebView();
void initialize(); void initialize();
@ -24,7 +23,7 @@ private slots:
void onEventReceived(QString eventName, QJsonObject params); void onEventReceived(QString eventName, QJsonObject params);
private: private:
CDPCommandManager *cdp; CDPCommandManager *cdp { nullptr };
}; };
#endif // ENHANCEDWEBVIEW_H #endif // ENHANCEDWEBVIEW_H

View File

@ -38,7 +38,7 @@ void GatewayAuthenticator::authenticate()
void GatewayAuthenticator::login(const LoginParams &loginParams) 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()); auto *reply = createRequest(loginUrl, loginParams.toUtf8());
connect(reply, &QNetworkReply::finished, this, &GatewayAuthenticator::onLoginFinished); connect(reply, &QNetworkReply::finished, this, &GatewayAuthenticator::onLoginFinished);
@ -52,8 +52,8 @@ void GatewayAuthenticator::onLoginFinished()
if (reply->error() || response.contains("Authentication failure")) { if (reply->error() || response.contains("Authentication failure")) {
LOGE << QString("Failed to login the gateway at %1, %2").arg(loginUrl, reply->errorString()); LOGE << QString("Failed to login the gateway at %1, %2").arg(loginUrl, reply->errorString());
if (normalLoginWindow) { if (standardLoginWindow) {
normalLoginWindow->setProcessing(false); standardLoginWindow->setProcessing(false);
openMessageBox("Gateway login failed.", "Please check your credentials and try again."); openMessageBox("Gateway login failed.", "Please check your credentials and try again.");
} else { } else {
doAuth(); doAuth();
@ -68,8 +68,8 @@ void GatewayAuthenticator::onLoginFinished()
return; return;
} }
if (normalLoginWindow) { if (standardLoginWindow) {
normalLoginWindow->close(); standardLoginWindow->close();
} }
const auto params = gpclient::helper::parseGatewayResponse(response); const auto params = gpclient::helper::parseGatewayResponse(response);
@ -115,25 +115,21 @@ void GatewayAuthenticator::normalAuth(QString labelUsername, QString labelPasswo
{ {
LOGI << QString("Trying to perform the normal login with %1 / %2 credentials").arg(labelUsername, labelPassword); LOGI << QString("Trying to perform the normal login with %1 / %2 credentials").arg(labelUsername, labelPassword);
normalLoginWindow = new NormalLoginWindow; standardLoginWindow = new StandardLoginWindow {gateway, labelUsername, labelPassword, authMessage};
normalLoginWindow->setPortalAddress(gateway);
normalLoginWindow->setAuthMessage(authMessage);
normalLoginWindow->setUsernameLabel(labelUsername);
normalLoginWindow->setPasswordLabel(labelPassword);
// Do login // Do login
connect(normalLoginWindow, &NormalLoginWindow::performLogin, this, &GatewayAuthenticator::onPerformNormalLogin); connect(standardLoginWindow, &StandardLoginWindow::performLogin, this, &GatewayAuthenticator::onPerformStandardLogin);
connect(normalLoginWindow, &NormalLoginWindow::rejected, this, &GatewayAuthenticator::onLoginWindowRejected); connect(standardLoginWindow, &StandardLoginWindow::rejected, this, &GatewayAuthenticator::onLoginWindowRejected);
connect(normalLoginWindow, &NormalLoginWindow::finished, this, &GatewayAuthenticator::onLoginWindowFinished); 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..."; LOGI << "Start to perform normal login...";
normalLoginWindow->setProcessing(true); standardLoginWindow->setProcessing(true);
params.setUsername(username); params.setUsername(username);
params.setPassword(password); params.setPassword(password);
@ -147,15 +143,15 @@ void GatewayAuthenticator::onLoginWindowRejected()
void GatewayAuthenticator::onLoginWindowFinished() void GatewayAuthenticator::onLoginWindowFinished()
{ {
delete normalLoginWindow; delete standardLoginWindow;
normalLoginWindow = nullptr; standardLoginWindow = nullptr;
} }
void GatewayAuthenticator::samlAuth(QString samlMethod, QString samlRequest, QString preloginUrl) void GatewayAuthenticator::samlAuth(QString samlMethod, QString samlRequest, QString preloginUrl)
{ {
LOGI << "Trying to perform SAML login with saml-method " << samlMethod; 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<QString, QString> &samlResult) { connect(loginWindow, &SAMLLoginWindow::success, [this, loginWindow](const QMap<QString, QString> &samlResult) {
this->onSAMLLoginSuccess(samlResult); this->onSAMLLoginSuccess(samlResult);
@ -215,8 +211,8 @@ void GatewayAuthenticator::showChallenge(const QString &responseText)
}); });
connect(challengeDialog, &ChallengeDialog::rejected, this, [this] { connect(challengeDialog, &ChallengeDialog::rejected, this, [this] {
if (normalLoginWindow) { if (standardLoginWindow) {
normalLoginWindow->close(); standardLoginWindow->close();
} }
emit fail(); emit fail();
}); });

View File

@ -3,7 +3,7 @@
#include <QtCore/QObject> #include <QtCore/QObject>
#include "normalloginwindow.h" #include "standardloginwindow.h"
#include "challengedialog.h" #include "challengedialog.h"
#include "loginparams.h" #include "loginparams.h"
#include "gatewayauthenticatorparams.h" #include "gatewayauthenticatorparams.h"
@ -17,13 +17,13 @@ public:
void authenticate(); void authenticate();
signals: signals:
void success(const QString& authCookie); void success(const QString &authCookie);
void fail(const QString& msg = ""); void fail(const QString &msg = "");
private slots: private slots:
void onLoginFinished(); void onLoginFinished();
void onPreloginFinished(); void onPreloginFinished();
void onPerformNormalLogin(const QString &username, const QString &password); void onPerformStandardLogin(const QString &username, const QString &password);
void onLoginWindowRejected(); void onLoginWindowRejected();
void onLoginWindowFinished(); void onLoginWindowFinished();
void onSAMLLoginSuccess(const QMap<QString, QString> &samlResult); void onSAMLLoginSuccess(const QMap<QString, QString> &samlResult);
@ -35,8 +35,8 @@ private:
QString preloginUrl; QString preloginUrl;
QString loginUrl; QString loginUrl;
NormalLoginWindow *normalLoginWindow{ nullptr }; StandardLoginWindow *standardLoginWindow { nullptr };
ChallengeDialog *challengeDialog{ nullptr }; ChallengeDialog *challengeDialog { nullptr };
void login(const LoginParams& loginParams); void login(const LoginParams& loginParams);
void doAuth(); void doAuth();

View File

@ -1,64 +0,0 @@
#include <QtGui/QCloseEvent>
#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();
}

View File

@ -1,37 +0,0 @@
#ifndef PORTALAUTHWINDOW_H
#define PORTALAUTHWINDOW_H
#include <QtWidgets/QDialog>
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

View File

@ -3,7 +3,7 @@
#include "portalauthenticator.h" #include "portalauthenticator.h"
#include "gphelper.h" #include "gphelper.h"
#include "normalloginwindow.h" #include "standardloginwindow.h"
#include "samlloginwindow.h" #include "samlloginwindow.h"
#include "loginparams.h" #include "loginparams.h"
#include "preloginresponse.h" #include "preloginresponse.h"
@ -25,7 +25,7 @@ PortalAuthenticator::PortalAuthenticator(const QString& portal, const QString& c
PortalAuthenticator::~PortalAuthenticator() PortalAuthenticator::~PortalAuthenticator()
{ {
delete normalLoginWindow; delete standardLoginWindow;
} }
void PortalAuthenticator::authenticate() void PortalAuthenticator::authenticate()
@ -87,23 +87,19 @@ void PortalAuthenticator::normalAuth()
{ {
LOGI << "Trying to launch the normal login window..."; LOGI << "Trying to launch the normal login window...";
normalLoginWindow = new NormalLoginWindow; standardLoginWindow = new StandardLoginWindow {portal, preloginResponse.labelUsername(), preloginResponse.labelPassword(), preloginResponse.authMessage() };
normalLoginWindow->setPortalAddress(portal);
normalLoginWindow->setAuthMessage(preloginResponse.authMessage());
normalLoginWindow->setUsernameLabel(preloginResponse.labelUsername());
normalLoginWindow->setPasswordLabel(preloginResponse.labelPassword());
// Do login // Do login
connect(normalLoginWindow, &NormalLoginWindow::performLogin, this, &PortalAuthenticator::onPerformNormalLogin); connect(standardLoginWindow, &StandardLoginWindow::performLogin, this, &PortalAuthenticator::onPerformNormalLogin);
connect(normalLoginWindow, &NormalLoginWindow::rejected, this, &PortalAuthenticator::onLoginWindowRejected); connect(standardLoginWindow, &StandardLoginWindow::rejected, this, &PortalAuthenticator::onLoginWindowRejected);
connect(normalLoginWindow, &NormalLoginWindow::finished, this, &PortalAuthenticator::onLoginWindowFinished); connect(standardLoginWindow, &StandardLoginWindow::finished, this, &PortalAuthenticator::onLoginWindowFinished);
normalLoginWindow->show(); standardLoginWindow->show();
} }
void PortalAuthenticator::onPerformNormalLogin(const QString &username, const QString &password) void PortalAuthenticator::onPerformNormalLogin(const QString &username, const QString &password)
{ {
normalLoginWindow->setProcessing(true); standardLoginWindow->setProcessing(true);
fetchConfig(username, password); fetchConfig(username, password);
} }
@ -114,15 +110,15 @@ void PortalAuthenticator::onLoginWindowRejected()
void PortalAuthenticator::onLoginWindowFinished() void PortalAuthenticator::onLoginWindowFinished()
{ {
delete normalLoginWindow; delete standardLoginWindow;
normalLoginWindow = nullptr; standardLoginWindow = nullptr;
} }
void PortalAuthenticator::samlAuth() void PortalAuthenticator::samlAuth()
{ {
LOGI << "Trying to perform SAML login with saml-method " << preloginResponse.samlMethod(); 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<QString, QString> samlResult) { connect(loginWindow, &SAMLLoginWindow::success, [this, loginWindow](const QMap<QString, QString> samlResult) {
this->onSAMLLoginSuccess(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()); 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 // Login failed, enable the fields of the normal login window
if (normalLoginWindow) { if (standardLoginWindow) {
normalLoginWindow->setProcessing(false); standardLoginWindow->setProcessing(false);
openMessageBox("Portal login failed.", "Please check your credentials and try again."); openMessageBox("Portal login failed.", "Please check your credentials and try again.");
} else if (isAutoLogin) { } else if (isAutoLogin) {
isAutoLogin = false; isAutoLogin = false;
@ -208,10 +204,10 @@ void PortalAuthenticator::onFetchConfigFinished()
response.setPassword(password); response.setPassword(password);
// Close the login window // Close the login window
if (normalLoginWindow) { if (standardLoginWindow) {
LOGI << "Closing the NormalLoginWindow..."; LOGI << "Closing the StandardLoginWindow...";
normalLoginWindow->close(); standardLoginWindow->close();
} }
emit success(response, preloginResponse.region()); emit success(response, preloginResponse.region());

View File

@ -4,7 +4,7 @@
#include <QtCore/QObject> #include <QtCore/QObject>
#include "portalconfigresponse.h" #include "portalconfigresponse.h"
#include "normalloginwindow.h" #include "standardloginwindow.h"
#include "samlloginwindow.h" #include "samlloginwindow.h"
#include "preloginresponse.h" #include "preloginresponse.h"
@ -48,7 +48,7 @@ private:
bool isAutoLogin{ false }; bool isAutoLogin{ false };
NormalLoginWindow *normalLoginWindow{ nullptr }; StandardLoginWindow *standardLoginWindow { nullptr };
void tryAutoLogin(); void tryAutoLogin();
void normalAuth(); void normalAuth();

View File

@ -33,23 +33,18 @@ SAMLLoginWindow::SAMLLoginWindow(QWidget *parent)
}); });
} }
SAMLLoginWindow::~SAMLLoginWindow()
{
delete webView;
}
void SAMLLoginWindow::closeEvent(QCloseEvent *event) void SAMLLoginWindow::closeEvent(QCloseEvent *event)
{ {
event->accept(); event->accept();
reject(); 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(); webView->page()->profile()->cookieStore()->deleteSessionCookies();
if (samlMethod == "POST") { if (samlMethod == "POST") {
webView->setHtml(samlRequest, preloingUrl); webView->setHtml(samlRequest, preloginUrl);
} else if (samlMethod == "REDIRECT") { } else if (samlMethod == "REDIRECT") {
LOGI << "Redirect to " << samlRequest; LOGI << "Redirect to " << samlRequest;
webView->load(samlRequest); webView->load(samlRequest);

View File

@ -13,9 +13,8 @@ class SAMLLoginWindow : public QDialog
public: public:
explicit SAMLLoginWindow(QWidget *parent = nullptr); 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: signals:
void success(QMap<QString, QString> samlResult); void success(QMap<QString, QString> samlResult);
@ -30,7 +29,7 @@ private:
static const auto MAX_WAIT_TIME { 10 * 1000 }; static const auto MAX_WAIT_TIME { 10 * 1000 };
bool failed { false }; bool failed { false };
EnhancedWebView *webView; QWebEngineView *webView { nullptr };
QMap<QString, QString> samlResult; QMap<QString, QString> samlResult;
void closeEvent(QCloseEvent *event); void closeEvent(QCloseEvent *event);

View File

@ -0,0 +1,41 @@
#include <QtGui/QCloseEvent>
#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();
}

View File

@ -0,0 +1,33 @@
#ifndef STANDARDLOGINWINDOW_H
#define STANDARDLOGINWINDOW_H
#include <QtWidgets/QDialog>
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

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"> <ui version="4.0">
<class>NormalLoginWindow</class> <class>StandardLoginWindow</class>
<widget class="QDialog" name="NormalLoginWindow"> <widget class="QDialog" name="StandardLoginWindow">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>0</x> <x>0</x>