QGIS API Documentation 3.39.0-Master (3aed037ce22)
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;
57class QTimer;
58
59
68class CORE_EXPORT QgsAuthManager : public QObject
69{
70 Q_OBJECT
71
72 public:
73
76 {
77 INFO = 0,
78 WARNING = 1,
79 CRITICAL = 2
80 };
81 Q_ENUM( MessageLevel )
82
83
92 Q_DECL_DEPRECATED bool init( const QString &pluginPath = QString(), const QString &authDatabasePath = QString() ) SIP_DEPRECATED;
93
104 void setup( const QString &pluginPath = QString(), const QString &authDatabasePath = QString() );
105
113 bool ensureInitialized() const;
114
115 ~QgsAuthManager() override;
116
118 QSqlDatabase authDatabaseConnection() const;
119
121 const QString authDatabaseConfigTable() const { return AUTH_CONFIG_TABLE; }
122
124 const QString authDatabaseServersTable() const { return AUTH_SERVERS_TABLE; }
125
126
128 bool isDisabled() const;
129
131 const QString disabledMessage() const;
132
137 const QString authenticationDatabasePath() const { return mAuthDbPath; }
138
144 bool setMasterPassword( bool verify = false );
145
152 bool setMasterPassword( const QString &pass, bool verify = false );
153
159 bool verifyMasterPassword( const QString &compare = QString() );
160
162 bool masterPasswordIsSet() const;
163
165 bool masterPasswordHashInDatabase() const;
166
171 void clearMasterPassword() { mMasterPass = QString(); }
172
177 bool masterPasswordSame( const QString &pass ) const;
178
187 bool resetMasterPassword( const QString &newpass, const QString &oldpass, bool keepbackup, QString *backuppath SIP_INOUT = nullptr );
188
193 bool scheduledAuthDatabaseErase() { return mScheduledDbErase; } SIP_SKIP
194
207 void setScheduledAuthDatabaseErase( bool scheduleErase ) SIP_SKIP;
208
217 void setScheduledAuthDatabaseEraseRequestEmitted( bool emitted ) { mScheduledDbEraseRequestEmitted = emitted; }
218
220 QString authManTag() const { return AUTH_MAN_TAG; }
221
223 bool registerCoreAuthMethods();
224
226 QgsAuthMethodConfigsMap availableAuthMethodConfigs( const QString &dataprovider = QString() );
227
229 void updateConfigAuthMethods();
230
235 QgsAuthMethod *configAuthMethod( const QString &authcfg );
236
241 QString configAuthMethodKey( const QString &authcfg ) const;
242
246 QStringList authMethodsKeys( const QString &dataprovider = QString() );
247
252 QgsAuthMethod *authMethod( const QString &authMethodKey );
253
259 const QgsAuthMethodMetadata *authMethodMetadata( const QString &authMethodKey ) SIP_SKIP;
260
266 QgsAuthMethodsMap authMethodsMap( const QString &dataprovider = QString() ) SIP_SKIP;
267
268#ifdef HAVE_GUI
269 SIP_IF_FEATURE( HAVE_GUI )
270
271
276 QWidget *authMethodEditWidget( const QString &authMethodKey, QWidget *parent );
277 SIP_END
278#endif
279
284 QgsAuthMethod::Expansions supportedAuthMethodExpansions( const QString &authcfg );
285
287 const QString uniqueConfigId() const;
288
293 bool configIdUnique( const QString &id ) const;
294
299 static bool hasConfigId( const QString &txt );
300
302 QString configIdRegex() const { return AUTH_CFG_REGEX;}
303
305 QStringList configIds() const;
306
313 bool storeAuthenticationConfig( QgsAuthMethodConfig &mconfig SIP_INOUT, bool overwrite = false );
314
320 bool updateAuthenticationConfig( const QgsAuthMethodConfig &config );
321
329 bool loadAuthenticationConfig( const QString &authcfg, QgsAuthMethodConfig &mconfig SIP_INOUT, bool full = false );
330
336 bool removeAuthenticationConfig( const QString &authcfg );
337
345 bool exportAuthenticationConfigsToXml( const QString &filename, const QStringList &authcfgs, const QString &password = QString() );
346
354 bool importAuthenticationConfigsFromXml( const QString &filename, const QString &password = QString(), bool overwrite = false );
355
360 bool removeAllAuthenticationConfigs();
361
366 bool backupAuthenticationDatabase( QString *backuppath SIP_INOUT = nullptr );
367
374 bool eraseAuthenticationDatabase( bool backup, QString *backuppath SIP_INOUT = nullptr );
375
376
378
386 bool updateNetworkRequest( QNetworkRequest &request SIP_INOUT, const QString &authcfg,
387 const QString &dataprovider = QString() );
388
396 bool updateNetworkReply( QNetworkReply *reply, const QString &authcfg,
397 const QString &dataprovider = QString() );
398
406 bool updateDataSourceUriItems( QStringList &connectionItems SIP_INOUT, const QString &authcfg,
407 const QString &dataprovider = QString() );
408
416 bool updateNetworkProxy( QNetworkProxy &proxy SIP_INOUT, const QString &authcfg,
417 const QString &dataprovider = QString() );
418
420
422 bool storeAuthSetting( const QString &key, const QVariant &value, bool encrypt = false );
423
431 QVariant authSetting( const QString &key, const QVariant &defaultValue = QVariant(), bool decrypt = false );
432
434 bool existsAuthSetting( const QString &key );
435
437 bool removeAuthSetting( const QString &key );
438
439#ifndef QT_NO_SSL
441
443 bool initSslCaches();
444
446 bool storeCertIdentity( const QSslCertificate &cert, const QSslKey &key );
447
453 const QSslCertificate certIdentity( const QString &id );
454
461 const QPair<QSslCertificate, QSslKey> certIdentityBundle( const QString &id ) SIP_SKIP;
462
468 const QStringList certIdentityBundleToPem( const QString &id );
469
474 const QList<QSslCertificate> certIdentities();
475
477
482 QStringList certIdentityIds() const;
483
485 bool existsCertIdentity( const QString &id );
486
488 bool removeCertIdentity( const QString &id );
489
490
492 bool storeSslCertCustomConfig( const QgsAuthConfigSslServer &config );
493
500 const QgsAuthConfigSslServer sslCertCustomConfig( const QString &id, const QString &hostport );
501
507 const QgsAuthConfigSslServer sslCertCustomConfigByHost( const QString &hostport );
508
513 const QList<QgsAuthConfigSslServer> sslCertCustomConfigs();
514
516 bool existsSslCertCustomConfig( const QString &id, const QString &hostport );
517
519 bool removeSslCertCustomConfig( const QString &id, const QString &hostport );
520
526 QHash<QString, QSet<QSslError::SslError> > ignoredSslErrorCache() { return mIgnoredSslErrorsCache; } SIP_SKIP
527
529 void dumpIgnoredSslErrorsCache_();
530
532 bool updateIgnoredSslErrorsCacheFromConfig( const QgsAuthConfigSslServer &config );
533
535 bool updateIgnoredSslErrorsCache( const QString &shahostport, const QList<QSslError> &errors );
536
538 bool rebuildIgnoredSslErrorCache();
539
540
542 bool storeCertAuthorities( const QList<QSslCertificate> &certs );
543
545 bool storeCertAuthority( const QSslCertificate &cert );
546
548
554 const QSslCertificate certAuthority( const QString &id );
555
557 bool existsCertAuthority( const QSslCertificate &cert );
558
560 bool removeCertAuthority( const QSslCertificate &cert );
561
566 static const QList<QSslCertificate> systemRootCAs();
567
572 const QList<QSslCertificate> extraFileCAs();
573
578 const QList<QSslCertificate> databaseCAs();
579
584 const QMap<QString, QSslCertificate> mappedDatabaseCAs();
585
591 const QMap<QString, QPair<QgsAuthCertUtils::CaCertSource, QSslCertificate> > caCertsCache() SIP_SKIP
592 {
593 return mCaCertsCache;
594 }
595
597 bool rebuildCaCertsCache();
598
600 bool storeCertTrustPolicy( const QSslCertificate &cert, QgsAuthCertUtils::CertTrustPolicy policy );
601
607 QgsAuthCertUtils::CertTrustPolicy certTrustPolicy( const QSslCertificate &cert );
608
610 bool removeCertTrustPolicies( const QList<QSslCertificate> &certs );
611
613 bool removeCertTrustPolicy( const QSslCertificate &cert );
614
620 QgsAuthCertUtils::CertTrustPolicy certificateTrustPolicy( const QSslCertificate &cert );
621
623 bool setDefaultCertTrustPolicy( QgsAuthCertUtils::CertTrustPolicy policy );
624
626 QgsAuthCertUtils::CertTrustPolicy defaultCertTrustPolicy();
627
632 const QMap<QgsAuthCertUtils::CertTrustPolicy, QStringList > certTrustCache() { return mCertTrustCache; }
633
635 bool rebuildCertTrustCache();
636
642 const QList<QSslCertificate> trustedCaCerts( bool includeinvalid = false );
643
648 const QList<QSslCertificate> untrustedCaCerts( QList<QSslCertificate> trustedCAs = QList<QSslCertificate>() );
649
651 bool rebuildTrustedCaCertsCache();
652
657 const QList<QSslCertificate> trustedCaCertsCache() { return mTrustedCaCertsCache; }
658
663 const QByteArray trustedCaCertsPemText();
664
665#endif
666
671 const QString passwordHelperErrorMessage() { return mPasswordHelperErrorMessage; } SIP_SKIP
672
677 bool passwordHelperDelete() SIP_SKIP;
678
683 static bool passwordHelperEnabled();
684
689 void setPasswordHelperEnabled( bool enabled );
690
695 static bool passwordHelperLoggingEnabled() SIP_SKIP;
696
701 static void setPasswordHelperLoggingEnabled( bool enabled ) SIP_SKIP;
702
707 bool passwordHelperSync();
708
710 static const QString AUTH_PASSWORD_HELPER_DISPLAY_NAME;
711
713 static const QString AUTH_MAN_TAG;
714
715 signals:
716
721 void passwordHelperFailure();
722
727 void passwordHelperSuccess();
728
736 void messageOut( const QString &message, const QString &tag = QgsAuthManager::AUTH_MAN_TAG, QgsAuthManager::MessageLevel level = QgsAuthManager::INFO ) const;
737
745 void passwordHelperMessageOut( const QString &message, const QString &tag = QgsAuthManager::AUTH_MAN_TAG, QgsAuthManager::MessageLevel level = QgsAuthManager::INFO );
746
747
752 void masterPasswordVerified( bool verified );
753
755 void authDatabaseEraseRequested();
756
758 void authDatabaseChanged();
759
760 public slots:
762 void clearAllCachedConfigs();
763
765 void clearCachedConfig( const QString &authcfg );
766
767 private slots:
768 void writeToConsole( const QString &message, const QString &tag = QString(), QgsAuthManager::MessageLevel level = INFO );
769
779 void tryToStartDbErase();
780
781 protected:
782
787 static QgsAuthManager *instance() SIP_SKIP;
788
789
790#ifdef Q_OS_WIN
791 public:
792 explicit QgsAuthManager() SIP_SKIP;
793#else
794 protected:
795 explicit QgsAuthManager() SIP_SKIP;
796#endif
797
798 private:
799
800 bool initPrivate( const QString &pluginPath, const QString &authDatabasePath );
801
803 // Password Helper methods
804
806 QString passwordHelperName() const;
807
809 void passwordHelperLog( const QString &msg ) const;
810
812 QString passwordHelperRead();
813
815 bool passwordHelperWrite( const QString &password );
816
818 void passwordHelperSetErrorMessage( const QString &errorMessage ) { mPasswordHelperErrorMessage = errorMessage; }
819
821 void passwordHelperClearErrors();
822
827 void passwordHelperProcessError();
828
829 bool createConfigTables();
830
831 bool createCertTables();
832
833 bool masterPasswordInput();
834
835 bool masterPasswordRowsInDb( int *rows ) const;
836
837 bool masterPasswordCheckAgainstDb( const QString &compare = QString() ) const;
838
839 bool masterPasswordStoreInDb() const;
840
841 bool masterPasswordClearDb();
842
843 const QString masterPasswordCiv() const;
844
845 bool verifyPasswordCanDecryptConfigs() const;
846
847 bool reencryptAllAuthenticationConfigs( const QString &prevpass, const QString &prevciv );
848
849 bool reencryptAuthenticationConfig( const QString &authcfg, const QString &prevpass, const QString &prevciv );
850
851 bool reencryptAllAuthenticationSettings( const QString &prevpass, const QString &prevciv );
852
853 bool reencryptAllAuthenticationIdentities( const QString &prevpass, const QString &prevciv );
854
855 bool reencryptAuthenticationIdentity( const QString &identid, const QString &prevpass, const QString &prevciv );
856
857 bool authDbOpen() const;
858
859 bool authDbQuery( QSqlQuery *query ) const;
860
861 bool authDbStartTransaction() const;
862
863 bool authDbCommit() const;
864
865 bool authDbTransactionQuery( QSqlQuery *query ) const;
866
867#ifndef QT_NO_SSL
868 void insertCaCertInCache( QgsAuthCertUtils::CaCertSource source, const QList<QSslCertificate> &certs );
869#endif
870
871 const QString authDbPassTable() const { return AUTH_PASS_TABLE; }
872
873 const QString authDbSettingsTable() const { return AUTH_SETTINGS_TABLE; }
874
875 const QString authDbIdentitiesTable() const { return AUTH_IDENTITIES_TABLE; }
876
877 const QString authDbAuthoritiesTable() const { return AUTH_AUTHORITIES_TABLE; }
878
879 const QString authDbTrustTable() const { return AUTH_TRUST_TABLE; }
880
881 QString authPasswordHelperKeyName() const;
882
883 static QgsAuthManager *sInstance;
884 static const QString AUTH_CONFIG_TABLE;
885 static const QString AUTH_PASS_TABLE;
886 static const QString AUTH_SETTINGS_TABLE;
887 static const QString AUTH_IDENTITIES_TABLE;
888 static const QString AUTH_SERVERS_TABLE;
889 static const QString AUTH_AUTHORITIES_TABLE;
890 static const QString AUTH_TRUST_TABLE;
891 static const QString AUTH_CFG_REGEX;
892
893 QString mPluginPath;
894 QString mAuthDatabasePath;
895 mutable bool mLazyInitResult = false;
896
897 bool mAuthInit = false;
898 QString mAuthDbPath;
899
900 std::unique_ptr<QCA::Initializer> mQcaInitializer;
901
902 QHash<QString, QString> mConfigAuthMethods;
903 QHash<QString, QgsAuthMethod *> mAuthMethods;
904
905 QString mMasterPass;
906 int mPassTries = 0;
907 bool mAuthDisabled = false;
908 QString mAuthDisabledMessage;
909 QTimer *mScheduledDbEraseTimer = nullptr;
910 bool mScheduledDbErase = false;
911 int mScheduledDbEraseRequestWait = 3 ; // in seconds
912 bool mScheduledDbEraseRequestEmitted = false;
913 int mScheduledDbEraseRequestCount = 0;
914
915 std::unique_ptr<QRecursiveMutex> mMutex;
916 std::unique_ptr<QRecursiveMutex> mMasterPasswordMutex;
917#ifndef QT_NO_SSL
918 // mapping of sha1 digest and cert source and cert
919 // appending removes duplicates
920 QMap<QString, QPair<QgsAuthCertUtils::CaCertSource, QSslCertificate> > mCaCertsCache;
921 // list of sha1 digests per policy
922 QMap<QgsAuthCertUtils::CertTrustPolicy, QStringList > mCertTrustCache;
923 // cache of certs ready to be utilized in network connections
924 QList<QSslCertificate> mTrustedCaCertsCache;
925 // cache of SSL errors to be ignored in network connections, per sha-hostport
926 QHash<QString, QSet<QSslError::SslError> > mIgnoredSslErrorsCache;
927
928 bool mHasCustomConfigByHost = false;
929 bool mHasCheckedIfCustomConfigByHostExists = false;
930 QMap< QString, QgsAuthConfigSslServer > mCustomConfigByHostCache;
931#endif
932
934 // Password Helper Variables
935
937 bool mPasswordHelperVerificationError = false;
938
940 QString mPasswordHelperErrorMessage;
941
943 QKeychain::Error mPasswordHelperErrorCode = QKeychain::NoError;
944
946 bool mPasswordHelperLoggingEnabled = false;
947
949 bool mPasswordHelperFailedInit = false;
950
952 static const QLatin1String AUTH_PASSWORD_HELPER_KEY_NAME_BASE;
953
955 static const QLatin1String AUTH_PASSWORD_HELPER_FOLDER_NAME;
956
957 mutable QMap<QThread *, QMetaObject::Connection> mConnectedThreads;
958
959 friend class QgsApplication;
960
961};
962
963#endif // QGSAUTHMANAGER_H
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.
Singleton offering an interface to manage the authentication configuration database and to utilize co...
const QString authDatabaseServersTable() const
Name of the authentication database table that stores server exceptions/configs.
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
bool scheduledAuthDatabaseErase()
Whether there is a scheduled opitonal erase of authentication database.
const QString authenticationDatabasePath() const
The standard authentication database file in ~/.qgis3/ or defined location.
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.
void clearMasterPassword()
Clear supplied master password.
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.
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
#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