QGIS API Documentation 3.41.0-Master (64d82d4c163)
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#include <QObject>
23#include <QRecursiveMutex>
24#include <QNetworkReply>
25#include <QNetworkRequest>
26#include <QSqlDatabase>
27#include <QSqlError>
28#include <QSqlQuery>
29#include <QStringList>
30
31#ifndef QT_NO_SSL
32#include <QSslCertificate>
33#include <QSslKey>
34#include <QtCrypto>
35#include "qgsauthcertutils.h"
36#endif
37
38#include "qgsauthconfig.h"
39#include "qgsauthmethod.h"
40
41#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
42#include <qt6keychain/keychain.h>
43#else
44#include <qt5keychain/keychain.h>
45#endif
46
47#ifndef SIP_RUN
48namespace QCA
49{
50 class Initializer;
51}
52#endif
53class QgsAuthMethod;
55class QgsAuthProvider;
58class QTimer;
62
71class CORE_EXPORT QgsAuthManager : public QObject
72{
73 Q_OBJECT
74
75 public:
76
77 static const QgsSettingsEntryBool *settingsGenerateRandomPasswordForPasswordHelper SIP_SKIP;
78 static const QgsSettingsEntryBool *settingsUsingGeneratedRandomPassword SIP_SKIP;
79
82 {
83 INFO = 0,
84 WARNING = 1,
85 CRITICAL = 2
86 };
87 Q_ENUM( MessageLevel )
88
89
98 Q_DECL_DEPRECATED bool init( const QString &pluginPath = QString(), const QString &authDatabasePath = QString() ) SIP_DEPRECATED;
99
110 void setup( const QString &pluginPath = QString(), const QString &authDatabasePath = QString() );
111
119 bool ensureInitialized() const;
120
121 ~QgsAuthManager() override;
122
127 QgsAuthConfigurationStorageRegistry *authConfigurationStorageRegistry() const;
128
134 Q_DECL_DEPRECATED QSqlDatabase authDatabaseConnection() const SIP_DEPRECATED;
135
140 Q_DECL_DEPRECATED const QString authDatabaseConfigTable() const SIP_DEPRECATED { return AUTH_CONFIG_TABLE; }
141
147 const QString methodConfigTableName() const;
148
153 Q_DECL_DEPRECATED const QString authDatabaseServersTable() const SIP_DEPRECATED { return AUTH_SERVERS_TABLE; }
154
155
157 bool isDisabled() const;
158
160 const QString disabledMessage() const;
161
167 Q_DECL_DEPRECATED const QString authenticationDatabasePath() const;
168
172 static bool isFilesystemBasedDatabase( const QString &uri );
173
179 QgsAuthConfigurationStorageDb *defaultDbStorage() const SIP_SKIP;
180
185 const QString authenticationDatabaseUri() const;
186
191 const QString authenticationDatabaseUriStripped() const;
192
200 bool createAndStoreRandomMasterPasswordInKeyChain() SIP_SKIP;
201
207 bool setMasterPassword( bool verify = false );
208
215 bool setMasterPassword( const QString &password, bool verify = false );
216
222 bool verifyMasterPassword( const QString &compare = QString() );
223
225 bool masterPasswordIsSet() const;
226
228 bool masterPasswordHashInDatabase() const;
229
234 void clearMasterPassword() { mMasterPass = QString(); }
235
240 bool masterPasswordSame( const QString &password ) const;
241
251 bool resetMasterPassword( const QString &newpass, const QString &oldpass, bool keepbackup, QString *backuppath SIP_INOUT = nullptr );
252
266 bool resetMasterPasswordUsingStoredPasswordHelper( const QString &newPassword, bool keepBackup, QString *backupPath = nullptr ) SIP_SKIP;
267
272 bool scheduledAuthDatabaseErase() SIP_SKIP { return mScheduledDbErase; }
273
286 void setScheduledAuthDatabaseErase( bool scheduleErase ) SIP_SKIP;
287
296 void setScheduledAuthDatabaseEraseRequestEmitted( bool emitted ) { mScheduledDbEraseRequestEmitted = emitted; }
297
299 QString authManTag() const { return AUTH_MAN_TAG; }
300
302 bool registerCoreAuthMethods();
303
305 QgsAuthMethodConfigsMap availableAuthMethodConfigs( const QString &dataprovider = QString() );
306
308 void updateConfigAuthMethods();
309
314 QgsAuthMethod *configAuthMethod( const QString &authcfg );
315
320 QString configAuthMethodKey( const QString &authcfg ) const;
321
325 QStringList authMethodsKeys( const QString &dataprovider = QString() );
326
331 QgsAuthMethod *authMethod( const QString &authMethodKey );
332
338 const QgsAuthMethodMetadata *authMethodMetadata( const QString &authMethodKey ) SIP_SKIP;
339
345 QgsAuthMethodsMap authMethodsMap( const QString &dataprovider = QString() ) SIP_SKIP;
346
347#ifdef HAVE_GUI
348 SIP_IF_FEATURE( HAVE_GUI )
349
350
355 QWidget *authMethodEditWidget( const QString &authMethodKey, QWidget *parent );
356 SIP_END
357#endif
358
363 QgsAuthMethod::Expansions supportedAuthMethodExpansions( const QString &authcfg );
364
366 const QString uniqueConfigId() const;
367
372 bool configIdUnique( const QString &id ) const;
373
378 static bool hasConfigId( const QString &txt );
379
381 QString configIdRegex() const { return AUTH_CFG_REGEX;}
382
384 QStringList configIds() const;
385
392 bool storeAuthenticationConfig( QgsAuthMethodConfig &mconfig SIP_INOUT, bool overwrite = false );
393
399 bool updateAuthenticationConfig( const QgsAuthMethodConfig &config );
400
408 bool loadAuthenticationConfig( const QString &authcfg, QgsAuthMethodConfig &mconfig SIP_INOUT, bool full = false );
409
415 bool removeAuthenticationConfig( const QString &authcfg );
416
424 bool exportAuthenticationConfigsToXml( const QString &filename, const QStringList &authcfgs, const QString &password = QString() );
425
433 bool importAuthenticationConfigsFromXml( const QString &filename, const QString &password = QString(), bool overwrite = false );
434
439 bool removeAllAuthenticationConfigs();
440
445 bool backupAuthenticationDatabase( QString *backuppath SIP_INOUT = nullptr );
446
453 bool eraseAuthenticationDatabase( bool backup, QString *backuppath SIP_INOUT = nullptr );
454
455
457
465 bool updateNetworkRequest( QNetworkRequest &request SIP_INOUT, const QString &authcfg,
466 const QString &dataprovider = QString() );
467
475 bool updateNetworkReply( QNetworkReply *reply, const QString &authcfg,
476 const QString &dataprovider = QString() );
477
485 bool updateDataSourceUriItems( QStringList &connectionItems SIP_INOUT, const QString &authcfg,
486 const QString &dataprovider = QString() );
487
495 bool updateNetworkProxy( QNetworkProxy &proxy SIP_INOUT, const QString &authcfg,
496 const QString &dataprovider = QString() );
497
499
501 bool storeAuthSetting( const QString &key, const QVariant &value, bool encrypt = false );
502
510 QVariant authSetting( const QString &key, const QVariant &defaultValue = QVariant(), bool decrypt = false );
511
513 bool existsAuthSetting( const QString &key );
514
516 bool removeAuthSetting( const QString &key );
517
518#ifndef QT_NO_SSL
520
522 bool initSslCaches();
523
525 bool storeCertIdentity( const QSslCertificate &cert, const QSslKey &key );
526
532 const QSslCertificate certIdentity( const QString &id );
533
540 const QPair<QSslCertificate, QSslKey> certIdentityBundle( const QString &id ) SIP_SKIP;
541
547 const QStringList certIdentityBundleToPem( const QString &id );
548
553 const QList<QSslCertificate> certIdentities();
554
556
561 QStringList certIdentityIds() const;
562
564 bool existsCertIdentity( const QString &id );
565
567 bool removeCertIdentity( const QString &id );
568
569
571 bool storeSslCertCustomConfig( const QgsAuthConfigSslServer &config );
572
579 const QgsAuthConfigSslServer sslCertCustomConfig( const QString &id, const QString &hostport );
580
586 const QgsAuthConfigSslServer sslCertCustomConfigByHost( const QString &hostport );
587
592 const QList<QgsAuthConfigSslServer> sslCertCustomConfigs();
593
595 bool existsSslCertCustomConfig( const QString &id, const QString &hostport );
596
598 bool removeSslCertCustomConfig( const QString &id, const QString &hostport );
599
605 QHash<QString, QSet<QSslError::SslError> > ignoredSslErrorCache() SIP_SKIP { return mIgnoredSslErrorsCache; }
606
608 void dumpIgnoredSslErrorsCache_();
609
611 bool updateIgnoredSslErrorsCacheFromConfig( const QgsAuthConfigSslServer &config );
612
614 bool updateIgnoredSslErrorsCache( const QString &shahostport, const QList<QSslError> &errors );
615
617 bool rebuildIgnoredSslErrorCache();
618
619
621 bool storeCertAuthorities( const QList<QSslCertificate> &certs );
622
624 bool storeCertAuthority( const QSslCertificate &cert );
625
627
633 const QSslCertificate certAuthority( const QString &id );
634
636 bool existsCertAuthority( const QSslCertificate &cert );
637
639 bool removeCertAuthority( const QSslCertificate &cert );
640
645 static const QList<QSslCertificate> systemRootCAs();
646
651 const QList<QSslCertificate> extraFileCAs();
652
657 const QList<QSslCertificate> databaseCAs();
658
663 const QMap<QString, QSslCertificate> mappedDatabaseCAs();
664
670 const QMap<QString, QPair<QgsAuthCertUtils::CaCertSource, QSslCertificate> > caCertsCache() SIP_SKIP
671 {
672 return mCaCertsCache;
673 }
674
676 bool rebuildCaCertsCache();
677
679 bool storeCertTrustPolicy( const QSslCertificate &cert, QgsAuthCertUtils::CertTrustPolicy policy );
680
686 QgsAuthCertUtils::CertTrustPolicy certTrustPolicy( const QSslCertificate &cert );
687
689 bool removeCertTrustPolicies( const QList<QSslCertificate> &certs );
690
692 bool removeCertTrustPolicy( const QSslCertificate &cert );
693
699 QgsAuthCertUtils::CertTrustPolicy certificateTrustPolicy( const QSslCertificate &cert );
700
702 bool setDefaultCertTrustPolicy( QgsAuthCertUtils::CertTrustPolicy policy );
703
705 QgsAuthCertUtils::CertTrustPolicy defaultCertTrustPolicy();
706
711 const QMap<QgsAuthCertUtils::CertTrustPolicy, QStringList > certTrustCache() { return mCertTrustCache; }
712
714 bool rebuildCertTrustCache();
715
721 const QList<QSslCertificate> trustedCaCerts( bool includeinvalid = false );
722
727 const QList<QSslCertificate> untrustedCaCerts( QList<QSslCertificate> trustedCAs = QList<QSslCertificate>() );
728
730 bool rebuildTrustedCaCertsCache();
731
736 const QList<QSslCertificate> trustedCaCertsCache() { return mTrustedCaCertsCache; }
737
742 const QByteArray trustedCaCertsPemText();
743
744#endif
745
750 const QString passwordHelperErrorMessage() SIP_SKIP { return mPasswordHelperErrorMessage; }
751
756 bool passwordHelperDelete() SIP_SKIP;
757
762 static bool passwordHelperEnabled();
763
768 void setPasswordHelperEnabled( bool enabled );
769
774 static bool passwordHelperLoggingEnabled() SIP_SKIP;
775
780 static void setPasswordHelperLoggingEnabled( bool enabled ) SIP_SKIP;
781
786 bool passwordHelperSync();
787
794 bool verifyStoredPasswordHelperPassword() SIP_SKIP;
795
796 // TODO QGIS 4.0 -- remove
797
803 static const QString AUTH_PASSWORD_HELPER_DISPLAY_NAME;
804
813 static QString passwordHelperDisplayName( bool titleCase = false );
814
816 static const QString AUTH_MAN_TAG;
817
823 QString sqliteDatabasePath() const SIP_SKIP;
824
825 signals:
826
831 void passwordHelperFailure();
832
837 void passwordHelperSuccess();
838
847 Q_DECL_DEPRECATED void messageOut( const QString &message, const QString &tag = QgsAuthManager::AUTH_MAN_TAG, QgsAuthManager::MessageLevel level = QgsAuthManager::INFO ) const SIP_DEPRECATED;
848
857 void messageLog( const QString &message, const QString &tag = QgsAuthManager::AUTH_MAN_TAG, Qgis::MessageLevel level = Qgis::MessageLevel::Info ) const;
858
867 Q_DECL_DEPRECATED void passwordHelperMessageOut( const QString &message, const QString &tag = QgsAuthManager::AUTH_MAN_TAG, QgsAuthManager::MessageLevel level = QgsAuthManager::INFO ) SIP_DEPRECATED;
868
877 void passwordHelperMessageLog( const QString &message, const QString &tag = QgsAuthManager::AUTH_MAN_TAG, Qgis::MessageLevel level = Qgis::MessageLevel::Info );
878
883 void masterPasswordVerified( bool verified );
884
886 void authDatabaseEraseRequested();
887
889 void authDatabaseChanged();
890
891 public slots:
893 void clearAllCachedConfigs();
894
896 void clearCachedConfig( const QString &authcfg );
897
898 private slots:
899 void writeToConsole( const QString &message, const QString &tag = QString(), Qgis::MessageLevel level = Qgis::MessageLevel::Info );
900
910 void tryToStartDbErase();
911
912 protected:
913
918 static QgsAuthManager *instance() SIP_SKIP;
919
920
921#ifdef Q_OS_WIN
922 public:
923 explicit QgsAuthManager() SIP_SKIP;
924#else
925 protected:
926 explicit QgsAuthManager() SIP_SKIP;
927#endif
928
929 private:
930
934 static QString generatePassword();
935
936 bool initPrivate( const QString &pluginPath );
937
939 // Password Helper methods
940
942 QString passwordHelperName() const;
943
945 void passwordHelperLog( const QString &msg ) const;
946
948 QString passwordHelperRead( bool &ok );
949
951 bool passwordHelperWrite( const QString &password );
952
954 void passwordHelperSetErrorMessage( const QString &errorMessage ) { mPasswordHelperErrorMessage = errorMessage; }
955
957 void passwordHelperClearErrors();
958
963 void passwordHelperProcessError();
964
965 bool masterPasswordInput();
966
967 bool masterPasswordRowsInDb( int *rows ) const;
968
969 bool masterPasswordCheckAgainstDb( const QString &compare = QString() ) const;
970
971 bool masterPasswordStoreInDb() const;
972
973 bool masterPasswordClearDb();
974
975 const QString masterPasswordCiv() const;
976
977 bool verifyPasswordCanDecryptConfigs() const;
978
979 bool reencryptAllAuthenticationConfigs( const QString &prevpass, const QString &prevciv );
980
981 bool reencryptAuthenticationConfig( const QString &authcfg, const QString &prevpass, const QString &prevciv );
982
983 bool reencryptAllAuthenticationSettings( const QString &prevpass, const QString &prevciv );
984
985 bool reencryptAllAuthenticationIdentities( const QString &prevpass, const QString &prevciv );
986
987 bool reencryptAuthenticationIdentity( const QString &identid, const QString &prevpass, const QString &prevciv );
988
989#ifndef QT_NO_SSL
990 void insertCaCertInCache( QgsAuthCertUtils::CaCertSource source, const QList<QSslCertificate> &certs );
991#endif
992
993 QString authPasswordHelperKeyName() const;
994
998 QgsAuthConfigurationStorage *firstStorageWithCapability( Qgis::AuthConfigurationStorageCapability capability ) const;
999
1000 static QgsAuthManager *sInstance;
1001 static const QString AUTH_CONFIG_TABLE;
1002 static const QString AUTH_SERVERS_TABLE;
1003 static const QString AUTH_CFG_REGEX;
1004
1005 QString mPluginPath;
1006 QString mAuthDatabaseConnectionUri;
1007
1008 mutable bool mLazyInitResult = false;
1009 bool mAuthInit = false;
1010
1011 mutable std::unique_ptr<QgsAuthConfigurationStorageRegistry> mAuthConfigurationStorageRegistry;
1012
1013 std::unique_ptr<QCA::Initializer> mQcaInitializer;
1014
1015 QHash<QString, QString> mConfigAuthMethods;
1016 QHash<QString, QgsAuthMethod *> mAuthMethods;
1017
1018 QString mMasterPass;
1019 int mPassTries = 0;
1020 bool mAuthDisabled = false;
1021 QString mAuthDisabledMessage;
1022 QTimer *mScheduledDbEraseTimer = nullptr;
1023 bool mScheduledDbErase = false;
1024 int mScheduledDbEraseRequestWait = 3 ; // in seconds
1025 bool mScheduledDbEraseRequestEmitted = false;
1026 int mScheduledDbEraseRequestCount = 0;
1027
1028 std::unique_ptr<QRecursiveMutex> mMutex;
1029 std::unique_ptr<QRecursiveMutex> mMasterPasswordMutex;
1030#ifndef QT_NO_SSL
1031 // mapping of sha1 digest and cert source and cert
1032 // appending removes duplicates
1033 QMap<QString, QPair<QgsAuthCertUtils::CaCertSource, QSslCertificate> > mCaCertsCache;
1034 // list of sha1 digests per policy
1035 QMap<QgsAuthCertUtils::CertTrustPolicy, QStringList > mCertTrustCache;
1036 // cache of certs ready to be utilized in network connections
1037 QList<QSslCertificate> mTrustedCaCertsCache;
1038 // cache of SSL errors to be ignored in network connections, per sha-hostport
1039 QHash<QString, QSet<QSslError::SslError> > mIgnoredSslErrorsCache;
1040
1041 bool mHasCustomConfigByHost = false;
1042 bool mHasCheckedIfCustomConfigByHostExists = false;
1043 QMap< QString, QgsAuthConfigSslServer > mCustomConfigByHostCache;
1044#endif
1045
1047 // Password Helper Variables
1048
1050 bool mPasswordHelperVerificationError = false;
1051
1053 QString mPasswordHelperErrorMessage;
1054
1056 QKeychain::Error mPasswordHelperErrorCode = QKeychain::NoError;
1057
1059 bool mPasswordHelperLoggingEnabled = false;
1060
1062 bool mPasswordHelperFailedInit = false;
1063
1065 static const QLatin1String AUTH_PASSWORD_HELPER_KEY_NAME_BASE;
1066
1068 static const QLatin1String AUTH_PASSWORD_HELPER_FOLDER_NAME;
1069
1070 mutable QMap<QThread *, QMetaObject::Connection> mConnectedThreads;
1071
1072 friend class QgsApplication;
1073
1074};
1075
1076#endif // QGSAUTHMANAGER_H
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
AuthConfigurationStorageCapability
Authentication configuration storage capabilities.
Definition qgis.h:100
Extends QApplication to provide access to QGIS specific resources such as theme paths,...
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 offering an interface to manage the authentication configuration database and to utilize co...
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
const QMap< QgsAuthCertUtils::CertTrustPolicy, QStringList > certTrustCache()
certTrustCache get cache of certificate sha1s, per trust policy
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.
QString configIdRegex() const
Returns the regular expression for authcfg=.{7} key/value token for authentication ids.
void setScheduledAuthDatabaseEraseRequestEmitted(bool emitted)
Re-emit a signal to schedule an optional erase of authentication database.
const QString passwordHelperErrorMessage()
Error message getter.
QHash< QString, QSet< QSslError::SslError > > ignoredSslErrorCache()
ignoredSslErrorCache Get ignored SSL error cache, keyed with cert/connection's sha:host:port.
Q_DECL_DEPRECATED const QString authDatabaseServersTable() const
Name of the authentication database table that stores server exceptions/configs.
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:106
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_IF_FEATURE(feature)
Definition qgis_sip.h:181
#define SIP_INOUT
Definition qgis_sip.h:71
#define SIP_END
Definition qgis_sip.h:208
QHash< QString, QgsAuthMethodConfig > QgsAuthMethodConfigsMap
QHash< QString, QgsAuthMethod * > QgsAuthMethodsMap