Skip to content

Commit 5e097d8

Browse files
authoredOct 11, 2017
Merge pull request #5345 from boundlessgeo/auth_mutex_backport
Auth mutex backport
2 parents 5231ef6 + 79837b0 commit 5e097d8

File tree

9 files changed

+77
-39
lines changed

9 files changed

+77
-39
lines changed
 

‎src/auth/basic/qgsauthbasicmethod.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020
#include "qgsauthmanager.h"
2121
#include "qgslogger.h"
2222

23+
#include <QNetworkProxy>
24+
#include <QMutexLocker>
25+
2326
static const QString AUTH_METHOD_KEY = "Basic";
2427
static const QString AUTH_METHOD_DESCRIPTION = "Basic authentication";
2528

26-
QMap<QString, QgsAuthMethodConfig> QgsAuthBasicMethod::mAuthConfigCache = QMap<QString, QgsAuthMethodConfig>();
29+
QMap<QString, QgsAuthMethodConfig> QgsAuthBasicMethod::sAuthConfigCache = QMap<QString, QgsAuthMethodConfig>();
2730

2831

2932
QgsAuthBasicMethod::QgsAuthBasicMethod()
@@ -149,12 +152,13 @@ void QgsAuthBasicMethod::clearCachedConfig( const QString &authcfg )
149152

150153
QgsAuthMethodConfig QgsAuthBasicMethod::getMethodConfig( const QString &authcfg, bool fullconfig )
151154
{
155+
QMutexLocker locker( &mConfigMutex );
152156
QgsAuthMethodConfig mconfig;
153157

154158
// check if it is cached
155-
if ( mAuthConfigCache.contains( authcfg ) )
159+
if ( sAuthConfigCache.contains( authcfg ) )
156160
{
157-
mconfig = mAuthConfigCache.value( authcfg );
161+
mconfig = sAuthConfigCache.value( authcfg );
158162
QgsDebugMsg( QString( "Retrieved config for authcfg: %1" ).arg( authcfg ) );
159163
return mconfig;
160164
}
@@ -167,22 +171,25 @@ QgsAuthMethodConfig QgsAuthBasicMethod::getMethodConfig( const QString &authcfg,
167171
}
168172

169173
// cache bundle
174+
locker.unlock();
170175
putMethodConfig( authcfg, mconfig );
171176

172177
return mconfig;
173178
}
174179

175180
void QgsAuthBasicMethod::putMethodConfig( const QString &authcfg, const QgsAuthMethodConfig& mconfig )
176181
{
182+
QMutexLocker locker( &mConfigMutex );
177183
QgsDebugMsg( QString( "Putting basic config for authcfg: %1" ).arg( authcfg ) );
178-
mAuthConfigCache.insert( authcfg, mconfig );
184+
sAuthConfigCache.insert( authcfg, mconfig );
179185
}
180186

181187
void QgsAuthBasicMethod::removeMethodConfig( const QString &authcfg )
182188
{
183-
if ( mAuthConfigCache.contains( authcfg ) )
189+
QMutexLocker locker( &mConfigMutex );
190+
if ( sAuthConfigCache.contains( authcfg ) )
184191
{
185-
mAuthConfigCache.remove( authcfg );
192+
sAuthConfigCache.remove( authcfg );
186193
QgsDebugMsg( QString( "Removed basic config for authcfg: %1" ).arg( authcfg ) );
187194
}
188195
}

‎src/auth/basic/qgsauthbasicmethod.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define QGSAUTHBASICMETHOD_H
1919

2020
#include <QObject>
21+
#include <QMutex>
2122

2223
#include "qgsauthconfig.h"
2324
#include "qgsauthmethod.h"
@@ -57,7 +58,9 @@ class QgsAuthBasicMethod : public QgsAuthMethod
5758

5859
QString escapeUserPass( const QString &theVal, QChar delim = '\'' ) const;
5960

60-
static QMap<QString, QgsAuthMethodConfig> mAuthConfigCache;
61+
static QMap<QString, QgsAuthMethodConfig> sAuthConfigCache;
62+
63+
QMutex mConfigMutex;
6164
};
6265

