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();
connect(webView, &EnhancedWebView::responseReceived, this, &SAMLLoginWindow::onResponseReceived);
connect(webView, &EnhancedWebView::loadFinished, this, &SAMLLoginWindow::onLoadFinished);
connect(this, SIGNAL(getHTML(QString)), this, SLOT(handleHTML(QString)));
}
SAMLLoginWindow::~SAMLLoginWindow()
@ -59,12 +58,12 @@ void SAMLLoginWindow::onResponseReceived(QJsonObject params)
QJsonObject response = params.value("response").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 preloginCookie = headers.value("prelogin-cookie").toString();
const QString userAuthCookie = headers.value("portal-userauthcookie").toString();
LOGI << "Response received from " << response.value("url").toString();
this->checkSamlResult(username, preloginCookie, userAuthCookie);
}
@ -96,36 +95,38 @@ void SAMLLoginWindow::checkSamlResult(QString username, QString preloginCookie,
emit success(samlResult);
accept();
} else {
this->show();
show();
}
}
void SAMLLoginWindow::onLoadFinished()
{
LOGI << "Load finished " << this->webView->page()->url().toString();
webView->page()->toHtml([this](const QString& result) mutable {emit getHTML(result);});
LOGI << "Load finished " << webView->page()->url().toString();
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
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);
if (samlAuthStatusOnBody == "1") {
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 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 QRegularExpression userAuthCookieRegex("<portal-userauthcookie>(.*)</portal-userauthcookie>");
const QRegularExpressionMatch userAuthCookieMatch = userAuthCookieRegex.match(sHTML);
const QRegularExpressionMatch userAuthCookieMatch = userAuthCookieRegex.match(html);
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:
void success(QMap<QString, QString> samlResult);
void fail(const QString msg);
void getHTML(QString sHTML);
protected slots:
void handleHTML(QString sHTML);
private slots:
void onResponseReceived(QJsonObject params);
@ -35,6 +31,7 @@ private:
QMap<QString, QString> samlResult;
void closeEvent(QCloseEvent *event);
void handleHtml(const QString &html);
};
#endif // SAMLLOGINWINDOW_H