Code refactor, support multiple gateways and non-SAML authentication (#9)

* Code refactor

* Update README.md
This commit is contained in:
Kevin Yue
2020-05-23 15:51:10 +08:00
committed by GitHub
parent 76a4977e92
commit 7f5bf0ce52
30 changed files with 1665 additions and 192 deletions

View File

@@ -1,19 +1,24 @@
#include "samlloginwindow.h"
#include <QVBoxLayout>
#include <plog/Log.h>
#include <QWebEngineProfile>
SAMLLoginWindow::SAMLLoginWindow(QWidget *parent)
: QDialog(parent)
{
setWindowTitle("SAML Login");
resize(610, 406);
setWindowTitle("GlobalProtect SAML Login");
resize(700, 550);
QVBoxLayout *verticalLayout = new QVBoxLayout(this);
webView = new EnhancedWebView(this);
webView->setUrl(QUrl("about:blank"));
// webView->page()->profile()->setPersistentCookiesPolicy(QWebEngineProfile::NoPersistentCookies);
verticalLayout->addWidget(webView);
webView->initialize();
QObject::connect(webView, &EnhancedWebView::responseReceived, this, &SAMLLoginWindow::onResponseReceived);
connect(webView, &EnhancedWebView::responseReceived, this, &SAMLLoginWindow::onResponseReceived);
connect(webView, &EnhancedWebView::loadFinished, this, &SAMLLoginWindow::onLoadFinished);
}
SAMLLoginWindow::~SAMLLoginWindow()
@@ -29,7 +34,7 @@ void SAMLLoginWindow::closeEvent(QCloseEvent *event)
void SAMLLoginWindow::login(QString url, QString html)
{
if (html == "") {
if (html.isEmpty()) {
webView->load(QUrl(url));
} else {
webView->setHtml(html, url);
@@ -47,17 +52,24 @@ void SAMLLoginWindow::onResponseReceived(QJsonObject params)
QJsonObject response = params.value("response").toObject();
QJsonObject headers = response.value("headers").toObject();
foreach (const QString& key, headers.keys()) {
if (key.startsWith("saml-") || key == "prelogin-cookie" || key == "portal-userauthcookie") {
samlResult.insert(key, headers.value(key));
}
}
const QString username = headers.value("saml-username").toString();
const QString preloginCookie = headers.value("prelogin-cookie").toString();
// Check the SAML result
if (samlResult.contains("saml-username")
&& (samlResult.contains("prelogin-cookie") || samlResult.contains("portal-userauthcookie"))) {
samlResult.insert("server", QUrl(response.value("url").toString()).authority());
emit success(samlResult);
accept();
if (!username.isEmpty() && !preloginCookie.isEmpty()) {
samlResult.insert("username", username);
samlResult.insert("preloginCookie", preloginCookie);
}
}
void SAMLLoginWindow::onLoadFinished()
{
LOGI << "Load finished " << this->webView->page()->url().toString();
// Check the SAML result
if (!samlResult.value("username").isEmpty() && !samlResult.value("preloginCookie").isEmpty()) {
emit success(samlResult);
accept();
} else {
open();
}
}