29using namespace Qt::StringLiterals;
31#ifdef HAVE_STATIC_PROVIDERS
32#include "qgsauthbasicmethod.h"
33#include "qgsauthesritokenmethod.h"
34#include "qgsauthidentcertmethod.h"
35#ifdef HAVE_OAUTH2_PLUGIN
36#include "qgsauthoauth2method.h"
38#include "qgsauthpkipathsmethod.h"
39#include "qgsauthpkcs12method.h"
45#include <QRegularExpression>
56 const QMutexLocker locker( &sMutex );
59 sInstance =
new QgsAuthMethodRegistry( pluginPath );
75 const QgsAuthMethodRegistry::AuthMethods::const_iterator i = metaData.find( authMethodKey );
77 if ( i != metaData.end() )
85QgsAuthMethodRegistry::QgsAuthMethodRegistry(
const QString &pluginPath )
91 char **argv = qApp->argv();
92 QString appDir = argv[0];
93 int bin = appDir.findRev(
"/bin", -1,
false );
94 QString baseDir = appDir.left( bin );
95 QString mLibraryDirectory = baseDir +
"/lib";
97 mLibraryDirectory.setPath( pluginPath );
98 mLibraryDirectory.setSorting( QDir::Name | QDir::IgnoreCase );
99 mLibraryDirectory.setFilter( QDir::Files | QDir::NoSymLinks );
104void QgsAuthMethodRegistry::init()
106#ifdef HAVE_STATIC_PROVIDERS
107 mAuthMethods[QgsAuthBasicMethod::AUTH_METHOD_KEY] =
new QgsAuthBasicMethodMetadata();
108 mAuthMethods[QgsAuthEsriTokenMethod::AUTH_METHOD_KEY] =
new QgsAuthEsriTokenMethodMetadata();
109 mAuthMethods[QgsAuthIdentCertMethod::AUTH_METHOD_KEY] =
new QgsAuthIdentCertMethodMetadata();
110#ifdef HAVE_OAUTH2_PLUGIN
111 mAuthMethods[QgsAuthOAuth2Method::AUTH_METHOD_KEY] =
new QgsAuthOAuth2MethodMetadata();
113 mAuthMethods[QgsAuthPkiPathsMethod::AUTH_METHOD_KEY] =
new QgsAuthPkiPathsMethodMetadata();
114 mAuthMethods[QgsAuthPkcs12Method::AUTH_METHOD_KEY] =
new QgsAuthPkcs12MethodMetadata();
116 typedef QgsAuthMethodMetadata *factory_function();
118#if defined( Q_OS_WIN ) || defined( __CYGWIN__ )
119 mLibraryDirectory.setNameFilters( QStringList(
"*authmethod_*.dll" ) );
121 mLibraryDirectory.setNameFilters( QStringList( u
"*authmethod_*.so"_s ) );
123 QgsDebugMsgLevel( u
"Checking for auth method plugins in: %1"_s.arg( mLibraryDirectory.path() ), 2 );
125 if ( mLibraryDirectory.count() == 0 )
127 QString msg = QObject::tr(
"No QGIS auth method plugins found in:\n%1\n" ).arg( mLibraryDirectory.path() );
128 msg += QObject::tr(
"No authentication methods can be used. Check your QGIS installation" );
131 output->
setTitle( QObject::tr(
"No Authentication Methods" ) );
138 const QString filePattern = getenv(
"QGIS_AUTHMETHOD_FILE" );
139 QRegularExpression fileRegexp;
140 if ( !filePattern.isEmpty() )
142 fileRegexp.setPattern( filePattern );
145 QListIterator<QFileInfo> it( mLibraryDirectory.entryInfoList() );
146 while ( it.hasNext() )
148 const QFileInfo fi( it.next() );
150 if ( !filePattern.isEmpty() )
152 if ( fi.fileName().indexOf( fileRegexp ) == -1 )
154 QgsDebugError(
"auth method " + fi.fileName() +
" skipped because doesn't match pattern " + filePattern );
159 QLibrary myLib( fi.filePath() );
162 QgsDebugError( u
"Checking %1: ...invalid (lib not loadable): %2"_s.arg( myLib.fileName(), myLib.errorString() ) );
166 bool libraryLoaded {
false };
167 QFunctionPointer func = myLib.resolve( u
"authMethodMetadataFactory"_s.toLatin1().data() );
168 factory_function *function =
reinterpret_cast< factory_function *
>(
cast_to_fptr( func ) );
171 QgsAuthMethodMetadata *meta = function();
174 if ( findMetadata_( mAuthMethods, meta->
key() ) )
176 QgsDebugError( u
"Checking %1: ...invalid (key %2 already registered)"_s.arg( myLib.fileName() ).arg( meta->
key() ) );
181 mAuthMethods[meta->
key()] = meta;
182 libraryLoaded =
true;
185 if ( !libraryLoaded )
187 QgsDebugMsgLevel( u
"Checking %1: ...invalid (no authMethodMetadataFactory method)"_s.arg( myLib.fileName() ), 2 );
199 if ( sInstance ==
this )
203void QgsAuthMethodRegistry::clean()
205 AuthMethods::const_iterator it = mAuthMethods.begin();
207 while ( it != mAuthMethods.end() )
210 const QString lib = it->second->library();
211 QLibrary myLib( lib );
212 if ( myLib.isLoaded() )
223 mAuthMethods.clear();
243 AuthMethods::const_iterator it = mAuthMethods.begin();
245 if ( mAuthMethods.empty() )
247 return QObject::tr(
"No authentication method plugins are available." );
257 while ( it != mAuthMethods.end() )
264 list += it->second->description();
268 list +=
"<br></li>"_L1;
288 return mLibraryDirectory;
293 mLibraryDirectory = path;
302 return findMetadata_( mAuthMethods, authMethodKey );
320 for ( AuthMethods::const_iterator it = mAuthMethods.begin(); it != mAuthMethods.end(); ++it )
322 lst.append( it->first );
A registry / canonical manager of authentication methods.
const QgsAuthMethodMetadata * authMethodMetadata(const QString &authMethodKey) const
Returns metadata of the auth method or nullptr if not found.
static QgsAuthMethodRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
QString pluginList(bool asHtml=false) const
Returns list of auth method plugins found.
void setLibraryDirectory(const QDir &path)
Sets library directory where to search for plugins.
QDir libraryDirectory() const
Returns library directory where plugins are found.
virtual ~QgsAuthMethodRegistry()
Q_DECL_DEPRECATED QString library(const QString &authMethodKey) const
Returns path for the library of the auth method.
QStringList authMethodList() const
Returns list of available auth methods by their keys.
std::map< QString, QgsAuthMethodMetadata * > AuthMethods
Type for auth method metadata associative container.
QgsAuthMethod * createAuthMethod(const QString &authMethodKey)
Create an instance of the auth method.
Abstract base class for authentication method plugins.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
Adds a message to the log instance (and creates it if necessary).
virtual void showMessage(bool blocking=true)=0
display the message to the user and deletes itself
static QgsMessageOutput * createMessageOutput()
function that returns new class derived from QgsMessageOutput (don't forget to delete it then if show...
virtual void setMessage(const QString &message, Qgis::StringFormat format)=0
Sets message, it won't be displayed until.
virtual void setTitle(const QString &title)=0
Sets title for the messages.
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH
void cleanupAuthMethod_t()
QgsAuthMethod * classFactoryFunction_t()
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)