fix: add support for parsing tokens from HTML

This commit is contained in:
Kevin Yue 2022-06-06 15:01:50 +08:00
parent 5db77e8404
commit 3067e6e911
2 changed files with 14 additions and 16 deletions

View File

@ -22,7 +22,6 @@ SAMLLoginWindow::SAMLLoginWindow(QWidget *parent)
webView->initialize(); webView->initialize();
connect(webView, &EnhancedWebView::responseReceived, this, &SAMLLoginWindow::onResponseReceived); connect(webView, &EnhancedWebView::responseReceived, this, &SAMLLoginWindow::onResponseReceived);
connect(webView, &EnhancedWebView::loadFinished, this, &SAMLLoginWindow::onLoadFinished); connect(webView, &EnhancedWebView::loadFinished, this, &SAMLLoginWindow::onLoadFinished);
connect(this, SIGNAL(getHTML(QString)), this, SLOT(handleHTML(QString)));
} }
SAMLLoginWindow::~SAMLLoginWindow() SAMLLoginWindow::~SAMLLoginWindow()
@ -59,12 +58,12 @@ void SAMLLoginWindow::onResponseReceived(QJsonObject params)
QJsonObject response = params.value("response").toObject(); QJsonObject response = params.value("response").toObject();
QJsonObject headers = response.value("headers").toObject(); QJsonObject headers = response.value("headers").toObject();
LOGI << "Trying to receive from " << response.value("url").toString();
const QString username = headers.value("saml-username").toString(); const QString username = headers.value("saml-username").toString();
const QString preloginCookie = headers.value("prelogin-cookie").toString(); const QString preloginCookie = headers.value("prelogin-cookie").toString();
const QString userAuthCookie = headers.value("portal-userauthcookie").toString(); const QString userAuthCookie = headers.value("portal-userauthcookie").toString();
LOGI << "Response received from " << response.value("url").toString();
this->checkSamlResult(username, preloginCookie, userAuthCookie); this->checkSamlResult(username, preloginCookie, userAuthCookie);
} }
@ -96,36 +95,38 @@ void SAMLLoginWindow::checkSamlResult(QString username, QString preloginCookie,
emit success(samlResult); emit success(samlResult);
accept(); accept();
} else { } else {
this->show(); show();
} }
} }
void SAMLLoginWindow::onLoadFinished() void SAMLLoginWindow::onLoadFinished()
{ {
LOGI << "Load finished " << this->webView->page()->url().toString(); LOGI << "Load finished " << webView->page()->url().toString();
webView->page()->toHtml([this](const QString& result) mutable {emit getHTML(result);}); webView->page()->toHtml([this] (const QString &html) { this->handleHtml(html); });
} }
void SAMLLoginWindow::handleHTML(QString sHTML) void SAMLLoginWindow::handleHtml(const QString &html)
{ {
// try to check the html body and extract from there // try to check the html body and extract from there
const QRegularExpression regex("<saml-auth-status>(.*)</saml-auth-status>"); const QRegularExpression regex("<saml-auth-status>(.*)</saml-auth-status>");
const QRegularExpressionMatch match = regex.match(sHTML); const QRegularExpressionMatch match = regex.match(html);
const QString samlAuthStatusOnBody = match.captured(1); const QString samlAuthStatusOnBody = match.captured(1);
if (samlAuthStatusOnBody == "1") { if (samlAuthStatusOnBody == "1") {
const QRegularExpression preloginCookieRegex("<prelogin-cookie>(.*)</prelogin-cookie>"); const QRegularExpression preloginCookieRegex("<prelogin-cookie>(.*)</prelogin-cookie>");
const QRegularExpressionMatch preloginCookieMatch = preloginCookieRegex.match(sHTML); const QRegularExpressionMatch preloginCookieMatch = preloginCookieRegex.match(html);
const QString preloginCookie = preloginCookieMatch.captured(1); const QString preloginCookie = preloginCookieMatch.captured(1);
const QRegularExpression usernameRegex("<saml-username>(.*)</saml-username>"); const QRegularExpression usernameRegex("<saml-username>(.*)</saml-username>");
const QRegularExpressionMatch usernameMatch = usernameRegex.match(sHTML); const QRegularExpressionMatch usernameMatch = usernameRegex.match(html);
const QString username = usernameMatch.captured(1); const QString username = usernameMatch.captured(1);
const QRegularExpression userAuthCookieRegex("<portal-userauthcookie>(.*)</portal-userauthcookie>"); const QRegularExpression userAuthCookieRegex("<portal-userauthcookie>(.*)</portal-userauthcookie>");
const QRegularExpressionMatch userAuthCookieMatch = userAuthCookieRegex.match(sHTML); const QRegularExpressionMatch userAuthCookieMatch = userAuthCookieRegex.match(html);
const QString userAuthCookie = userAuthCookieMatch.captured(1); const QString userAuthCookie = userAuthCookieMatch.captured(1);
this->checkSamlResult(username, preloginCookie, userAuthCookie); checkSamlResult(username, preloginCookie, userAuthCookie);
} else {
show();
} }
} }

View File

@ -20,10 +20,6 @@ public:
signals: signals:
void success(QMap<QString, QString> samlResult); void success(QMap<QString, QString> samlResult);
void fail(const QString msg); void fail(const QString msg);
void getHTML(QString sHTML);
protected slots:
void handleHTML(QString sHTML);
private slots: private slots:
void onResponseReceived(QJsonObject params); void onResponseReceived(QJsonObject params);
@ -35,6 +31,7 @@ private:
QMap<QString, QString> samlResult; QMap<QString, QString> samlResult;
void closeEvent(QCloseEvent *event); void closeEvent(QCloseEvent *event);
void handleHtml(const QString &html);
}; };
#endif // SAMLLOGINWINDOW_H #endif // SAMLLOGINWINDOW_H