6366
#endif // QGSAUTHBASICMETHOD_H

‎src/auth/identcert/qgsauthidentcertmethod.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <QSslConfiguration>
2626
#include <QSslError>
2727
#endif
28+
#include <QMutexLocker>
2829

2930
#include "qgsauthcertutils.h"
3031
#include "qgsauthmanager.h"
@@ -33,7 +34,7 @@
3334
static const QString AUTH_METHOD_KEY = "Identity-Cert";
3435
static const QString AUTH_METHOD_DESCRIPTION = "Identity certificate authentication";
3536

36-
QMap<QString, QgsPkiConfigBundle *> QgsAuthIdentCertMethod::mPkiConfigBundleCache = QMap<QString, QgsPkiConfigBundle *>();
37+
QMap<QString, QgsPkiConfigBundle *> QgsAuthIdentCertMethod::sPkiConfigBundleCache = QMap<QString, QgsPkiConfigBundle *>();
3738

3839

3940
QgsAuthIdentCertMethod::QgsAuthIdentCertMethod()
@@ -51,8 +52,9 @@ QgsAuthIdentCertMethod::QgsAuthIdentCertMethod()
5152

5253
QgsAuthIdentCertMethod::~QgsAuthIdentCertMethod()
5354
{
54-
qDeleteAll( mPkiConfigBundleCache );
55-
mPkiConfigBundleCache.clear();
55+
QMutexLocker locker( &mConfigMutex );
56+
qDeleteAll( sPkiConfigBundleCache );
57+
sPkiConfigBundleCache.clear();
5658
}
5759

5860
QString QgsAuthIdentCertMethod::key() const
@@ -220,12 +222,13 @@ void QgsAuthIdentCertMethod::updateMethodConfig( QgsAuthMethodConfig &mconfig )
220222

