Skip to content
Permalink
Browse files

[auth system] Data source integration for OWS connections

  • Loading branch information
dakcarto committed Sep 21, 2015
1 parent 0d2b945 commit 907b019bb56c9660fdc2edd97c18a6ddde68a4e5
@@ -19,9 +19,10 @@ class QgsGml: QObject
* @param extent retrieved extents
* @param userName username for authentication
* @param password password for authentication
* @param authcfg authentication configuration id
* @return 0 in case of success
*/
int getFeatures( const QString& uri, QGis::WkbType* wkbType, QgsRectangle* extent = 0, const QString& userName = QString(), const QString& password = QString() ) /PyName=getFeaturesUri/;
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/;

/** Read from GML data. Constructor uri param is ignored
* Supports only UTF-8, UTF-16, ISO-8859-1, ISO-8859-1 XML encodings.
@@ -13,6 +13,7 @@
* *
***************************************************************************/
#include "qgsgml.h"
#include "qgsauthmanager.h"
#include "qgsrectangle.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsgeometry.h"
@@ -69,7 +70,7 @@ QgsGml::~QgsGml()
{
}

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

QNetworkRequest request( mUri );
if ( !userName.isNull() || !password.isNull() )
if ( !authcfg.isEmpty() )
{
if ( !QgsAuthManager::instance()->updateNetworkRequest( request, authcfg ) )
{
QgsMessageLog::logMessage(
tr( "GML Getfeature network request update failed for authcfg %1" ).arg( authcfg ),
tr( "Network" ),
QgsMessageLog::CRITICAL
);
return 1;
}
}
else if ( !userName.isNull() || !password.isNull() )
{
request.setRawHeader( "Authorization", "Basic " + QString( "%1:%2" ).arg( userName ).arg( password ).toAscii().toBase64() );
}
@@ -56,9 +56,15 @@ class CORE_EXPORT QgsGml : public QObject
* @param extent retrieved extents
* @param userName username for authentication
* @param password password for authentication
* @param authcfg authentication configuration id
* @return 0 in case of success
*/
int getFeatures( const QString& uri, QGis::WkbType* wkbType, QgsRectangle* extent = 0, const QString& userName = QString(), const QString& password = QString() );
int getFeatures( const QString& uri,
QGis::WkbType* wkbType,
QgsRectangle* extent = 0,
const QString& userName = QString(),
const QString& password = QString(),
const QString& authcfg = QString() );

