Skip to content

Commit 907b019

Browse files
committedSep 21, 2015
[auth system] Data source integration for OWS connections
1 parent 0d2b945 commit 907b019

22 files changed

+376
-132
lines changed
 

‎python/core/qgsgml.sip

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ class QgsGml: QObject
1919
* @param extent retrieved extents
2020
* @param userName username for authentication
2121
* @param password password for authentication
22+
* @param authcfg authentication configuration id
2223
* @return 0 in case of success
2324
*/
24-
int getFeatures( const QString& uri, QGis::WkbType* wkbType, QgsRectangle* extent = 0, const QString& userName = QString(), const QString& password = QString() ) /PyName=getFeaturesUri/;
25+
int getFeatures( const QString& uri, QGis::WkbType* wkbType, QgsRectangle* extent = 0, const QString& userName = QString(), const QString& password = QString(), const QString& authcfg = QString() ) /PyName=getFeaturesUri/;
2526

2627
/** Read from GML data. Constructor uri param is ignored
2728
* Supports only UTF-8, UTF-16, ISO-8859-1, ISO-8859-1 XML encodings.

‎src/core/qgsgml.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* *
1414
***************************************************************************/
1515
#include "qgsgml.h"
16+
#include "qgsauthmanager.h"
1617
#include "qgsrectangle.h"
1718
#include "qgscoordinatereferencesystem.h"
1819
#include "qgsgeometry.h"
@@ -69,7 +70,7 @@ QgsGml::~QgsGml()
6970
{
7071
}
7172

