Skip to content
Permalink
Browse files
[authentication] Migrate away from QRegExp in favor of QRegularExpres…
…sion
  • Loading branch information
nirvn committed Jul 13, 2021
1 parent 666b229 commit 0b12d48e71346583d7ebb43535e4e5e09536b2a1
@@ -26,6 +26,7 @@

#include <QNetworkProxy>
#include <QMutexLocker>
#include <QRegularExpression>
#include <QUuid>

const QString QgsAuthBasicMethod::AUTH_METHOD_KEY = QStringLiteral( "Basic" );
@@ -112,7 +113,7 @@ bool QgsAuthBasicMethod::updateDataSourceUriItems( QStringList &connectionItems,
}

QString sslMode = QStringLiteral( "prefer" );
int sslModeIdx = connectionItems.indexOf( QRegExp( "^sslmode=.*" ) );
int sslModeIdx = connectionItems.indexOf( QRegularExpression( "^sslmode=.*" ) );
if ( sslModeIdx != -1 )
{
sslMode = connectionItems.at( sslModeIdx ).split( '=' ).at( 1 );
@@ -171,7 +172,7 @@ bool QgsAuthBasicMethod::updateDataSourceUriItems( QStringList &connectionItems,
}
else if ( uri.startsWith( QLatin1String( "SDE:" ) ) )
{
uri = uri.replace( QRegExp( ",$" ), QStringLiteral( ",%1,%2" ).arg( username, password ) );
uri = uri.replace( QRegularExpression( ",$" ), QStringLiteral( ",%1,%2" ).arg( username, password ) );
}
else if ( uri.startsWith( QLatin1String( "IDB" ) ) )
{
@@ -209,7 +210,7 @@ bool QgsAuthBasicMethod::updateDataSourceUriItems( QStringList &connectionItems,
}
else if ( uri.startsWith( QLatin1String( "ODBC:" ) ) )
{
uri = uri.replace( QRegExp( "^ODBC:@?" ), "ODBC:" + username + '/' + password + '@' );
uri = uri.replace( QRegularExpression( "^ODBC:@?" ), "ODBC:" + username + '/' + password + '@' );
}
else if ( uri.startsWith( QLatin1String( "couchdb" ) )
|| uri.startsWith( QLatin1String( "DODS" ) )
@@ -240,7 +241,7 @@ bool QgsAuthBasicMethod::updateDataSourceUriItems( QStringList &connectionItems,
else // Not-ogr
{
QString userparam = "user='" + escapeUserPass( username ) + '\'';
int userindx = connectionItems.indexOf( QRegExp( "^user='.*" ) );
int userindx = connectionItems.indexOf( QRegularExpression( "^user='.*" ) );
if ( userindx != -1 )
{
connectionItems.replace( userindx, userparam );
@@ -251,7 +252,7 @@ bool QgsAuthBasicMethod::updateDataSourceUriItems( QStringList &connectionItems,
}

QString passparam = "password='" + escapeUserPass( password ) + '\'';
int passindx = connectionItems.indexOf( QRegExp( "^password='.*" ) );
int passindx = connectionItems.indexOf( QRegularExpression( "^password='.*" ) );
if ( passindx != -1 )
{
connectionItems.replace( passindx, passparam );
@@ -263,7 +264,7 @@ bool QgsAuthBasicMethod::updateDataSourceUriItems( QStringList &connectionItems,
// add extra CAs
if ( ! caparam.isEmpty() )
{
int sslcaindx = connectionItems.indexOf( QRegExp( "^sslrootcert='.*" ) );
int sslcaindx = connectionItems.indexOf( QRegularExpression( "^sslrootcert='.*" ) );
if ( sslcaindx != -1 )
{
connectionItems.replace( sslcaindx, caparam );
@@ -26,6 +26,7 @@

#include <QDir>
#include <QFile>
#include <QRegularExpression>
#include <QUuid>
#ifndef QT_NO_SSL
#include <QtCrypto>
@@ -161,7 +162,7 @@ bool QgsAuthIdentCertMethod::updateDataSourceUriItems( QStringList &connectionIt

// add uri parameters
QString userparam = "user='" + commonName + "'";
int userindx = connectionItems.indexOf( QRegExp( "^user='.*" ) );
int userindx = connectionItems.indexOf( QRegularExpression( "^user='.*" ) );
if ( userindx != -1 )
{
connectionItems.replace( userindx, userparam );
@@ -172,7 +173,7 @@ bool QgsAuthIdentCertMethod::updateDataSourceUriItems( QStringList &connectionIt
}

QString certparam = "sslcert='" + certFilePath + "'";
int sslcertindx = connectionItems.indexOf( QRegExp( "^sslcert='.*" ) );
int sslcertindx = connectionItems.indexOf( QRegularExpression( "^sslcert='.*" ) );
if ( sslcertindx != -1 )
{
connectionItems.replace( sslcertindx, certparam );
@@ -183,7 +184,7 @@ bool QgsAuthIdentCertMethod::updateDataSourceUriItems( QStringList &connectionIt
}

QString keyparam = "sslkey='" + keyFilePath + "'";
int sslkeyindx = connectionItems.indexOf( QRegExp( "^sslkey='.*" ) );
int sslkeyindx = connectionItems.indexOf( QRegularExpression( "^sslkey='.*" ) );
if ( sslkeyindx != -1 )
{
connectionItems.replace( sslkeyindx, keyparam );
@@ -194,7 +195,7 @@ bool QgsAuthIdentCertMethod::updateDataSourceUriItems( QStringList &connectionIt
}

QString caparam = "sslrootcert='" + caFilePath + "'";
int sslcaindx = connectionItems.indexOf( QRegExp( "^sslrootcert='.*" ) );
int sslcaindx = connectionItems.indexOf( QRegularExpression( "^sslrootcert='.*" ) );
if ( sslcaindx != -1 )
{
connectionItems.replace( sslcaindx, caparam );
@@ -26,6 +26,7 @@

#include <QDir>
#include <QFile>
#include <QRegularExpression>
#include <QUuid>
#ifndef QT_NO_SSL
#include <QtCrypto>
@@ -193,7 +194,7 @@ bool QgsAuthPkiPathsMethod::updateDataSourceUriItems( QStringList &connectionIte

// add uri parameters
QString userparam = "user='" + commonName + "'";
int userindx = connectionItems.indexOf( QRegExp( "^user='.*" ) );
int userindx = connectionItems.indexOf( QRegularExpression( "^user='.*" ) );
if ( userindx != -1 )
{
connectionItems.replace( userindx, userparam );
@@ -205,7 +206,7 @@ bool QgsAuthPkiPathsMethod::updateDataSourceUriItems( QStringList &connectionIte

// add uri parameters
QString certparam = "sslcert='" + certFilePath + "'";
int sslcertindx = connectionItems.indexOf( QRegExp( "^sslcert='.*" ) );
int sslcertindx = connectionItems.indexOf( QRegularExpression( "^sslcert='.*" ) );
if ( sslcertindx != -1 )
{
connectionItems.replace( sslcertindx, certparam );
@@ -216,7 +217,7 @@ bool QgsAuthPkiPathsMethod::updateDataSourceUriItems( QStringList &connectionIte
}

QString keyparam = "sslkey='" + keyFilePath + "'";
int sslkeyindx = connectionItems.indexOf( QRegExp( "^sslkey='.*" ) );
int sslkeyindx = connectionItems.indexOf( QRegularExpression( "^sslkey='.*" ) );
if ( sslkeyindx != -1 )
{
connectionItems.replace( sslkeyindx, keyparam );
@@ -227,7 +228,7 @@ bool QgsAuthPkiPathsMethod::updateDataSourceUriItems( QStringList &connectionIte
}

QString caparam = "sslrootcert='" + caFilePath + "'";
int sslcaindx = connectionItems.indexOf( QRegExp( "^sslrootcert='.*" ) );
int sslcaindx = connectionItems.indexOf( QRegularExpression( "^sslrootcert='.*" ) );
if ( sslcaindx != -1 )
{
connectionItems.replace( sslcaindx, caparam );
@@ -16,8 +16,17 @@

#include "qgsauthpkcs12method.h"

#include "qgsauthcertutils.h"
#include "qgsauthmanager.h"
#include "qgslogger.h"
#include "qgsapplication.h"
#ifdef HAVE_GUI
#include "qgsauthpkcs12edit.h"
#endif

#include <QDir>
#include <QFile>
#include <QRegularExpression>
#include <QUuid>
#ifndef QT_NO_SSL
#include <QtCrypto>
@@ -26,14 +35,6 @@
#endif
#include <QMutexLocker>

#include "qgsauthcertutils.h"
#include "qgsauthmanager.h"
#include "qgslogger.h"
#include "qgsapplication.h"
#ifdef HAVE_GUI
#include "qgsauthpkcs12edit.h"
#endif

const QString QgsAuthPkcs12Method::AUTH_METHOD_KEY = QStringLiteral( "PKI-PKCS#12" );
const QString QgsAuthPkcs12Method::AUTH_METHOD_DESCRIPTION = QStringLiteral( "PKI PKCS#12 authentication" );
const QString QgsAuthPkcs12Method::AUTH_METHOD_DISPLAY_DESCRIPTION = tr( "PKI PKCS#12 authentication" );
@@ -192,7 +193,7 @@ bool QgsAuthPkcs12Method::updateDataSourceUriItems( QStringList &connectionItems

// add uri parameters
QString userparam = "user='" + commonName + "'";
int userindx = connectionItems.indexOf( QRegExp( "^user='.*" ) );
int userindx = connectionItems.indexOf( QRegularExpression( "^user='.*" ) );
if ( userindx != -1 )
{
connectionItems.replace( userindx, userparam );
@@ -203,7 +204,7 @@ bool QgsAuthPkcs12Method::updateDataSourceUriItems( QStringList &connectionItems
}

QString certparam = "sslcert='" + certFilePath + "'";
int sslcertindx = connectionItems.indexOf( QRegExp( "^sslcert='.*" ) );
int sslcertindx = connectionItems.indexOf( QRegularExpression( "^sslcert='.*" ) );
if ( sslcertindx != -1 )
{
connectionItems.replace( sslcertindx, certparam );
@@ -214,7 +215,7 @@ bool QgsAuthPkcs12Method::updateDataSourceUriItems( QStringList &connectionItems
}

QString keyparam = "sslkey='" + keyFilePath + "'";
int sslkeyindx = connectionItems.indexOf( QRegExp( "^sslkey='.*" ) );
int sslkeyindx = connectionItems.indexOf( QRegularExpression( "^sslkey='.*" ) );
if ( sslkeyindx != -1 )
{
connectionItems.replace( sslkeyindx, keyparam );
@@ -225,7 +226,7 @@ bool QgsAuthPkcs12Method::updateDataSourceUriItems( QStringList &connectionItems
}

QString caparam = "sslrootcert='" + caFilePath + "'";
int sslcaindx = connectionItems.indexOf( QRegExp( "^sslrootcert='.*" ) );
int sslcaindx = connectionItems.indexOf( QRegularExpression( "^sslrootcert='.*" ) );
if ( sslcaindx != -1 )
{
connectionItems.replace( sslcaindx, caparam );
@@ -33,6 +33,7 @@
#include <QSqlDriver>
#include <QDomElement>
#include <QDomDocument>
#include <QRegularExpression>

#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
#include <QRandomGenerator>
@@ -917,8 +918,7 @@ bool QgsAuthManager::configIdUnique( const QString &id ) const

bool QgsAuthManager::hasConfigId( const QString &txt ) const
{
QRegExp rx( AUTH_CFG_REGEX );
return rx.indexIn( txt ) != -1;
return txt.indexOf( QRegularExpression( AUTH_CFG_REGEX ) ) != -1;
}

QgsAuthMethodConfigsMap QgsAuthManager::availableAuthMethodConfigs( const QString &dataprovider )
@@ -2437,8 +2437,9 @@ bool QgsAuthManager::updateIgnoredSslErrorsCacheFromConfig( const QgsAuthConfigS
bool QgsAuthManager::updateIgnoredSslErrorsCache( const QString &shahostport, const QList<QSslError> &errors )
{
QMutexLocker locker( mMutex.get() );
QRegExp rx( "\\S+:\\S+:\\d+" );
if ( !rx.exactMatch( shahostport ) )
const QRegularExpression rx( QRegularExpression::anchoredPattern( "\\S+:\\S+:\\d+" ) );
const QRegularExpressionMatch match = rx.match( shahostport );
if ( !match.hasMatch() )
{
QgsDebugMsg( "Passed shahostport does not match \\S+:\\S+:\\d+, "
"e.g. 74a4ef5ea94512a43769b744cda0ca5049a72491:www.example.com:443" );
@@ -16,10 +16,6 @@

#include "qgsauthmethodregistry.h"

#include <QString>
#include <QDir>
#include <QLibrary>

#include "qgis.h"
#include "qgsauthconfig.h"
#include "qgsauthmethod.h"
@@ -39,6 +35,11 @@
#include "qgsauthpkcs12method.h"
#endif

#include <QString>
#include <QDir>
#include <QLibrary>
#include <QRegularExpression>


static QgsAuthMethodRegistry *sInstance = nullptr;

@@ -133,7 +134,7 @@ void QgsAuthMethodRegistry::init()

// auth method file regex pattern, only files matching the pattern are loaded if the variable is defined
QString filePattern = getenv( "QGIS_AUTHMETHOD_FILE" );
QRegExp fileRegexp;
QRegularExpression fileRegexp;
if ( !filePattern.isEmpty() )
{
fileRegexp.setPattern( filePattern );
@@ -144,9 +145,9 @@ void QgsAuthMethodRegistry::init()
{
QFileInfo fi( it.next() );

if ( !fileRegexp.isEmpty() )
if ( !filePattern.isEmpty() )
{
if ( fileRegexp.indexIn( fi.fileName() ) == -1 )
if ( fi.fileName().indexOf( fileRegexp ) == -1 )
{
QgsDebugMsg( "auth method " + fi.fileName() + " skipped because doesn't match pattern " + filePattern );
continue;
@@ -21,6 +21,8 @@
#include "qgsauthmanager.h"
#include "qgsapplication.h"

#include <QRegularExpression>


QgsAuthConfigIdEdit::QgsAuthConfigIdEdit( QWidget *parent, const QString &authcfg, bool allowEmpty )
: QWidget( parent )
@@ -117,6 +119,5 @@ void QgsAuthConfigIdEdit::leAuthCfg_textChanged( const QString &txt )

bool QgsAuthConfigIdEdit::isAlphaNumeric( const QString &authcfg )
{
QRegExp rx( "([a-z]|[A-Z]|[0-9]){7}" );
return rx.indexIn( authcfg ) != -1;
return authcfg.indexOf( QRegularExpression( "([a-z]|[A-Z]|[0-9]){7}" ) ) != -1;
}
@@ -17,10 +17,6 @@
#include "qgsauthconfigselect.h"
#include "ui_qgsauthconfigselect.h"

#include <QHash>
#include <QMessageBox>
#include <QTimer>

#include "qgsauthconfig.h"
#include "qgsauthguiutils.h"
#include "qgsauthmanager.h"
@@ -29,6 +25,11 @@
#include "qgsapplication.h"
#include "qgsauthmethodmetadata.h"

#include <QHash>
#include <QMessageBox>
#include <QTimer>
#include <QRegularExpression>


QgsAuthConfigSelect::QgsAuthConfigSelect( QWidget *parent, const QString &dataprovider )
: QWidget( parent )
@@ -382,8 +383,7 @@ void QgsAuthConfigUriEdit::authCfgRemoved( const QString &authcfg )

int QgsAuthConfigUriEdit::authCfgIndex()
{
QRegExp rx( QgsApplication::authManager()->configIdRegex() );
return rx.indexIn( mDataUri );
return mDataUri.indexOf( QRegularExpression( QgsApplication::authManager()->configIdRegex() ) );
}

QString QgsAuthConfigUriEdit::authCfgFromUri()

0 comments on commit 0b12d48

Please sign in to comment.