Skip to content

Commit 07e1bd9

Browse files
committed
[auth] Added updateNetworkProxy method to auth manager
This allows to apply stored proxy auth settings to the proxy configuration.
1 parent e94471a commit 07e1bd9

7 files changed

+105
-1
lines changed

python/core/auth/qgsauthmanager.sip

+11
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,17 @@ Get list of authentication ids from database
359359
:rtype: bool
360360
%End
361361

362+
bool updateNetworkProxy( QNetworkProxy &proxy /In,Out/, const QString &authcfg,
363+
const QString &dataprovider = QString() );
364+
%Docstring
365+
Provider call to update a QNetworkProxy with an authentication config
366+
\param proxy the QNetworkProxy
367+
\param authcfg Associated authentication config id
368+
\param dataprovider Provider key filter, offering logic branching in authentication method
369+
:return: Whether operation succeeded
370+
:rtype: bool
371+
%End
372+
362373

363374
bool storeAuthSetting( const QString &key, const QVariant &value, bool encrypt = false );
364375
%Docstring

python/core/auth/qgsauthmethod.sip

+13
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class QgsAuthMethod : QObject
2828
NetworkReply,
2929
DataSourceUri,
3030
GenericDataSourceUri,
31+
NetworkProxy,
3132
All
3233
};
3334
typedef QFlags<QgsAuthMethod::Expansion> Expansions;
@@ -110,6 +111,18 @@ Increment this if method is significantly updated, allow updater code to be writ
110111
:rtype: bool
111112
%End
112113

114+
virtual bool updateNetworkProxy( QNetworkProxy &proxy, const QString &authcfg,
115+
const QString &dataprovider = QString() );
116+
%Docstring
117+
Update proxy settings with authentication components
118+
\param proxy
119+
\param authcfg Authentication configuration ID
120+
\param dataprovider Textual key for a data provider, e.g. 'proxy', that allows
121+
for custom updater code specific to the provider
122+
:return: Whether the update succeeded
123+
:rtype: bool
124+
%End
125+
113126
virtual void clearCachedConfig( const QString &authcfg ) = 0;
114127
%Docstring
115128
Clear any cached configuration. Called when the QgsAuthManager deletes an authentication configuration (authcfg).

src/auth/basic/qgsauthbasicmethod.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "qgsauthmanager.h"
2121
#include "qgslogger.h"
2222

23+
#include <QNetworkProxy>
24+
2325
static const QString AUTH_METHOD_KEY = QStringLiteral( "Basic" );
2426
static const QString AUTH_METHOD_DESCRIPTION = QStringLiteral( "Basic authentication" );
2527

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

