From 911e19d07c648f46fa6cd3289696585124e6694b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Sun, 3 Dec 2023 11:54:02 -0500
Subject: [PATCH] JamiAccount, libjami: add getPasswordKey

Change-Id: I362b44636319021437f853cddcb6af647cc11778
---
 bin/jni/configurationmanager.i            |  2 ++
 src/client/configurationmanager.cpp       | 13 ++++++++++++-
 src/jami/configurationmanager_interface.h |  1 +
 src/jamidht/account_manager.h             |  1 +
 src/jamidht/archive_account_manager.cpp   | 12 ++++++++++++
 src/jamidht/archive_account_manager.h     |  1 +
 src/jamidht/jamiaccount.cpp               |  7 +++++++
 src/jamidht/jamiaccount.h                 |  1 +
 src/manager.cpp                           |  9 ---------
 src/manager.h                             |  2 --
 10 files changed, 37 insertions(+), 12 deletions(-)

diff --git a/bin/jni/configurationmanager.i b/bin/jni/configurationmanager.i
index fd2b7dd805..4f54775462 100644
--- a/bin/jni/configurationmanager.i
+++ b/bin/jni/configurationmanager.i
@@ -105,6 +105,8 @@ bool cancelMessage(const std::string& accountID, uint64_t id);
 void setIsComposing(const std::string& accountID, const std::string& conversationUri, bool isWriting);
 bool setMessageDisplayed(const std::string& accountID, const std::string& conversationUri, const std::string& messageId, int status);
 bool changeAccountPassword(const std::string& accountID, const std::string& password_old, const std::string& password_new);
+bool isPasswordValid(const std::string& accountId, const std::string& password);
+std::vector<uint8_t> getPasswordKey(const std::string& accountId, const std::string& password);
 
 bool lookupName(const std::string& account, const std::string& nameserver, const std::string& name);
 bool lookupAddress(const std::string& account, const std::string& nameserver, const std::string& address);
diff --git a/src/client/configurationmanager.cpp b/src/client/configurationmanager.cpp
index 3db4b7bde5..289fc07dff 100644
--- a/src/client/configurationmanager.cpp
+++ b/src/client/configurationmanager.cpp
@@ -251,9 +251,20 @@ sendRegister(const std::string& accountId, bool enable)
 bool
 isPasswordValid(const std::string& accountId, const std::string& password)
 {
-    return jami::Manager::instance().isPasswordValid(accountId, password);
+    if (auto acc = jami::Manager::instance().getAccount<JamiAccount>(accountId))
+        return acc->isPasswordValid(password);
+    return false;
+}
+
+std::vector<uint8_t>
+getPasswordKey(const std::string& accountID, const std::string& password)
+{
+    if (auto acc = jami::Manager::instance().getAccount<JamiAccount>(accountID))
+        return acc->getPasswordKey(password);
+    return {};
 }
 