221223
QgsPkiConfigBundle *QgsAuthIdentCertMethod::getPkiConfigBundle( const QString &authcfg )
222224
{
223-
QgsPkiConfigBundle * bundle = nullptr;
225+
QMutexLocker locker( &mConfigMutex );
226+
QgsPkiConfigBundle *bundle = nullptr;
224227

225228
// check if it is cached
226-
if ( mPkiConfigBundleCache.contains( authcfg ) )
229+
if ( sPkiConfigBundleCache.contains( authcfg ) )
227230
{
228-
bundle = mPkiConfigBundleCache.value( authcfg );
231+
bundle = sPkiConfigBundleCache.value( authcfg );
229232
if ( bundle )
230233
{
231234
QgsDebugMsg( QString( "Retrieved PKI bundle for authcfg %1" ).arg( authcfg ) );
@@ -264,6 +267,7 @@ QgsPkiConfigBundle *QgsAuthIdentCertMethod::getPkiConfigBundle( const QString &a
264267

265268
bundle = new QgsPkiConfigBundle( mconfig, clientcert, clientkey );
266269

270+
locker.unlock();
267271
// cache bundle
268272
putPkiConfigBundle( authcfg, bundle );
269273

@@ -272,15 +276,17 @@ QgsPkiConfigBundle *QgsAuthIdentCertMethod::getPkiConfigBundle( const QString &a
272276

273277
void QgsAuthIdentCertMethod::putPkiConfigBundle( const QString &authcfg, QgsPkiConfigBundle *pkibundle )
274278
{
279+
QMutexLocker locker( &mConfigMutex );
275280
QgsDebugMsg( QString( "Putting PKI bundle for authcfg %1" ).arg( authcfg ) );
276-
mPkiConfigBundleCache.insert( authcfg, pkibundle );
281+
sPkiConfigBundleCache.insert( authcfg, pkibundle );
277282
}
278283

279284
void QgsAuthIdentCertMethod::removePkiConfigBundle( const QString &authcfg )
280285
{
281-
if ( mPkiConfigBundleCache.contains( authcfg ) )
286+
QMutexLocker locker( &mConfigMutex );
287+
if ( sPkiConfigBundleCache.contains( authcfg ) )
282288
{
283-
QgsPkiConfigBundle * pkibundle = mPkiConfigBundleCache.take( authcfg );
289+
QgsPkiConfigBundle * pkibundle = sPkiConfigBundleCache.take( authcfg );
284290
delete pkibundle;
285291
pkibundle = nullptr;
286292
QgsDebugMsg( QString( "Removed PKI bundle for authcfg: %1" ).arg( authcfg ) );

‎src/auth/identcert/qgsauthidentcertmethod.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define QGSAUTHIDENTCERTMETHOD_H
1919

2020
#include <QObject>
21+
#include <QMutex>
2122

2223
#include "qgsauthconfig.h"
2324
#include "qgsauthmethod.h"
@@ -56,7 +57,9 @@ class QgsAuthIdentCertMethod : public QgsAuthMethod
5657

5758
void removePkiConfigBundle( const QString &authcfg );
5859

59-
static QMap<QString, QgsPkiConfigBundle *> mPkiConfigBundleCache;
60+
static QMap<QString, QgsPkiConfigBundle *> sPkiConfigBundleCache;
61+
62+
QMutex mConfigMutex;
6063
};
6164

6265
#endif // QGSAUTHIDENTCERTMETHOD_H

‎src/auth/pkipaths/qgsauthpkipathsmethod.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <QSslConfiguration>
2626
#include <QSslError>
2727
#endif
28+
#include <QMutexLocker>
2829

2930
#include "qgsauthcertutils.h"
3031
#include "qgsauthmanager.h"
@@ -34,7 +35,7 @@
3435
static const QString AUTH_METHOD_KEY = "PKI-Paths";
3536
static const QString AUTH_METHOD_DESCRIPTION = "PKI paths authentication";
3637

37-
QMap<QString, QgsPkiConfigBundle *> QgsAuthPkiPathsMethod::mPkiConfigBundleCache = QMap<QString, QgsPkiConfigBundle *>();
38+
QMap<QString, QgsPkiConfigBundle *> QgsAuthPkiPathsMethod::sPkiConfigBundleCache = QMap<QString, QgsPkiConfigBundle *>();
3839

3940

4041
QgsAuthPkiPathsMethod::QgsAuthPkiPathsMethod()
@@ -52,8 +53,9 @@ QgsAuthPkiPathsMethod::QgsAuthPkiPathsMethod()
5253

5354
QgsAuthPkiPathsMethod::~QgsAuthPkiPathsMethod()
5455
{
55-
qDeleteAll( mPkiConfigBundleCache );
56-
mPkiConfigBundleCache.clear();
56+
QMutexLocker locker( &mConfigMutex );
57+
qDeleteAll( sPkiConfigBundleCache );
58+
sPkiConfigBundleCache.clear();
5759
}
5860

5961
QString QgsAuthPkiPathsMethod::key() const
@@ -224,12 +226,13 @@ void QgsAuthPkiPathsMethod::updateMethodConfig( QgsAuthMethodConfig &mconfig )
224226

225227
QgsPkiConfigBundle *QgsAuthPkiPathsMethod::getPkiConfigBundle( const QString &authcfg )
226228
{
227-
QgsPkiConfigBundle * bundle = nullptr;
229+
QMutexLocker locker( &mConfigMutex );
230+
QgsPkiConfigBundle *bundle = nullptr;
228231

229232
// check if it is cached
230-
if ( mPkiConfigBundleCache.contains( authcfg ) )
233+
if ( sPkiConfigBundleCache.contains( authcfg ) )
231234
{
232-
bundle = mPkiConfigBundleCache.value( authcfg );
235+
bundle = sPkiConfigBundleCache.value( authcfg );
233236
if ( bundle )
234237
{
235238
QgsDebugMsg( QString( "Retrieved PKI bundle for authcfg %1" ).arg( authcfg ) );
@@ -266,6 +269,7 @@ QgsPkiConfigBundle *QgsAuthPkiPathsMethod::getPkiConfigBundle( const QString &au
266269

267270
bundle = new QgsPkiConfigBundle( mconfig, clientcert, clientkey );
268271

272+
locker.unlock();
269273
// cache bundle
270274
putPkiConfigBundle( authcfg, bundle );
271275

@@ -274,15 +278,17 @@ QgsPkiConfigBundle *QgsAuthPkiPathsMethod::getPkiConfigBundle( const QString &au
274278

275279
void QgsAuthPkiPathsMethod::putPkiConfigBundle( const QString &authcfg, QgsPkiConfigBundle *pkibundle )
276280
{
281+
QMutexLocker locker( &mConfigMutex );
277282
QgsDebugMsg( QString( "Putting PKI bundle for authcfg %1" ).arg( authcfg ) );
278-
mPkiConfigBundleCache.insert( authcfg, pkibundle );
283+
sPkiConfigBundleCache.insert( authcfg, pkibundle );
279284
}
280285

281286
void QgsAuthPkiPathsMethod::removePkiConfigBundle( const QString &authcfg )
282287
{
283-
if ( mPkiConfigBundleCache.contains( authcfg ) )
288+
QMutexLocker locker( &mConfigMutex );
289+
if ( sPkiConfigBundleCache.contains( authcfg ) )
284290
{
285-
QgsPkiConfigBundle * pkibundle = mPkiConfigBundleCache.take( authcfg );
291+
QgsPkiConfigBundle * pkibundle = sPkiConfigBundleCache.take( authcfg );
286292
delete pkibundle;
287293
pkibundle = nullptr;
288294
QgsDebugMsg( QString( "Removed PKI bundle for authcfg: %1" ).arg( authcfg ) );

‎src/auth/pkipaths/qgsauthpkipathsmethod.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define QGSAUTHPKIPATHSMETHOD_H
1919

2020
#include <QObject>
21+
#include <QMutex>
2122

2223
#include "qgsauthconfig.h"
2324
#include "qgsauthmethod.h"
@@ -56,7 +57,10 @@ class QgsAuthPkiPathsMethod : public QgsAuthMethod
5657

5758
void removePkiConfigBundle( const QString &authcfg );
5859

59-
static QMap<QString, QgsPkiConfigBundle *> mPkiConfigBundleCache;
60+
static QMap<QString, QgsPkiConfigBundle *> sPkiConfigBundleCache;
61+
62+
QMutex mConfigMutex;
63+
6064
};
6165

6266
#endif // QGSAUTHPKIPATHSMETHOD_H

‎src/auth/pkipkcs12/qgsauthpkcs12method.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <QSslConfiguration>
2626
#include <QSslError>
2727
#endif
28+
#include <QMutexLocker>
2829

2930
#include "qgsauthcertutils.h"
3031
#include "qgsauthmanager.h"
@@ -34,7 +35,7 @@
3435
static const QString AUTH_METHOD_KEY = "PKI-PKCS#12";
3536
static const QString AUTH_METHOD_DESCRIPTION = "PKI PKCS#12 authentication";
3637

37-
QMap<QString, QgsPkiConfigBundle *> QgsAuthPkcs12Method::mPkiConfigBundleCache = QMap<QString, QgsPkiConfigBundle *>();
38+
QMap<QString, QgsPkiConfigBundle *> QgsAuthPkcs12Method::sPkiConfigBundleCache = QMap<QString, QgsPkiConfigBundle *>();
3839

3940

4041
QgsAuthPkcs12Method::QgsAuthPkcs12Method()
@@ -52,8 +53,9 @@ QgsAuthPkcs12Method::QgsAuthPkcs12Method()
5253

5354
QgsAuthPkcs12Method::~QgsAuthPkcs12Method()
5455
{
55-
qDeleteAll( mPkiConfigBundleCache );
56-
mPkiConfigBundleCache.clear();
56+
QMutexLocker locker( &mConfigMutex );
57+
qDeleteAll( sPkiConfigBundleCache );
58+
sPkiConfigBundleCache.clear();
5759
}
5860

5961
QString QgsAuthPkcs12Method::key() const
@@ -222,12 +224,13 @@ void QgsAuthPkcs12Method::updateMethodConfig( QgsAuthMethodConfig &mconfig )
222224

223225
QgsPkiConfigBundle *QgsAuthPkcs12Method::getPkiConfigBundle( const QString &authcfg )
224226
{
225-
QgsPkiConfigBundle * bundle = nullptr;
227+
QMutexLocker locker( &mConfigMutex );
228+
QgsPkiConfigBundle *bundle = nullptr;
226229

227230
// check if it is cached
228-
if ( mPkiConfigBundleCache.contains( authcfg ) )
231+
if ( sPkiConfigBundleCache.contains( authcfg ) )
229232
{
230-
bundle = mPkiConfigBundleCache.value( authcfg );
233+
bundle = sPkiConfigBundleCache.value( authcfg );
231234
if ( bundle )
232235
{
233236
QgsDebugMsg( QString( "Retrieved PKI bundle for authcfg %1" ).arg( authcfg ) );
@@ -272,6 +275,7 @@ QgsPkiConfigBundle *QgsAuthPkcs12Method::getPkiConfigBundle( const QString &auth
272275

273276
bundle = new QgsPkiConfigBundle( mconfig, clientcert, clientkey );
274277

278+
locker.unlock();
275279
// cache bundle
276280
putPkiConfigBundle( authcfg, bundle );
277281

@@ -280,15 +284,17 @@ QgsPkiConfigBundle *QgsAuthPkcs12Method::getPkiConfigBundle( const QString &auth
280284

281285
void QgsAuthPkcs12Method::putPkiConfigBundle( const QString &authcfg, QgsPkiConfigBundle *pkibundle )
282286
{
287+
QMutexLocker locker( &mConfigMutex );
283288
QgsDebugMsg( QString( "Putting PKI bundle for authcfg %1" ).arg( authcfg ) );
284-
mPkiConfigBundleCache.insert( authcfg, pkibundle );
289+
sPkiConfigBundleCache.insert( authcfg, pkibundle );
285290
}
286291

287292
void QgsAuthPkcs12Method::removePkiConfigBundle( const QString &authcfg )
288293
{
289-
if ( mPkiConfigBundleCache.contains( authcfg ) )
294+
QMutexLocker locker( &mConfigMutex );
295+
if ( sPkiConfigBundleCache.contains( authcfg ) )
290296
{
291-
QgsPkiConfigBundle * pkibundle = mPkiConfigBundleCache.take( authcfg );
297+
QgsPkiConfigBundle * pkibundle = sPkiConfigBundleCache.take( authcfg );
292298
delete pkibundle;
293299
pkibundle = nullptr;
294300
QgsDebugMsg( QString( "Removed PKI bundle for authcfg: %1" ).arg( authcfg ) );

‎src/auth/pkipkcs12/qgsauthpkcs12method.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define QGSAUTHPKCS12METHOD_H
1919

2020
#include <QObject>
21+
#include <QMutex>
2122

2223
#include "qgsauthconfig.h"
2324
#include "qgsauthmethod.h"
@@ -57,7 +58,9 @@ class QgsAuthPkcs12Method : public QgsAuthMethod
5758

5859
void removePkiConfigBundle( const QString &authcfg );
5960

60-
static QMap<QString, QgsPkiConfigBundle *> mPkiConfigBundleCache;
61+
static QMap<QString, QgsPkiConfigBundle *> sPkiConfigBundleCache;
62+
63+
QMutex mConfigMutex;
6164
};
6265

6366
#endif // QGSAUTHPKCS12METHOD_H

‎src/providers/wms/qgswmscapabilities.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,12 +501,12 @@ struct QgsWmsAuthorization
501501
{
502502
return QgsAuthManager::instance()->updateNetworkRequest( request, mAuthCfg );
503503
}
504-
else if ( !mUserName.isNull() || !mPassword.isNull() )
504+
else if ( !mUserName.isEmpty() || !mPassword.isEmpty() )
505505
{
506506
request.setRawHeader( "Authorization", "Basic " + QString( "%1:%2" ).arg( mUserName, mPassword ).toAscii().toBase64() );
507507
}
508508

509-
if ( !mReferer.isNull() )
509+
if ( !mReferer.isEmpty() )
510510
{
511511
request.setRawHeader( "Referer", QString( "%1" ).arg( mReferer ).toAscii() );
512512
}

0 commit comments

Comments
 (0)
Please sign in to comment.