Skip to content

Commit 829702b

Browse files
authoredSep 27, 2017
Merge pull request #5253 from boundlessgeo/auth_proxy
[auth][feature][needs-docs] Proxy authentication integration with QGIS authentication system
2 parents 6ce8b4c + 385cca6 commit 829702b

14 files changed

+324
-74
lines changed
 

‎python/core/auth/qgsauthmanager.sip

Lines changed: 11 additions & 0 deletions
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

Lines changed: 13 additions & 0 deletions
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/app/qgisapp.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -630,8 +630,6 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
630630
connect( mUserProfileManager, &QgsUserProfileManager::profilesChanged, this, &QgisApp::refreshProfileMenu );
631631
endProfile();
632632

633-
namSetup();
634-
635633
// load GUI: actions, menus, toolbars
636634
profiler->beginGroup( QStringLiteral( "qgisapp" ) );
637635
profiler->beginGroup( QStringLiteral( "startup" ) );
@@ -656,6 +654,10 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
656654
}
657655
endProfile();
658656

657+
mTray = new QSystemTrayIcon();
658+
mTray->setIcon( QIcon( QgsApplication::appIconPath() ) );
659+
mTray->hide();
660+
659661
startProfile( QStringLiteral( "Initializing authentication" ) );
660662
mSplash->showMessage( tr( "Initializing authentication" ), Qt::AlignHCenter | Qt::AlignBottom );
661663
qApp->processEvents();
@@ -666,6 +668,9 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
666668
}
667669
endProfile();
668670

671+
// Setup QgsNetworkAccessManager (this needs to happen after authentication, for proxy settings)
672+
namSetup();
673+
669674
// Create the themes folder for the user
670675
startProfile( QStringLiteral( "Creating theme folder" ) );
671676
QgsApplication::createThemeFolder();
@@ -1156,11 +1161,6 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
11561161
grabGesture( Qt::TapAndHoldGesture );
11571162
}
11581163

1159-
1160-
mTray = new QSystemTrayIcon();
1161-
mTray->setIcon( QIcon( QgsApplication::appIconPath() ) );
1162-
mTray->hide();
1163-
11641164
connect( QgsApplication::taskManager(), &QgsTaskManager::statusChanged, this, &QgisApp::onTaskCompleteShowNotify );
11651165

11661166
#ifdef Q_OS_WIN

‎src/app/qgsoptions.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "qgstolerance.h"
2929
#include "qgsscaleutils.h"
3030
#include "qgsnetworkaccessmanager.h"
31+
#include "qgsauthconfigselect.h"
3132
#include "qgsproject.h"
3233
#include "qgsdualview.h"
3334
#include "qgsrasterlayer.h"
@@ -307,6 +308,16 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl, const QList<QgsOpti
307308
// WMS/WMS-C default max retry in case of tile request errors
308309
mDefaultTileMaxRetrySpinBox->setValue( mSettings->value( QStringLiteral( "/qgis/defaultTileMaxRetry" ), "3" ).toInt() );
309310

311+
// Proxy stored authentication configurations
312+
mProxyAuthConfigSelect = new QgsAuthConfigSelect( this, QStringLiteral( "proxy" ) );
313+
tabAuth->insertTab( 1, mProxyAuthConfigSelect, tr( "Configurations" ) );
314+
QString authcfg = mSettings->value( QStringLiteral( "proxy/authcfg" ) ).toString();
315+
mProxyAuthConfigSelect->setConfigId( authcfg );
316+
if ( !authcfg.isEmpty() )
317+
{
318+
tabAuth->setCurrentIndex( tabAuth->indexOf( mProxyAuthConfigSelect ) );
319+
}
320+
310321
//Web proxy settings
311322
grpProxy->setChecked( mSettings->value( QStringLiteral( "proxy/proxyEnabled" ), "0" ).toBool() );
312323
leProxyHost->setText( mSettings->value( QStringLiteral( "proxy/proxyHost" ), "" ).toString() );
@@ -1162,6 +1173,9 @@ void QgsOptions::saveOptions()
11621173
// WMS/WMS-C default max retry in case of tile request errors
11631174
mSettings->setValue( QStringLiteral( "/qgis/defaultTileMaxRetry" ), mDefaultTileMaxRetrySpinBox->value() );
11641175

1176+
// Proxy stored authentication configurations
1177+
mSettings->setValue( QStringLiteral( "proxy/authcfg" ), mProxyAuthConfigSelect->configId( ) );
1178+
11651179
//Web proxy settings
11661180
mSettings->setValue( QStringLiteral( "proxy/proxyEnabled" ), grpProxy->isChecked() );
11671181
mSettings->setValue( QStringLiteral( "proxy/proxyHost" ), leProxyHost->text() );

