Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport release-3_40] Set additional HTTP headers for WCS provider #59833

Merged
merged 8 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ set(QGIS_CORE_SRCS
auth/qgsauthmethod.cpp
auth/qgsauthmethodmetadata.cpp
auth/qgsauthmethodregistry.cpp
auth/qgsauthorizationsettings.cpp

auth/qgsauthconfigurationstoragesqlite.cpp
auth/qgsauthconfigurationstoragedb.cpp
Expand Down Expand Up @@ -1387,6 +1388,7 @@ set(QGIS_CORE_HDRS
auth/qgsauthmethod.h
auth/qgsauthmethodmetadata.h
auth/qgsauthmethodregistry.h
auth/qgsauthorizationsettings.h

auth/qgsauthconfigurationstoragesqlite.h
auth/qgsauthconfigurationstoragedb.h
Expand Down
48 changes: 48 additions & 0 deletions src/core/auth/qgsauthorizationsettings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/***************************************************************************
qgsauthorizationsettings.cpp
---------------------
begin : December 2024
copyright : (C) 2024 by Damiano Lombardi
email : damiano at opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsauthorizationsettings.h"

QgsAuthorizationSettings::QgsAuthorizationSettings( const QString &userName, const QString &password, const QgsHttpHeaders &httpHeaders, const QString &authcfg )
: mUserName( userName )
, mPassword( password )
, mHttpHeaders( httpHeaders )
, mAuthCfg( authcfg )
{}

bool QgsAuthorizationSettings::setAuthorization( QNetworkRequest &request ) const
{
if ( !mAuthCfg.isEmpty() ) // must be non-empty value
{
return QgsApplication::authManager()->updateNetworkRequest( request, mAuthCfg );
}
else if ( !mUserName.isEmpty() || !mPassword.isEmpty() )
{
request.setRawHeader( "Authorization", "Basic " + QStringLiteral( "%1:%2" ).arg( mUserName, mPassword ).toUtf8().toBase64() );
}

mHttpHeaders.updateNetworkRequest( request );

return true;
}

bool QgsAuthorizationSettings::setAuthorizationReply( QNetworkReply *reply ) const
{
if ( !mAuthCfg.isEmpty() )
{
return QgsApplication::authManager()->updateNetworkReply( reply, mAuthCfg );
}
return true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,42 @@

#include "qgsauthmanager.h"
#include "qgsapplication.h"
#include "qgshttpheaders.h"

#include <QString>
#include <QNetworkRequest>
#include <QNetworkReply>

// TODO: merge with QgsWmsAuthorization?
struct QgsAuthorizationSettings
#define SIP_NO_FILE

/**
* \ingroup core
* \class QgsAuthorizationSettings
* \brief Utility class that contains authorization information.
* \since QGIS 3.42
*/
class CORE_EXPORT QgsAuthorizationSettings
{
QgsAuthorizationSettings( const QString &userName = QString(), const QString &password = QString(), const QString &authcfg = QString() )
: mUserName( userName )
, mPassword( password )
, mAuthCfg( authcfg )
{}
public:

//! Constructor for QgsAuthorizationSettings.
QgsAuthorizationSettings( const QString &userName = QString(), const QString &password = QString(), const QgsHttpHeaders &httpHeaders = QgsHttpHeaders(), const QString &authcfg = QString() );

//! update authorization for request
bool setAuthorization( QNetworkRequest &request ) const
{
if ( !mAuthCfg.isEmpty() ) // must be non-empty value
{
return QgsApplication::authManager()->updateNetworkRequest( request, mAuthCfg );
}
else if ( !mUserName.isNull() || !mPassword.isNull() ) // allow empty values
{
request.setRawHeader( "Authorization", "Basic " + QStringLiteral( "%1:%2" ).arg( mUserName, mPassword ).toLatin1().toBase64() );
}
return true;
}
//! Update authorization for request
bool setAuthorization( QNetworkRequest &request ) const;

//! update authorization for reply
bool setAuthorizationReply( QNetworkReply *reply ) const
{
if ( !mAuthCfg.isEmpty() )
{
return QgsApplication::authManager()->updateNetworkReply( reply, mAuthCfg );
}
return true;
}
//! Update authorization for reply
bool setAuthorizationReply( QNetworkReply *reply ) const;

//! Username for basic http authentication
QString mUserName;

//! Password for basic http authentication
QString mPassword;

//! headers for http requests
QgsHttpHeaders mHttpHeaders;

//! Authentication configuration ID
QString mAuthCfg;
};
Expand Down
4 changes: 2 additions & 2 deletions src/gui/qgsowssourceselect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ QgsNewHttpConnection::ConnectionType connectionTypeFromServiceString( const QStr
void QgsOWSSourceSelect::mNewButton_clicked()
{
const QgsNewHttpConnection::ConnectionType type = connectionTypeFromServiceString( mService );
QgsNewHttpConnection *nc = new QgsNewHttpConnection( this, type, mService.toUpper() );
QgsNewHttpConnection *nc = new QgsNewHttpConnection( this, type, mService.toUpper(), QString(), QgsNewHttpConnection::FlagShowHttpSettings );

if ( nc->exec() )
{
Expand All @@ -281,7 +281,7 @@ void QgsOWSSourceSelect::mNewButton_clicked()
void QgsOWSSourceSelect::mEditButton_clicked()
{
const QgsNewHttpConnection::ConnectionType type = connectionTypeFromServiceString( mService );
QgsNewHttpConnection *nc = new QgsNewHttpConnection( this, type, mService.toUpper(), mConnectionsComboBox->currentText() );
QgsNewHttpConnection *nc = new QgsNewHttpConnection( this, type, mService.toUpper(), mConnectionsComboBox->currentText(), QgsNewHttpConnection::FlagShowHttpSettings );

if ( nc->exec() )
{
Expand Down
4 changes: 2 additions & 2 deletions src/providers/wcs/qgswcsdataitemguiprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void QgsWcsDataItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *m

void QgsWcsDataItemGuiProvider::newConnection( QgsDataItem *item )
{
QgsNewHttpConnection nc( nullptr, QgsNewHttpConnection::ConnectionWcs, QStringLiteral( "WCS" ) );
QgsNewHttpConnection nc( nullptr, QgsNewHttpConnection::ConnectionWcs, QStringLiteral( "WCS" ), QString(), QgsNewHttpConnection::FlagShowHttpSettings );

if ( nc.exec() )
{
Expand All @@ -82,7 +82,7 @@ void QgsWcsDataItemGuiProvider::newConnection( QgsDataItem *item )

void QgsWcsDataItemGuiProvider::editConnection( QgsDataItem *item )
{
QgsNewHttpConnection nc( nullptr, QgsNewHttpConnection::ConnectionWcs, QStringLiteral( "WCS" ), item->name() );
QgsNewHttpConnection nc( nullptr, QgsNewHttpConnection::ConnectionWcs, QStringLiteral( "WCS" ), item->name(), QgsNewHttpConnection::FlagShowHttpSettings );

if ( nc.exec() )
{
Expand Down
4 changes: 3 additions & 1 deletion src/providers/wcs/qgswcsprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ bool QgsWcsProvider::parseUri( const QString &uriString )
}
QgsDebugMsgLevel( "set authcfg to " + mAuth.mAuthCfg, 2 );

mAuth.mHttpHeaders = uri.httpHeaders();

mIdentifier = uri.param( QStringLiteral( "identifier" ) );

mTime = uri.param( QStringLiteral( "time" ) );
Expand Down Expand Up @@ -1648,7 +1650,7 @@ QgsWcsProvider *QgsWcsProviderMetadata::createProvider( const QString &uri, cons

int QgsWcsDownloadHandler::sErrors = 0;

QgsWcsDownloadHandler::QgsWcsDownloadHandler( const QUrl &url, QgsWcsAuthorization &auth, QNetworkRequest::CacheLoadControl cacheLoadControl, QByteArray &cachedData, const QString &wcsVersion, QgsError &cachedError, QgsRasterBlockFeedback *feedback )
QgsWcsDownloadHandler::QgsWcsDownloadHandler( const QUrl &url, QgsAuthorizationSettings &auth, QNetworkRequest::CacheLoadControl cacheLoadControl, QByteArray &cachedData, const QString &wcsVersion, QgsError &cachedError, QgsRasterBlockFeedback *feedback )
: mAuth( auth )
, mEventLoop( new QEventLoop )
, mCachedData( cachedData )
Expand Down
49 changes: 4 additions & 45 deletions src/providers/wcs/qgswcsprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "qgscoordinatetransform.h"
#include "qgsogrutils.h"
#include "qgsapplication.h"
#include "qgsauthorizationsettings.h"

#include "qgsprovidermetadata.h"

Expand All @@ -53,48 +54,6 @@ class QNetworkRequest;
#include <gdal.h>
#include "cpl_conv.h"

// TODO: merge with QgsWmsAuthorization?
struct QgsWcsAuthorization
{
QgsWcsAuthorization( const QString &userName = QString(), const QString &password = QString(), const QString &authcfg = QString() )
: mUserName( userName )
, mPassword( password )
, mAuthCfg( authcfg )
{}

//! Sets authorization header
bool setAuthorization( QNetworkRequest &request ) const
{
if ( !mAuthCfg.isEmpty() )
{
return QgsApplication::authManager()->updateNetworkRequest( request, mAuthCfg );
}
else if ( !mUserName.isNull() || !mPassword.isNull() )
{
request.setRawHeader( "Authorization", "Basic " + QStringLiteral( "%1:%2" ).arg( mUserName, mPassword ).toLatin1().toBase64() );
}
return true;
}

//! Sets authorization reply
bool setAuthorizationReply( QNetworkReply *reply ) const
{
if ( !mAuthCfg.isEmpty() )
{
return QgsApplication::authManager()->updateNetworkReply( reply, mAuthCfg );
}
return true;
}

//! Username for basic http authentication
QString mUserName;

//! Password for basic http authentication
QString mPassword;

//! Authentication configuration ID
QString mAuthCfg;
};

/**
*
Expand Down Expand Up @@ -384,7 +343,7 @@ class QgsWcsProvider final : public QgsRasterDataProvider, QgsGdalProviderBase
//QMap<int, QStringList> mLayerParentNames;

//! http authorization details
mutable QgsWcsAuthorization mAuth;
mutable QgsAuthorizationSettings mAuth;

//! whether to use hrefs from GetCapabilities (default) or
// the given base urls for GetMap and GetFeatureInfo
Expand Down Expand Up @@ -413,7 +372,7 @@ class QgsWcsDownloadHandler : public QObject
{
Q_OBJECT
public:
QgsWcsDownloadHandler( const QUrl &url, QgsWcsAuthorization &auth, QNetworkRequest::CacheLoadControl cacheLoadControl, QByteArray &cachedData, const QString &wcsVersion, QgsError &cachedError, QgsRasterBlockFeedback *feedback );
QgsWcsDownloadHandler( const QUrl &url, QgsAuthorizationSettings &auth, QNetworkRequest::CacheLoadControl cacheLoadControl, QByteArray &cachedData, const QString &wcsVersion, QgsError &cachedError, QgsRasterBlockFeedback *feedback );
~QgsWcsDownloadHandler() override;

void blockingDownload();
Expand All @@ -426,7 +385,7 @@ class QgsWcsDownloadHandler : public QObject
protected:
void finish() { QMetaObject::invokeMethod( mEventLoop, "quit", Qt::QueuedConnection ); }

QgsWcsAuthorization &mAuth;
QgsAuthorizationSettings &mAuth;
QEventLoop *mEventLoop = nullptr;

QNetworkReply *mCacheReply = nullptr;
Expand Down
4 changes: 2 additions & 2 deletions src/providers/wms/qgswmscapabilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2441,7 +2441,7 @@ QgsWmsCapabilitiesDownload::QgsWmsCapabilitiesDownload( bool forceRefresh, QObje
{
}

QgsWmsCapabilitiesDownload::QgsWmsCapabilitiesDownload( const QString &baseUrl, const QgsWmsAuthorization &auth, bool forceRefresh, QObject *parent )
QgsWmsCapabilitiesDownload::QgsWmsCapabilitiesDownload( const QString &baseUrl, const QgsAuthorizationSettings &auth, bool forceRefresh, QObject *parent )
: QObject( parent )
, mBaseUrl( baseUrl )
, mAuth( auth )
Expand All @@ -2465,7 +2465,7 @@ void QgsWmsCapabilitiesDownload::setForceRefresh( bool forceRefresh )
mForceRefresh = forceRefresh;
}

bool QgsWmsCapabilitiesDownload::downloadCapabilities( const QString &baseUrl, const QgsWmsAuthorization &auth )
bool QgsWmsCapabilitiesDownload::downloadCapabilities( const QString &baseUrl, const QgsAuthorizationSettings &auth )
{
mBaseUrl = baseUrl;
mAuth = auth;
Expand Down
58 changes: 6 additions & 52 deletions src/providers/wms/qgswmscapabilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "qgstemporalutils.h"
#include "qgshttpheaders.h"
#include "qgscoordinatetransformcontext.h"
#include "qgsauthorizationsettings.h"

class QNetworkReply;

Expand Down Expand Up @@ -686,53 +687,6 @@ struct QgsWmsParserSettings
bool invertAxisOrientation;
};

struct QgsWmsAuthorization
{
QgsWmsAuthorization( const QString &userName = QString(), const QString &password = QString(), const QgsHttpHeaders &httpHeaders = QgsHttpHeaders(), const QString &authcfg = QString() )
: mUserName( userName )
, mPassword( password )
, mHttpHeaders( httpHeaders )
, mAuthCfg( authcfg )
{}

bool setAuthorization( QNetworkRequest &request ) const
{
if ( !mAuthCfg.isEmpty() )
{
return QgsApplication::authManager()->updateNetworkRequest( request, mAuthCfg );
}
else if ( !mUserName.isEmpty() || !mPassword.isEmpty() )
{
request.setRawHeader( "Authorization", "Basic " + QStringLiteral( "%1:%2" ).arg( mUserName, mPassword ).toUtf8().toBase64() );
}

mHttpHeaders.updateNetworkRequest( request );

return true;
}
//! Sets authorization reply
bool setAuthorizationReply( QNetworkReply *reply ) const
{
if ( !mAuthCfg.isEmpty() )
{
return QgsApplication::authManager()->updateNetworkReply( reply, mAuthCfg );
}
return true;
}

//! Username for basic http authentication
QString mUserName;

//! Password for basic http authentication
QString mPassword;

//! headers for http requests
QgsHttpHeaders mHttpHeaders;

//! Authentication configuration ID
QString mAuthCfg;
};


//! URI that gets passed to provider
class QgsWmsSettings
Expand All @@ -741,7 +695,7 @@ class QgsWmsSettings
bool parseUri( const QString &uriString );

QString baseUrl() const { return mBaseUrl; }
QgsWmsAuthorization authorization() const { return mAuth; }
QgsAuthorizationSettings authorization() const { return mAuth; }

QgsWmsParserSettings parserSettings() const { return mParserSettings; }

Expand Down Expand Up @@ -861,7 +815,7 @@ class QgsWmsSettings
//! URL part of URI (httpuri)
QString mBaseUrl;

QgsWmsAuthorization mAuth;
QgsAuthorizationSettings mAuth;

bool mIgnoreGetMapUrl;
bool mIgnoreGetFeatureInfoUrl;
Expand Down Expand Up @@ -1078,13 +1032,13 @@ class QgsWmsCapabilitiesDownload : public QObject
public:
explicit QgsWmsCapabilitiesDownload( bool forceRefresh, QObject *parent = nullptr );

QgsWmsCapabilitiesDownload( const QString &baseUrl, const QgsWmsAuthorization &auth, bool forceRefresh, QObject *parent = nullptr );
QgsWmsCapabilitiesDownload( const QString &baseUrl, const QgsAuthorizationSettings &auth, bool forceRefresh, QObject *parent = nullptr );

~QgsWmsCapabilitiesDownload() override;

bool downloadCapabilities();

bool downloadCapabilities( const QString &baseUrl, const QgsWmsAuthorization &auth );
bool downloadCapabilities( const QString &baseUrl, const QgsAuthorizationSettings &auth );

/**
* Returns the download refresh state.
Expand Down Expand Up @@ -1124,7 +1078,7 @@ class QgsWmsCapabilitiesDownload : public QObject
//! URL part of URI (httpuri)
QString mBaseUrl;

QgsWmsAuthorization mAuth;
QgsAuthorizationSettings mAuth;

//! The reply to the capabilities request
QNetworkReply *mCapabilitiesReply = nullptr;
Expand Down
Loading
Loading