From 42d8036576c340e9d4cca45361c448f184957aa6 Mon Sep 17 00:00:00 2001 From: Michal Klocek Date: Tue, 5 Feb 2019 13:54:04 +0100 Subject: Try using authentication from QNetworkProxy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use credentials for QNetworkProxy if set. Done-With: Allan Sandfeld Jensen Task-number: QTBUG-58121 Change-Id: I9c7328af90f3c60144c5ecf385529f663f0e46b2 Reviewed-by: Michael BrĂ¼ning --- src/core/login_delegate_qt.cpp | 19 +++++++++++++++++++ src/core/login_delegate_qt.h | 1 + src/core/web_engine_context.cpp | 13 +++++++++++++ src/core/web_engine_context.h | 3 +++ src/webengine/doc/src/qtwebengine-overview.qdoc | 14 +++++++++----- 5 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/core/login_delegate_qt.cpp b/src/core/login_delegate_qt.cpp index 5b407c094..0050f87c7 100644 --- a/src/core/login_delegate_qt.cpp +++ b/src/core/login_delegate_qt.cpp @@ -68,6 +68,7 @@ #include "resource_context_qt.h" #include "type_conversion.h" #include "web_contents_view_qt.h" +#include "web_engine_context.h" namespace QtWebEngineCore { @@ -118,6 +119,11 @@ QString LoginDelegateQt::host() const return QString::fromStdString(m_authInfo->challenger.host()); } +int LoginDelegateQt::port() const +{ + return m_authInfo->challenger.port(); +} + bool LoginDelegateQt::isProxy() const { return m_authInfo->is_proxy; @@ -126,6 +132,19 @@ bool LoginDelegateQt::isProxy() const void LoginDelegateQt::triggerDialogOnUI() { Q_ASSERT(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); + + if (isProxy()) { + // workaround for 'ws' redefined symbols when including QNetworkProxy + auto authentication = WebEngineContext::qProxyNetworkAuthentication(host(), port()); + if (std::get<0>(authentication)) { + base::PostTaskWithTraits( + FROM_HERE, { content::BrowserThread::IO }, + base::BindOnce(&LoginDelegateQt::sendAuthToRequester, this, true, + std::get<1>(authentication), std::get<2>(authentication))); + + return; + } + } content::WebContentsImpl *webContents = static_cast(m_webContentsGetter.Run()); if (!webContents) diff --git a/src/core/login_delegate_qt.h b/src/core/login_delegate_qt.h index ccd1f7322..3a9c073cd 100644 --- a/src/core/login_delegate_qt.h +++ b/src/core/login_delegate_qt.h @@ -74,6 +74,7 @@ public: QUrl url() const; QString realm() const; QString host() const; + int port() const; bool isProxy() const; void sendAuthToRequester(bool success, const QString &user, const QString &password); diff --git a/src/core/web_engine_context.cpp b/src/core/web_engine_context.cpp index 67639dc4f..adf98a3e1 100644 --- a/src/core/web_engine_context.cpp +++ b/src/core/web_engine_context.cpp @@ -114,6 +114,7 @@ #include #include #include +#include using namespace QtWebEngineCore; @@ -335,6 +336,18 @@ void WebEngineContext::destroyContextPostRoutine() m_destroyed = true; } +ProxyAuthentication WebEngineContext::qProxyNetworkAuthentication(QString host, int port) +{ + if (!QNetworkProxyFactory::usesSystemConfiguration()) { + QNetworkProxy proxy = QNetworkProxy::applicationProxy(); + if (host == proxy.hostName() && port == proxy.port() && !proxy.user().isEmpty() + && !proxy.password().isEmpty()) { + return std::make_tuple(true, proxy.user(), proxy.password()); + } + } + return std::make_tuple(false, QString(), QString()); +} + #ifndef CHROMIUM_VERSION #error Chromium version should be defined at gyp-time. Something must have gone wrong #define CHROMIUM_VERSION // This is solely to keep Qt Creator happy. diff --git a/src/core/web_engine_context.h b/src/core/web_engine_context.h index 4dc5251cc..2364bacbe 100644 --- a/src/core/web_engine_context.h +++ b/src/core/web_engine_context.h @@ -80,10 +80,13 @@ class ProfileAdapter; bool usingSoftwareDynamicGL(); +typedef std::tuple ProxyAuthentication; + class WebEngineContext : public base::RefCounted { public: static WebEngineContext *current(); static void destroyContextPostRoutine(); + static ProxyAuthentication qProxyNetworkAuthentication(QString host, int port); ProfileAdapter *createDefaultProfileAdapter(); ProfileAdapter *defaultProfileAdapter(); diff --git a/src/webengine/doc/src/qtwebengine-overview.qdoc b/src/webengine/doc/src/qtwebengine-overview.qdoc index bd5569e3f..4ac5108b1 100644 --- a/src/webengine/doc/src/qtwebengine-overview.qdoc +++ b/src/webengine/doc/src/qtwebengine-overview.qdoc @@ -228,14 +228,18 @@ are automatically retrieved from the system. Settings from an installed QNetworkProxyFactory will be ignored, though. - Not all properties of QNetworkProxy are supported by Qt WebEngine. That is, - QNetworkProxy::type(), QNetworkProxy::hostName() and QNetworkProxy::port() are taken into - account. All other proxy settings such as QNetworkProxy::rawHeader(), QNetworkProxy::user(), or - QNetworkProxy::password() are ignored. + In case QNetworkProxy::user() and QNetworkProxy::password() are set, these credentials + will be automatically used for proxy authentication. It is up to the user to provide valid + credentials, since there is no error handling callback. - If a proxy requires authentication, QWebEnginePage::proxyAuthenticationRequired is emitted. + If no credentials are set with QNetworkProxy, but the proxy requires authentication, + QWebEnginePage::proxyAuthenticationRequired is emitted. For Qt Quick, a dialog is shown. + Not all properties of QNetworkProxy are supported by Qt WebEngine. That is, + QNetworkProxy::type(), QNetworkProxy::hostName() and QNetworkProxy::port() are taken into + account. All other proxy settings such as QNetworkProxy::rawHeader() are ignored. + \section1 High DPI Support To support High DPI devices, it is recommended that the application attribute -- cgit v1.2.3