Skip to content
Permalink
Browse files

Handle ssl mode, service, authcfg in postgresql project URIs

  • Loading branch information
wonder-sk committed Apr 7, 2018
1 parent 2e847e2 commit 47d5b7fc11f90daf19e71f4943c4da1bd00e22b4
@@ -274,6 +274,20 @@ Returns the srid
void setSrid( const QString &srid );
%Docstring
Sets the srid
%End

static SslMode decodeSslMode( const QString &sslMode );
%Docstring
Decodes SSL mode string into enum value. If the string is not recognized, SslPrefer is returned.

.. versionadded:: 3.2
%End

static QString encodeSslMode( SslMode sslMode );
%Docstring
Encodes SSL mode enum value into a string.

.. versionadded:: 3.2
%End

};
@@ -186,18 +186,7 @@ QgsDataSourceUri::QgsDataSourceUri( QString uri )
}
else if ( pname == QLatin1String( "sslmode" ) )
{
if ( pval == QLatin1String( "disable" ) )
mSSLmode = SslDisable;
else if ( pval == QLatin1String( "allow" ) )
mSSLmode = SslAllow;
else if ( pval == QLatin1String( "prefer" ) )
mSSLmode = SslPrefer;
else if ( pval == QLatin1String( "require" ) )
mSSLmode = SslRequire;
else if ( pval == QLatin1String( "verify-ca" ) )
mSSLmode = SslVerifyCa;
else if ( pval == QLatin1String( "verify-full" ) )
mSSLmode = SslVerifyFull;
mSSLmode = decodeSslMode( pval );
}
else if ( pname == QLatin1String( "requiressl" ) )
{
@@ -508,20 +497,10 @@ QString QgsDataSourceUri::connectionInfo( bool expandAuthConfig ) const
}
}

if ( mSSLmode == SslDisable )
connectionItems << QStringLiteral( "sslmode=disable" );
else if ( mSSLmode == SslAllow )
connectionItems << QStringLiteral( "sslmode=allow" );
else if ( mSSLmode == SslRequire )
connectionItems << QStringLiteral( "sslmode=require" );
#if 0
else if ( mSSLmode == SSLprefer ) // no need to output the default
connectionItems << "sslmode=prefer";
#endif
else if ( mSSLmode == SslVerifyCa )
connectionItems << QStringLiteral( "sslmode=verify-ca" );
else if ( mSSLmode == SslVerifyFull )
connectionItems << QStringLiteral( "sslmode=verify-full" );
if ( mSSLmode != SslPrefer ) // no need to output the default
{
connectionItems << QStringLiteral( "sslmode=" ) + encodeSslMode( mSSLmode );
}

if ( !mAuthConfigId.isEmpty() )
{
@@ -707,6 +686,38 @@ void QgsDataSourceUri::setSrid( const QString &srid )
mSrid = srid;
}

QgsDataSourceUri::SslMode QgsDataSourceUri::decodeSslMode( const QString &sslMode )
{
if ( sslMode == QLatin1String( "prefer" ) )
return SslPrefer;
else if ( sslMode == QLatin1String( "disable" ) )
return SslDisable;
else if ( sslMode == QLatin1String( "allow" ) )
return SslAllow;
else if ( sslMode == QLatin1String( "require" ) )
return SslRequire;
else if ( sslMode == QLatin1String( "verify-ca" ) )
return SslVerifyCa;
else if ( sslMode == QLatin1String( "verify-full" ) )
return SslVerifyFull;
else
return SslPrefer; // default
}

QString QgsDataSourceUri::encodeSslMode( QgsDataSourceUri::SslMode sslMode )
{
switch ( sslMode )
{
case SslPrefer: return QStringLiteral( "prefer" );
case SslDisable: return QStringLiteral( "disable" );
case SslAllow: return QStringLiteral( "allow" );
case SslRequire: return QStringLiteral( "require" );
case SslVerifyCa: return QStringLiteral( "verify-ca" );
case SslVerifyFull: return QStringLiteral( "verify-full" );
}
return QString();
}

void QgsDataSourceUri::setParam( const QString &key, const QString &value )
{
// may be multiple
@@ -221,6 +221,18 @@ class CORE_EXPORT QgsDataSourceUri
//! Sets the srid
void setSrid( const QString &srid );

/**
* Decodes SSL mode string into enum value. If the string is not recognized, SslPrefer is returned.
* \since QGIS 3.2
*/
static SslMode decodeSslMode( const QString &sslMode );

/**
* Encodes SSL mode enum value into a string.
* \since QGIS 3.2
*/
static QString encodeSslMode( SslMode sslMode );

private:
void skipBlanks( const QString &uri, int &i );
QString getValue( const QString &uri, int &i );
@@ -261,7 +261,13 @@ QString QgsPostgresProjectStorage::encodeUri( const QgsPostgresProjectUri &postU
u.setPort( postUri.connInfo.port().toInt() );
u.setUserName( postUri.connInfo.username() );
u.setPassword( postUri.connInfo.password() );
// TODO: sslmode, authcfg, service

if ( !postUri.connInfo.service().isEmpty() )
urlQuery.addQueryItem( "service", postUri.connInfo.service() );
if ( !postUri.connInfo.authConfigId().isEmpty() )
urlQuery.addQueryItem( "authcfg", postUri.connInfo.authConfigId() );
if ( !postUri.connInfo.sslMode() != QgsDataSourceUri::SslPrefer )
urlQuery.addQueryItem( "sslmode", QgsDataSourceUri::encodeSslMode( postUri.connInfo.sslMode() ) );

urlQuery.addQueryItem( "dbname", postUri.connInfo.database() );

@@ -287,11 +293,14 @@ QgsPostgresProjectUri QgsPostgresProjectStorage::decodeUri( const QString &uri )
QString port = u.port() != -1 ? QString::number( u.port() ) : QString();
QString username = u.userName();
QString password = u.password();
QgsDataSourceUri::SslMode sslMode = QgsDataSourceUri::SslPrefer;
QString authConfigId; // TODO: ?
QgsDataSourceUri::SslMode sslMode = QgsDataSourceUri::decodeSslMode( urlQuery.queryItemValue( "sslmode" ) );
QString authConfigId = urlQuery.queryItemValue( "authcfg" );
QString dbName = urlQuery.queryItemValue( "dbname" );
postUri.connInfo.setConnection( host, port, dbName, username, password, sslMode, authConfigId );
// TODO: "service"
QString service = urlQuery.queryItemValue( "service" );
if ( !service.isEmpty() )
postUri.connInfo.setConnection( service, dbName, username, password, sslMode, authConfigId );
else
postUri.connInfo.setConnection( host, port, dbName, username, password, sslMode, authConfigId );

postUri.schemaName = urlQuery.queryItemValue( "schema" );
postUri.projectName = urlQuery.queryItemValue( "project" );

0 comments on commit 47d5b7f

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