+/*** Authenticators ***/
+
+// Authentication handling, now independent from storage.
+template<typename Authenticator>
+void Core::registerAuthenticator()
+{
+ auto authenticator = makeDeferredShared<Authenticator>(this);
+ if (authenticator->isAvailable())
+ _registeredAuthenticators.emplace_back(std::move(authenticator));
+ else
+ authenticator->deleteLater();
+}
+
+
+void Core::registerAuthenticators()
+{
+ if (_registeredAuthenticators.empty()) {
+ registerAuthenticator<SqlAuthenticator>();
+#ifdef HAVE_LDAP
+ registerAuthenticator<LdapAuthenticator>();
+#endif
+ }
+}
+
+
+DeferredSharedPtr<Authenticator> Core::authenticator(const QString &backendId) const
+{
+ auto it = std::find_if(_registeredAuthenticators.begin(), _registeredAuthenticators.end(),
+ [backendId](const DeferredSharedPtr<Authenticator> &authenticator) {
+ return authenticator->backendId() == backendId;
+ });
+ return it != _registeredAuthenticators.end() ? *it : nullptr;
+}
+
+
+// FIXME: Apparently, this is the legacy way of initting storage backends?
+// If there's a not-legacy way, it should be used here
+bool Core::initAuthenticator(const QString &backend, const QVariantMap &settings,
+ const QProcessEnvironment &environment, bool loadFromEnvironment,
+ bool setup)
+{
+ if (backend.isEmpty()) {
+ quWarning() << "No authenticator selected!";
+ return false;
+ }
+
+ auto auth = authenticator(backend);
+ if (!auth) {
+ qCritical() << "Selected auth backend is not available:" << backend;
+ return false;
+ }
+
+ Authenticator::State authState = auth->init(settings, environment, loadFromEnvironment);
+ switch (authState) {
+ case Authenticator::NeedsSetup:
+ if (!setup)
+ return false; // trigger setup process
+ if (auth->setup(settings, environment, loadFromEnvironment))
+ return initAuthenticator(backend, settings, environment, loadFromEnvironment, false);
+ return false;
+
+ // if initialization wasn't successful, we quit to keep from coming up unconfigured
+ case Authenticator::NotAvailable:
+ qCritical() << "FATAL: Selected auth backend is not available:" << backend;
+ if (!setup)
+ exit(EXIT_FAILURE);
+ return false;
+
+ case Authenticator::IsReady:
+ // delete all other backends
+ _registeredAuthenticators.clear();
+ break;
+ }
+ _authenticator = std::move(auth);
+ return true;
+}
+
+