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
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

View File

@ -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");
}

View File

@ -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

View File

@ -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,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);
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);
@ -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<QString, QString> &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();
});

View File

@ -3,7 +3,7 @@
#include <QtCore/QObject>
#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<QString, QString> &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();

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 "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<QString, QString> 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());

View File

@ -4,7 +4,7 @@
#include <QtCore/QObject>
#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();

View File

@ -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);

View File

@ -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<QString, QString> samlResult);
@ -30,7 +29,7 @@ private:
static const auto MAX_WAIT_TIME { 10 * 1000 };
bool failed { false };
EnhancedWebView *webView;
QWebEngineView *webView { nullptr };
QMap<QString, QString> samlResult;
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"?>
<ui version="4.0">
<class>NormalLoginWindow</class>
<widget class="QDialog" name="NormalLoginWindow">
<class>StandardLoginWindow</class>
<widget class="QDialog" name="StandardLoginWindow">
<property name="geometry">
<rect>
<x>0</x>