From 79ecad5a0930850f3dbff7cba361f6df68183d80 Mon Sep 17 00:00:00 2001
From: Nicolas Werner <nicolas.werner@hotmail.de>
Date: Tue, 31 Aug 2021 01:51:03 +0200
Subject: [PATCH] Require a working secrets storage

---
 src/Cache.cpp | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)

diff --git a/src/Cache.cpp b/src/Cache.cpp
index bc364c345..a966fe57d 100644
--- a/src/Cache.cpp
+++ b/src/Cache.cpp
@@ -13,6 +13,7 @@
 #include <QFile>
 #include <QHash>
 #include <QMap>
+#include <QMessageBox>
 #include <QStandardPaths>
 
 #if __has_include(<keychain.h>)
@@ -754,6 +755,24 @@ Cache::backupVersion()
         }
 }
 
+static void
+fatalSecretError()
+{
+        QMessageBox::critical(
+          ChatPage::instance(),
+          QCoreApplication::translate("SecretStorage", "Failed to connect to secret storage"),
+          QCoreApplication::translate(
+            "SecretStorage",
+            "Nheko could not connect to the secure storage to save encryption secrets to. "
+            "This can have multiple reasons. Check if your D-Bus service is running and "
+            "you have configured a service like KWallet, Gnome Secrets or the equivalent "
+            "for your platform. If you are having trouble, feel free to open an issue "
+            "here: https://github.com/Nheko-Reborn/nheko/issues"));
+
+        QCoreApplication::exit(1);
+        exit(1);
+}
+
 void
 Cache::storeSecret(const std::string name, const std::string secret)
 {
@@ -780,6 +799,7 @@ Cache::storeSecret(const std::string name, const std::string secret)
                           nhlog::db()->warn("Storing secret '{}' failed: {}",
                                             name,
                                             job->errorString().toStdString());
+                          fatalSecretError();
                   } else {
                           // if we emit the signal directly, qtkeychain breaks and won't execute new
                           // jobs. You can't start a job from the finish signal of a job.
@@ -840,8 +860,14 @@ Cache::secret(const std::string name)
 
         const QString secret = job.textData();
         if (job.error()) {
-                nhlog::db()->debug(
-                  "Restoring secret '{}' failed: {}", name, job.errorString().toStdString());
+                if (job.error() == QKeychain::Error::EntryNotFound)
+                        return std::nullopt;
+                nhlog::db()->error("Restoring secret '{}' failed ({}): {}",
+                                   name,
+                                   job.error(),
+                                   job.errorString().toStdString());
+
+                fatalSecretError();
                 return std::nullopt;
         }
         if (secret.isEmpty()) {
-- 
GitLab