Skip to content

Commit 0d360d4

Browse files
committedJun 22, 2014
fix interactive network authentication
1 parent 91b1435 commit 0d360d4

File tree

7 files changed

+106
-27
lines changed

7 files changed

+106
-27
lines changed
 

‎python/core/qgscredentials.sip

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,19 @@ class QgsCredentials
2121
* @note added in 2.4
2222
*/
2323
void lock();
24+
2425
/**
2526
* Unlock the instance after being locked.
2627
* @note added in 2.4
2728
*/
2829
void unlock();
2930

31+
/**
32+
* Return pointer to mutex
33+
* @note added in 2.4
34+
*/
35+
QMutex *mutex();
36+
3037
protected:
3138
QgsCredentials();
3239

‎src/app/qgisapp.cpp

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9761,7 +9761,8 @@ void QgisApp::namSetup()
97619761
connect( nam, SIGNAL( proxyAuthenticationRequired( const QNetworkProxy &, QAuthenticator * ) ),
97629762
this, SLOT( namProxyAuthenticationRequired( const QNetworkProxy &, QAuthenticator * ) ) );
97639763

9764-
connect( nam, SIGNAL( requestTimedOut( QNetworkReply* ) ), this, SLOT( namRequestTimedOut( QNetworkReply* ) ) );
9764+
connect( nam, SIGNAL( requestTimedOut( QNetworkReply* ) ),
9765+
this, SLOT( namRequestTimedOut( QNetworkReply* ) ) );
97659766

