17 #ifndef QGSAUTHMANAGER_H
18 #define QGSAUTHMANAGER_H
20 #include "qgis_core.h"
23 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
26 #include <QRecursiveMutex>
28 #include <QNetworkReply>
29 #include <QNetworkRequest>
30 #include <QSqlDatabase>
33 #include <QStringList>
36 #include <QSslCertificate>
56 class QgsAuthProvider;
82 Q_ENUM( MessageLevel )
92 bool init(
const QString &pluginPath = QString(),
const QString &authDatabasePath = QString() );
97 QSqlDatabase authDatabaseConnection()
const;
107 bool isDisabled()
const;
110 const QString disabledMessage()
const;
123 bool setMasterPassword(
bool verify =
false );
131 bool setMasterPassword(
const QString &pass,
bool verify =
false );
138 bool verifyMasterPassword(
const QString &compare = QString() );
141 bool masterPasswordIsSet()
const;
144 bool masterPasswordHashInDatabase()
const;
156 bool masterPasswordSame(
const QString &pass )
const;
166 bool resetMasterPassword(
const QString &newpass,
const QString &oldpass,
bool keepbackup, QString *backuppath
SIP_INOUT =
nullptr );
186 void setScheduledAuthDatabaseErase(
bool scheduleErase )
SIP_SKIP;
202 bool registerCoreAuthMethods();
208 void updateConfigAuthMethods();
220 QString configAuthMethodKey(
const QString &authcfg )
const;
225 QStringList authMethodsKeys(
const QString &dataprovider = QString() );
255 QWidget *authMethodEditWidget(
const QString &authMethodKey, QWidget *parent );
263 QgsAuthMethod::Expansions supportedAuthMethodExpansions(
const QString &authcfg );
266 const QString uniqueConfigId()
const;
272 bool configIdUnique(
const QString &
id )
const;
278 bool hasConfigId(
const QString &txt )
const;
284 QStringList configIds()
const;
315 bool removeAuthenticationConfig(
const QString &authcfg );
324 bool exportAuthenticationConfigsToXml(
const QString &filename,
const QStringList &authcfgs,
const QString &password = QString() );
333 bool importAuthenticationConfigsFromXml(
const QString &filename,
const QString &password = QString(),
bool overwrite =
false );
339 bool removeAllAuthenticationConfigs();
345 bool backupAuthenticationDatabase( QString *backuppath
SIP_INOUT =
nullptr );
353 bool eraseAuthenticationDatabase(
bool backup, QString *backuppath
SIP_INOUT =
nullptr );
365 bool updateNetworkRequest( QNetworkRequest &request
SIP_INOUT,
const QString &authcfg,
366 const QString &dataprovider = QString() );
375 bool updateNetworkReply( QNetworkReply *reply,
const QString &authcfg,
376 const QString &dataprovider = QString() );
385 bool updateDataSourceUriItems( QStringList &connectionItems
SIP_INOUT,
const QString &authcfg,
386 const QString &dataprovider = QString() );
395 bool updateNetworkProxy( QNetworkProxy &proxy
SIP_INOUT,
const QString &authcfg,
396 const QString &dataprovider = QString() );
401 bool storeAuthSetting(
const QString &key,
const QVariant &value,
bool encrypt =
false );
411 QVariant authSetting(
const QString &key,
const QVariant &defaultValue = QVariant(),
bool decrypt =
false );
414 bool existsAuthSetting(
const QString &key );
417 bool removeAuthSetting(
const QString &key );
423 bool initSslCaches();
426 bool storeCertIdentity(
const QSslCertificate &cert,
const QSslKey &key );
434 const QSslCertificate certIdentity(
const QString &
id );
443 const QPair<QSslCertificate, QSslKey> certIdentityBundle(
const QString &
id )
SIP_SKIP;
451 const QStringList certIdentityBundleToPem(
const QString &
id );
458 const QList<QSslCertificate> certIdentities();
467 QStringList certIdentityIds()
const;
470 bool existsCertIdentity(
const QString &
id );
473 bool removeCertIdentity(
const QString &
id );
501 const QList<QgsAuthConfigSslServer> sslCertCustomConfigs();
504 bool existsSslCertCustomConfig(
const QString &
id,
const QString &hostport );
507 bool removeSslCertCustomConfig(
const QString &
id,
const QString &hostport );
518 void dumpIgnoredSslErrorsCache_();
524 bool updateIgnoredSslErrorsCache(
const QString &shahostport,
const QList<QSslError> &errors );
527 bool rebuildIgnoredSslErrorCache();
531 bool storeCertAuthorities(
const QList<QSslCertificate> &certs );
534 bool storeCertAuthority(
const QSslCertificate &cert );
544 const QSslCertificate certAuthority(
const QString &
id );
547 bool existsCertAuthority(
const QSslCertificate &cert );
550 bool removeCertAuthority(
const QSslCertificate &cert );
557 const QList<QSslCertificate> systemRootCAs();
564 const QList<QSslCertificate> extraFileCAs();
571 const QList<QSslCertificate> databaseCAs();
578 const QMap<QString, QSslCertificate> mappedDatabaseCAs();
588 return mCaCertsCache;
592 bool rebuildCaCertsCache();
606 bool removeCertTrustPolicies(
const QList<QSslCertificate> &certs );
609 bool removeCertTrustPolicy(
const QSslCertificate &cert );
630 const QMap<QgsAuthCertUtils::CertTrustPolicy, QStringList >
certTrustCache() {
return mCertTrustCache; }
633 bool rebuildCertTrustCache();
641 const QList<QSslCertificate> trustedCaCerts(
bool includeinvalid =
false );
648 const QList<QSslCertificate> untrustedCaCerts( QList<QSslCertificate> trustedCAs = QList<QSslCertificate>() );
651 bool rebuildTrustedCaCertsCache();
665 const QByteArray trustedCaCertsPemText();
679 bool passwordHelperDelete()
SIP_SKIP;
685 bool passwordHelperEnabled() const;
691 void setPasswordHelperEnabled(
bool enabled );
697 bool passwordHelperLoggingEnabled() const
SIP_SKIP;
703 void setPasswordHelperLoggingEnabled(
bool enabled )
SIP_SKIP;
709 bool passwordHelperSync();
712 static const QString AUTH_PASSWORD_HELPER_DISPLAY_NAME;
715 static const QString AUTH_MAN_TAG;
723 void passwordHelperFailure();
729 void passwordHelperSuccess();
754 void masterPasswordVerified(
bool verified );
757 void authDatabaseEraseRequested();
760 void authDatabaseChanged();
764 void clearAllCachedConfigs();
767 void clearCachedConfig( const QString &authcfg );
781 void tryToStartDbErase();
806 QString passwordHelperName()
const;
809 void passwordHelperLog(
const QString &msg )
const;
812 QString passwordHelperRead();
815 bool passwordHelperWrite(
const QString &password );
818 void passwordHelperSetErrorMessage(
const QString &errorMessage ) { mPasswordHelperErrorMessage = errorMessage; }
821 void passwordHelperClearErrors();
827 void passwordHelperProcessError();
829 bool createConfigTables();
831 bool createCertTables();
833 bool masterPasswordInput();
835 bool masterPasswordRowsInDb(
int *rows )
const;
837 bool masterPasswordCheckAgainstDb(
const QString &compare = QString() )
const;
839 bool masterPasswordStoreInDb()
const;
841 bool masterPasswordClearDb();
843 const QString masterPasswordCiv()
const;
845 bool verifyPasswordCanDecryptConfigs()
const;
847 bool reencryptAllAuthenticationConfigs(
const QString &prevpass,
const QString &prevciv );
849 bool reencryptAuthenticationConfig(
const QString &authcfg,
const QString &prevpass,
const QString &prevciv );
851 bool reencryptAllAuthenticationSettings(
const QString &prevpass,
const QString &prevciv );
853 bool reencryptAllAuthenticationIdentities(
const QString &prevpass,
const QString &prevciv );
855 bool reencryptAuthenticationIdentity(
const QString &identid,
const QString &prevpass,
const QString &prevciv );
857 bool authDbOpen()
const;
859 bool authDbQuery( QSqlQuery *query )
const;
861 bool authDbStartTransaction()
const;
863 bool authDbCommit()
const;
865 bool authDbTransactionQuery( QSqlQuery *query )
const;
871 const QString authDbPassTable()
const {
return AUTH_PASS_TABLE; }
873 const QString authDbSettingsTable()
const {
return AUTH_SETTINGS_TABLE; }
875 const QString authDbIdentitiesTable()
const {
return AUTH_IDENTITIES_TABLE; }
877 const QString authDbAuthoritiesTable()
const {
return AUTH_AUTHORITIES_TABLE; }
879 const QString authDbTrustTable()
const {
return AUTH_TRUST_TABLE; }
882 static const QString AUTH_CONFIG_TABLE;
883 static const QString AUTH_PASS_TABLE;
884 static const QString AUTH_SETTINGS_TABLE;
885 static const QString AUTH_IDENTITIES_TABLE;
886 static const QString AUTH_SERVERS_TABLE;
887 static const QString AUTH_AUTHORITIES_TABLE;
888 static const QString AUTH_TRUST_TABLE;
889 static const QString AUTH_CFG_REGEX;
891 bool mAuthInit =
false;
894 std::unique_ptr<QCA::Initializer> mQcaInitializer;
896 QHash<QString, QString> mConfigAuthMethods;
897 QHash<QString, QgsAuthMethod *> mAuthMethods;
901 bool mAuthDisabled =
false;
902 QString mAuthDisabledMessage;
903 QTimer *mScheduledDbEraseTimer =
nullptr;
904 bool mScheduledDbErase =
false;
905 int mScheduledDbEraseRequestWait = 3 ;
906 bool mScheduledDbEraseRequestEmitted =
false;
907 int mScheduledDbEraseRequestCount = 0;
909 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
910 std::unique_ptr<QMutex> mMutex;
911 std::unique_ptr<QMutex> mMasterPasswordMutex;
913 std::unique_ptr<QRecursiveMutex> mMutex;
914 std::unique_ptr<QRecursiveMutex> mMasterPasswordMutex;
919 QMap<QString, QPair<QgsAuthCertUtils::CaCertSource, QSslCertificate> > mCaCertsCache;
921 QMap<QgsAuthCertUtils::CertTrustPolicy, QStringList > mCertTrustCache;
923 QList<QSslCertificate> mTrustedCaCertsCache;
925 QHash<QString, QSet<QSslError::SslError> > mIgnoredSslErrorsCache;
927 bool mHasCustomConfigByHost =
false;
928 bool mHasCheckedIfCustomConfigByHostExists =
false;
929 QMap< QString, QgsAuthConfigSslServer > mCustomConfigByHostCache;
936 bool mPasswordHelperVerificationError =
false;
939 QString mPasswordHelperErrorMessage;
942 QKeychain::Error mPasswordHelperErrorCode = QKeychain::NoError;
945 bool mPasswordHelperLoggingEnabled =
false;
948 bool mPasswordHelperFailedInit =
false;
951 static const QLatin1String AUTH_PASSWORD_HELPER_KEY_NAME;
954 static const QLatin1String AUTH_PASSWORD_HELPER_FOLDER_NAME;
956 mutable QMap<QThread *, QMetaObject::Connection> mConnectedThreads;
962 #endif // QGSAUTHMANAGER_H