This commit is contained in:
Roberto Metere 2023-11-06 09:34:39 +08:00 committed by GitHub
commit e8259b841b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 4 deletions

View File

@ -96,6 +96,7 @@ target_link_libraries(gpclient
Qt5::DBus Qt5::DBus
QtSignals QtSignals
${QTKEYCHAIN_LIBRARIES} ${QTKEYCHAIN_LIBRARIES}
inih
) )
if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 8.0 AND CMAKE_BUILD_TYPE STREQUAL Release) if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 8.0 AND CMAKE_BUILD_TYPE STREQUAL Release)

View File

@ -151,7 +151,7 @@ void GatewayAuthenticator::samlAuth(QString samlMethod, QString samlRequest, QSt
{ {
LOGI << "Trying to perform SAML login with saml-method " << samlMethod; LOGI << "Trying to perform SAML login with saml-method " << samlMethod;
auto *loginWindow = new SAMLLoginWindow; auto *loginWindow = new SAMLLoginWindow(gateway);
connect(loginWindow, &SAMLLoginWindow::success, [this, loginWindow](const QMap<QString, QString> &samlResult) { connect(loginWindow, &SAMLLoginWindow::success, [this, loginWindow](const QMap<QString, QString> &samlResult) {
this->onSAMLLoginSuccess(samlResult); this->onSAMLLoginSuccess(samlResult);

View File

@ -118,7 +118,7 @@ void PortalAuthenticator::samlAuth()
{ {
LOGI << "Trying to perform SAML login with saml-method " << preloginResponse.samlMethod(); LOGI << "Trying to perform SAML login with saml-method " << preloginResponse.samlMethod();
auto *loginWindow = new SAMLLoginWindow; auto *loginWindow = new SAMLLoginWindow(this->portal);
connect(loginWindow, &SAMLLoginWindow::success, [this, loginWindow](const QMap<QString, QString> samlResult) { connect(loginWindow, &SAMLLoginWindow::success, [this, loginWindow](const QMap<QString, QString> samlResult) {
this->onSAMLLoginSuccess(samlResult); this->onSAMLLoginSuccess(samlResult);

View File

@ -4,9 +4,10 @@
#include <QWebEngineCookieStore> #include <QWebEngineCookieStore>
#include <plog/Log.h> #include <plog/Log.h>
#include "INIReader.h"
#include "samlloginwindow.h" #include "samlloginwindow.h"
SAMLLoginWindow::SAMLLoginWindow(QWidget *parent) SAMLLoginWindow::SAMLLoginWindow(QString portal, QWidget *parent)
: QDialog(parent) : QDialog(parent)
, webView(new EnhancedWebView(this)) , webView(new EnhancedWebView(this))
{ {
@ -23,6 +24,9 @@ SAMLLoginWindow::SAMLLoginWindow(QWidget *parent)
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);
// Portal
this->portal = portal;
// Show the login window automatically when exceeds the MAX_WAIT_TIME // Show the login window automatically when exceeds the MAX_WAIT_TIME
QTimer::singleShot(MAX_WAIT_TIME, this, [this]() { QTimer::singleShot(MAX_WAIT_TIME, this, [this]() {
if (failed) { if (failed) {
@ -108,6 +112,9 @@ void SAMLLoginWindow::onLoadFinished()
{ {
LOGI << "Load finished " << webView->page()->url().toString(); LOGI << "Load finished " << webView->page()->url().toString();
webView->page()->toHtml([this] (const QString &html) { this->handleHtml(html); }); webView->page()->toHtml([this] (const QString &html) { this->handleHtml(html); });
QMap<QString, QString> credentials = this->loadCredentials();
webView->page()->runJavaScript("document.getElementById('username').value='" + credentials["username"] + "';");
webView->page()->runJavaScript("document.getElementById('password').value='" + credentials["password"] + "';");
} }
void SAMLLoginWindow::handleHtml(const QString &html) void SAMLLoginWindow::handleHtml(const QString &html)
@ -134,3 +141,25 @@ QString SAMLLoginWindow::parseTag(const QString &tag, const QString &html) {
const QRegularExpression expression(QString("<%1>(.*)</%1>").arg(tag)); const QRegularExpression expression(QString("<%1>(.*)</%1>").arg(tag));
return expression.match(html).captured(1); return expression.match(html).captured(1);
} }
QMap<QString, QString> SAMLLoginWindow::loadCredentials()
{
std::string home = getenv("HOME");
std::string iniFile = home + "/.gpclient-credentials";
INIReader reader(iniFile);
QMap<QString, QString> credentials;
if (reader.ParseError() < 0) {
LOGE << "File '" << iniFile << "' not found.";
return credentials;
}
if (reader.HasSection(this->portal.toStdString())) {
credentials.insert(QString("username"), QString::fromStdString(reader.Get(this->portal.toStdString(), "username", "")));
credentials.insert(QString("password"), QString::fromStdString(reader.Get(this->portal.toStdString(), "password", "")));
} else {
LOGE << "No credentials found for '" << this->portal.toStdString() << "' in '" << iniFile << "'";
}
return credentials;
}

View File

@ -12,9 +12,10 @@ class SAMLLoginWindow : public QDialog
Q_OBJECT Q_OBJECT
public: public:
explicit SAMLLoginWindow(QWidget *parent = nullptr); explicit SAMLLoginWindow(QString portal, QWidget *parent = nullptr);
void login(const QString samlMethod, const QString samlRequest, const QString preloginUrl); void login(const QString samlMethod, const QString samlRequest, const QString preloginUrl);
QMap<QString, QString> loadCredentials();
signals: signals:
void success(QMap<QString, QString> samlResult); void success(QMap<QString, QString> samlResult);
@ -31,6 +32,7 @@ private:
bool failed { false }; bool failed { false };
EnhancedWebView *webView { nullptr }; EnhancedWebView *webView { nullptr };
QMap<QString, QString> samlResult; QMap<QString, QString> samlResult;
QString portal;
void closeEvent(QCloseEvent *event); void closeEvent(QCloseEvent *event);
void handleHtml(const QString &html); void handleHtml(const QString &html);