97669767
#ifndef QT_NO_OPENSSL
97679768
connect( nam, SIGNAL( sslErrors( QNetworkReply *, const QList<QSslError> & ) ),
@@ -9774,15 +9775,41 @@ void QgisApp::namAuthenticationRequired( QNetworkReply *reply, QAuthenticator *a
97749775
QString username = auth->user();
97759776
QString password = auth->password();
97769777

9777-
bool ok = QgsCredentials::instance()->get(
9778-
QString( "%1 at %2" ).arg( auth->realm() ).arg( reply->url().host() ),
9779-
username, password,
9780-
tr( "Authentication required" ) );
9781-
if ( !ok )
9782-
return;
9778+
QMutexLocker lock( QgsCredentials::instance()->mutex() );
97839779

9784-
if ( reply->isFinished() )
9785-
return;
9780+
do
9781+
{
9782+
bool ok = QgsCredentials::instance()->get(
9783+
QString( "%1 at %2" ).arg( auth->realm() ).arg( reply->url().host() ),
9784+
username, password,
9785+
tr( "Authentication required" ) );
9786+
if ( !ok )
9787+
return;
9788+
9789+
if ( reply->isFinished() )
9790+
return;
9791+
9792+
if ( auth->user() == username && password == auth->password() )
9793+
{
9794+
if ( !password.isNull() )
9795+
{
9796+
// credentials didn't change - stored ones probably wrong? clear password and retry
9797+
QgsCredentials::instance()->put(
9798+
QString( "%1 at %2" ).arg( auth->realm() ).arg( reply->url().host() ),
9799+
username, QString::null );
9800+
continue;
9801+
}
9802+
}
9803+
else
9804+
{
9805+
// save credentials
9806+
QgsCredentials::instance()->put(
9807+
QString( "%1 at %2" ).arg( auth->realm() ).arg( reply->url().host() ),
9808+
username, password
9809+
);
9810+
}
9811+
}
9812+
while ( 0 );
97869813

97879814
auth->setUser( username );
97889815
auth->setPassword( password );
@@ -9801,12 +9828,37 @@ void QgisApp::namProxyAuthenticationRequired( const QNetworkProxy &proxy, QAuthe
98019828
QString username = auth->user();
98029829
QString password = auth->password();
98039830

9804-
bool ok = QgsCredentials::instance()->get(
9805-
QString( "proxy %1:%2 [%3]" ).arg( proxy.hostName() ).arg( proxy.port() ).arg( auth->realm() ),
9806-
username, password,
9807-
tr( "Proxy authentication required" ) );
9808-
if ( !ok )
9809-
return;
9831+
QMutexLocker lock( QgsCredentials::instance()->mutex() );
9832+
9833+
do
9834+
{
9835+
bool ok = QgsCredentials::instance()->get(
9836+
QString( "proxy %1:%2 [%3]" ).arg( proxy.hostName() ).arg( proxy.port() ).arg( auth->realm() ),
9837+
username, password,
9838+
tr( "Proxy authentication required" ) );
9839+
if ( !ok )
9840+
return;
9841+
9842+
if ( auth->user() == username && password == auth->password() )
9843+
{
9844+
if ( !password.isNull() )
9845+
{
9846+
// credentials didn't change - stored ones probably wrong? clear password and retry
9847+
QgsCredentials::instance()->put(
9848+
QString( "proxy %1:%2 [%3]" ).arg( proxy.hostName() ).arg( proxy.port() ).arg( auth->realm() ),
9849+
username, QString::null );
9850+
continue;
9851+
}
9852+
}
9853+
else
9854+
{
9855+
QgsCredentials::instance()->put(
9856+
QString( "proxy %1:%2 [%3]" ).arg( proxy.hostName() ).arg( proxy.port() ).arg( auth->realm() ),
9857+
username, password
9858+
);
9859+
}
9860+
}
9861+
while ( 0 );
98109862

98119863
auth->setUser( username );
98129864
auth->setPassword( password );
@@ -9850,8 +9902,11 @@ void QgisApp::namSslErrors( QNetworkReply *reply, const QList<QSslError> &errors
98509902

98519903
void QgisApp::namRequestTimedOut( QNetworkReply *reply )
98529904
{
9853-
QgsMessageLog::logMessage( tr( "The request '%1' timed out. Any data received is likely incomplete." ).arg( reply->url().toString() ), QString::null, QgsMessageLog::WARNING );
9854-
messageBar()->pushMessage( tr( "Network request timeout" ), tr( "A network request timed out, any data received is likely incomplete." ), QgsMessageBar::WARNING, messageTimeout() );
9905+
QLabel *msgLabel = new QLabel( tr( "A network request timed out, any data received is likely incomplete." ) +
9906+
tr( " Please check the <a href=\"#messageLog\">message log</a> for further info." ), messageBar() );
9907+
msgLabel->setWordWrap( true );
9908+
connect( msgLabel, SIGNAL( linkActivated( QString ) ), mLogDock, SLOT( show() ) );
9909+
messageBar()->pushItem( new QgsMessageBarItem( msgLabel, QgsMessageBar::WARNING, messageTimeout() ) );
98559910
}
98569911

98579912
void QgisApp::namUpdate()

‎src/app/qgsidentifyresultsdialog.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,14 @@ void QgsIdentifyResultsDialog::addFeature( QgsRasterLayer *layer,
738738
{
739739
QgsIdentifyResultsWebViewItem *attrItem = new QgsIdentifyResultsWebViewItem( lstResults );
740740
featItem->addChild( attrItem ); // before setHtml()!
741-
attrItem->setContent( attributes.begin().value().toUtf8(), currentFormat == QgsRaster::IdentifyFormatHtml ? "text/html" : "text/plain" );
741+
if ( !attributes.isEmpty() )
742+
{
743+
attrItem->setContent( attributes.begin().value().toUtf8(), currentFormat == QgsRaster::IdentifyFormatHtml ? "text/html" : "text/plain" );
744+
}
745+
else
746+
{
747+
attrItem->setContent( tr( "No attributes." ).toUtf8(), "text/plain" );
748+
}
742749
}
743750
else
744751
{

‎src/core/qgscredentials.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ bool QgsCredentials::get( QString realm, QString &username, QString &password, Q
5454
username = credentials.first;
5555
password = credentials.second;
5656
QgsDebugMsg( QString( "retrieved realm:%1 username:%2 password:%3" ).arg( realm ).arg( username ).arg( password ) );
57-
return true;
57+
58+
if ( !password.isNull() )
59+
return true;
5860
}
59-
else if ( request( realm, username, password, message ) )
61+
62+
if ( request( realm, username, password, message ) )
6063
{
6164
QgsDebugMsg( QString( "requested realm:%1 username:%2 password:%3" ).arg( realm ).arg( username ).arg( password ) );
6265
return true;
@@ -74,7 +77,6 @@ void QgsCredentials::put( QString realm, QString username, QString password )
7477
mCredentialCache.insert( realm, QPair<QString, QString>( username, password ) );
7578
}
7679

77-
7880
void QgsCredentials::lock()
7981
{
8082
mMutex.lock();

‎src/core/qgscredentials.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,19 @@ class CORE_EXPORT QgsCredentials
5353
* @note added in 2.4
5454
*/
5555
void lock();
56+
5657
/**
5758
* Unlock the instance after being locked.
5859
* @note added in 2.4
5960
*/
6061
void unlock();
6162

63+
/**
64+
* Return pointer to mutex
65+
* @note added in 2.4
66+
*/
67+
QMutex *mutex() { return &mMutex; }
68+
6269
protected:
6370
QgsCredentials();
6471

‎src/core/qgsnetworkaccessmanager.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,22 +246,23 @@ void QgsNetworkAccessManager::setupDefaultProxyAndCache()
246246

247247
if ( this != instance() )
248248
{
249+
Qt::ConnectionType connectionType = thread() == instance()->thread() ? Qt::AutoConnection : Qt::BlockingQueuedConnection;
250+
249251
connect( this, SIGNAL( authenticationRequired( QNetworkReply *, QAuthenticator * ) ),
250252
instance(), SIGNAL( authenticationRequired( QNetworkReply *, QAuthenticator * ) ),
251-
Qt::BlockingQueuedConnection );
253+
connectionType );
252254

253255
connect( this, SIGNAL( proxyAuthenticationRequired( const QNetworkProxy &, QAuthenticator * ) ),
254256
instance(), SIGNAL( proxyAuthenticationRequired( const QNetworkProxy &, QAuthenticator * ) ),
255-
Qt::BlockingQueuedConnection );
257+
connectionType );
256258

257259
connect( this, SIGNAL( requestTimedOut( QNetworkReply* ) ),
258-
instance(), SIGNAL( requestTimedOut( QNetworkReply* ) ),
259-
Qt::BlockingQueuedConnection );
260+
instance(), SIGNAL( requestTimedOut( QNetworkReply* ) ) );
260261

261262
#ifndef QT_NO_OPENSSL
262263
connect( this, SIGNAL( sslErrors( QNetworkReply *, const QList<QSslError> & ) ),
263264
instance(), SIGNAL( sslErrors( QNetworkReply *, const QList<QSslError> & ) ),
264-
Qt::BlockingQueuedConnection );
265+
connectionType );
265266
#endif
266267
}
267268

‎src/providers/wms/qgswmsprovider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ void QgsWmsProvider::setFormatQueryItem( QUrl &url )
439439
setQueryItem( url, "FORMAT", mSettings.mImageMimeType );
440440
}
441441

442-
QImage *QgsWmsProvider::draw( QgsRectangle const &viewExtent, int pixelWidth, int pixelHeight )
442+
QImage *QgsWmsProvider::draw( QgsRectangle const &viewExtent, int pixelWidth, int pixelHeight )
443443
{
444444
QgsDebugMsg( "Entering." );
445445

0 commit comments

Comments
 (0)
Please sign in to comment.