Skip to content
Permalink
Browse files

Add signal for logging after network authentication details have been…

… added
  • Loading branch information
nyalldawson committed Jan 29, 2019
1 parent 8ee2e79 commit cc6d88e265caf7ee3d18a52fe762362a7a1743d3
@@ -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

@@ -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 );
}

@@ -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

/**
@@ -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();
}

0 comments on commit cc6d88e

Please sign in to comment.
You can’t perform that action at this time.