Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add signal for logging after network authentication details have been…
… added
  • Loading branch information
nyalldawson committed Feb 1, 2019
1 parent 8ee2e79 commit cc6d88e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
20 changes: 20 additions & 0 deletions python/core/auto_generated/qgsnetworkaccessmanager.sip.in
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsnetworkaccessmanager.cpp
Expand Up @@ -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 );
}

Expand Down
18 changes: 18 additions & 0 deletions src/core/qgsnetworkaccessmanager.h
Expand Up @@ -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

/**
Expand Down
28 changes: 23 additions & 5 deletions tests/src/core/testqgsnetworkaccessmanager.cpp
Expand Up @@ -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;
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down

0 comments on commit cc6d88e

Please sign in to comment.