Skip to content
Permalink
Browse files

[auth][ogr][feature] Add basic authentication to selected OGR sources

- Protocol
- DB (integration with QGIS auth system)
  • Loading branch information
elpaso committed Nov 2, 2017
1 parent fe4f150 commit 3919cc2f4a044cab2324d0aa260a4809fc7a136a
@@ -42,6 +42,7 @@ QgsAuthBasicMethod::QgsAuthBasicMethod()
<< QStringLiteral( "wfs" ) // convert to lowercase
<< QStringLiteral( "wcs" )
<< QStringLiteral( "wms" )
<< QStringLiteral( "ogr" )
<< QStringLiteral( "proxy" ) );
}

@@ -85,7 +86,6 @@ bool QgsAuthBasicMethod::updateNetworkRequest( QNetworkRequest &request, const Q
bool QgsAuthBasicMethod::updateDataSourceUriItems( QStringList &connectionItems, const QString &authcfg,
const QString &dataprovider )
{
Q_UNUSED( dataprovider )
QgsAuthMethodConfig mconfig = getMethodConfig( authcfg );
if ( !mconfig.isValid() )
{
@@ -102,6 +102,21 @@ bool QgsAuthBasicMethod::updateDataSourceUriItems( QStringList &connectionItems,
return false;
}

// Branch for OGR protocol:
if ( dataprovider == QStringLiteral( "ogr" ) )
{
if ( ! password.isEmpty() )
{
// inject username and password into the URL
connectionItems.replaceInStrings( QStringLiteral( "://" ), QStringLiteral( "://%1:%2@" ).arg( username, password ) );
}
else
{
QgsDebugMsg( QString( "Update URI items FAILED for authcfg: %1: password empty" ).arg( authcfg ) );
}
}

// OGR database might use the standard URI way, we need to process this part in all cases
QString userparam = "user='" + escapeUserPass( username ) + '\'';
int userindx = connectionItems.indexOf( QRegExp( "^user='.*" ) );
if ( userindx != -1 )
@@ -57,18 +57,25 @@ QgsNewOgrConnection::QgsNewOgrConnection( QWidget *parent, const QString &connTy
txtDatabase->setText( settings.value( key + "/database" ).toString() );
QString port = settings.value( key + "/port" ).toString();
txtPort->setText( port );
txtUsername->setText( settings.value( key + "/username" ).toString() );
if ( settings.value( key + "/save" ).toString() == QLatin1String( "true" ) )
if ( settings.value( key + "/store_username" ).toString() == QLatin1String( "true" ) )
{
txtPassword->setText( settings.value( key + "/password" ).toString() );
chkStorePassword->setChecked( true );
mAuthSettingsDatabase->setUsername( settings.value( key + "/username" ).toString() );
mAuthSettingsDatabase->setStoreUsernameChecked( true );
}
if ( settings.value( key + "/store_password" ).toString() == QLatin1String( "true" ) )
{
mAuthSettingsDatabase->setPassword( settings.value( key + "/password" ).toString() );
mAuthSettingsDatabase->setStorePasswordChecked( true );
}
mAuthSettingsDatabase->setConfigId( settings.value( key + "/configid" ).toString() );
cmbDatabaseTypes->setCurrentIndex( cmbDatabaseTypes->findText( connType ) );
txtName->setText( connName );
txtName->setEnabled( false );
cmbDatabaseTypes->setEnabled( false );
}
txtName->setValidator( new QRegExpValidator( QRegExp( "[^\\/]+" ), txtName ) );
mAuthSettingsDatabase->setDataprovider( QStringLiteral( "ogr" ) );
mAuthSettingsDatabase->showStoreCheckboxes( true );
}

QgsNewOgrConnection::~QgsNewOgrConnection()
@@ -80,9 +87,13 @@ QgsNewOgrConnection::~QgsNewOgrConnection()
void QgsNewOgrConnection::testConnection()
{
QString uri;
uri = createDatabaseURI( cmbDatabaseTypes->currentText(), txtHost->text(),
txtDatabase->text(), txtPort->text(),
txtUsername->text(), txtPassword->text() );
uri = createDatabaseURI( cmbDatabaseTypes->currentText(),
txtHost->text(),
txtDatabase->text(),
txtPort->text(),
mAuthSettingsDatabase->configId(),
mAuthSettingsDatabase->username(),
mAuthSettingsDatabase->password() );
QgsDebugMsg( "Connecting using uri = " + uri );
OGRRegisterAll();
OGRDataSourceH poDS;
@@ -133,9 +144,11 @@ void QgsNewOgrConnection::accept()
settings.setValue( baseKey + "/host", txtHost->text() );
settings.setValue( baseKey + "/database", txtDatabase->text() );
settings.setValue( baseKey + "/port", txtPort->text() );
settings.setValue( baseKey + "/username", txtUsername->text() );
settings.setValue( baseKey + "/password", chkStorePassword->isChecked() ? txtPassword->text() : QLatin1String( "" ) );
settings.setValue( baseKey + "/save", chkStorePassword->isChecked() ? "true" : "false" );
settings.setValue( baseKey + "/username", mAuthSettingsDatabase->storeUsernameIsChecked() ? mAuthSettingsDatabase->username() : QLatin1String( "" ) );
settings.setValue( baseKey + "/password", mAuthSettingsDatabase->storePasswordIsChecked() ? mAuthSettingsDatabase->password() : QLatin1String( "" ) );
settings.setValue( baseKey + "/store_username", mAuthSettingsDatabase->storeUsernameIsChecked() ? "true" : "false" );
settings.setValue( baseKey + "/store_password", mAuthSettingsDatabase->storePasswordIsChecked() ? "true" : "false" );
settings.setValue( baseKey + "/configid", mAuthSettingsDatabase->configId() );

QDialog::accept();
}
@@ -18,12 +18,31 @@

#include "qgsogrhelperfunctions.h"
#include "qgslogger.h"
#include "qgsapplication.h"
#include "qgsauthmanager.h"
#include <QRegExp>

QString createDatabaseURI( const QString &connectionType, const QString &host, const QString &database, QString port, const QString &user, const QString &password )
QString createDatabaseURI( const QString &connectionType, const QString &host, const QString &database, QString port, const QString &configId, QString username, QString password )
{
QString uri;

// If an auth configuration is set, override username and password
// Note that only Basic auth (username/password) is for now supported for OGR connections
if ( ! configId.isEmpty() )
{
// Pass to updateDataSourceUriItems empty user/password in the format it likes
QStringList connectionItems;
connectionItems << QStringLiteral( "user=''" ) << QStringLiteral( "password=''" );
if ( QgsApplication::authManager()->updateDataSourceUriItems( connectionItems, configId, QStringLiteral( "ogr" ) ) )
{
QRegExp userRe( "^user='([^']+)'" );
QRegExp passRe( "^password='([^']+)'" );
// Extracts the username and password
username = QString( connectionItems.at( 0 ) ).replace( userRe, "\\1" );
password = QString( connectionItems.at( 1 ) ).replace( passRe, "\\1" );
}
}

//todo:add default ports for all kind of databases
if ( connectionType == QLatin1String( "ESRI Personal GeoDatabase" ) )
{
@@ -34,7 +53,7 @@ QString createDatabaseURI( const QString &connectionType, const QString &host, c
if ( port.isEmpty() )
port = QStringLiteral( "5151" );

uri = "SDE:" + host + ",PORT:" + port + ',' + database + ',' + user + ',' + password;
uri = "SDE:" + host + ",PORT:" + port + ',' + database + ',' + username + ',' + password;
}
else if ( connectionType == QLatin1String( "Informix DataBlade" ) )
{
@@ -44,9 +63,9 @@ QString createDatabaseURI( const QString &connectionType, const QString &host, c
if ( !host.isEmpty() )
uri += QStringLiteral( " server=%1" ).arg( host );

if ( !user.isEmpty() )
if ( !username.isEmpty() )
{
uri += QStringLiteral( " user=%1" ).arg( user );
uri += QStringLiteral( " user=%1" ).arg( username );

if ( !password.isEmpty() )
uri += QStringLiteral( " pass=%1" ).arg( password );
@@ -56,9 +75,9 @@ QString createDatabaseURI( const QString &connectionType, const QString &host, c
{
//not tested
uri = "@driver=ingres,dbname=" + database;
if ( !user.isEmpty() )
if ( !username.isEmpty() )
{
uri += QStringLiteral( ",userid=%1" ).arg( user );
uri += QStringLiteral( ",userid=%1" ).arg( username );

if ( !password.isEmpty() )
uri += QStringLiteral( ",password=%1" ).arg( password );
@@ -76,9 +95,9 @@ QString createDatabaseURI( const QString &connectionType, const QString &host, c
uri += QStringLiteral( ",port=%1" ).arg( port );
}

if ( !user.isEmpty() )
if ( !username.isEmpty() )
{
uri += QStringLiteral( ",user=%1" ).arg( user );
uri += QStringLiteral( ",user=%1" ).arg( username );

if ( !password.isEmpty() )
uri += QStringLiteral( ",password=%1" ).arg( password );
@@ -96,9 +115,9 @@ QString createDatabaseURI( const QString &connectionType, const QString &host, c
uri += QStringLiteral( ",%1" ).arg( port );
}

if ( !user.isEmpty() )
if ( !username.isEmpty() )
{
uri += QStringLiteral( ";uid=%1" ).arg( user );
uri += QStringLiteral( ";uid=%1" ).arg( username );

if ( !password.isEmpty() )
uri += QStringLiteral( ";pwd=%1" ).arg( password );
@@ -111,10 +130,10 @@ QString createDatabaseURI( const QString &connectionType, const QString &host, c
}
else if ( connectionType == QLatin1String( "Oracle Spatial" ) )
{
uri = "OCI:" + user;
uri = "OCI:" + username;

if ( ( !user.isEmpty() && !password.isEmpty() ) ||
( user.isEmpty() && password.isEmpty() ) )
if ( ( !username.isEmpty() && !password.isEmpty() ) ||
( username.isEmpty() && password.isEmpty() ) )
{
uri += '/';
if ( !password.isEmpty() )
@@ -142,15 +161,15 @@ QString createDatabaseURI( const QString &connectionType, const QString &host, c
}
else if ( connectionType == QLatin1String( "ODBC" ) )
{
if ( !user.isEmpty() )
if ( !username.isEmpty() )
{
if ( password.isEmpty() )
{
uri = "ODBC:" + user + '@' + database;
uri = "ODBC:" + username + '@' + database;
}
else
{
uri = "ODBC:" + user + '/' + password + '@' + database;
uri = "ODBC:" + username + '/' + password + '@' + database;
}

}
@@ -174,9 +193,9 @@ QString createDatabaseURI( const QString &connectionType, const QString &host, c
uri += QStringLiteral( " port='%1'" ).arg( port );
}

if ( !user.isEmpty() )
if ( !username.isEmpty() )
{
uri += QStringLiteral( " user='%1'" ).arg( user );
uri += QStringLiteral( " user='%1'" ).arg( username );

if ( !password.isEmpty() )
uri += QStringLiteral( " password='%1'" ).arg( password );
@@ -190,7 +209,7 @@ QString createDatabaseURI( const QString &connectionType, const QString &host, c
}


QString createProtocolURI( const QString &type, const QString &url )
QString createProtocolURI( const QString &type, const QString &url, const QString &configId, const QString &username, const QString &password )
{
QString uri;
if ( type == QLatin1String( "GeoJSON" ) )
@@ -206,5 +225,19 @@ QString createProtocolURI( const QString &type, const QString &url )
uri = QStringLiteral( "DODS:%1" ).arg( url );
}
QgsDebugMsg( "Connection type is=" + type + " and uri=" + uri );
// Update URI with authentication information
if ( ! configId.isEmpty() )
{
QStringList connectionItems;
connectionItems << uri;
if ( QgsApplication::authManager()->updateDataSourceUriItems( connectionItems, configId, QStringLiteral( "ogr" ) ) )
{
uri = connectionItems.join( QString() );
}
}
else if ( !( username.isEmpty() || password.isEmpty( ) ) )
{
uri.replace( QStringLiteral( "://" ), QStringLiteral( "://%1:%2@" ).arg( username, password ) );
}
return uri;
}
@@ -26,11 +26,11 @@
* \brief Create database uri from connection parameters
* \note not available in python bindings
*/
QString GUI_EXPORT createDatabaseURI( const QString &connectionType, const QString &host, const QString &database, QString port, const QString &user, const QString &password );
QString GUI_EXPORT createDatabaseURI( const QString &connectionType, const QString &host, const QString &database, QString port, const QString &configId, QString username, QString password );

/**
* CreateProtocolURI
* \brief Create protocol uri from connection parameters
* \note not available in python bindings
*/
QString GUI_EXPORT createProtocolURI( const QString &type, const QString &url );
QString GUI_EXPORT createProtocolURI( const QString &type, const QString &url, const QString &configId, const QString &username, const QString &password );

0 comments on commit 3919cc2

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