Rework the handling of storage/auth backends and config
[quassel.git] / src / core / core.h
index d544744..333d067 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-2015 by the Quassel Project                        *
+ *   Copyright (C) 2005-2016 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
  ***************************************************************************/
 
-#ifndef CORE_H
-#define CORE_H
+#pragma once
+
+#include <memory>
+#include <vector>
 
 #include <QDateTime>
 #include <QString>
@@ -34,7 +36,9 @@
 #  include <QTcpServer>
 #endif
 
+#include "authenticator.h"
 #include "bufferinfo.h"
+#include "deferredptr.h"
 #include "message.h"
 #include "oidentdconfiggenerator.h"
 #include "sessionthread.h"
@@ -74,6 +78,52 @@ public:
         return instance()->_storage->validateUser(userName, password);
     }
 
+    //! Authenticate user against auth backend
+    /**
+     * \param userName The user's login name
+     * \param password The user's uncrypted password
+     * \return The user's ID if valid; 0 otherwise
+     */
+    static inline UserId authenticateUser(const QString &userName, const QString &password) {
+        return instance()->_authenticator->validateUser(userName, password);
+    }
+
+    //! Add a new user, exposed so auth providers can call this without being the storage.
+    /**
+     * \param userName The user's login name
+     * \param password The user's uncrypted password
+     * \param authenticator The name of the auth provider service used to log the user in, defaults to "Database".
+     * \return The user's ID if valid; 0 otherwise
+     */
+    static inline UserId addUser(const QString &userName, const QString &password, const QString &authenticator = "Database") {
+        return instance()->_storage->addUser(userName, password, authenticator);
+    }
+
+    //! Does a comparison test against the authenticator in the database and the authenticator currently in use for a UserID.
+    /**
+     * \param userid The user's ID (note: not login name).
+     * \param authenticator The name of the auth provider service used to log the user in, defaults to "Database".
+     * \return True if the userid was configured with the passed authenticator, false otherwise.
+     */
+    static inline bool checkAuthProvider(const UserId userid, const QString &authenticator) {
+        return instance()->_storage->getUserAuthenticator(userid) == authenticator;
+    }
+
+    //! Change a user's password
+    /**
+     * \param userId     The user's ID
+     * \param password   The user's unencrypted new password
+     * \return true, if the password change was successful
+     */
+    static bool changeUserPassword(UserId userId, const QString &password);
+
+    //! Check if we can change a user password.
+    /**
+     * \param userID     The user's ID
+     * \return true, if we can change their password, false otherwise
+     */
+    static bool canChangeUserPassword(UserId userId);
+
     //! Store a user setting persistently
     /**
      * \param userId       The users Id
@@ -485,9 +535,18 @@ public:
     static inline QDateTime startTime() { return instance()->_startTime; }
     static inline bool isConfigured() { return instance()->_configured; }
     static bool sslSupported();
+
+    /**
+     * Reloads SSL certificates used for connection with clients
+     *
+     * @return True if certificates reloaded successfully, otherwise false.
+     */
+    static bool reloadCerts();
+
     static QVariantList backendInfo();
+    static QVariantList authenticatorInfo();
 
-    static QString setup(const QString &adminUser, const QString &adminPassword, const QString &backend, const QVariantMap &setupData);
+    static QString setup(const QString &adminUser, const QString &adminPassword, const QString &backend, const QVariantMap &setupData, const QString &authenticator, const QVariantMap &authSetupMap);
 
     static inline QTimer &syncTimer() { return instance()->_storageSyncTimer; }
 
@@ -501,7 +560,7 @@ public slots:
      */
     void syncStorage();
     void setupInternalClientSession(InternalPeer *clientConnection);
-    QString setupCore(const QString &adminUser, const QString &adminPassword, const QString &backend, const QVariantMap &setupData);
+    QString setupCore(const QString &adminUser, const QString &adminPassword, const QString &backend, const QVariantMap &setupData, const QString &authenticator, const QVariantMap &authSetupMap);
 
 signals:
     //! Sent when a BufferInfo is updated in storage.
@@ -520,35 +579,52 @@ private slots:
     void clientDisconnected();
 
     bool initStorage(const QString &backend, const QVariantMap &settings, bool setup = false);
+    bool initAuthenticator(const QString &backend, const QVariantMap &settings, bool setup = false);
 
     void socketError(QAbstractSocket::SocketError err, const QString &errorString);
     void setupClientSession(RemotePeer *, UserId);
 
+    bool changeUserPass(const QString &username);
+
 private:
     Core();
     ~Core();
     void init();
     static Core *instanceptr;
 
-    SessionThread *createSession(UserId userId, bool restoreState = false);
+    SessionThread *sessionForUser(UserId userId, bool restoreState = false);
     void addClientHelper(RemotePeer *peer, UserId uid);
     //void processCoreSetup(QTcpSocket *socket, QVariantMap &msg);
     QString setupCoreForInternalUsage();
 
+    bool createUser();
+
+    template<typename Storage>
+    void registerStorageBackend();
+
+    template<typename Authenticator>
+    void registerAuthenticator();
+
     void registerStorageBackends();
-    bool registerStorageBackend(Storage *);
-    void unregisterStorageBackends();
-    void unregisterStorageBackend(Storage *);
+    void registerAuthenticators();
+
+    DeferredSharedPtr<Storage>       storageBackend(const QString& backendId) const;
+    DeferredSharedPtr<Authenticator> authenticator(const QString& authenticatorId) const;
+
     bool selectBackend(const QString &backend);
-    void createUser();
-    void changeUserPass(const QString &username);
-    void saveBackendSettings(const QString &backend, const QVariantMap &settings);
-    QVariantMap promptForSettings(const Storage *storage);
+    bool selectAuthenticator(const QString &backend);
+
+    bool saveBackendSettings(const QString &backend, const QVariantMap &settings);
+    void saveAuthenticatorSettings(const QString &backend, const QVariantMap &settings);
+
+    template<typename Backend>
+    QVariantMap promptForSettings(const Backend *backend);
 
 private:
     QSet<CoreAuthHandler *> _connectingClients;
-    QHash<UserId, SessionThread *> sessions;
-    Storage *_storage;
+    QHash<UserId, SessionThread *> _sessions;
+    DeferredSharedPtr<Storage>       _storage;        ///< Active storage backend
+    DeferredSharedPtr<Authenticator> _authenticator;  ///< Active authenticator
     QTimer _storageSyncTimer;
 
 #ifdef HAVE_SSL
@@ -557,20 +633,18 @@ private:
     QTcpServer _server, _v6server;
 #endif
 
-    OidentdConfigGenerator *_oidentdConfigGenerator;
+    OidentdConfigGenerator *_oidentdConfigGenerator {nullptr};
 
-    QHash<QString, Storage *> _storageBackends;
+    std::vector<DeferredSharedPtr<Storage>>       _registeredStorageBackends;
+    std::vector<DeferredSharedPtr<Authenticator>> _registeredAuthenticators;
 
     QDateTime _startTime;
 
     bool _configured;
 
-    static AbstractSqlMigrationReader *getMigrationReader(Storage *storage);
-    static AbstractSqlMigrationWriter *getMigrationWriter(Storage *storage);
+    static std::unique_ptr<AbstractSqlMigrationReader> getMigrationReader(Storage *storage);
+    static std::unique_ptr<AbstractSqlMigrationWriter> getMigrationWriter(Storage *storage);
     static void stdInEcho(bool on);
     static inline void enableStdInEcho() { stdInEcho(true); }
     static inline void disableStdInEcho() { stdInEcho(false); }
 };
-
-
-#endif