Skip to content

Commit

Permalink
[auth][ogr][feature] Add basic authentication to selected OGR sources
Browse files Browse the repository at this point in the history
- Protocol
- DB (integration with QGIS auth system)
  • Loading branch information
elpaso committed Nov 13, 2017
1 parent fe4f150 commit 3919cc2
Show file tree
Hide file tree
Showing 9 changed files with 933 additions and 225 deletions.
17 changes: 16 additions & 1 deletion src/auth/basic/qgsauthbasicmethod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ QgsAuthBasicMethod::QgsAuthBasicMethod()
<< QStringLiteral( "wfs" ) // convert to lowercase
<< QStringLiteral( "wcs" )
<< QStringLiteral( "wms" )
<< QStringLiteral( "ogr" )
<< QStringLiteral( "proxy" ) );
}

Expand Down Expand Up @@ -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() )
{
Expand All @@ -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 )
Expand Down
33 changes: 23 additions & 10 deletions src/gui/ogr/qgsnewogrconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
71 changes: 52 additions & 19 deletions src/gui/ogr/qgsogrhelperfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" ) )
{
Expand All @@ -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" ) )
{
Expand All @@ -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 );
Expand All @@ -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 );
Expand All @@ -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 );
Expand All @@ -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 );
Expand All @@ -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() )
Expand Down Expand Up @@ -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;
}

}
Expand All @@ -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 );
Expand All @@ -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" ) )
Expand All @@ -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;
}
4 changes: 2 additions & 2 deletions src/gui/ogr/qgsogrhelperfunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Loading

0 comments on commit 3919cc2

Please sign in to comment.