handle html comment for saml result with okta 2fa (#156)

This commit is contained in:
Samar Dhwoj Acharya 2022-06-06 00:39:06 -05:00 committed by GitHub
parent 5714063457
commit 5db77e8404
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 0 deletions

View File

@ -22,6 +22,7 @@ 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()
@ -64,6 +65,11 @@ void SAMLLoginWindow::onResponseReceived(QJsonObject params)
LOGI << "Response received from " << response.value("url").toString(); LOGI << "Response received from " << response.value("url").toString();
this->checkSamlResult(username, preloginCookie, userAuthCookie);
}
void SAMLLoginWindow::checkSamlResult(QString username, QString preloginCookie, QString userAuthCookie)
{
if (!username.isEmpty()) { if (!username.isEmpty()) {
LOGI << "Got username from SAML response headers " << username; LOGI << "Got username from SAML response headers " << username;
samlResult.insert("username", username); samlResult.insert("username", username);
@ -97,4 +103,29 @@ void SAMLLoginWindow::onResponseReceived(QJsonObject params)
void SAMLLoginWindow::onLoadFinished() void SAMLLoginWindow::onLoadFinished()
{ {
LOGI << "Load finished " << this->webView->page()->url().toString(); LOGI << "Load finished " << this->webView->page()->url().toString();
webView->page()->toHtml([this](const QString& result) mutable {emit getHTML(result);});
}
void SAMLLoginWindow::handleHTML(QString sHTML)
{
// 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 QString samlAuthStatusOnBody = match.captured(1);
if (samlAuthStatusOnBody == "1") {
const QRegularExpression preloginCookieRegex("<prelogin-cookie>(.*)</prelogin-cookie>");
const QRegularExpressionMatch preloginCookieMatch = preloginCookieRegex.match(sHTML);
const QString preloginCookie = preloginCookieMatch.captured(1);
const QRegularExpression usernameRegex("<saml-username>(.*)</saml-username>");
const QRegularExpressionMatch usernameMatch = usernameRegex.match(sHTML);
const QString username = usernameMatch.captured(1);
const QRegularExpression userAuthCookieRegex("<portal-userauthcookie>(.*)</portal-userauthcookie>");
const QRegularExpressionMatch userAuthCookieMatch = userAuthCookieRegex.match(sHTML);
const QString userAuthCookie = userAuthCookieMatch.captured(1);
this->checkSamlResult(username, preloginCookie, userAuthCookie);
}
} }

View File

@ -20,10 +20,15 @@ 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);
void onLoadFinished(); void onLoadFinished();
void checkSamlResult(QString username, QString preloginCookie, QString userAuthCookie);
private: private:
EnhancedWebView *webView; EnhancedWebView *webView;