72-
int QgsGml::getFeatures( const QString& uri, QGis::WkbType* wkbType, QgsRectangle* extent, const QString& userName, const QString& password )
73+
int QgsGml::getFeatures( const QString& uri, QGis::WkbType* wkbType, QgsRectangle* extent, const QString& userName, const QString& password , const QString& authcfg )
7374
{
7475
mUri = uri;
7576
mWkbType = wkbType;
@@ -83,7 +84,19 @@ int QgsGml::getFeatures( const QString& uri, QGis::WkbType* wkbType, QgsRectangl
8384
mExtent.setMinimal();
8485

8586
QNetworkRequest request( mUri );
86-
if ( !userName.isNull() || !password.isNull() )
87+
if ( !authcfg.isEmpty() )
88+
{
89+
if ( !QgsAuthManager::instance()->updateNetworkRequest( request, authcfg ) )
90+
{
91+
QgsMessageLog::logMessage(
92+
tr( "GML Getfeature network request update failed for authcfg %1" ).arg( authcfg ),
93+
tr( "Network" ),
94+
QgsMessageLog::CRITICAL
95+
);
96+
return 1;
97+
}
98+
}
99+
else if ( !userName.isNull() || !password.isNull() )
87100
{
88101
request.setRawHeader( "Authorization", "Basic " + QString( "%1:%2" ).arg( userName ).arg( password ).toAscii().toBase64() );
89102
}

‎src/core/qgsgml.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,15 @@ class CORE_EXPORT QgsGml : public QObject
5656
* @param extent retrieved extents
5757
* @param userName username for authentication
5858
* @param password password for authentication
59+
* @param authcfg authentication configuration id
5960
* @return 0 in case of success
6061
*/
61-
int getFeatures( const QString& uri, QGis::WkbType* wkbType, QgsRectangle* extent = 0, const QString& userName = QString(), const QString& password = QString() );
62+
int getFeatures( const QString& uri,
63+
QGis::WkbType* wkbType,
64+
QgsRectangle* extent = 0,
65+
const QString& userName = QString(),
66+
const QString& password = QString(),
67+
const QString& authcfg = QString() );
6268

6369
/** Read from GML data. Constructor uri param is ignored
6470
* Supports only UTF-8, UTF-16, ISO-8859-1, ISO-8859-1 XML encodings.

‎src/core/qgsowsconnection.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ QgsOWSConnection::QgsOWSConnection( const QString & theService, const QString &
6161
mUri.setParam( "password", password );
6262
}
6363

64+
QString authcfg = settings.value( credentialsKey + "/authcfg" ).toString();
65+
if ( !authcfg.isEmpty() )
66+
{
67+
mUri.setParam( "authcfg", authcfg );
68+
}
69+
mConnectionInfo.append( ",authcfg=" + authcfg );
70+
6471
bool ignoreGetMap = settings.value( key + "/ignoreGetMapURI", false ).toBool();
6572
bool ignoreGetFeatureInfo = settings.value( key + "/ignoreGetFeatureInfoURI", false ).toBool();
6673
bool ignoreAxisOrientation = settings.value( key + "/ignoreAxisOrientation", false ).toBool();

‎src/gui/qgsnewhttpconnection.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
#include <QRegExpValidator>
2424

2525
QgsNewHttpConnection::QgsNewHttpConnection(
26-
QWidget *parent, const QString& baseKey, const QString& connName, Qt::WindowFlags fl ):
27-
QDialog( parent, fl ),
28-
mBaseKey( baseKey ),
29-
mOriginalConnName( connName )
26+
QWidget *parent, const QString& baseKey, const QString& connName, Qt::WindowFlags fl )
27+
: QDialog( parent, fl )
28+
, mBaseKey( baseKey )
29+
, mOriginalConnName( connName )
30+
, mAuthConfigSelect( 0 )
3031
{
3132
setupUi( this );
3233

@@ -49,6 +50,9 @@ QgsNewHttpConnection::QgsNewHttpConnection(
4950
cmbDpiMode->addItem( tr( "UMN" ) );
5051
cmbDpiMode->addItem( tr( "GeoServer" ) );
5152

53+
mAuthConfigSelect = new QgsAuthConfigSelect( this );
54+
tabAuth->insertTab( 1, mAuthConfigSelect, tr( "Configurations" ) );
55+
5256
if ( !connName.isEmpty() )
5357
{
5458
// populate the dialog with the information stored for the connection
@@ -92,6 +96,13 @@ QgsNewHttpConnection::QgsNewHttpConnection(
9296

9397
txtUserName->setText( settings.value( credentialsKey + "/username" ).toString() );
9498
txtPassword->setText( settings.value( credentialsKey + "/password" ).toString() );
99+
100+
QString authcfg = settings.value( credentialsKey + "/authcfg" ).toString();
101+
mAuthConfigSelect->setConfigId( authcfg );
102+
if ( !authcfg.isEmpty() )
103+
{
104+
tabAuth->setCurrentIndex( tabAuth->indexOf( mAuthConfigSelect ) );
105+
}
95106
}
96107

97108
if ( mBaseKey != "/Qgis/connections-wms/" )
@@ -247,6 +258,8 @@ void QgsNewHttpConnection::accept()
247258
settings.setValue( credentialsKey + "/username", txtUserName->text() );
248259
settings.setValue( credentialsKey + "/password", txtPassword->text() );
249260

261+
settings.setValue( credentialsKey + "/authcfg", mAuthConfigSelect->configId() );
262+
250263
settings.setValue( mBaseKey + "/selected", txtName->text() );
251264

252265
QDialog::accept();

‎src/gui/qgsnewhttpconnection.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "ui_qgsnewhttpconnectionbase.h"
2020
#include "qgisgui.h"
2121
#include "qgscontexthelp.h"
22+
#include "qgsauthconfigselect.h"
23+
2224
/*!
2325
* \brief Dialog to allow the user to configure and save connection
2426
* information for an HTTP Server for WMS, etc.
@@ -46,6 +48,7 @@ class GUI_EXPORT QgsNewHttpConnection : public QDialog, private Ui::QgsNewHttpCo
4648
QString mBaseKey;
4749
QString mCredentialsBaseKey;
4850
QString mOriginalConnName; //store initial name to delete entry in case of rename
51+
QgsAuthConfigSelect * mAuthConfigSelect;
4952
};
5053

5154
#endif // QGSNEWHTTPCONNECTION_H

‎src/providers/ows/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ SET(OWS_MOC_HDRS
99

1010
INCLUDE_DIRECTORIES (
1111
../../core
12+
../../core/auth
1213
../../gui
14+
../../gui/auth
1315
${CMAKE_CURRENT_BINARY_DIR}/../../ui
1416
)
1517

‎src/providers/wcs/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,17 @@ SET (WCS_MOC_HDRS
1616

1717
QT4_WRAP_CPP (WCS_MOC_SRCS ${WCS_MOC_HDRS})
1818

19-
INCLUDE_DIRECTORIES( . ../../core ../../core/raster ../../gui
19+
INCLUDE_DIRECTORIES(
20+
.
21+
../../core
22+
../../core/auth
23+
../../core/raster
24+
../../gui
25+
../../gui/auth
2026
../gdal
2127
${CMAKE_CURRENT_BINARY_DIR}/../../ui
2228
${GDAL_INCLUDE_DIR}
29+
${QCA_INCLUDE_DIR}
2330
)
2431

2532
ADD_LIBRARY(wcsprovider MODULE ${WCS_SRCS} ${WCS_MOC_SRCS})

‎src/providers/wcs/qgswcscapabilities.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <cmath>
2828

29+
#include "qgsauthmanager.h"
2930
#include "qgscoordinatetransform.h"
3031
#include "qgsdatasourceuri.h"
3132
#include "qgsrasterlayer.h"
@@ -159,7 +160,12 @@ bool QgsWcsCapabilities::sendRequest( QString const & url )
159160
QgsDebugMsg( "url = " + url );
160161
mError = "";
161162
QNetworkRequest request( url );
162-
setAuthorization( request );
163+
if ( !setAuthorization( request ) )
164+
{
165+
mError = tr( "Download of capabilities failed: network request update failed for authentication config" );
166+
QgsMessageLog::logMessage( mError, tr( "WCS" ) );
167+
return false;
168+
}
163169
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
164170
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, mCacheLoadControl );
165171
QgsDebugMsg( QString( "mCacheLoadControl = %1" ).arg( mCacheLoadControl ) );
@@ -359,7 +365,13 @@ void QgsWcsCapabilities::capabilitiesReplyFinished()
359365
emit statusChanged( tr( "Capabilities request redirected." ) );
360366

361367
QNetworkRequest request( redirect.toUrl() );
362-
setAuthorization( request );
368+
if ( !setAuthorization( request ) )
369+
{
370+
mCapabilitiesResponse.clear();
371+
mError = tr( "Download of capabilities failed: network request update failed for authentication config" );
372+
QgsMessageLog::logMessage( mError, tr( "WCS" ) );
373+
return;
374+
}
363375
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferNetwork );
364376
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
365377

@@ -1162,14 +1174,19 @@ QString QgsWcsCapabilities::lastErrorFormat()
11621174
return mErrorFormat;
11631175
}
11641176

1165-
void QgsWcsCapabilities::setAuthorization( QNetworkRequest &request ) const
1177+
bool QgsWcsCapabilities::setAuthorization( QNetworkRequest &request ) const
11661178
{
11671179
QgsDebugMsg( "entered" );
1168-
if ( mUri.hasParam( "username" ) && mUri.hasParam( "password" ) )
1180+
if ( mUri.hasParam( "authcfg" ) && !mUri.param( "authcfg" ).isEmpty() )
1181+
{
1182+
return QgsAuthManager::instance()->updateNetworkRequest( request, mUri.param( "authcfg" ) );
1183+
}
1184+
else if ( mUri.hasParam( "username" ) && mUri.hasParam( "password" ) )
11691185
{
11701186
QgsDebugMsg( "setAuthorization " + mUri.param( "username" ) );
11711187
request.setRawHeader( "Authorization", "Basic " + QString( "%1:%2" ).arg( mUri.param( "username" ) ).arg( mUri.param( "password" ) ).toAscii().toBase64() );
11721188
}
1189+
return true;
11731190
}
11741191

11751192
void QgsWcsCapabilities::showMessageBox( const QString& title, const QString& text )

‎src/providers/wcs/qgswcscapabilities.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class QgsWcsCapabilities : public QObject
153153
bool parseDescribeCoverageDom11( QByteArray const &xml, QgsWcsCoverageSummary *coverage );
154154

155155
//! set authorization header
156-
void setAuthorization( QNetworkRequest &request ) const;
156+
bool setAuthorization( QNetworkRequest &request ) const;
157157

158158
QString version() const { return mCapabilities.version; }
159159

‎src/providers/wcs/qgswcsprovider.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,12 @@ bool QgsWcsProvider::parseUri( QString uriString )
408408
mAuth.mPassword = uri.param( "password" );
409409
QgsDebugMsg( "set password to " + mAuth.mPassword );
410410

411+
if ( uri.hasParam( "authcfg" ) )
412+
{
413+
mAuth.mAuthCfg = uri.param( "authcfg" );
414+
}
415+
QgsDebugMsg( "set authcfg to " + mAuth.mAuthCfg );
416+
411417
mIdentifier = uri.param( "identifier" );
412418

413419
mTime = uri.param( "time" );
@@ -1667,6 +1673,7 @@ int QgsWcsDownloadHandler::sErrors = 0;
16671673

16681674
QgsWcsDownloadHandler::QgsWcsDownloadHandler( const QUrl& url, QgsWcsAuthorization& auth, QNetworkRequest::CacheLoadControl cacheLoadControl, QByteArray& cachedData, const QString& wcsVersion, QgsError& cachedError )
16691675
: mNAM( new QgsNetworkAccessManager )
1676+
, mAuth( auth )
16701677
, mEventLoop( new QEventLoop )
16711678
, mCachedData( cachedData )
16721679
, mWcsVersion( wcsVersion )
@@ -1675,7 +1682,12 @@ QgsWcsDownloadHandler::QgsWcsDownloadHandler( const QUrl& url, QgsWcsAuthorizati
16751682
mNAM->setupDefaultProxyAndCache();
16761683

16771684
QNetworkRequest request( url );
1678-
auth.setAuthorization( request );
1685+
if ( !mAuth.setAuthorization( request ) )
1686+
{
1687+
QgsMessageLog::logMessage( tr( "Network request update failed for authentication config" ),
1688+
tr( "WCS" ) );
1689+
return;
1690+
}
16791691
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
16801692
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, cacheLoadControl );
16811693

@@ -1708,7 +1720,14 @@ void QgsWcsDownloadHandler::cacheReplyFinished()
17081720
mCacheReply->deleteLater();
17091721

17101722
QgsDebugMsg( QString( "redirected getmap: %1" ).arg( redirect.toString() ) );
1711-
mCacheReply = mNAM->get( QNetworkRequest( redirect.toUrl() ) );
1723+
QNetworkRequest request( redirect.toUrl() );
1724+
if ( !mAuth.setAuthorization( request ) )
1725+
{
1726+
QgsMessageLog::logMessage( tr( "Network request update failed for authentication config" ),
1727+
tr( "WCS" ) );
1728+
return;
1729+
}
1730+
mCacheReply = mNAM->get( request );
17121731
connect( mCacheReply, SIGNAL( finished() ), this, SLOT( cacheReplyFinished() ) );
17131732
connect( mCacheReply, SIGNAL( downloadProgress( qint64, qint64 ) ), this, SLOT( cacheReplyProgress( qint64, qint64 ) ) );
17141733

‎src/providers/wcs/qgswcsprovider.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "qgserror.h"
2626
#include "qgswcscapabilities.h"
27+
#include "qgsauthmanager.h"
2728
#include "qgsrasterdataprovider.h"
2829
#include "qgsgdalproviderbase.h"
2930
#include "qgsrectangle.h"
@@ -50,22 +51,34 @@ class QNetworkRequest;
5051
// TODO: merge with QgsWmsAuthorization?
5152
struct QgsWcsAuthorization
5253
{
53-
QgsWcsAuthorization( const QString& userName = QString(), const QString& password = QString() ) : mUserName( userName ), mPassword( password ) {}
54+
QgsWcsAuthorization( const QString& userName = QString(), const QString& password = QString(), const QString& authcfg = QString() )
55+
: mUserName( userName )
56+
, mPassword( password )
57+
, mAuthCfg( authcfg )
58+
{}
5459

5560
//! set authorization header
56-
void setAuthorization( QNetworkRequest &request ) const
61+
bool setAuthorization( QNetworkRequest &request ) const
5762
{
58-
if ( !mUserName.isNull() || !mPassword.isNull() )
63+
if ( !mAuthCfg.isEmpty() )
64+
{
65+
return QgsAuthManager::instance()->updateNetworkRequest( request, mAuthCfg );
66+
}
67+
else if ( !mUserName.isNull() || !mPassword.isNull() )
5968
{
6069
request.setRawHeader( "Authorization", "Basic " + QString( "%1:%2" ).arg( mUserName ).arg( mPassword ).toAscii().toBase64() );
6170
}
71+
return true;
6272
}
6373

6474
//! Username for basic http authentication
6575
QString mUserName;
6676

6777
//! Password for basic http authentication
6878
QString mPassword;
79+
80+
//! Authentication configuration ID
81+
QString mAuthCfg;
6982
};
7083

7184
/**
@@ -424,6 +437,7 @@ class QgsWcsDownloadHandler : public QObject
424437
void finish() { QMetaObject::invokeMethod( mEventLoop, "quit", Qt::QueuedConnection ); }
425438

426439
QgsNetworkAccessManager* mNAM;
440+
QgsWcsAuthorization& mAuth;
427441
QEventLoop* mEventLoop;
428442

429443
QNetworkReply* mCacheReply;

‎src/providers/wfs/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ QT4_WRAP_CPP(WFS_MOC_SRCS ${WFS_MOC_HDRS})
2525

2626
INCLUDE_DIRECTORIES (
2727
../../core
28+
../../core/auth
2829
../../core/geometry
2930
../../gui
31+
../../gui/auth
3032
${CMAKE_CURRENT_BINARY_DIR}/../../ui
3133
${GEOS_INCLUDE_DIR}
3234
${GEOS_INCLUDE_DIR}/geos
3335
${EXPAT_INCLUDE_DIR}
3436
${QSCINTILLA_INCLUDE_DIR}
37+
${QCA_INCLUDE_DIR}
3538
)
3639

3740
ADD_LIBRARY (wfsprovider MODULE ${WFS_SRCS} ${WFS_MOC_SRCS})

0 commit comments

Comments
 (0)
Please sign in to comment.