From cf34f9f70fdf049f82c6cfd0ca0c2544f5bff8c0 Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Sun, 31 May 2020 12:14:56 +0800 Subject: [PATCH] Improve the authentication workflow (#18) --- GPClient/gpclient.cpp | 48 ++++++++++++++++++++++--------- GPClient/gpclient.h | 6 ++-- GPClient/portalauthenticator.cpp | 6 ++-- GPClient/portalauthenticator.h | 3 +- GPClient/portalconfigresponse.cpp | 2 +- GPClient/portalconfigresponse.h | 2 +- 6 files changed, 45 insertions(+), 22 deletions(-) diff --git a/GPClient/gpclient.cpp b/GPClient/gpclient.cpp index a67de7a..3de730b 100644 --- a/GPClient/gpclient.cpp +++ b/GPClient/gpclient.cpp @@ -243,6 +243,7 @@ void GPClient::portalLogin() connect(portalAuth, &PortalAuthenticator::success, this, &GPClient::onPortalSuccess); // Prelogin failed on the portal interface, try to treat the portal as a gateway interface connect(portalAuth, &PortalAuthenticator::preloginFailed, this, &GPClient::onPortalPreloginFail); + connect(portalAuth, &PortalAuthenticator::portalConfigFailed, this, &GPClient::onPortalConfigFail); // Portal login failed connect(portalAuth, &PortalAuthenticator::fail, this, &GPClient::onPortalFail); @@ -251,21 +252,49 @@ void GPClient::portalLogin() portalAuth->authenticate(); } -void GPClient::onPortalSuccess(const PortalConfigResponse portalConfig, const GPGateway gateway, QList allGateways) +void GPClient::onPortalSuccess(const PortalConfigResponse portalConfig, const QString region) { PLOGI << "Portal authentication succeeded."; - this->portalConfig = portalConfig; + // No gateway found in protal configuration + if (portalConfig.allGateways().size() == 0) { + PLOGI << "No gateway found in portal configuration, treat the portal address as a gateway."; + tryGatewayLogin(); + return; + } - setAllGateways(allGateways); + GPGateway gateway = filterPreferredGateway(portalConfig.allGateways(), region); + setAllGateways(portalConfig.allGateways()); setCurrentGateway(gateway); + this->portalConfig = portalConfig; gatewayLogin(); } -void GPClient::onPortalPreloginFail() +void GPClient::onPortalPreloginFail(const QString msg) { - PLOGI << "Portal prelogin failed, try to preform login on the the gateway interface..."; + PLOGI << "Portal prelogin failed: " << msg; + tryGatewayLogin(); +} + +void GPClient::onPortalConfigFail(const QString msg) +{ + PLOGI << "Failed to get the portal configuration, " << msg << " Treat the portal address as gateway."; + tryGatewayLogin(); +} + +void GPClient::onPortalFail(const QString &msg) +{ + if (!msg.isEmpty()) { + openMessageBox("Portal authentication failed.", msg); + } + + updateConnectionStatus(VpnStatus::disconnected); +} + +void GPClient::tryGatewayLogin() +{ + PLOGI << "Try to preform login on the the gateway interface..."; // Treat the portal input as the gateway address GPGateway g; @@ -281,15 +310,6 @@ void GPClient::onPortalPreloginFail() gatewayLogin(); } -void GPClient::onPortalFail(const QString &msg) -{ - if (!msg.isEmpty()) { - openMessageBox("Portal authentication failed.", msg); - } - - updateConnectionStatus(VpnStatus::disconnected); -} - // Login to the gateway void GPClient::gatewayLogin() { diff --git a/GPClient/gpclient.h b/GPClient/gpclient.h index 71c08a1..07ca0fd 100644 --- a/GPClient/gpclient.h +++ b/GPClient/gpclient.h @@ -30,8 +30,9 @@ private slots: void onSystemTrayActivated(QSystemTrayIcon::ActivationReason reason); void onGatewayChanged(QAction *action); - void onPortalSuccess(const PortalConfigResponse portalConfig, const GPGateway gateway, QList allGateways); - void onPortalPreloginFail(); + void onPortalSuccess(const PortalConfigResponse portalConfig, const QString region); + void onPortalPreloginFail(const QString msg); + void onPortalConfigFail(const QString msg); void onPortalFail(const QString &msg); void onGatewaySuccess(const QString &authCookie); @@ -72,6 +73,7 @@ private: void doConnect(); void portalLogin(); + void tryGatewayLogin(); void gatewayLogin(); QString portal() const; diff --git a/GPClient/portalauthenticator.cpp b/GPClient/portalauthenticator.cpp index 74a41d9..450e3b9 100644 --- a/GPClient/portalauthenticator.cpp +++ b/GPClient/portalauthenticator.cpp @@ -57,7 +57,7 @@ void PortalAuthenticator::onPreloginFinished() tryAutoLogin(); } else { PLOGE << QString("Unknown prelogin response for %1 got %2").arg(preloginUrl).arg(QString::fromUtf8(preloginResponse.rawResponse())); - emitFail("Unknown response for portal prelogin interface."); + emit preloginFailed("Unknown response for portal prelogin interface."); } delete reply; @@ -175,7 +175,7 @@ void PortalAuthenticator::onFetchConfigFinished() isAutoLogin = false; normalAuth(); } else { - emitFail("Failed to fetch the portal config."); + emit portalConfigFailed("Failed to fetch the portal config."); } return; } @@ -197,7 +197,7 @@ void PortalAuthenticator::onFetchConfigFinished() normalLoginWindow->close(); } - emit success(response, filterPreferredGateway(response.allGateways(), preloginResponse.region()), response.allGateways()); + emit success(response, preloginResponse.region()); } void PortalAuthenticator::emitFail(const QString& msg) diff --git a/GPClient/portalauthenticator.h b/GPClient/portalauthenticator.h index 2d52f47..c74fa5f 100644 --- a/GPClient/portalauthenticator.h +++ b/GPClient/portalauthenticator.h @@ -18,9 +18,10 @@ public: void authenticate(); signals: - void success(const PortalConfigResponse, const GPGateway, QList allGateways); + void success(const PortalConfigResponse response, const QString region); void fail(const QString& msg); void preloginFailed(const QString& msg); + void portalConfigFailed(const QString msg); private slots: void onPreloginFinished(); diff --git a/GPClient/portalconfigresponse.cpp b/GPClient/portalconfigresponse.cpp index fedfff4..c861991 100644 --- a/GPClient/portalconfigresponse.cpp +++ b/GPClient/portalconfigresponse.cpp @@ -132,7 +132,7 @@ QString PortalConfigResponse::prelogonUserAuthCookie() const return _prelogonAuthCookie; } -QList PortalConfigResponse::allGateways() +QList PortalConfigResponse::allGateways() const { return _gateways; } diff --git a/GPClient/portalconfigresponse.h b/GPClient/portalconfigresponse.h index 5a5bace..2424daa 100644 --- a/GPClient/portalconfigresponse.h +++ b/GPClient/portalconfigresponse.h @@ -20,7 +20,7 @@ public: QString password() const; QString userAuthCookie() const; QString prelogonUserAuthCookie() const; - QList allGateways(); + QList allGateways() const; void setAllGateways(QList gateways); void setUsername(const QString username);