diff --git a/GPClient/CMakeLists.txt b/GPClient/CMakeLists.txt index 4a172fa..366b0e6 100644 --- a/GPClient/CMakeLists.txt +++ b/GPClient/CMakeLists.txt @@ -96,6 +96,7 @@ target_link_libraries(gpclient Qt5::DBus QtSignals ${QTKEYCHAIN_LIBRARIES} + inih ) if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 8.0 AND CMAKE_BUILD_TYPE STREQUAL Release) diff --git a/GPClient/gatewayauthenticator.cpp b/GPClient/gatewayauthenticator.cpp index 0d429a5..b7b4352 100644 --- a/GPClient/gatewayauthenticator.cpp +++ b/GPClient/gatewayauthenticator.cpp @@ -151,7 +151,7 @@ void GatewayAuthenticator::samlAuth(QString samlMethod, QString samlRequest, QSt { 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 &samlResult) { this->onSAMLLoginSuccess(samlResult); diff --git a/GPClient/portalauthenticator.cpp b/GPClient/portalauthenticator.cpp index 6ba00ce..ef82d90 100644 --- a/GPClient/portalauthenticator.cpp +++ b/GPClient/portalauthenticator.cpp @@ -118,7 +118,7 @@ void PortalAuthenticator::samlAuth() { 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 samlResult) { this->onSAMLLoginSuccess(samlResult); diff --git a/GPClient/samlloginwindow.cpp b/GPClient/samlloginwindow.cpp index d7b8d16..b5eccda 100644 --- a/GPClient/samlloginwindow.cpp +++ b/GPClient/samlloginwindow.cpp @@ -4,9 +4,10 @@ #include #include +#include "INIReader.h" #include "samlloginwindow.h" -SAMLLoginWindow::SAMLLoginWindow(QWidget *parent) +SAMLLoginWindow::SAMLLoginWindow(QString portal, QWidget *parent) : QDialog(parent) , webView(new EnhancedWebView(this)) { @@ -23,6 +24,9 @@ SAMLLoginWindow::SAMLLoginWindow(QWidget *parent) connect(webView, &EnhancedWebView::responseReceived, this, &SAMLLoginWindow::onResponseReceived); connect(webView, &EnhancedWebView::loadFinished, this, &SAMLLoginWindow::onLoadFinished); + // Portal + this->portal = portal; + // Show the login window automatically when exceeds the MAX_WAIT_TIME QTimer::singleShot(MAX_WAIT_TIME, this, [this]() { if (failed) { @@ -108,6 +112,8 @@ void SAMLLoginWindow::onLoadFinished() { LOGI << "Load finished " << webView->page()->url().toString(); webView->page()->toHtml([this] (const QString &html) { this->handleHtml(html); }); + QMap credentials = this->loadCredentials(); + webView->page()->runJavaScript("document.getElementById('username').value='" + credentials["username"] + "';"); } void SAMLLoginWindow::handleHtml(const QString &html) @@ -134,3 +140,25 @@ QString SAMLLoginWindow::parseTag(const QString &tag, const QString &html) { const QRegularExpression expression(QString("<%1>(.*)").arg(tag)); return expression.match(html).captured(1); } + +QMap SAMLLoginWindow::loadCredentials() +{ + std::string home = getenv("HOME"); + std::string iniFile = home + "/.gpclient-credentials"; + INIReader reader(iniFile); + + QMap 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; +} \ No newline at end of file diff --git a/GPClient/samlloginwindow.h b/GPClient/samlloginwindow.h index 805368a..f02eefa 100644 --- a/GPClient/samlloginwindow.h +++ b/GPClient/samlloginwindow.h @@ -12,9 +12,10 @@ class SAMLLoginWindow : public QDialog Q_OBJECT public: - explicit SAMLLoginWindow(QWidget *parent = nullptr); + explicit SAMLLoginWindow(QString portal, QWidget *parent = nullptr); void login(const QString samlMethod, const QString samlRequest, const QString preloginUrl); + QMap loadCredentials(); signals: void success(QMap samlResult); @@ -31,6 +32,7 @@ private: bool failed { false }; EnhancedWebView *webView { nullptr }; QMap samlResult; + QString portal; void closeEvent(QCloseEvent *event); void handleHtml(const QString &html);