Skip to content
Permalink
Browse files
[gdal] Fix authcfg support for remote URLs
  • Loading branch information
nirvn authored and nyalldawson committed Nov 10, 2021
1 parent 7cec307 commit f10443b34241a444e974f1aea4d9e2df7d3437f1
Showing with 44 additions and 18 deletions.
  1. +23 −18 src/core/providers/gdal/qgsgdalprovider.cpp
  2. +6 −0 src/core/providers/gdal/qgsgdalprovider.h
  3. +15 −0 src/core/providers/gdal/qgsgdalproviderbase.cpp
@@ -279,29 +279,34 @@ QString QgsGdalProvider::dataSourceUri( bool expandAuthConfig ) const
{
if ( expandAuthConfig && QgsDataProvider::dataSourceUri( ).contains( QLatin1String( "authcfg" ) ) )
{
QString uri( QgsDataProvider::dataSourceUri() );
// Check for authcfg
QRegularExpression authcfgRe( R"raw(authcfg='?([^'\s]+)'?)raw" );
QRegularExpressionMatch match;
if ( uri.contains( authcfgRe, &match ) )
{
uri = uri.replace( match.captured( 0 ), QString() );
QString configId( match.captured( 1 ) );
QStringList connectionItems;
connectionItems << uri;
if ( QgsApplication::authManager()->updateDataSourceUriItems( connectionItems, configId, QStringLiteral( "gdal" ) ) )
{
uri = connectionItems.first( );
}
}
return uri;
return QgsGdalProvider::expandAuthConfig( QgsDataProvider::dataSourceUri() );
}
else
{
return QgsDataProvider::dataSourceUri();
}
}

QString QgsGdalProvider::expandAuthConfig( const QString &dsName )
{
QString uri( dsName );
// Check for authcfg
QRegularExpression authcfgRe( " authcfg='([^']+)'" );
QRegularExpressionMatch match;
if ( uri.contains( authcfgRe, &match ) )
{
uri = uri.replace( match.captured( 0 ), QString() );
QString configId( match.captured( 1 ) );
QStringList connectionItems;
connectionItems << uri;
if ( QgsApplication::authManager()->updateDataSourceUriItems( connectionItems, configId, QStringLiteral( "ogr" ) ) )
{
uri = connectionItems.first( );
}
}
return uri;
}

QgsGdalProvider *QgsGdalProvider::clone() const
{
return new QgsGdalProvider( *this );
@@ -2645,7 +2650,7 @@ bool QgsGdalProvider::isValidRasterFileName( QString const &fileNameQString, QSt

CPLErrorReset();

QString fileName = fileNameQString;
QString fileName = QgsGdalProvider::expandAuthConfig( fileNameQString );

// Try to open using VSIFileHandler (see qgsogrprovider.cpp)
// TODO suppress error messages and report in debug, like in OGR provider
@@ -3617,7 +3622,7 @@ QList<QgsProviderSublayerDetails> QgsGdalProviderMetadata::querySublayers( const

QgsGdalProviderBase::registerGdalDrivers();

QString gdalUri = uri;
QString gdalUri = QgsGdalProvider::expandAuthConfig( uri );

QVariantMap uriParts = decodeUri( gdalUri );

@@ -125,6 +125,12 @@ class QgsGdalProvider final: public QgsRasterDataProvider, QgsGdalProviderBase
*/
static QString helpCreationOptionsFormat( QString format );

/**
* Replaces the authcfg part of the string with authentication information
* \since QGIS 3.22
*/
static QString expandAuthConfig( const QString &dsName );

QString description() const override;
QgsRasterDataProvider::ProviderCapabilities providerCapabilities() const override;
QgsCoordinateReferenceSystem crs() const override;
@@ -325,8 +325,17 @@ QVariantMap QgsGdalProviderBase::decodeGdalUri( const QString &uri )
{
QString path = uri;
QString layerName;
QString authcfg;
QStringList openOptions;

const QRegularExpression authcfgRegex( " authcfg='([^']+)'" );
QRegularExpressionMatch match;
if ( path.contains( authcfgRegex, &match ) )
{
path = path.remove( match.capturedStart( 0 ), match.capturedLength( 0 ) );
authcfg = match.captured( 1 );
}

QString vsiPrefix = qgsVsiPrefix( path );
QString vsiSuffix;
if ( path.startsWith( vsiPrefix, Qt::CaseInsensitive ) )
@@ -389,6 +398,8 @@ QVariantMap QgsGdalProviderBase::decodeGdalUri( const QString &uri )
uriComponents.insert( QStringLiteral( "vsiPrefix" ), vsiPrefix );
if ( !vsiSuffix.isEmpty() )
uriComponents.insert( QStringLiteral( "vsiSuffix" ), vsiSuffix );
if ( !authcfg.isEmpty() )
uriComponents.insert( QStringLiteral( "authcfg" ), authcfg );
return uriComponents;
}

@@ -398,6 +409,7 @@ QString QgsGdalProviderBase::encodeGdalUri( const QVariantMap &parts )
const QString vsiSuffix = parts.value( QStringLiteral( "vsiSuffix" ) ).toString();
const QString path = parts.value( QStringLiteral( "path" ) ).toString();
const QString layerName = parts.value( QStringLiteral( "layerName" ) ).toString();
const QString authcfg = parts.value( QStringLiteral( "authcfg" ) ).toString();

QString uri = vsiPrefix + path + vsiSuffix;
if ( !layerName.isEmpty() && uri.endsWith( QLatin1String( "gpkg" ) ) )
@@ -413,6 +425,9 @@ QString QgsGdalProviderBase::encodeGdalUri( const QVariantMap &parts )
uri += openOption;
}

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

return uri;
}

0 comments on commit f10443b

Please sign in to comment.