QGIS API Documentation 3.99.0-Master (a8882ad4560)
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:
75
78
81 {
82 INFO = 0,
85 };
86 Q_ENUM( MessageLevel )
87
88
97 Q_DECL_DEPRECATED bool init( const QString &pluginPath = QString(), const QString &authDatabasePath = QString() ) SIP_DEPRECATED;
98
109 void setup( const QString &pluginPath = QString(), const QString &authDatabasePath = QString() );
110
118 bool ensureInitialized() const;
119
120 ~QgsAuthManager() override;
121
126 QgsAuthConfigurationStorageRegistry *authConfigurationStorageRegistry() const;
127
133 Q_DECL_DEPRECATED QSqlDatabase authDatabaseConnection() const SIP_DEPRECATED;
134
139 Q_DECL_DEPRECATED const QString authDatabaseConfigTable() const SIP_DEPRECATED { return AUTH_CONFIG_TABLE; }
140
146 const QString methodConfigTableName() const;
147
152 Q_DECL_DEPRECATED const QString authDatabaseServersTable() const SIP_DEPRECATED { return AUTH_SERVERS_TABLE; }
153
154
156 bool isDisabled() const;
157
159 const QString disabledMessage() const;
160
166 Q_DECL_DEPRECATED const QString authenticationDatabasePath() const;
167
171 static bool isFilesystemBasedDatabase( const QString &uri );
172
178 QgsAuthConfigurationStorageDb *defaultDbStorage() const SIP_SKIP;
179
184 const QString authenticationDatabaseUri() const;
185
190 const QString authenticationDatabaseUriStripped() const;
191
199 bool createAndStoreRandomMasterPasswordInKeyChain() SIP_SKIP;
200
206 bool setMasterPassword( bool verify = false );
207
214 bool setMasterPassword( const QString &password, bool verify = false );
215
221 bool verifyMasterPassword( const QString &compare = QString() );
222
224 bool masterPasswordIsSet() const;
225
227 bool masterPasswordHashInDatabase() const;
228
233 void clearMasterPassword() { mMasterPass = QString(); }
234
239 bool masterPasswordSame( const QString &password ) const;
240
250 bool resetMasterPassword( const QString &newpass, const QString &oldpass, bool keepbackup, QString *backuppath SIP_INOUT = nullptr );
251
265 bool resetMasterPasswordUsingStoredPasswordHelper( const QString &newPassword, bool keepBackup, QString *backupPath = nullptr ) SIP_SKIP;
266
271 bool scheduledAuthDatabaseErase() SIP_SKIP { return mScheduledDbErase; }
272
285 void setScheduledAuthDatabaseErase( bool scheduleErase ) SIP_SKIP;
286
295 void setScheduledAuthDatabaseEraseRequestEmitted( bool emitted ) { mScheduledDbEraseRequestEmitted = emitted; }
296
298 QString authManTag() const { return AUTH_MAN_TAG; }
299
301 bool registerCoreAuthMethods();
302
304 QgsAuthMethodConfigsMap availableAuthMethodConfigs( const QString &dataprovider = QString() );
305
307 void updateConfigAuthMethods();
308
313 QgsAuthMethod *configAuthMethod( const QString &authcfg );
314
319 QString configAuthMethodKey( const QString &authcfg ) const;
320
324 QStringList authMethodsKeys( const QString &dataprovider = QString() );
325
330 QgsAuthMethod *authMethod( const QString &authMethodKey );
331
337 const QgsAuthMethodMetadata *authMethodMetadata( const QString &authMethodKey ) SIP_SKIP;
338
344 QgsAuthMethodsMap authMethodsMap( const QString &dataprovider = QString() ) SIP_SKIP;
345
346#ifdef HAVE_GUI
347 SIP_IF_FEATURE( HAVE_GUI )
348
349
354 QWidget *authMethodEditWidget( const QString &authMethodKey, QWidget *parent );
355 SIP_END
356#endif
357
362 QgsAuthMethod::Expansions supportedAuthMethodExpansions( const QString &authcfg );
363
365 const QString uniqueConfigId() const;
366
371 bool configIdUnique( const QString &id ) const;
372
377 static bool hasConfigId( const QString &txt );
378
380 QString configIdRegex() const { return AUTH_CFG_REGEX;}
381
383 QStringList configIds() const;
384
391 bool storeAuthenticationConfig( QgsAuthMethodConfig &mconfig SIP_INOUT, bool overwrite = false );
392
398 bool updateAuthenticationConfig( const QgsAuthMethodConfig &config );
399
407 bool loadAuthenticationConfig( const QString &authcfg, QgsAuthMethodConfig &mconfig SIP_INOUT, bool full = false );
408
414 bool removeAuthenticationConfig( const QString &authcfg );
415
423 bool exportAuthenticationConfigsToXml( const QString &filename, const QStringList &authcfgs, const QString &password = QString() );
424
432 bool importAuthenticationConfigsFromXml( const QString &filename, const QString &password = QString(), bool overwrite = false );
433
438 bool removeAllAuthenticationConfigs();
439
444 bool backupAuthenticationDatabase( QString *backuppath SIP_INOUT = nullptr );
445
452 bool eraseAuthenticationDatabase( bool backup, QString *backuppath SIP_INOUT = nullptr );
453
454
456
464 bool updateNetworkRequest( QNetworkRequest &request SIP_INOUT, const QString &authcfg,
465 const QString &dataprovider = QString() );
466
474 bool updateNetworkReply( QNetworkReply *reply, const QString &authcfg,
475 const QString &dataprovider = QString() );
476
484 bool updateDataSourceUriItems( QStringList &connectionItems SIP_INOUT, const QString &authcfg,
485 const QString &dataprovider = QString() );
486
494 bool updateNetworkProxy( QNetworkProxy &proxy SIP_INOUT, const QString &authcfg,
495 const QString &dataprovider = QString() );
496
498
506 bool storeAuthSetting( const QString &key, const QVariant &value, bool encrypt = false );
507
520 QVariant authSetting( const QString &key, const QVariant &defaultValue = QVariant(), bool decrypt = false );
521
523 bool existsAuthSetting( const QString &key );
524
526 bool removeAuthSetting( const QString &key );
527
528#ifndef QT_NO_SSL
530
532 bool initSslCaches();
533
535 bool storeCertIdentity( const QSslCertificate &cert, const QSslKey &key );
536
542 const QSslCertificate certIdentity( const QString &id );
543
550 const QPair<QSslCertificate, QSslKey> certIdentityBundle( const QString &id ) SIP_SKIP;
551
557 const QStringList certIdentityBundleToPem( const QString &id );
558
563 const QList<QSslCertificate> certIdentities();
564
566
571 QStringList certIdentityIds() const;
572
574 bool existsCertIdentity( const QString &id );
575
577 bool removeCertIdentity( const QString &id );
578
579
581 bool storeSslCertCustomConfig( const QgsAuthConfigSslServer &config );
582
589 const QgsAuthConfigSslServer sslCertCustomConfig( const QString &id, const QString &hostport );
590
596 const QgsAuthConfigSslServer sslCertCustomConfigByHost( const QString &hostport );
597
602 const QList<QgsAuthConfigSslServer> sslCertCustomConfigs();
603
605 bool existsSslCertCustomConfig( const QString &id, const QString &hostport );
606
608 bool removeSslCertCustomConfig( const QString &id, const QString &hostport );
609
615 QHash<QString, QSet<QSslError::SslError> > ignoredSslErrorCache() SIP_SKIP { return mIgnoredSslErrorsCache; }
616
618 void dumpIgnoredSslErrorsCache_();
619
621 bool updateIgnoredSslErrorsCacheFromConfig( const QgsAuthConfigSslServer &config );
622
624 bool updateIgnoredSslErrorsCache( const QString &shahostport, const QList<QSslError> &errors );
625
627 bool rebuildIgnoredSslErrorCache();
628
629
631 bool storeCertAuthorities( const QList<QSslCertificate> &certs );
632
634 bool storeCertAuthority( const QSslCertificate &cert );
635
637
643 const QSslCertificate certAuthority( const QString &id );
644
646 bool existsCertAuthority( const QSslCertificate &cert );
647
649 bool removeCertAuthority( const QSslCertificate &cert );
650
655 static const QList<QSslCertificate> systemRootCAs();
656
661 const QList<QSslCertificate> extraFileCAs();
662
667 const QList<QSslCertificate> databaseCAs();
668
673 const QMap<QString, QSslCertificate> mappedDatabaseCAs();
674
680 const QMap<QString, QPair<QgsAuthCertUtils::CaCertSource, QSslCertificate> > caCertsCache() SIP_SKIP
681 {
682 return mCaCertsCache;
683 }
684
686 bool rebuildCaCertsCache();
687
689 bool storeCertTrustPolicy( const QSslCertificate &cert, QgsAuthCertUtils::CertTrustPolicy policy );
690
696 QgsAuthCertUtils::CertTrustPolicy certTrustPolicy( const QSslCertificate &cert );
697
699 bool removeCertTrustPolicies( const QList<QSslCertificate> &certs );
700
702 bool removeCertTrustPolicy( const QSslCertificate &cert );
703
709 QgsAuthCertUtils::CertTrustPolicy certificateTrustPolicy( const QSslCertificate &cert );
710
712 bool setDefaultCertTrustPolicy( QgsAuthCertUtils::CertTrustPolicy policy );
713
715 QgsAuthCertUtils::CertTrustPolicy defaultCertTrustPolicy();
716
721 const QMap<QgsAuthCertUtils::CertTrustPolicy, QStringList > certTrustCache() { return mCertTrustCache; }
722
724 bool rebuildCertTrustCache();
725
731 const QList<QSslCertificate> trustedCaCerts( bool includeinvalid = false );
732
737 const QList<QSslCertificate> untrustedCaCerts( QList<QSslCertificate> trustedCAs = QList<QSslCertificate>() );
738
740 bool rebuildTrustedCaCertsCache();
741
746 const QList<QSslCertificate> trustedCaCertsCache() { return mTrustedCaCertsCache; }
747
752 const QByteArray trustedCaCertsPemText();
753
754#endif
755
760 const QString passwordHelperErrorMessage() SIP_SKIP { return mPasswordHelperErrorMessage; }
761
766 bool passwordHelperDelete() SIP_SKIP;
767
772 static bool passwordHelperEnabled();
773
778 void setPasswordHelperEnabled( bool enabled );
779
784 static bool passwordHelperLoggingEnabled() SIP_SKIP;
785
790 static void setPasswordHelperLoggingEnabled( bool enabled ) SIP_SKIP;
791
796 bool passwordHelperSync();
797
804 bool verifyStoredPasswordHelperPassword() SIP_SKIP;
805
806 // TODO QGIS 5.0 -- remove
807
814
823 static QString passwordHelperDisplayName( bool titleCase = false );
824
826 static const QString AUTH_MAN_TAG;
827
833 QString sqliteDatabasePath() const SIP_SKIP;
834
835 signals:
836
842
848
857 Q_DECL_DEPRECATED void messageOut( const QString &message, const QString &tag = QgsAuthManager::AUTH_MAN_TAG, QgsAuthManager::MessageLevel level = QgsAuthManager::INFO ) const SIP_DEPRECATED;
858
867 void messageLog( const QString &message, const QString &tag = QgsAuthManager::AUTH_MAN_TAG, Qgis::MessageLevel level = Qgis::MessageLevel::Info ) const;
868
877 Q_DECL_DEPRECATED void passwordHelperMessageOut( const QString &message, const QString &tag = QgsAuthManager::AUTH_MAN_TAG, QgsAuthManager::MessageLevel level = QgsAuthManager::INFO ) SIP_DEPRECATED;
878
887 void passwordHelperMessageLog( const QString &message, const QString &tag = QgsAuthManager::AUTH_MAN_TAG, Qgis::MessageLevel level = Qgis::MessageLevel::Info );
888
893 void masterPasswordVerified( bool verified );
894
897
900
901 public slots:
904
906 void clearCachedConfig( const QString &authcfg );
907
908 private slots:
909 void writeToConsole( const QString &message, const QString &tag = QString(), Qgis::MessageLevel level = Qgis::MessageLevel::Info );
910
920 void tryToStartDbErase();
921
922 protected:
923
929
930
931#ifdef Q_OS_WIN
932 public:
933 explicit QgsAuthManager() SIP_SKIP;
934#else
935 protected:
936 explicit QgsAuthManager() SIP_SKIP;
937#endif
938
939 private:
940
944 static QString generatePassword();
945
946 bool initPrivate( const QString &pluginPath );
947
949 // Password Helper methods
950
952 QString passwordHelperName() const;
953
955 void passwordHelperLog( const QString &msg ) const;
956
958 QString passwordHelperRead( bool &ok );
959
961 bool passwordHelperWrite( const QString &password );
962
964 void passwordHelperSetErrorMessage( const QString &errorMessage ) { mPasswordHelperErrorMessage = errorMessage; }
965
967 void passwordHelperClearErrors();
968
973 void passwordHelperProcessError();
974
975 bool masterPasswordInput();
976
983 bool masterPasswordRowsInDb( int &rows ) const;
984
985 bool masterPasswordCheckAgainstDb( const QString &compare = QString() ) const;
986
987 bool masterPasswordStoreInDb() const;
988
989 bool masterPasswordClearDb();
990
991 const QString masterPasswordCiv() const;
992
993 bool verifyPasswordCanDecryptConfigs() const;
994
995 bool reencryptAllAuthenticationConfigs( const QString &prevpass, const QString &prevciv );
996
997 bool reencryptAuthenticationConfig( const QString &authcfg, const QString &prevpass, const QString &prevciv );
998
999 bool reencryptAllAuthenticationSettings( const QString &prevpass, const QString &prevciv );
1000
1001 bool reencryptAllAuthenticationIdentities( const QString &prevpass, const QString &prevciv );
1002
1003 bool reencryptAuthenticationIdentity( const QString &identid, const QString &prevpass, const QString &prevciv );
1004
1005#ifndef QT_NO_SSL
1006 void insertCaCertInCache( QgsAuthCertUtils::CaCertSource source, const QList<QSslCertificate> &certs );
1007#endif
1008
1009 QString authPasswordHelperKeyName() const;
1010
1014 QgsAuthConfigurationStorage *firstStorageWithCapability( Qgis::AuthConfigurationStorageCapability capability ) const;
1015
1016 static QgsAuthManager *sInstance;
1017 static const QString AUTH_CONFIG_TABLE;
1018 static const QString AUTH_SERVERS_TABLE;
1019 static const QString AUTH_CFG_REGEX;
1020
1021 QString mPluginPath;
1022 QString mAuthDatabaseConnectionUri;
1023
1024 mutable bool mLazyInitResult = false;
1025 bool mAuthInit = false;
1026
1027 mutable std::unique_ptr<QgsAuthConfigurationStorageRegistry> mAuthConfigurationStorageRegistry;
1028#ifdef HAVE_AUTH
1029 std::unique_ptr<QCA::Initializer> mQcaInitializer;
1030#endif
1031
1032 QHash<QString, QString> mConfigAuthMethods;
1033 QHash<QString, QgsAuthMethod *> mAuthMethods;
1034
1035 QString mMasterPass;
1036 int mPassTries = 0;
1037 bool mAuthDisabled = false;
1038 QString mAuthDisabledMessage;
1039 std::unique_ptr<QTimer> mScheduledDbEraseTimer;
1040 bool mScheduledDbErase = false;
1041 int mScheduledDbEraseRequestWait = 3 ; // in seconds
1042 bool mScheduledDbEraseRequestEmitted = false;
1043 int mScheduledDbEraseRequestCount = 0;
1044
1045 std::unique_ptr<QRecursiveMutex> mMutex;
1046 std::unique_ptr<QRecursiveMutex> mMasterPasswordMutex;
1047#ifndef QT_NO_SSL
1048 // mapping of sha1 digest and cert source and cert
1049 // appending removes duplicates
1050 QMap<QString, QPair<QgsAuthCertUtils::CaCertSource, QSslCertificate> > mCaCertsCache;
1051 // list of sha1 digests per policy
1052 QMap<QgsAuthCertUtils::CertTrustPolicy, QStringList > mCertTrustCache;
1053 // cache of certs ready to be utilized in network connections
1054 QList<QSslCertificate> mTrustedCaCertsCache;
1055 // cache of SSL errors to be ignored in network connections, per sha-hostport
1056 QHash<QString, QSet<QSslError::SslError> > mIgnoredSslErrorsCache;
1057
1058 bool mHasCustomConfigByHost = false;
1059 bool mHasCheckedIfCustomConfigByHostExists = false;
1060 QMap< QString, QgsAuthConfigSslServer > mCustomConfigByHostCache;
1061#endif
1062
1064 // Password Helper Variables
1065
1067 bool mPasswordHelperVerificationError = false;
1068
1070 QString mPasswordHelperErrorMessage;
1071
1073 QKeychain::Error mPasswordHelperErrorCode = QKeychain::NoError;
1074
1076 bool mPasswordHelperLoggingEnabled = false;
1077
1079 bool mPasswordHelperFailedInit = false;
1080
1082 static const QLatin1String AUTH_PASSWORD_HELPER_KEY_NAME_BASE;
1083
1085 static const QLatin1String AUTH_PASSWORD_HELPER_FOLDER_NAME;
1086
1087 mutable QMap<QThread *, QMetaObject::Connection> mConnectedThreads;
1088
1089 friend class QgsApplication;
1090
1091};
1092
1093#endif // QGSAUTHMANAGER_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:59
AuthConfigurationStorageCapability
Authentication configuration storage capabilities.
Definition qgis.h:105
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:114
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_IF_FEATURE(feature)
Definition qgis_sip.h:189
#define SIP_INOUT
Definition qgis_sip.h:79
#define SIP_END
Definition qgis_sip.h:216
QHash< QString, QgsAuthMethodConfig > QgsAuthMethodConfigsMap
QHash< QString, QgsAuthMethod * > QgsAuthMethodsMap