‎src/app/qgsoptions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
class QgsExpressionContext;
3333
class QgsOptionsPageWidget;
3434
class QgsLocatorOptionsWidget;
35+
class QgsAuthConfigSelect;
3536

3637
/**
3738
* \class QgsOptions
@@ -255,6 +256,7 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption
255256

256257
QList< QgsOptionsPageWidget * > mAdditionalOptionWidgets;
257258
QgsLocatorOptionsWidget *mLocatorOptionsWidget = nullptr;
259+
QgsAuthConfigSelect *mProxyAuthConfigSelect = nullptr;
258260

259261
};
260262

‎src/auth/basic/qgsauthbasicmethod.cpp

Lines changed: 26 additions & 1 deletion
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

@@ -37,7 +39,8 @@ QgsAuthBasicMethod::QgsAuthBasicMethod()
3739
<< QStringLiteral( "ows" )
3840
<< QStringLiteral( "wfs" ) // convert to lowercase
3941
<< QStringLiteral( "wcs" )
40-
<< QStringLiteral( "wms" ) );
42+
<< QStringLiteral( "wms" )
43+
<< QStringLiteral( "proxy" ) );
4144
}
4245

4346
QgsAuthBasicMethod::~QgsAuthBasicMethod()
@@ -126,6 +129,28 @@ bool QgsAuthBasicMethod::updateDataSourceUriItems( QStringList &connectionItems,
126129
return true;
127130
}
128131

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+
129154
void QgsAuthBasicMethod::updateMethodConfig( QgsAuthMethodConfig &mconfig )
130155
{
131156
if ( mconfig.hasConfig( QStringLiteral( "oldconfigstyle" ) ) )

‎src/auth/basic/qgsauthbasicmethod.h

Lines changed: 4 additions & 0 deletions
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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,32 @@ 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( QStringLiteral( "Proxy 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+
QgsDebugMsg( QStringLiteral( "Proxy updated successfully from authcfg: %1" ).arg( authcfg ) );
1480+
return true;
1481+
}
1482+
1483+
return false;
1484+
}
1485+
14601486
bool QgsAuthManager::storeAuthSetting( const QString &key, const QVariant &value, bool encrypt )
14611487
{
14621488
if ( key.isEmpty() )

‎src/core/auth/qgsauthmanager.h

Lines changed: 10 additions & 0 deletions
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

Lines changed: 18 additions & 1 deletion
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

‎src/core/qgsnetworkaccessmanager.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ void QgsNetworkAccessManager::setupDefaultProxyAndCache()
321321
//read type, host, port, user, passw from settings
322322
QString proxyHost = settings.value( QStringLiteral( "proxy/proxyHost" ), "" ).toString();
323323
int proxyPort = settings.value( QStringLiteral( "proxy/proxyPort" ), "" ).toString().toInt();
324+
324325
QString proxyUser = settings.value( QStringLiteral( "proxy/proxyUser" ), "" ).toString();
325326
QString proxyPassword = settings.value( QStringLiteral( "proxy/proxyPassword" ), "" ).toString();
326327

@@ -356,7 +357,7 @@ void QgsNetworkAccessManager::setupDefaultProxyAndCache()
356357
{
357358
proxyType = QNetworkProxy::FtpCachingProxy;
358359
}
359-
QgsDebugMsg( QString( "setting proxy %1 %2:%3 %4/%5" )
360+
QgsDebugMsg( QStringLiteral( "setting proxy %1 %2:%3 %4/%5" )
360361
.arg( proxyType )
361362
.arg( proxyHost ).arg( proxyPort )
362363
.arg( proxyUser, proxyPassword )
@@ -365,6 +366,14 @@ void QgsNetworkAccessManager::setupDefaultProxyAndCache()
365366
}
366367
}
367368

369+
// Setup network proxy authentication configuration
370+
QString authcfg = settings.value( QStringLiteral( "proxy/authcfg" ), "" ).toString();
371+
if ( !authcfg.isEmpty( ) )
372+
{
373+
QgsDebugMsg( QStringLiteral( "setting proxy from stored authentication configuration %1" ).arg( authcfg ) );
374+
QgsAuthManager::instance()->updateNetworkProxy( proxy, authcfg );
375+
}
376+
368377
setFallbackProxyAndExcludes( proxy, excludes );
369378

370379
QgsNetworkDiskCache *newcache = qobject_cast<QgsNetworkDiskCache *>( cache() );

0 commit comments

Comments
 (0)
Please sign in to comment.