QGIS API Documentation 4.1.0-Master (60fea48833c)
Loading...
Searching...
No Matches
qgsauthmanager.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsauthmanager.h
3 ---------------------
4 begin : October 5, 2014
5 copyright : (C) 2014 by Boundless Spatial, Inc. USA
6 author : Larry Shaffer
7 email : lshaffer at boundlessgeo dot com
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#ifndef QGSAUTHMANAGER_H
18#define QGSAUTHMANAGER_H
19
20#include "qgis_core.h"
21#include "qgis_sip.h"
22
23#include <QNetworkReply>
24#include <QNetworkRequest>
25#include <QObject>
26#include <QRecursiveMutex>
27#include <QSqlDatabase>
28#include <QSqlError>
29#include <QSqlQuery>
30#include <QStringList>
31
32#ifndef QT_NO_SSL
33#include <QSslCertificate>
34#include <QSslKey>
35#include <QtCrypto>
36#include "qgsauthcertutils.h"
37#endif
38
39#include "qgsauthconfig.h"
40#include "qgsauthmethod.h"
41
42#include <qt6keychain/keychain.h>
43
44#ifndef SIP_RUN
45#ifdef HAVE_AUTH
46namespace QCA
47{
48 class Initializer;
49}
50#endif
51#endif
52class QgsAuthMethod;
54class QgsAuthProvider;
57class QTimer;
61
70class CORE_EXPORT QgsAuthManager : public QObject
71{
72 Q_OBJECT
73
74 public:
77
80 {
81 INFO = 0,
84 };
85 Q_ENUM( MessageLevel )
86
87
96 Q_DECL_DEPRECATED bool init( const QString &pluginPath = QString(), const QString &authDatabasePath = QString() ) SIP_DEPRECATED;
97
108 void setup( const QString &pluginPath = QString(), const QString &authDatabasePath = QString() );
109
117 bool ensureInitialized() const;
118
119 ~QgsAuthManager() override;
120
125 QgsAuthConfigurationStorageRegistry *authConfigurationStorageRegistry() const;
126
132 Q_DECL_DEPRECATED QSqlDatabase authDatabaseConnection() const SIP_DEPRECATED;
133
138 Q_DECL_DEPRECATED const QString authDatabaseConfigTable() const SIP_DEPRECATED { return AUTH_CONFIG_TABLE; }
139
145 const QString methodConfigTableName() const;
146
151 Q_DECL_DEPRECATED const QString authDatabaseServersTable() const SIP_DEPRECATED { return AUTH_SERVERS_TABLE; }
152
153
155 bool isDisabled() const;
156
158 const QString disabledMessage() const;
159
165 Q_DECL_DEPRECATED const QString authenticationDatabasePath() const;
166
170 static bool isFilesystemBasedDatabase( const QString &uri );
171
177 QgsAuthConfigurationStorageDb *defaultDbStorage() const SIP_SKIP;
178
183 const QString authenticationDatabaseUri() const;
184
189 const QString authenticationDatabaseUriStripped() const;
190
198 bool createAndStoreRandomMasterPasswordInKeyChain() SIP_SKIP;
199
205 bool setMasterPassword( bool verify = false );
206
213 bool setMasterPassword( const QString &password, bool verify = false );
214
220 bool verifyMasterPassword( const QString &compare = QString() );
221
223 bool masterPasswordIsSet() const;
224
226 bool masterPasswordHashInDatabase() const;
227
232 void clearMasterPassword() { mMasterPass = QString(); }
233
238 bool masterPasswordSame( const QString &password ) const;
239
249 bool resetMasterPassword( const QString &newpass, const QString &oldpass, bool keepbackup, QString *backuppath SIP_INOUT = nullptr );
250
264 bool resetMasterPasswordUsingStoredPasswordHelper( const QString &newPassword, bool keepBackup, QString *backupPath = nullptr ) SIP_SKIP;
265
270 bool scheduledAuthDatabaseErase() SIP_SKIP { return mScheduledDbErase; }
271
284 void setScheduledAuthDatabaseErase( bool scheduleErase ) SIP_SKIP;
285
294 void setScheduledAuthDatabaseEraseRequestEmitted( bool emitted ) { mScheduledDbEraseRequestEmitted = emitted; }
295
297 QString authManTag() const { return AUTH_MAN_TAG; }
298
300 bool registerCoreAuthMethods();
301
303 QgsAuthMethodConfigsMap availableAuthMethodConfigs( const QString &dataprovider = QString() );
304
306 void updateConfigAuthMethods();
307
312 QgsAuthMethod *configAuthMethod( const QString &authcfg );
313
318 QString configAuthMethodKey( const QString &authcfg ) const;
319
323 QStringList authMethodsKeys( const QString &dataprovider = QString() );
324
329 QgsAuthMethod *authMethod( const QString &authMethodKey );
330
336 const QgsAuthMethodMetadata *authMethodMetadata( const QString &authMethodKey ) SIP_SKIP;
337
343 QgsAuthMethodsMap authMethodsMap( const QString &dataprovider = QString() ) SIP_SKIP;
344
345#ifdef HAVE_GUI
346 SIP_IF_FEATURE( HAVE_GUI )
347
348
353 QWidget *authMethodEditWidget( const QString &authMethodKey, QWidget *parent );
354 SIP_END
355#endif
356
361 QgsAuthMethod::Expansions supportedAuthMethodExpansions( const QString &authcfg );
362
364 const QString uniqueConfigId() const;
365
370 bool configIdUnique( const QString &id ) const;
371
376 static bool hasConfigId( const QString &txt );
377
379 QString configIdRegex() const { return AUTH_CFG_REGEX; }
380
382 QStringList configIds() const;
383
390 bool storeAuthenticationConfig( QgsAuthMethodConfig &mconfig SIP_INOUT, bool overwrite = false );
391
397 bool updateAuthenticationConfig( const QgsAuthMethodConfig &config );
398
406 bool loadAuthenticationConfig( const QString &authcfg, QgsAuthMethodConfig &mconfig SIP_INOUT, bool full = false );
407
413 bool removeAuthenticationConfig( const QString &authcfg );
414
422 bool exportAuthenticationConfigsToXml( const QString &filename, const QStringList &authcfgs, const QString &password = QString() );
423
431 bool importAuthenticationConfigsFromXml( const QString &filename, const QString &password = QString(), bool overwrite = false );
432
437 bool removeAllAuthenticationConfigs();
438
443 bool backupAuthenticationDatabase( QString *backuppath SIP_INOUT = nullptr );
444
451 bool eraseAuthenticationDatabase( bool backup, QString *backuppath SIP_INOUT = nullptr );
452
453
455
463 bool updateNetworkRequest( QNetworkRequest &request SIP_INOUT, const QString &authcfg, const QString &dataprovider = QString() );
464
472 bool updateNetworkReply( QNetworkReply *reply, const QString &authcfg, const QString &dataprovider = QString() );
473
481 bool updateDataSourceUriItems( QStringList &connectionItems SIP_INOUT, const QString &authcfg, const QString &dataprovider = QString() );
482
490 bool updateNetworkProxy( QNetworkProxy &proxy SIP_INOUT, const QString &authcfg, const QString &dataprovider = QString() );
491
493
501 bool storeAuthSetting( const QString &key, const QVariant &value, bool encrypt = false );
502
515 QVariant authSetting( const QString &key, const QVariant &defaultValue = QVariant(), bool decrypt = false );
516
518 bool existsAuthSetting( const QString &key );
519
521 bool removeAuthSetting( const QString &key );
522
523#ifndef QT_NO_SSL
525
527 bool initSslCaches();
528
530 bool storeCertIdentity( const QSslCertificate &cert, const QSslKey &key );
531
537 const QSslCertificate certIdentity( const QString &id );
538
545 const QPair<QSslCertificate, QSslKey> certIdentityBundle( const QString &id ) SIP_SKIP;
546
552 const QStringList certIdentityBundleToPem( const QString &id );
553
558 const QList<QSslCertificate> certIdentities();
559
561
566 QStringList certIdentityIds() const;
567
569 bool existsCertIdentity( const QString &id );
570
572 bool removeCertIdentity( const QString &id );
573
574
576 bool storeSslCertCustomConfig( const QgsAuthConfigSslServer &config );
577
584 const QgsAuthConfigSslServer sslCertCustomConfig( const QString &id, const QString &hostport );
585
591 const QgsAuthConfigSslServer sslCertCustomConfigByHost( const QString &hostport );
592
597 const QList<QgsAuthConfigSslServer> sslCertCustomConfigs();
598
600 bool existsSslCertCustomConfig( const QString &id, const QString &hostport );
601
603 bool removeSslCertCustomConfig( const QString &id, const QString &hostport );
604
610 QHash<QString, QSet<QSslError::SslError> > ignoredSslErrorCache() SIP_SKIP { return mIgnoredSslErrorsCache; }
611
613 void dumpIgnoredSslErrorsCache_();
614
616 bool updateIgnoredSslErrorsCacheFromConfig( const QgsAuthConfigSslServer &config );
617
619 bool updateIgnoredSslErrorsCache( const QString &shahostport, const QList<QSslError> &errors );
620
622 bool rebuildIgnoredSslErrorCache();
623
624
626 bool storeCertAuthorities( const QList<QSslCertificate> &certs );
627
629 bool storeCertAuthority( const QSslCertificate &cert );
630
632
638 const QSslCertificate certAuthority( const QString &id );
639
641 bool existsCertAuthority( const QSslCertificate &cert );
642
644 bool removeCertAuthority( const QSslCertificate &cert );
645
650 static const QList<QSslCertificate> systemRootCAs();
651
656 const QList<QSslCertificate> extraFileCAs();
657
662 const QList<QSslCertificate> databaseCAs();
663
668 const QMap<QString, QSslCertificate> mappedDatabaseCAs();
669
675 const QMap<QString, QPair<QgsAuthCertUtils::CaCertSource, QSslCertificate> > caCertsCache() SIP_SKIP { return mCaCertsCache; }
676
678 bool rebuildCaCertsCache();
679
681 bool storeCertTrustPolicy( const QSslCertificate &cert, QgsAuthCertUtils::CertTrustPolicy policy );
682
688 QgsAuthCertUtils::CertTrustPolicy certTrustPolicy( const QSslCertificate &cert );
689
691 bool removeCertTrustPolicies( const QList<QSslCertificate> &certs );
692
694 bool removeCertTrustPolicy( const QSslCertificate &cert );
695
701 QgsAuthCertUtils::CertTrustPolicy certificateTrustPolicy( const QSslCertificate &cert );
702
704 bool setDefaultCertTrustPolicy( QgsAuthCertUtils::CertTrustPolicy policy );
705
707 QgsAuthCertUtils::CertTrustPolicy defaultCertTrustPolicy();
708
713 const QMap<QgsAuthCertUtils::CertTrustPolicy, QStringList > certTrustCache() { return mCertTrustCache; }
714
716 bool rebuildCertTrustCache();
717
723 const QList<QSslCertificate> trustedCaCerts( bool includeinvalid = false );
724
729 const QList<QSslCertificate> untrustedCaCerts( QList<QSslCertificate> trustedCAs = QList<QSslCertificate>() );
730
732 bool rebuildTrustedCaCertsCache();
733
738 const QList<QSslCertificate> trustedCaCertsCache() { return mTrustedCaCertsCache; }
739
744 const QByteArray trustedCaCertsPemText();
745
746#endif
747
752 const QString passwordHelperErrorMessage() SIP_SKIP { return mPasswordHelperErrorMessage; }
753
758 bool passwordHelperDelete() SIP_SKIP;
759
764 static bool passwordHelperEnabled();
765
770 void setPasswordHelperEnabled( bool enabled );
771
776 static bool passwordHelperLoggingEnabled() SIP_SKIP;
777
782 static void setPasswordHelperLoggingEnabled( bool enabled ) SIP_SKIP;
783
788 bool passwordHelperSync();
789
796 bool verifyStoredPasswordHelperPassword() SIP_SKIP;
797
798 // TODO QGIS 5.0 -- remove
799
806
815 static QString passwordHelperDisplayName( bool titleCase = false );
816
818 static const QString AUTH_MAN_TAG;
819
825 QString sqliteDatabasePath() const SIP_SKIP;
826
827 signals:
828
834
840
849 Q_DECL_DEPRECATED void messageOut( const QString &message, const QString &tag = QgsAuthManager::AUTH_MAN_TAG, QgsAuthManager::MessageLevel level = QgsAuthManager::INFO ) const SIP_DEPRECATED;
850
859 void messageLog( const QString &message, const QString &tag = QgsAuthManager::AUTH_MAN_TAG, Qgis::MessageLevel level = Qgis::MessageLevel::Info ) const;
860
869 Q_DECL_DEPRECATED void passwordHelperMessageOut( const QString &message, const QString &tag = QgsAuthManager::AUTH_MAN_TAG, QgsAuthManager::MessageLevel level = QgsAuthManager::INFO )
871
880 void passwordHelperMessageLog( const QString &message, const QString &tag = QgsAuthManager::AUTH_MAN_TAG, Qgis::MessageLevel level = Qgis::MessageLevel::Info );
881
886 void masterPasswordVerified( bool verified );
887
890
893
894 public slots:
897
899 void clearCachedConfig( const QString &authcfg );
900
901 private slots:
902 void writeToConsole( const QString &message, const QString &tag = QString(), Qgis::MessageLevel level = Qgis::MessageLevel::Info );
903
913 void tryToStartDbErase();
914
915 protected:
921
922
923#ifdef Q_OS_WIN
924 public:
925 explicit QgsAuthManager() SIP_SKIP;
926#else
927 protected:
928 explicit QgsAuthManager() SIP_SKIP;
929#endif
930
931 private:
935 static QString generatePassword();
936
937 bool initPrivate( const QString &pluginPath );
938
940 // Password Helper methods
941
943 QString passwordHelperName() const;
944
946 void passwordHelperLog( const QString &msg ) const;
947
949 QString passwordHelperRead( bool &ok );
950
952 bool passwordHelperWrite( const QString &password );
953
955 void passwordHelperSetErrorMessage( const QString &errorMessage ) { mPasswordHelperErrorMessage = errorMessage; }
956
958 void passwordHelperClearErrors();
959
964 void passwordHelperProcessError();
965
966 bool masterPasswordInput();
967
974 bool masterPasswordRowsInDb( int &rows ) const;
975
976 bool masterPasswordCheckAgainstDb( const QString &compare = QString() ) const;
977
978 bool masterPasswordStoreInDb() const;
979
980 bool masterPasswordClearDb();
981
982 const QString masterPasswordCiv() const;
983
984 bool verifyPasswordCanDecryptConfigs() const;
985
986 bool reencryptAllAuthenticationConfigs( const QString &prevpass, const QString &prevciv );
987
988 bool reencryptAuthenticationConfig( const QString &authcfg, const QString &prevpass, const QString &prevciv );
989
990 bool reencryptAllAuthenticationSettings( const QString &prevpass, const QString &prevciv );
991
992 bool reencryptAllAuthenticationIdentities( const QString &prevpass, const QString &prevciv );
993
994 bool reencryptAuthenticationIdentity( const QString &identid, const QString &prevpass, const QString &prevciv );
995
996#ifndef QT_NO_SSL
997 void insertCaCertInCache( QgsAuthCertUtils::CaCertSource source, const QList<QSslCertificate> &certs );
998#endif
999
1000 QString authPasswordHelperKeyName() const;
1001
1005 QgsAuthConfigurationStorage *firstStorageWithCapability( Qgis::AuthConfigurationStorageCapability capability ) const;
1006
1007 static QgsAuthManager *sInstance;
1008 static const QString AUTH_CONFIG_TABLE;
1009 static const QString AUTH_SERVERS_TABLE;
1010 static const QString AUTH_CFG_REGEX;
1011
1012 QString mPluginPath;
1013 QString mAuthDatabaseConnectionUri;
1014
1015 mutable bool mLazyInitResult = false;
1016 bool mAuthInit = false;
1017
1018 mutable std::unique_ptr<QgsAuthConfigurationStorageRegistry> mAuthConfigurationStorageRegistry;
1019#ifdef HAVE_AUTH
1020 std::unique_ptr<QCA::Initializer> mQcaInitializer;
1021#endif
1022
1023 QHash<QString, QString> mConfigAuthMethods;
1024 QHash<QString, QgsAuthMethod *> mAuthMethods;
1025
1026 QString mMasterPass;
1027 int mPassTries = 0;
1028 bool mAuthDisabled = false;
1029 QString mAuthDisabledMessage;
1030 std::unique_ptr<QTimer> mScheduledDbEraseTimer;
1031 bool mScheduledDbErase = false;
1032 int mScheduledDbEraseRequestWait = 3; // in seconds
1033 bool mScheduledDbEraseRequestEmitted = false;
1034 int mScheduledDbEraseRequestCount = 0;
1035
1036 std::unique_ptr<QRecursiveMutex> mMutex;
1037 std::unique_ptr<QRecursiveMutex> mMasterPasswordMutex;
1038#ifndef QT_NO_SSL
1039 // mapping of sha1 digest and cert source and cert
1040 // appending removes duplicates
1041 QMap<QString, QPair<QgsAuthCertUtils::CaCertSource, QSslCertificate> > mCaCertsCache;
1042 // list of sha1 digests per policy
1043 QMap<QgsAuthCertUtils::CertTrustPolicy, QStringList > mCertTrustCache;
1044 // cache of certs ready to be utilized in network connections
1045 QList<QSslCertificate> mTrustedCaCertsCache;
1046 // cache of SSL errors to be ignored in network connections, per sha-hostport
1047 QHash<QString, QSet<QSslError::SslError> > mIgnoredSslErrorsCache;
1048
1049 bool mHasCustomConfigByHost = false;
1050 bool mHasCheckedIfCustomConfigByHostExists = false;
1051 QMap< QString, QgsAuthConfigSslServer > mCustomConfigByHostCache;
1052#endif
1053
1055 // Password Helper Variables
1056
1058 bool mPasswordHelperVerificationError = false;
1059
1061 QString mPasswordHelperErrorMessage;
1062
1064 QKeychain::Error mPasswordHelperErrorCode = QKeychain::NoError;
1065
1067 bool mPasswordHelperLoggingEnabled = false;
1068
1070 bool mPasswordHelperFailedInit = false;
1071
1073 static const QLatin1String AUTH_PASSWORD_HELPER_KEY_NAME_BASE;
1074
1076 static const QLatin1String AUTH_PASSWORD_HELPER_FOLDER_NAME;
1077
1078 mutable QMap<QThread *, QMetaObject::Connection> mConnectedThreads;
1079
1080 friend class QgsApplication;
1081};
1082
1083#endif // QGSAUTHMANAGER_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
AuthConfigurationStorageCapability
Authentication configuration storage capabilities.
Definition qgis.h:107
CertTrustPolicy
Type of certificate trust policy.
CaCertSource
Type of CA certificate source.
Configuration container for SSL server connection exceptions or overrides.
QSqlDatabase based implementation of QgsAuthConfigurationStorage.
Registry for authentication configuration storages.
Abstract class that defines the interface for all authentication configuration storage implementation...
Singleton which offers an interface to manage the authentication configuration database and to utiliz...
void clearAllCachedConfigs()
Clear all authentication configs from authentication method caches.
MessageLevel
Message log level (mirrors that of QgsMessageLog, so it can also output there).
const QList< QSslCertificate > trustedCaCertsCache()
trustedCaCertsCache cache of trusted certificate authorities, ready for network connections
Q_DECL_DEPRECATED void passwordHelperMessageOut(const QString &message, const QString &tag=QgsAuthManager::AUTH_MAN_TAG, QgsAuthManager::MessageLevel level=QgsAuthManager::INFO)
Custom logging signal to inform the user about master password <-> password manager interactions.
const QMap< QgsAuthCertUtils::CertTrustPolicy, QStringList > certTrustCache()
certTrustCache get cache of certificate sha1s, per trust policy
bool scheduledAuthDatabaseErase()
Whether there is a scheduled opitonal erase of authentication database.
void passwordHelperMessageLog(const QString &message, const QString &tag=QgsAuthManager::AUTH_MAN_TAG, Qgis::MessageLevel level=Qgis::MessageLevel::Info)
Custom logging signal to inform the user about master password <-> password manager interactions.
QString sqliteDatabasePath() const
Returns the path to the authentication database file or an empty string if the database is not SQLite...
void authDatabaseChanged()
Emitted when the authentication db is significantly changed, e.g. large record removal,...
static const QString AUTH_MAN_TAG
The display name of the Authentication Manager.
friend class QgsApplication
static const QgsSettingsEntryBool * settingsGenerateRandomPasswordForPasswordHelper
Q_DECL_DEPRECATED const QString authDatabaseConfigTable() const
Name of the authentication database table that stores configs.
QString authManTag() const
Simple text tag describing authentication system for message logs.
const QMap< QString, QPair< QgsAuthCertUtils::CaCertSource, QSslCertificate > > caCertsCache()
caCertsCache get all CA certs mapped to their sha1 from cache.
Q_DECL_DEPRECATED void messageOut(const QString &message, const QString &tag=QgsAuthManager::AUTH_MAN_TAG, QgsAuthManager::MessageLevel level=QgsAuthManager::INFO) const
Custom logging signal to relay to console output and QgsMessageLog.
void passwordHelperSuccess()
Signals emitted on password helper success, mainly used in the tests to exit main application loop.
QString configIdRegex() const
Returns the regular expression for authcfg=.{7} key/value token for authentication ids.
void messageLog(const QString &message, const QString &tag=QgsAuthManager::AUTH_MAN_TAG, Qgis::MessageLevel level=Qgis::MessageLevel::Info) const
Custom logging signal to relay to console output and QgsMessageLog.
void authDatabaseEraseRequested()
Emitted when a user has indicated they may want to erase the authentication db.
void setScheduledAuthDatabaseEraseRequestEmitted(bool emitted)
Re-emit a signal to schedule an optional erase of authentication database.
void passwordHelperFailure()
Signals emitted on password helper failure, mainly used in the tests to exit main application loop.
void clearCachedConfig(const QString &authcfg)
Clear an authentication config from its associated authentication method cache.
void clearMasterPassword()
Clear supplied master password.
const QString passwordHelperErrorMessage()
Error message getter.
static const QgsSettingsEntryBool * settingsUsingGeneratedRandomPassword
static QgsAuthManager * instance()
Enforce singleton pattern.
void masterPasswordVerified(bool verified)
Emitted when a password has been verify (or not).
QHash< QString, QSet< QSslError::SslError > > ignoredSslErrorCache()
ignoredSslErrorCache Get ignored SSL error cache, keyed with cert/connection's sha:host:port.
static const QString AUTH_PASSWORD_HELPER_DISPLAY_NAME
The display name of the password helper (platform dependent).
Q_DECL_DEPRECATED const QString authDatabaseServersTable() const
Name of the authentication database table that stores server exceptions/configs.
static QString passwordHelperDisplayName(bool titleCase=false)
Returns a translated display name of the password helper (platform dependent).
Configuration storage class for authentication method configurations.
Abstract base class for the edit widget of authentication method plugins.
Holds data auth method key, description, and associated shared library file information.
Abstract base class for authentication method plugins.
QFlags< Expansion > Expansions
A boolean settings entry.
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_IF_FEATURE(feature)
Definition qgis_sip.h:188
#define SIP_INOUT
Definition qgis_sip.h:78
#define SIP_END
Definition qgis_sip.h:215
QHash< QString, QgsAuthMethodConfig > QgsAuthMethodConfigsMap
QHash< QString, QgsAuthMethod * > QgsAuthMethodsMap