29using namespace Qt::StringLiterals;
31#ifdef HAVE_STATIC_PROVIDERS
32#include "qgsauthapiheadermethod.h"
33#include "qgsauthawss3method.h"
34#include "qgsauthbasicmethod.h"
35#include "qgsauthesritokenmethod.h"
36#include "qgsauthidentcertmethod.h"
37#include "qgsauthmaptilerhmacsha256method.h"
38#ifdef HAVE_OAUTH2_PLUGIN
39#include "qgsauthoauth2method.h"
41#include "qgsauthpkipathsmethod.h"
42#include "qgsauthpkcs12method.h"
43#include "qgsauthplanetarycomputermethod.h"
49#include <QRegularExpression>
60 const QMutexLocker locker( &sMutex );
63 sInstance =
new QgsAuthMethodRegistry( pluginPath );
79 const QgsAuthMethodRegistry::AuthMethods::const_iterator i = metaData.find( authMethodKey );
81 if ( i != metaData.end() )
89QgsAuthMethodRegistry::QgsAuthMethodRegistry(
const QString &pluginPath )
95 char **argv = qApp->argv();
96 QString appDir = argv[0];
97 int bin = appDir.findRev(
"/bin", -1,
false );
98 QString baseDir = appDir.left( bin );
99 QString mLibraryDirectory = baseDir +
"/lib";
101 mLibraryDirectory.setPath( pluginPath );
102 mLibraryDirectory.setSorting( QDir::Name | QDir::IgnoreCase );
103 mLibraryDirectory.setFilter( QDir::Files | QDir::NoSymLinks );
108void QgsAuthMethodRegistry::init()
110#ifdef HAVE_STATIC_PROVIDERS
111 mAuthMethods[QgsAuthApiHeaderMethod::AUTH_METHOD_KEY] =
new QgsAuthApiHeaderMethodMetadata();
112 mAuthMethods[QgsAuthAwsS3Method::AUTH_METHOD_KEY] =
new QgsAuthAwsS3MethodMetadata();
113 mAuthMethods[QgsAuthBasicMethod::AUTH_METHOD_KEY] =
new QgsAuthBasicMethodMetadata();
114 mAuthMethods[QgsAuthEsriTokenMethod::AUTH_METHOD_KEY] =
new QgsAuthEsriTokenMethodMetadata();
115 mAuthMethods[QgsAuthIdentCertMethod::AUTH_METHOD_KEY] =
new QgsAuthIdentCertMethodMetadata();
116 mAuthMethods[QgsAuthMapTilerHmacSha256Method::AUTH_METHOD_KEY] =
new QgsAuthMapTilerHmacSha256MethodMetadata();
117#ifdef HAVE_OAUTH2_PLUGIN
118 mAuthMethods[QgsAuthOAuth2Method::AUTH_METHOD_KEY] =
new QgsAuthOAuth2MethodMetadata();
120 mAuthMethods[QgsAuthPkiPathsMethod::AUTH_METHOD_KEY] =
new QgsAuthPkiPathsMethodMetadata();
121 mAuthMethods[QgsAuthPkcs12Method::AUTH_METHOD_KEY] =
new QgsAuthPkcs12MethodMetadata();
122 mAuthMethods[QgsAuthPlanetaryComputerMethod::AUTH_METHOD_KEY] =
new QgsAuthPlanetaryComputerMethodMetadata();
124 typedef QgsAuthMethodMetadata *factory_function();
126#if defined( Q_OS_WIN ) || defined( __CYGWIN__ )
127 mLibraryDirectory.setNameFilters( QStringList(
"*authmethod_*.dll" ) );
129 mLibraryDirectory.setNameFilters( QStringList( u
"*authmethod_*.so"_s ) );
131 QgsDebugMsgLevel( u
"Checking for auth method plugins in: %1"_s.arg( mLibraryDirectory.path() ), 2 );
133 if ( mLibraryDirectory.count() == 0 )
135 QString msg = QObject::tr(
"No QGIS auth method plugins found in:\n%1\n" ).arg( mLibraryDirectory.path() );
136 msg += QObject::tr(
"No authentication methods can be used. Check your QGIS installation" );
139 output->
setTitle( QObject::tr(
"No Authentication Methods" ) );
146 const QString filePattern = getenv(
"QGIS_AUTHMETHOD_FILE" );
147 QRegularExpression fileRegexp;
148 if ( !filePattern.isEmpty() )
150 fileRegexp.setPattern( filePattern );
153 QListIterator<QFileInfo> it( mLibraryDirectory.entryInfoList() );
154 while ( it.hasNext() )
156 const QFileInfo fi( it.next() );
158 if ( !filePattern.isEmpty() )
160 if ( fi.fileName().indexOf( fileRegexp ) == -1 )
162 QgsDebugError(
"auth method " + fi.fileName() +
" skipped because doesn't match pattern " + filePattern );
167 QLibrary myLib( fi.filePath() );
170 QgsDebugError( u
"Checking %1: ...invalid (lib not loadable): %2"_s.arg( myLib.fileName(), myLib.errorString() ) );
174 bool libraryLoaded {
false };
175 QFunctionPointer func = myLib.resolve( u
"authMethodMetadataFactory"_s.toLatin1().data() );
176 factory_function *function =
reinterpret_cast< factory_function *
>(
cast_to_fptr( func ) );
179 QgsAuthMethodMetadata *meta = function();
182 if ( findMetadata_( mAuthMethods, meta->
key() ) )
184 QgsDebugError( u
"Checking %1: ...invalid (key %2 already registered)"_s.arg( myLib.fileName() ).arg( meta->
key() ) );
189 mAuthMethods[meta->
key()] = meta;
190 libraryLoaded =
true;
193 if ( !libraryLoaded )
195 QgsDebugMsgLevel( u
"Checking %1: ...invalid (no authMethodMetadataFactory method)"_s.arg( myLib.fileName() ), 2 );
207 if ( sInstance ==
this )
211void QgsAuthMethodRegistry::clean()
213 AuthMethods::const_iterator it = mAuthMethods.begin();
215 while ( it != mAuthMethods.end() )
218 const QString lib = it->second->library();
219 QLibrary myLib( lib );
220 if ( myLib.isLoaded() )
231 mAuthMethods.clear();
251 AuthMethods::const_iterator it = mAuthMethods.begin();
253 if ( mAuthMethods.empty() )
255 return QObject::tr(
"No authentication method plugins are available." );
265 while ( it != mAuthMethods.end() )
272 list += it->second->description();
276 list +=
"<br></li>"_L1;
296 return mLibraryDirectory;
301 mLibraryDirectory = path;
310 return findMetadata_( mAuthMethods, authMethodKey );
328 for ( AuthMethods::const_iterator it = mAuthMethods.begin(); it != mAuthMethods.end(); ++it )
330 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)