132+
bool QgsAuthBasicMethod::updateNetworkProxy( QNetworkProxy &proxy, const QString &authcfg, const QString &dataprovider )
133+
{
134+
Q_UNUSED( dataprovider )
135+
136+
QgsAuthMethodConfig mconfig = getMethodConfig( authcfg );
137+
if ( !mconfig.isValid() )
138+
{
139+
QgsDebugMsg( QString( "Update proxy config FAILED for authcfg: %1: config invalid" ).arg( authcfg ) );
140+
return false;
141+
}
142+
143+
QString username = mconfig.config( QStringLiteral( "username" ) );
144+
QString password = mconfig.config( QStringLiteral( "password" ) );
145+
146+
if ( !username.isEmpty() )
147+
{
148+
proxy.setUser( username );
149+
proxy.setPassword( password );
150+
}
151+
return true;
152+
}
153+
130154
void QgsAuthBasicMethod::updateMethodConfig( QgsAuthMethodConfig &mconfig )
131155
{
132156
if ( mconfig.hasConfig( QStringLiteral( "oldconfigstyle" ) ) )

src/auth/basic/qgsauthbasicmethod.h

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ class QgsAuthBasicMethod : public QgsAuthMethod
4444
bool updateDataSourceUriItems( QStringList &connectionItems, const QString &authcfg,
4545
const QString &dataprovider = QString() ) override;
4646

47+
48+
bool updateNetworkProxy( QNetworkProxy &proxy, const QString &authcfg,
49+
const QString &dataprovider = QString() ) override;
50+
4751
void clearCachedConfig( const QString &authcfg ) override;
4852

4953
void updateMethodConfig( QgsAuthMethodConfig &mconfig ) override;

src/core/auth/qgsauthmanager.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,31 @@ bool QgsAuthManager::updateDataSourceUriItems( QStringList &connectionItems, con
14571457
return false;
14581458
}
14591459

1460+
bool QgsAuthManager::updateNetworkProxy( QNetworkProxy &proxy, const QString &authcfg, const QString &dataprovider )
1461+
{
1462+
if ( isDisabled() )
1463+
return false;
1464+
1465+
QgsAuthMethod *authmethod = configAuthMethod( authcfg );
1466+
if ( authmethod )
1467+
{
1468+
if ( !( authmethod->supportedExpansions() & QgsAuthMethod::NetworkProxy ) )
1469+
{
1470+
QgsDebugMsg( QString( "Network request updating not supported by authcfg: %1" ).arg( authcfg ) );
1471+
return true;
1472+
}
1473+
1474+
if ( !authmethod->updateNetworkProxy( proxy, authcfg, dataprovider.toLower() ) )
1475+
{
1476+
authmethod->clearCachedConfig( authcfg );
1477+
return false;
1478+
}
1479+
return true;
1480+
}
1481+
1482+
return false;
1483+
}
1484+
14601485
bool QgsAuthManager::storeAuthSetting( const QString &key, const QVariant &value, bool encrypt )
14611486
{
14621487
if ( key.isEmpty() )

src/core/auth/qgsauthmanager.h

+10
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,16 @@ class CORE_EXPORT QgsAuthManager : public QObject
332332
bool updateDataSourceUriItems( QStringList &connectionItems SIP_INOUT, const QString &authcfg,
333333
const QString &dataprovider = QString() );
334334

335+
/**
336+
* Provider call to update a QNetworkProxy with an authentication config
337+
* \param proxy the QNetworkProxy
338+
* \param authcfg Associated authentication config id
339+
* \param dataprovider Provider key filter, offering logic branching in authentication method
340+
* \returns Whether operation succeeded
341+
*/
342+
bool updateNetworkProxy( QNetworkProxy &proxy SIP_INOUT, const QString &authcfg,
343+
const QString &dataprovider = QString() );
344+
335345
////////////////// Generic settings ///////////////////////
336346

337347
//! Store an authentication setting (stored as string via QVariant( value ).toString() )

src/core/auth/qgsauthmethod.h

+18-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ class CORE_EXPORT QgsAuthMethod : public QObject
5151
NetworkReply = 0x2,
5252
DataSourceUri = 0x4,
5353
GenericDataSourceUri = 0x8,
54-
All = NetworkRequest | NetworkReply | DataSourceUri | GenericDataSourceUri
54+
NetworkProxy = 0x16,
55+
All = NetworkRequest | NetworkReply | DataSourceUri | GenericDataSourceUri | NetworkProxy
5556
};
5657
Q_DECLARE_FLAGS( Expansions, Expansion )
5758

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

130+
/** Update proxy settings with authentication components
131+
* \param proxy
132+
* \param authcfg Authentication configuration ID
133+
* \param dataprovider Textual key for a data provider, e.g. 'proxy', that allows
134+
* for custom updater code specific to the provider
135+
* \returns Whether the update succeeded
136+
*/
137+
virtual bool updateNetworkProxy( QNetworkProxy &proxy, const QString &authcfg,
138+
const QString &dataprovider = QString() )
139+
{
140+
Q_UNUSED( proxy )
141+
Q_UNUSED( authcfg )
142+
Q_UNUSED( dataprovider )
143+
return true; // noop
144+
}
145+
129146
/** Clear any cached configuration. Called when the QgsAuthManager deletes an authentication configuration (authcfg).
130147
* \note It is highly recommended that a cache of authentication components (per requested authcfg)
131148
* be implemented, to avoid excessive queries on the auth database. Such a cache could be as

0 commit comments

Comments
 (0)