Skip to content

Commit a98df93

Browse files
committedApr 3, 2014
[BUGFIX] 9234 - WFS client doesn't expose Header Authorization
Like for the others OGC providers: WMS and WCS, the user can set the login and password to access to WFS protected data. But the QGIS WFS provider doen't expose header authorization. It doesn't use login and password. This patch set header authorization for all WFS connexion if login and password are set. And I think this patch has to be backported to release_2-2, because QGIS can't acces to protected WFS data.
1 parent a43bd62 commit a98df93

File tree

6 files changed

+79
-4
lines changed

6 files changed

+79
-4
lines changed
 

‎src/core/qgsgml.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ QgsGml::~QgsGml()
6565
{
6666
}
6767

68-
int QgsGml::getFeatures( const QString& uri, QGis::WkbType* wkbType, QgsRectangle* extent )
68+
int QgsGml::getFeatures( const QString& uri, QGis::WkbType* wkbType, QgsRectangle* extent, const QString& userName, const QString& password )
6969
{
7070
mUri = uri;
7171
mWkbType = wkbType;
@@ -79,6 +79,10 @@ int QgsGml::getFeatures( const QString& uri, QGis::WkbType* wkbType, QgsRectangl
7979
mExtent.setMinimal();
8080

8181
QNetworkRequest request( mUri );
82+
if ( !userName.isNull() || !password.isNull() )
83+
{
84+
request.setRawHeader( "Authorization", "Basic " + QString( "%1:%2" ).arg( userName ).arg( password ).toAscii().toBase64() );
85+
}
8286
QNetworkReply* reply = QgsNetworkAccessManager::instance()->get( request );
8387

8488
connect( reply, SIGNAL( finished() ), this, SLOT( setFinished() ) );

‎src/core/qgsgml.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class CORE_EXPORT QgsGml : public QObject
5656
* @param extent retrieved extents
5757
* @return 0 in case of success
5858
*/
59-
int getFeatures( const QString& uri, QGis::WkbType* wkbType, QgsRectangle* extent = 0 );
59+
int getFeatures( const QString& uri, QGis::WkbType* wkbType, QgsRectangle* extent = 0, const QString& userName = QString(), const QString& password = QString() );
6060

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

‎src/providers/wfs/qgswfscapabilities.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,34 @@ QString QgsWFSCapabilities::uriGetFeature( QString typeName, QString crsString,
134134

135135
//add a wfs layer to the map
136136
uri += "SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME=" + typeName + crsString + bBoxString + filterString;
137+
138+
//add authorization information
139+
if ( mUri.hasParam( "username" ) && mUri.hasParam( "password" ) )
140+
{
141+
uri += "&username="+mUri.param( "username" );
142+
uri += "&password="+mUri.param( "password" );
143+
}
137144
QgsDebugMsg( uri );
138145
return uri;
139146
}
140147

148+
void QgsWFSCapabilities::setAuthorization( QNetworkRequest &request ) const
149+
{
150+
QgsDebugMsg( "entered" );
151+
if ( mUri.hasParam( "username" ) && mUri.hasParam( "password" ) )
152+
{
153+
QgsDebugMsg( "setAuthorization " + mUri.param( "username" ) );
154+
request.setRawHeader( "Authorization", "Basic " + QString( "%1:%2" ).arg( mUri.param( "username" ) ).arg( mUri.param( "password" ) ).toAscii().toBase64() );
155+
}
156+
}
141157

142158
void QgsWFSCapabilities::requestCapabilities()
143159
{
144160
mErrorCode = QgsWFSCapabilities::NoError;
145161
mErrorMessage.clear();
146162

147163
QNetworkRequest request( uriGetCapabilities() );
164+
setAuthorization( request );
148165
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
149166
mCapabilitiesReply = QgsNetworkAccessManager::instance()->get( request );
150167
connect( mCapabilitiesReply, SIGNAL( finished() ), this, SLOT( capabilitiesReplyFinished() ) );
@@ -171,6 +188,7 @@ void QgsWFSCapabilities::capabilitiesReplyFinished()
171188
{
172189
QgsDebugMsg( "redirecting to " + redirect.toUrl().toString() );
173190
QNetworkRequest request( redirect.toUrl() );
191+
setAuthorization( request );
174192
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferNetwork );
175193
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
176194

‎src/providers/wfs/qgswfscapabilities.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define QGSWFSCAPABILITIES_H
1717

1818
#include <QObject>
19+
#include <QNetworkRequest>
1920

2021
#include "qgsrectangle.h"
2122
#include "qgsdatasourceuri.h"
@@ -72,6 +73,9 @@ class QgsWFSCapabilities : public QObject
7273
//! return parsed capabilities - requestCapabilities() must be called before
7374
GetCapabilities capabilities() { return mCaps; }
7475

76+
//! set authorization header
77+
void setAuthorization( QNetworkRequest &request ) const;
78+
7579
signals:
7680
void gotCapabilities();
7781

@@ -90,6 +94,12 @@ class QgsWFSCapabilities : public QObject
9094
GetCapabilities mCaps;
9195
ErrorCode mErrorCode;
9296
QString mErrorMessage;
97+
98+
//! Username for basic http authentication
99+
QString mUserName;
100+
101+
//! Password for basic http authentication
102+
QString mPassword;
93103
};
94104

95105
#endif // QGSWFSCAPABILITIES_H

‎src/providers/wfs/qgswfsprovider.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "qgscoordinatereferencesystem.h"
2828
#include "qgswfsfeatureiterator.h"
2929
#include "qgswfsprovider.h"
30+
#include "qgsdatasourceuri.h"
3031
#include "qgsspatialindex.h"
3132
#include "qgslogger.h"
3233
#include "qgsmessagelog.h"
@@ -89,6 +90,9 @@ QgsWFSProvider::QgsWFSProvider( const QString& uri )
8990
mSourceCRS.createFromOgcWmsCrs( srsname );
9091
}
9192

93+
mAuth.mUserName = parameterFromUrl( "username" );
94+
mAuth.mPassword = parameterFromUrl( "password" );
95+
9296
//fetch attributes of layer and type of its geometry attribute
9397
//WBC 111221: extracting geometry type here instead of getFeature allows successful
9498
//layer creation even when no features are retrieved (due to, e.g., BBOX or FILTER)
@@ -682,7 +686,10 @@ int QgsWFSProvider::getFeatureGET( const QString& uri, const QString& geometryAt
682686
}
683687

684688
//if ( dataReader.getWFSData() != 0 )
685-
if ( dataReader.getFeatures( uri, &mWKBType, &mExtent ) != 0 )
689+
QUrl getFeatureUrl( uri );
690+
getFeatureUrl.removeQueryItem( "username" );
691+
getFeatureUrl.removeQueryItem( "password" );
692+
if ( dataReader.getFeatures( getFeatureUrl.toString(), &mWKBType, &mExtent, mAuth.mUserName, mAuth.mPassword ) != 0 )
686693
{
687694
QgsDebugMsg( "getWFSData returned with error" );
688695
return 1;
@@ -751,10 +758,13 @@ int QgsWFSProvider::describeFeatureTypeGET( const QString& uri, QString& geometr
751758
mNetworkRequestFinished = false;
752759

753760
QUrl describeFeatureUrl( uri );
761+
describeFeatureUrl.removeQueryItem( "username" );
762+
describeFeatureUrl.removeQueryItem( "password" );
754763
describeFeatureUrl.removeQueryItem( "SRSNAME" );
755764
describeFeatureUrl.removeQueryItem( "REQUEST" );
756765
describeFeatureUrl.addQueryItem( "REQUEST", "DescribeFeatureType" );
757766
QNetworkRequest request( describeFeatureUrl.toString() );
767+
mAuth.setAuthorization( request );
758768
QNetworkReply* reply = QgsNetworkAccessManager::instance()->get( request );
759769

760770
connect( reply, SIGNAL( finished() ), this, SLOT( networkRequestFinished() ) );
@@ -1338,6 +1348,8 @@ bool QgsWFSProvider::sendTransactionDocument( const QDomDocument& doc, QDomDocum
13381348
mNetworkRequestFinished = false;
13391349

13401350
QUrl typeDetectionUri( dataSourceUri() );
1351+
typeDetectionUri.removeQueryItem( "username" );
1352+
typeDetectionUri.removeQueryItem( "password" );
13411353
typeDetectionUri.removeQueryItem( "REQUEST" );
13421354
typeDetectionUri.removeQueryItem( "TYPENAME" );
13431355
typeDetectionUri.removeQueryItem( "BBOX" );
@@ -1349,6 +1361,7 @@ bool QgsWFSProvider::sendTransactionDocument( const QDomDocument& doc, QDomDocum
13491361
QString serverUrl = typeDetectionUri.toString();
13501362

13511363
QNetworkRequest request( serverUrl );
1364+
mAuth.setAuthorization( request );
13521365
request.setHeader( QNetworkRequest::ContentTypeHeader, "text/xml" );
13531366
QNetworkReply* reply = QgsNetworkAccessManager::instance()->post( request, doc.toByteArray( -1 ) );
13541367

@@ -1475,7 +1488,11 @@ void QgsWFSProvider::getLayerCapabilities()
14751488

14761489
QString uri = dataSourceUri();
14771490
uri.replace( QString( "GetFeature" ), QString( "GetCapabilities" ) );
1478-
QNetworkRequest request( uri );
1491+
QUrl getCapabilitiesUrl( uri );
1492+
getCapabilitiesUrl.removeQueryItem( "username" );
1493+
getCapabilitiesUrl.removeQueryItem( "password" );
1494+
QNetworkRequest request( getCapabilitiesUrl.toString() );
1495+
mAuth.setAuthorization( request );
14791496
QNetworkReply* reply = QgsNetworkAccessManager::instance()->get( request );
14801497

14811498
connect( reply, SIGNAL( finished() ), this, SLOT( networkRequestFinished() ) );

‎src/providers/wfs/qgswfsprovider.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,32 @@
2727
#include "qgsvectorlayer.h"
2828
#include "qgswfsfeatureiterator.h"
2929

30+
#include <QNetworkRequest>
31+
3032
class QgsRectangle;
3133
class QgsSpatialIndex;
3234

35+
// TODO: merge with QgsWmsAuthorization?
36+
struct QgsWFSAuthorization
37+
{
38+
QgsWFSAuthorization( const QString& userName = QString(), const QString& password = QString() ) : mUserName( userName ), mPassword( password ) {}
39+
40+
//! set authorization header
41+
void setAuthorization( QNetworkRequest &request ) const
42+
{
43+
if ( !mUserName.isNull() || !mPassword.isNull() )
44+
{
45+
request.setRawHeader( "Authorization", "Basic " + QString( "%1:%2" ).arg( mUserName ).arg( mPassword ).toAscii().toBase64() );
46+
}
47+
}
48+
49+
//! Username for basic http authentication
50+
QString mUserName;
51+
52+
//! Password for basic http authentication
53+
QString mPassword;
54+
};
55+
3356
/**A provider reading features from a WFS server*/
3457
class QgsWFSProvider: public QgsVectorDataProvider
3558
{
@@ -130,6 +153,9 @@ class QgsWFSProvider: public QgsVectorDataProvider
130153
private:
131154
bool mNetworkRequestFinished;
132155
friend class QgsWFSFeatureSource;
156+
157+
//! http authorization details
158+
QgsWFSAuthorization mAuth;
133159

134160
protected:
135161
/**Thematic attributes*/

0 commit comments

Comments
 (0)
Please sign in to comment.