/** Read from GML data. Constructor uri param is ignored
* Supports only UTF-8, UTF-16, ISO-8859-1, ISO-8859-1 XML encodings.
@@ -61,6 +61,13 @@ QgsOWSConnection::QgsOWSConnection( const QString & theService, const QString &
mUri.setParam( "password", password );
}

QString authcfg = settings.value( credentialsKey + "/authcfg" ).toString();
if ( !authcfg.isEmpty() )
{
mUri.setParam( "authcfg", authcfg );
}
mConnectionInfo.append( ",authcfg=" + authcfg );

bool ignoreGetMap = settings.value( key + "/ignoreGetMapURI", false ).toBool();
bool ignoreGetFeatureInfo = settings.value( key + "/ignoreGetFeatureInfoURI", false ).toBool();
bool ignoreAxisOrientation = settings.value( key + "/ignoreAxisOrientation", false ).toBool();
@@ -23,10 +23,11 @@
#include <QRegExpValidator>

QgsNewHttpConnection::QgsNewHttpConnection(
QWidget *parent, const QString& baseKey, const QString& connName, Qt::WindowFlags fl ):
QDialog( parent, fl ),
mBaseKey( baseKey ),
mOriginalConnName( connName )
QWidget *parent, const QString& baseKey, const QString& connName, Qt::WindowFlags fl )
: QDialog( parent, fl )
, mBaseKey( baseKey )
, mOriginalConnName( connName )
, mAuthConfigSelect( 0 )
{
setupUi( this );

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

mAuthConfigSelect = new QgsAuthConfigSelect( this );
tabAuth->insertTab( 1, mAuthConfigSelect, tr( "Configurations" ) );

if ( !connName.isEmpty() )
{
// populate the dialog with the information stored for the connection
@@ -92,6 +96,13 @@ QgsNewHttpConnection::QgsNewHttpConnection(

txtUserName->setText( settings.value( credentialsKey + "/username" ).toString() );
txtPassword->setText( settings.value( credentialsKey + "/password" ).toString() );

QString authcfg = settings.value( credentialsKey + "/authcfg" ).toString();
mAuthConfigSelect->setConfigId( authcfg );
if ( !authcfg.isEmpty() )
{
tabAuth->setCurrentIndex( tabAuth->indexOf( mAuthConfigSelect ) );
}
}

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

settings.setValue( credentialsKey + "/authcfg", mAuthConfigSelect->configId() );

settings.setValue( mBaseKey + "/selected", txtName->text() );

QDialog::accept();
@@ -19,6 +19,8 @@
#include "ui_qgsnewhttpconnectionbase.h"
#include "qgisgui.h"
#include "qgscontexthelp.h"
#include "qgsauthconfigselect.h"

/*!
* \brief Dialog to allow the user to configure and save connection
* information for an HTTP Server for WMS, etc.
@@ -46,6 +48,7 @@ class GUI_EXPORT QgsNewHttpConnection : public QDialog, private Ui::QgsNewHttpCo
QString mBaseKey;
QString mCredentialsBaseKey;
QString mOriginalConnName; //store initial name to delete entry in case of rename
QgsAuthConfigSelect * mAuthConfigSelect;
};

#endif // QGSNEWHTTPCONNECTION_H
@@ -9,7 +9,9 @@ SET(OWS_MOC_HDRS

INCLUDE_DIRECTORIES (
../../core
../../core/auth
../../gui
../../gui/auth
${CMAKE_CURRENT_BINARY_DIR}/../../ui
)

@@ -16,10 +16,17 @@ SET (WCS_MOC_HDRS

QT4_WRAP_CPP (WCS_MOC_SRCS ${WCS_MOC_HDRS})

INCLUDE_DIRECTORIES( . ../../core ../../core/raster ../../gui
INCLUDE_DIRECTORIES(
.
../../core
../../core/auth
../../core/raster
../../gui
../../gui/auth
../gdal
${CMAKE_CURRENT_BINARY_DIR}/../../ui
${GDAL_INCLUDE_DIR}
${QCA_INCLUDE_DIR}
)

ADD_LIBRARY(wcsprovider MODULE ${WCS_SRCS} ${WCS_MOC_SRCS})
@@ -26,6 +26,7 @@

#include <cmath>

#include "qgsauthmanager.h"
#include "qgscoordinatetransform.h"
#include "qgsdatasourceuri.h"
#include "qgsrasterlayer.h"
@@ -159,7 +160,12 @@ bool QgsWcsCapabilities::sendRequest( QString const & url )
QgsDebugMsg( "url = " + url );
mError = "";
QNetworkRequest request( url );
setAuthorization( request );
if ( !setAuthorization( request ) )
{
mError = tr( "Download of capabilities failed: network request update failed for authentication config" );
QgsMessageLog::logMessage( mError, tr( "WCS" ) );
return false;
}
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, mCacheLoadControl );
QgsDebugMsg( QString( "mCacheLoadControl = %1" ).arg( mCacheLoadControl ) );
@@ -359,7 +365,13 @@ void QgsWcsCapabilities::capabilitiesReplyFinished()
emit statusChanged( tr( "Capabilities request redirected." ) );

QNetworkRequest request( redirect.toUrl() );
setAuthorization( request );
if ( !setAuthorization( request ) )
{
mCapabilitiesResponse.clear();
mError = tr( "Download of capabilities failed: network request update failed for authentication config" );
QgsMessageLog::logMessage( mError, tr( "WCS" ) );
return;
}
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferNetwork );
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );

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

void QgsWcsCapabilities::setAuthorization( QNetworkRequest &request ) const
bool QgsWcsCapabilities::setAuthorization( QNetworkRequest &request ) const
{
QgsDebugMsg( "entered" );
if ( mUri.hasParam( "username" ) && mUri.hasParam( "password" ) )
if ( mUri.hasParam( "authcfg" ) && !mUri.param( "authcfg" ).isEmpty() )
{
return QgsAuthManager::instance()->updateNetworkRequest( request, mUri.param( "authcfg" ) );
}
else if ( mUri.hasParam( "username" ) && mUri.hasParam( "password" ) )
{
QgsDebugMsg( "setAuthorization " + mUri.param( "username" ) );
request.setRawHeader( "Authorization", "Basic " + QString( "%1:%2" ).arg( mUri.param( "username" ) ).arg( mUri.param( "password" ) ).toAscii().toBase64() );
}
return true;
}

void QgsWcsCapabilities::showMessageBox( const QString& title, const QString& text )
@@ -153,7 +153,7 @@ class QgsWcsCapabilities : public QObject
bool parseDescribeCoverageDom11( QByteArray const &xml, QgsWcsCoverageSummary *coverage );

//! set authorization header
void setAuthorization( QNetworkRequest &request ) const;
bool setAuthorization( QNetworkRequest &request ) const;

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

@@ -408,6 +408,12 @@ bool QgsWcsProvider::parseUri( QString uriString )
mAuth.mPassword = uri.param( "password" );
QgsDebugMsg( "set password to " + mAuth.mPassword );

if ( uri.hasParam( "authcfg" ) )
{
mAuth.mAuthCfg = uri.param( "authcfg" );
}
QgsDebugMsg( "set authcfg to " + mAuth.mAuthCfg );

mIdentifier = uri.param( "identifier" );

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

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

QNetworkRequest request( url );
auth.setAuthorization( request );
if ( !mAuth.setAuthorization( request ) )
{
QgsMessageLog::logMessage( tr( "Network request update failed for authentication config" ),
tr( "WCS" ) );
return;
}
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, cacheLoadControl );

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

QgsDebugMsg( QString( "redirected getmap: %1" ).arg( redirect.toString() ) );
mCacheReply = mNAM->get( QNetworkRequest( redirect.toUrl() ) );
QNetworkRequest request( redirect.toUrl() );
if ( !mAuth.setAuthorization( request ) )
{
QgsMessageLog::logMessage( tr( "Network request update failed for authentication config" ),
tr( "WCS" ) );
return;
}
mCacheReply = mNAM->get( request );
connect( mCacheReply, SIGNAL( finished() ), this, SLOT( cacheReplyFinished() ) );
connect( mCacheReply, SIGNAL( downloadProgress( qint64, qint64 ) ), this, SLOT( cacheReplyProgress( qint64, qint64 ) ) );

@@ -24,6 +24,7 @@

#include "qgserror.h"
#include "qgswcscapabilities.h"
#include "qgsauthmanager.h"
#include "qgsrasterdataprovider.h"
#include "qgsgdalproviderbase.h"
#include "qgsrectangle.h"
@@ -50,22 +51,34 @@ class QNetworkRequest;
// TODO: merge with QgsWmsAuthorization?
struct QgsWcsAuthorization
{
QgsWcsAuthorization( const QString& userName = QString(), const QString& password = QString() ) : mUserName( userName ), mPassword( password ) {}
QgsWcsAuthorization( const QString& userName = QString(), const QString& password = QString(), const QString& authcfg = QString() )
: mUserName( userName )
, mPassword( password )
, mAuthCfg( authcfg )
{}

//! set authorization header
void setAuthorization( QNetworkRequest &request ) const
bool setAuthorization( QNetworkRequest &request ) const
{
if ( !mUserName.isNull() || !mPassword.isNull() )
if ( !mAuthCfg.isEmpty() )
{
return QgsAuthManager::instance()->updateNetworkRequest( request, mAuthCfg );
}
else if ( !mUserName.isNull() || !mPassword.isNull() )
{
request.setRawHeader( "Authorization", "Basic " + QString( "%1:%2" ).arg( mUserName ).arg( mPassword ).toAscii().toBase64() );
}
return true;
}

//! Username for basic http authentication
QString mUserName;

//! Password for basic http authentication
QString mPassword;

//! Authentication configuration ID
QString mAuthCfg;
};

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

QgsNetworkAccessManager* mNAM;
QgsWcsAuthorization& mAuth;
QEventLoop* mEventLoop;

QNetworkReply* mCacheReply;
@@ -25,13 +25,16 @@ QT4_WRAP_CPP(WFS_MOC_SRCS ${WFS_MOC_HDRS})

INCLUDE_DIRECTORIES (
../../core
../../core/auth
../../core/geometry
../../gui
../../gui/auth
${CMAKE_CURRENT_BINARY_DIR}/../../ui
${GEOS_INCLUDE_DIR}
${GEOS_INCLUDE_DIR}/geos
${EXPAT_INCLUDE_DIR}
${QSCINTILLA_INCLUDE_DIR}
${QCA_INCLUDE_DIR}
)

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

0 comments on commit 907b019

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