+
 void
 registerAllAccounts()
 {
diff --git a/src/jami/configurationmanager_interface.h b/src/jami/configurationmanager_interface.h
index a4580eb887..4fa74f576d 100644
--- a/src/jami/configurationmanager_interface.h
+++ b/src/jami/configurationmanager_interface.h
@@ -82,6 +82,7 @@ LIBJAMI_PUBLIC bool changeAccountPassword(const std::string& accountID,
                                           const std::string& password_old,
                                           const std::string& password_new);
 LIBJAMI_PUBLIC bool isPasswordValid(const std::string& accountID, const std::string& password);
+LIBJAMI_PUBLIC std::vector<uint8_t> getPasswordKey(const std::string& accountID, const std::string& password);
 
 LIBJAMI_PUBLIC bool lookupName(const std::string& account,
                                const std::string& nameserver,
diff --git a/src/jamidht/account_manager.h b/src/jamidht/account_manager.h
index 3c8e10b578..b2d7986822 100644
--- a/src/jamidht/account_manager.h
+++ b/src/jamidht/account_manager.h
@@ -124,6 +124,7 @@ public:
     virtual void onSyncData(DeviceSync&& device, bool checkDevice = true);
 
     virtual bool isPasswordValid(const std::string& /*password*/) { return false; };
+    virtual std::vector<uint8_t> getPasswordKey(const std::string& /*password*/) { return {}; };
 
     dht::crypto::Identity loadIdentity(const std::string& accountId,
                                        const std::string& crt_path,
diff --git a/src/jamidht/archive_account_manager.cpp b/src/jamidht/archive_account_manager.cpp
index 0fc8c299af..0e90c1a2a2 100644
--- a/src/jamidht/archive_account_manager.cpp
+++ b/src/jamidht/archive_account_manager.cpp
@@ -651,6 +651,18 @@ ArchiveAccountManager::changePassword(const std::string& password_old,
     }
 }
 
+std::vector<uint8_t>
+ArchiveAccountManager::getPasswordKey(const std::string& password)
+{
+    try {
+        auto data = dhtnet::fileutils::loadFile(fileutils::getFullPath(path_, archivePath_));
+        return dht::crypto::aesGetKey(data, password);
+    } catch (const std::exception& e) {
+        JAMI_ERR("Error loading archive: %s", e.what());
+    }
+    return {};
+}
+
 std::string
 generatePIN(size_t length = 16, size_t split = 8)
 {
diff --git a/src/jamidht/archive_account_manager.h b/src/jamidht/archive_account_manager.h
index 55d9b30aea..03874a9705 100644
--- a/src/jamidht/archive_account_manager.h
+++ b/src/jamidht/archive_account_manager.h
@@ -52,6 +52,7 @@ public:
     void startSync(const OnNewDeviceCb&, const OnDeviceAnnouncedCb& dcb = {}, bool publishPresence = true) override;
 
     bool changePassword(const std::string& password_old, const std::string& password_new) override;
+    virtual std::vector<uint8_t> getPasswordKey(const std::string& /*password*/) override;
 
     void syncDevices() override;
 
diff --git a/src/jamidht/jamiaccount.cpp b/src/jamidht/jamiaccount.cpp
index 55546bd558..0479d1aa20 100644
--- a/src/jamidht/jamiaccount.cpp
+++ b/src/jamidht/jamiaccount.cpp
@@ -971,6 +971,13 @@ JamiAccount::isPasswordValid(const std::string& password)
     return accountManager_ and accountManager_->isPasswordValid(password);
 }
 
+std::vector<uint8_t>
+JamiAccount::getPasswordKey(const std::string& password)
+{
+    return accountManager_ ? accountManager_->getPasswordKey(password) : std::vector<uint8_t>();
+}
+
+
 void
 JamiAccount::addDevice(const std::string& password)
 {
diff --git a/src/jamidht/jamiaccount.h b/src/jamidht/jamiaccount.h
index 2747ce442b..a0c31287f5 100644
--- a/src/jamidht/jamiaccount.h
+++ b/src/jamidht/jamiaccount.h
@@ -349,6 +349,7 @@ public:
     std::map<std::string, std::string> getKnownDevices() const;
 
     bool isPasswordValid(const std::string& password);
+    std::vector<uint8_t> getPasswordKey(const std::string& password);
 
     bool changeArchivePassword(const std::string& password_old, const std::string& password_new);
 
diff --git a/src/manager.cpp b/src/manager.cpp
index 2513bd4076..4e5bce6f81 100644
--- a/src/manager.cpp
+++ b/src/manager.cpp
@@ -2976,15 +2976,6 @@ Manager::sendRegister(const std::string& accountID, bool enable)
         acc->doUnregister();
 }
 
-bool
-Manager::isPasswordValid(const std::string& accountID, const std::string& password)
-{
-    const auto acc = getAccount<JamiAccount>(accountID);
-    if (!acc)
-        return false;
-    return acc->isPasswordValid(password);
-}
-
 uint64_t
 Manager::sendTextMessage(const std::string& accountID,
                          const std::string& to,
diff --git a/src/manager.h b/src/manager.h
index 24945a6a52..a0e822d8f1 100644
--- a/src/manager.h
+++ b/src/manager.h
@@ -390,8 +390,6 @@ public:
      */
     void sendRegister(const std::string& accountId, bool enable);
 
-    bool isPasswordValid(const std::string& accountID, const std::string& password);
-
     uint64_t sendTextMessage(const std::string& accountID,
                              const std::string& to,
                              const std::map<std::string, std::string>& payloads,
-- 
GitLab