mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2025-04-02 18:31:50 -04:00
load credentials from ini file
portal is the section name in the ini file
This commit is contained in:
parent
705b03c0bb
commit
cd8d794655
@ -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)
|
||||
|
@ -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<QString, QString> &samlResult) {
|
||||
this->onSAMLLoginSuccess(samlResult);
|
||||
|
@ -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<QString, QString> samlResult) {
|
||||
this->onSAMLLoginSuccess(samlResult);
|
||||
|
@ -4,9 +4,10 @@
|
||||
#include <QWebEngineCookieStore>
|
||||
#include <plog/Log.h>
|
||||
|
||||
#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<QString, QString> 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>(.*)</%1>").arg(tag));
|
||||
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;
|
||||
}
|
@ -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<QString, QString> loadCredentials();
|
||||
|
||||
signals:
|
||||
void success(QMap<QString, QString> samlResult);
|
||||
@ -31,6 +32,7 @@ private:
|
||||
bool failed { false };
|
||||
EnhancedWebView *webView { nullptr };
|
||||
QMap<QString, QString> samlResult;
|
||||
QString portal;
|
||||
|
||||
void closeEvent(QCloseEvent *event);
|
||||
void handleHtml(const QString &html);
|
||||
|
Loading…
Reference in New Issue
Block a user