Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[WFS provider] Fix auth config extra expansion and auth prioritization
  • Loading branch information
dakcarto committed Oct 13, 2016
1 parent 35f1749 commit 37b00eb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
36 changes: 23 additions & 13 deletions src/providers/wfs/qgswfsdatasourceuri.cpp
Expand Up @@ -41,14 +41,19 @@ QgsWFSDataSourceURI::QgsWFSDataSourceURI( const QString& uri )
QString typeName = url.queryItemValue( QgsWFSConstants::URI_PARAM_TYPENAME );
QString version = url.queryItemValue( QgsWFSConstants::URI_PARAM_VERSION );
QString filter = url.queryItemValue( QgsWFSConstants::URI_PARAM_FILTER );
mAuth.mUserName = url.queryItemValue( QgsWFSConstants::URI_PARAM_USERNAME );
// In QgsDataSourceURI, the "username" param is named "user", check it
if ( mAuth.mUserName.isEmpty() )
mAuth.mAuthCfg = url.queryItemValue( QgsWFSConstants::URI_PARAM_AUTHCFG );
// NOTE: A defined authcfg overrides any older username/password auth
// Only check for older auth if it is undefined
if ( mAuth.mAuthCfg.isEmpty() )
{
mAuth.mUserName = url.queryItemValue( QgsWFSConstants::URI_PARAM_USER );
mAuth.mUserName = url.queryItemValue( QgsWFSConstants::URI_PARAM_USERNAME );
// In QgsDataSourceURI, the "username" param is named "user", check it
if ( mAuth.mUserName.isEmpty() )
{
mAuth.mUserName = url.queryItemValue( QgsWFSConstants::URI_PARAM_USER );
}
mAuth.mPassword = url.queryItemValue( QgsWFSConstants::URI_PARAM_PASSWORD );
}
mAuth.mPassword = url.queryItemValue( QgsWFSConstants::URI_PARAM_PASSWORD );
mAuth.mAuthCfg = url.queryItemValue( QgsWFSConstants::URI_PARAM_AUTHCFG );

// Now remove all stuff that is not the core URL
url.removeQueryItem( "SERVICE" );
Expand Down Expand Up @@ -90,19 +95,24 @@ QgsWFSDataSourceURI::QgsWFSDataSourceURI( const QString& uri )
const QString QgsWFSDataSourceURI::uri( bool expandAuthConfig ) const
{
QgsDataSourceUri theURI( mURI );
// Add auth params back into the uri
// Add authcfg param back into the uri (must be non-empty value)
if ( ! mAuth.mAuthCfg.isEmpty() )
{
theURI.setAuthConfigId( mAuth.mAuthCfg );
}
if ( ! mAuth.mUserName.isEmpty() )
{
theURI.setUsername( mAuth.mUserName );
}
if ( ! mAuth.mPassword.isEmpty() )
else
{
theURI.setPassword( mAuth.mPassword );
// Add any older username/password auth params back in (allow empty values)
if ( ! mAuth.mUserName.isNull() )
{
theURI.setUsername( mAuth.mUserName );
}
if ( ! mAuth.mPassword.isNull() )
{
theURI.setPassword( mAuth.mPassword );
}
}
// NOTE: avoid expanding authcfg here; it is handled during network access
return theURI.uri( expandAuthConfig );
}

Expand Down
8 changes: 4 additions & 4 deletions src/providers/wfs/qgswfsdatasourceuri.h
Expand Up @@ -35,11 +35,11 @@ struct QgsWFSAuthorization
//! set authorization header
bool setAuthorization( QNetworkRequest &request ) const
{
if ( !mAuthCfg.isEmpty() )
if ( !mAuthCfg.isEmpty() ) // must be non-empty value
{
return QgsAuthManager::instance()->updateNetworkRequest( request, mAuthCfg );
}
else if ( !mUserName.isNull() || !mPassword.isNull() )
else if ( !mUserName.isNull() || !mPassword.isNull() ) // allow empty values
{
request.setRawHeader( "Authorization", "Basic " + QString( "%1:%2" ).arg( mUserName, mPassword ).toLatin1().toBase64() );
}
Expand All @@ -65,8 +65,8 @@ class QgsWFSDataSourceURI

explicit QgsWFSDataSourceURI( const QString& uri );

/** Return the URI */
const QString uri( bool expandAuthConfig = true ) const;
/** Return the URI, avoiding expansion of authentication configuration, which is handled during network access */
const QString uri( bool expandAuthConfig = false ) const;

/** Return base URL (with SERVICE=WFS parameter if bIncludeServiceWFS=true) */
QUrl baseURL( bool bIncludeServiceWFS = true ) const;
Expand Down
6 changes: 3 additions & 3 deletions src/providers/wfs/qgswfsprovider.cpp
Expand Up @@ -1104,7 +1104,7 @@ bool QgsWFSProvider::describeFeatureType( QString& geometryAttribute, QgsFields&
{
fields.clear();

QgsWFSDescribeFeatureType describeFeatureType( mShared->mURI.uri( false ) );
QgsWFSDescribeFeatureType describeFeatureType( mShared->mURI.uri() );
if ( !describeFeatureType.requestFeatureType( mShared->mWFSVersion,
mShared->mURI.typeName() ) )
{
Expand Down Expand Up @@ -1340,7 +1340,7 @@ bool QgsWFSProvider::sendTransactionDocument( const QDomDocument& doc, QDomDocum
return false;
}

QgsWFSTransactionRequest request( mShared->mURI.uri( false ) );
QgsWFSTransactionRequest request( mShared->mURI.uri() );
return request.send( doc, serverResponse );
}

Expand Down Expand Up @@ -1443,7 +1443,7 @@ bool QgsWFSProvider::getCapabilities()

if ( mShared->mCaps.version.isEmpty() )
{
QgsWfsCapabilities getCapabilities( mShared->mURI.uri( false ) );
QgsWfsCapabilities getCapabilities( mShared->mURI.uri() );
const bool synchronous = true;
const bool forceRefresh = false;
if ( !getCapabilities.requestCapabilities( synchronous, forceRefresh ) )
Expand Down

0 comments on commit 37b00eb

Please sign in to comment.