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 );
74 QString
const &authMethodKey )
76 const QgsAuthMethodRegistry::AuthMethods::const_iterator i =
77 metaData.find( authMethodKey );
79 if ( i != metaData.end() )
87QgsAuthMethodRegistry::QgsAuthMethodRegistry(
const QString &pluginPath )
93 char **argv = qApp->argv();
94 QString appDir = argv[0];
95 int bin = appDir.findRev(
"/bin", -1,
false );
96 QString baseDir = appDir.left( bin );
97 QString mLibraryDirectory = baseDir +
"/lib";
99 mLibraryDirectory.setPath( pluginPath );
100 mLibraryDirectory.setSorting( QDir::Name | QDir::IgnoreCase );
101 mLibraryDirectory.setFilter( QDir::Files | QDir::NoSymLinks );
106void QgsAuthMethodRegistry::init()
108#ifdef HAVE_STATIC_PROVIDERS
109 mAuthMethods[ QgsAuthBasicMethod::AUTH_METHOD_KEY] =
new QgsAuthBasicMethodMetadata();
110 mAuthMethods[ QgsAuthEsriTokenMethod::AUTH_METHOD_KEY] =
new QgsAuthEsriTokenMethodMetadata();
111 mAuthMethods[ QgsAuthIdentCertMethod::AUTH_METHOD_KEY] =
new QgsAuthIdentCertMethodMetadata();
112#ifdef HAVE_OAUTH2_PLUGIN
113 mAuthMethods[ QgsAuthOAuth2Method::AUTH_METHOD_KEY] =
new QgsAuthOAuth2MethodMetadata();
115 mAuthMethods[ QgsAuthPkiPathsMethod::AUTH_METHOD_KEY] =
new QgsAuthPkiPathsMethodMetadata();
116 mAuthMethods[ QgsAuthPkcs12Method::AUTH_METHOD_KEY] =
new QgsAuthPkcs12MethodMetadata();
118 typedef QgsAuthMethodMetadata *factory_function( );
120#if defined(Q_OS_WIN) || defined(__CYGWIN__)
121 mLibraryDirectory.setNameFilters( QStringList(
"*authmethod_*.dll" ) );
123 mLibraryDirectory.setNameFilters( QStringList( u
"*authmethod_*.so"_s ) );
125 QgsDebugMsgLevel( u
"Checking for auth method plugins in: %1"_s.arg( mLibraryDirectory.path() ), 2 );
127 if ( mLibraryDirectory.count() == 0 )
129 QString msg = QObject::tr(
"No QGIS auth method plugins found in:\n%1\n" ).arg( mLibraryDirectory.path() );
130 msg += QObject::tr(
"No authentication methods can be used. Check your QGIS installation" );
133 output->
setTitle( QObject::tr(
"No Authentication Methods" ) );
140 const QString filePattern = getenv(
"QGIS_AUTHMETHOD_FILE" );
141 QRegularExpression fileRegexp;
142 if ( !filePattern.isEmpty() )
144 fileRegexp.setPattern( filePattern );
147 QListIterator<QFileInfo> it( mLibraryDirectory.entryInfoList() );
148 while ( it.hasNext() )
150 const QFileInfo fi( it.next() );
152 if ( !filePattern.isEmpty() )
154 if ( fi.fileName().indexOf( fileRegexp ) == -1 )
156 QgsDebugError(
"auth method " + fi.fileName() +
" skipped because doesn't match pattern " + filePattern );
161 QLibrary myLib( fi.filePath() );
164 QgsDebugError( u
"Checking %1: ...invalid (lib not loadable): %2"_s.arg( myLib.fileName(), myLib.errorString() ) );
168 bool libraryLoaded {
false };
169 QFunctionPointer func = myLib.resolve( u
"authMethodMetadataFactory"_s.toLatin1().data() );
170 factory_function *function =
reinterpret_cast< factory_function *
>(
cast_to_fptr( func ) );
173 QgsAuthMethodMetadata *meta = function();
176 if ( findMetadata_( mAuthMethods, meta->
key() ) )
178 QgsDebugError( u
"Checking %1: ...invalid (key %2 already registered)"_s.arg( myLib.fileName() ).arg( meta->
key() ) );
183 mAuthMethods[meta->
key()] = meta;
184 libraryLoaded =
true;
187 if ( ! libraryLoaded )
189 QgsDebugMsgLevel( u
"Checking %1: ...invalid (no authMethodMetadataFactory method)"_s.arg( myLib.fileName() ), 2 );
201 if ( sInstance ==
this )
205void QgsAuthMethodRegistry::clean()
207 AuthMethods::const_iterator it = mAuthMethods.begin();
209 while ( it != mAuthMethods.end() )
212 const QString lib = it->second->library();
213 QLibrary myLib( lib );
214 if ( myLib.isLoaded() )
225 mAuthMethods.clear();
245 AuthMethods::const_iterator it = mAuthMethods.begin();
247 if ( mAuthMethods.empty() )
249 return QObject::tr(
"No authentication method plugins are available." );
259 while ( it != mAuthMethods.end() )
266 list += it->second->description();
270 list +=
"<br></li>"_L1;
290 return mLibraryDirectory;
295 mLibraryDirectory = path;
304 return findMetadata_( mAuthMethods, authMethodKey );
322 for ( AuthMethods::const_iterator it = mAuthMethods.begin(); it != mAuthMethods.end(); ++it )
324 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())
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, MessageType msgType)=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)