Skip to content
Permalink
Browse files

[auth] Added updateNetworkProxy method to auth manager

This allows to apply stored proxy auth settings to the
proxy configuration.
  • Loading branch information
elpaso committed Sep 25, 2017
1 parent e94471a commit 07e1bd9c389cb59721d8f8775530469f7e4e2693
@@ -359,6 +359,17 @@ Get list of authentication ids from database
:rtype: bool
%End

bool updateNetworkProxy( QNetworkProxy &proxy /In,Out/, const QString &authcfg,
const QString &dataprovider = QString() );
%Docstring
Provider call to update a QNetworkProxy with an authentication config
\param proxy the QNetworkProxy
\param authcfg Associated authentication config id
\param dataprovider Provider key filter, offering logic branching in authentication method
:return: Whether operation succeeded
:rtype: bool
%End


bool storeAuthSetting( const QString &key, const QVariant &value, bool encrypt = false );
%Docstring
@@ -28,6 +28,7 @@ class QgsAuthMethod : QObject
NetworkReply,
DataSourceUri,
GenericDataSourceUri,
NetworkProxy,
All
};
typedef QFlags<QgsAuthMethod::Expansion> Expansions;
@@ -110,6 +111,18 @@ Increment this if method is significantly updated, allow updater code to be writ
:rtype: bool
%End

virtual bool updateNetworkProxy( QNetworkProxy &proxy, const QString &authcfg,
const QString &dataprovider = QString() );
%Docstring
Update proxy settings with authentication components
\param proxy
\param authcfg Authentication configuration ID
\param dataprovider Textual key for a data provider, e.g. 'proxy', that allows
for custom updater code specific to the provider
:return: Whether the update succeeded
:rtype: bool
%End

virtual void clearCachedConfig( const QString &authcfg ) = 0;
%Docstring
Clear any cached configuration. Called when the QgsAuthManager deletes an authentication configuration (authcfg).
@@ -20,6 +20,8 @@
#include "qgsauthmanager.h"
#include "qgslogger.h"

#include <QNetworkProxy>

static const QString AUTH_METHOD_KEY = QStringLiteral( "Basic" );
static const QString AUTH_METHOD_DESCRIPTION = QStringLiteral( "Basic authentication" );

@@ -127,6 +129,28 @@ bool QgsAuthBasicMethod::updateDataSourceUriItems( QStringList &connectionItems,
return true;
}

bool QgsAuthBasicMethod::updateNetworkProxy( QNetworkProxy &proxy, const QString &authcfg, const QString &dataprovider )
{
Q_UNUSED( dataprovider )

QgsAuthMethodConfig mconfig = getMethodConfig( authcfg );
if ( !mconfig.isValid() )
{
QgsDebugMsg( QString( "Update proxy config FAILED for authcfg: %1: config invalid" ).arg( authcfg ) );
return false;
}

QString username = mconfig.config( QStringLiteral( "username" ) );
QString password = mconfig.config( QStringLiteral( "password" ) );

if ( !username.isEmpty() )
{
proxy.setUser( username );
proxy.setPassword( password );
}
return true;
}

void QgsAuthBasicMethod::updateMethodConfig( QgsAuthMethodConfig &mconfig )
{
if ( mconfig.hasConfig( QStringLiteral( "oldconfigstyle" ) ) )
@@ -44,6 +44,10 @@ class QgsAuthBasicMethod : public QgsAuthMethod
bool updateDataSourceUriItems( QStringList &connectionItems, const QString &authcfg,
const QString &dataprovider = QString() ) override;


bool updateNetworkProxy( QNetworkProxy &proxy, const QString &authcfg,
const QString &dataprovider = QString() ) override;

void clearCachedConfig( const QString &authcfg ) override;

void updateMethodConfig( QgsAuthMethodConfig &mconfig ) override;
@@ -1457,6 +1457,31 @@ bool QgsAuthManager::updateDataSourceUriItems( QStringList &connectionItems, con
return false;
}

bool QgsAuthManager::updateNetworkProxy( QNetworkProxy &proxy, const QString &authcfg, const QString &dataprovider )
{
if ( isDisabled() )
return false;

QgsAuthMethod *authmethod = configAuthMethod( authcfg );
if ( authmethod )
{
if ( !( authmethod->supportedExpansions() & QgsAuthMethod::NetworkProxy ) )
{
QgsDebugMsg( QString( "Network request updating not supported by authcfg: %1" ).arg( authcfg ) );
return true;
}

if ( !authmethod->updateNetworkProxy( proxy, authcfg, dataprovider.toLower() ) )
{
authmethod->clearCachedConfig( authcfg );
return false;
}
return true;
}

return false;
}

bool QgsAuthManager::storeAuthSetting( const QString &key, const QVariant &value, bool encrypt )
{
if ( key.isEmpty() )
@@ -332,6 +332,16 @@ class CORE_EXPORT QgsAuthManager : public QObject
bool updateDataSourceUriItems( QStringList &connectionItems SIP_INOUT, const QString &authcfg,
const QString &dataprovider = QString() );

/**
* Provider call to update a QNetworkProxy with an authentication config
* \param proxy the QNetworkProxy
* \param authcfg Associated authentication config id
* \param dataprovider Provider key filter, offering logic branching in authentication method
* \returns Whether operation succeeded
*/
bool updateNetworkProxy( QNetworkProxy &proxy SIP_INOUT, const QString &authcfg,
const QString &dataprovider = QString() );

////////////////// Generic settings ///////////////////////

//! Store an authentication setting (stored as string via QVariant( value ).toString() )
@@ -51,7 +51,8 @@ class CORE_EXPORT QgsAuthMethod : public QObject
NetworkReply = 0x2,
DataSourceUri = 0x4,
GenericDataSourceUri = 0x8,
All = NetworkRequest | NetworkReply | DataSourceUri | GenericDataSourceUri
NetworkProxy = 0x16,
All = NetworkRequest | NetworkReply | DataSourceUri | GenericDataSourceUri | NetworkProxy
};
Q_DECLARE_FLAGS( Expansions, Expansion )

@@ -126,6 +127,22 @@ class CORE_EXPORT QgsAuthMethod : public QObject
return true; // noop
}

/** Update proxy settings with authentication components
* \param proxy
* \param authcfg Authentication configuration ID
* \param dataprovider Textual key for a data provider, e.g. 'proxy', that allows
* for custom updater code specific to the provider
* \returns Whether the update succeeded
*/
virtual bool updateNetworkProxy( QNetworkProxy &proxy, const QString &authcfg,
const QString &dataprovider = QString() )
{
Q_UNUSED( proxy )
Q_UNUSED( authcfg )
Q_UNUSED( dataprovider )
return true; // noop
}

/** Clear any cached configuration. Called when the QgsAuthManager deletes an authentication configuration (authcfg).
* \note It is highly recommended that a cache of authentication components (per requested authcfg)
* be implemented, to avoid excessive queries on the auth database. Such a cache could be as

0 comments on commit 07e1bd9

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