diff --git a/python/core/auto_generated/qgsnetworkaccessmanager.sip.in b/python/core/auto_generated/qgsnetworkaccessmanager.sip.in index c1a6d41fbf4..33f037e590b 100644 --- a/python/core/auto_generated/qgsnetworkaccessmanager.sip.in +++ b/python/core/auto_generated/qgsnetworkaccessmanager.sip.in @@ -284,6 +284,26 @@ from any thread. This signal is for debugging and logging purposes only, and cannot be used to respond to the requests. See QgsNetworkAuthenticationHandler for details on how to handle authentication requests. +.. seealso:: :py:func:`requestAuthDetailsAdded` + +.. versionadded:: 3.6 +%End + + void requestAuthDetailsAdded( int requestId, const QString &realm, const QString &user, const QString &password ); +%Docstring +Emitted when network authentication details have been added to a request. + +The ``requestId`` argument reflects the unique ID identifying the original request which the authentication relates to. + +This signal is always sent from the main thread QgsNetworkAccessManager instance, so it is necessary +only to connect to the main thread's signal in order to receive notifications about authentication requests +from any thread. + +This signal is for debugging and logging purposes only, and should not be used to respond to the +requests. See QgsNetworkAuthenticationHandler for details on how to handle authentication requests. + +.. seealso:: :py:func:`requestRequiresAuth` + .. versionadded:: 3.6 %End diff --git a/src/core/qgsnetworkaccessmanager.cpp b/src/core/qgsnetworkaccessmanager.cpp index c082e3874e2..4d3fecc3bfb 100644 --- a/src/core/qgsnetworkaccessmanager.cpp +++ b/src/core/qgsnetworkaccessmanager.cpp @@ -428,6 +428,9 @@ void QgsNetworkAccessManager::onAuthRequired( QNetworkReply *reply, QAuthenticat void QgsNetworkAccessManager::handleAuthRequest( QNetworkReply *reply, QAuthenticator *auth ) { mAuthHandler->handleAuthRequest( reply, auth ); + + emit requestAuthDetailsAdded( getRequestId( reply ), auth->realm(), auth->user(), auth->password() ); + afterAuthRequestHandled( reply ); } diff --git a/src/core/qgsnetworkaccessmanager.h b/src/core/qgsnetworkaccessmanager.h index 3805e6dcfa8..b71b729b9d2 100644 --- a/src/core/qgsnetworkaccessmanager.h +++ b/src/core/qgsnetworkaccessmanager.h @@ -426,10 +426,28 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager * This signal is for debugging and logging purposes only, and cannot be used to respond to the * requests. See QgsNetworkAuthenticationHandler for details on how to handle authentication requests. * + * \see requestAuthDetailsAdded() * \since QGIS 3.6 */ void requestRequiresAuth( int requestId, const QString &realm ); + /** + * Emitted when network authentication details have been added to a request. + * + * The \a requestId argument reflects the unique ID identifying the original request which the authentication relates to. + * + * This signal is always sent from the main thread QgsNetworkAccessManager instance, so it is necessary + * only to connect to the main thread's signal in order to receive notifications about authentication requests + * from any thread. + * + * This signal is for debugging and logging purposes only, and should not be used to respond to the + * requests. See QgsNetworkAuthenticationHandler for details on how to handle authentication requests. + * + * \see requestRequiresAuth() + * \since QGIS 3.6 + */ + void requestAuthDetailsAdded( int requestId, const QString &realm, const QString &user, const QString &password ); + #ifndef QT_NO_SSL /** diff --git a/tests/src/core/testqgsnetworkaccessmanager.cpp b/tests/src/core/testqgsnetworkaccessmanager.cpp index f444dbb3567..391b0c6c7ca 100644 --- a/tests/src/core/testqgsnetworkaccessmanager.cpp +++ b/tests/src/core/testqgsnetworkaccessmanager.cpp @@ -520,6 +520,9 @@ void TestQgsNetworkAccessManager::testAuthRequestHandler() bool loaded = false; bool gotRequestAboutToBeCreatedSignal = false; bool gotAuthRequest = false; + bool gotAuthDetailsAdded = false; + QString expectedUser; + QString expectedPassword; int requestId = -1; QUrl u = QUrl( QStringLiteral( "http://httpbin.org/basic-auth/me/secret" ) ); QNetworkReply::NetworkError expectedError = QNetworkReply::NoError; @@ -546,10 +549,19 @@ void TestQgsNetworkAccessManager::testAuthRequestHandler() gotAuthRequest = true; } ); + connect( QgsNetworkAccessManager::instance(), &QgsNetworkAccessManager::requestAuthDetailsAdded, &context, [&]( int authRequestId, const QString & realm, const QString & user, const QString & password ) + { + QCOMPARE( authRequestId, requestId ); + QCOMPARE( realm, QStringLiteral( "Fake Realm" ) ); + QCOMPARE( user, expectedUser ); + QCOMPARE( password, expectedPassword ); + gotAuthDetailsAdded = true; + } ); + expectedError = QNetworkReply::AuthenticationRequiredError; QgsNetworkAccessManager::instance()->get( QNetworkRequest( u ) ); - while ( !loaded || !gotAuthRequest || !gotRequestAboutToBeCreatedSignal ) + while ( !loaded || !gotAuthRequest || !gotRequestAboutToBeCreatedSignal || !gotAuthDetailsAdded ) { qApp->processEvents(); } @@ -560,13 +572,13 @@ void TestQgsNetworkAccessManager::testAuthRequestHandler() loaded = false; gotAuthRequest = false; gotRequestAboutToBeCreatedSignal = false; - + gotAuthDetailsAdded = false; BackgroundRequest *thread = new BackgroundRequest( QNetworkRequest( u ) ); thread->start(); - while ( !loaded || !gotAuthRequest || !gotRequestAboutToBeCreatedSignal ) + while ( !loaded || !gotAuthRequest || !gotRequestAboutToBeCreatedSignal || !gotAuthDetailsAdded ) { qApp->processEvents(); } @@ -580,10 +592,13 @@ void TestQgsNetworkAccessManager::testAuthRequestHandler() loaded = false; gotAuthRequest = false; gotRequestAboutToBeCreatedSignal = false; + gotAuthDetailsAdded = false; expectedError = QNetworkReply::NoError; + expectedUser = QStringLiteral( "me" ); + expectedPassword = QStringLiteral( "secret" ); QgsNetworkAccessManager::instance()->get( QNetworkRequest( u ) ); - while ( !loaded || !gotAuthRequest || !gotRequestAboutToBeCreatedSignal ) + while ( !loaded || !gotAuthRequest || !gotRequestAboutToBeCreatedSignal || !gotAuthDetailsAdded ) { qApp->processEvents(); } @@ -594,11 +609,14 @@ void TestQgsNetworkAccessManager::testAuthRequestHandler() loaded = false; gotAuthRequest = false; gotRequestAboutToBeCreatedSignal = false; + gotAuthDetailsAdded = false; expectedError = QNetworkReply::NoError; + expectedUser = QStringLiteral( "me2" ); + expectedPassword = QStringLiteral( "secret2" ); thread = new BackgroundRequest( QNetworkRequest( u ) ); thread->start(); - while ( !loaded || !gotAuthRequest || !gotRequestAboutToBeCreatedSignal ) + while ( !loaded || !gotAuthRequest || !gotRequestAboutToBeCreatedSignal || !gotAuthDetailsAdded ) { qApp->processEvents(); }