Line data Source code
1 : /*
2 : * Copyright (C) 2004-2024 Savoir-faire Linux Inc.
3 : *
4 : * This program is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * This program is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 : */
17 : #pragma once
18 :
19 : #include "account_manager.h"
20 :
21 : namespace jami {
22 :
23 : class ArchiveAccountManager : public AccountManager
24 : {
25 : public:
26 : using OnExportConfig = std::function<std::map<std::string, std::string>()>;
27 :
28 799 : ArchiveAccountManager(const std::filesystem::path& path,
29 : OnExportConfig&& onExportConfig,
30 : std::string archivePath,
31 : const std::string& nameServer)
32 799 : : AccountManager(path, nameServer)
33 799 : , onExportConfig_(std::move(onExportConfig))
34 1598 : , archivePath_(std::move(archivePath)) {}
35 :
36 : struct ArchiveAccountCredentials : AccountCredentials
37 : {
38 : in_port_t dhtPort;
39 : std::vector<std::string> dhtBootstrap;
40 : dht::crypto::Identity updateIdentity;
41 : };
42 :
43 : void initAuthentication(const std::string& accountId,
44 : PrivateKey request,
45 : std::string deviceName,
46 : std::unique_ptr<AccountCredentials> credentials,
47 : AuthSuccessCallback onSuccess,
48 : AuthFailureCallback onFailure,
49 : const OnChangeCallback& onChange) override;
50 :
51 : void startSync(const OnNewDeviceCb&, const OnDeviceAnnouncedCb& dcb = {}, bool publishPresence = true) override;
52 :
53 : bool changePassword(const std::string& password_old, const std::string& password_new) override;
54 : virtual std::vector<uint8_t> getPasswordKey(const std::string& /*password*/) override;
55 :
56 : void syncDevices() override;
57 :
58 : void addDevice(const std::string& password, AddDeviceCallback) override;
59 : bool revokeDevice(const std::string& device,
60 : std::string_view scheme, const std::string& password,
61 : RevokeDeviceCallback) override;
62 : bool exportArchive(const std::string& destinationPath, std::string_view scheme, const std::string& password);
63 : bool isPasswordValid(const std::string& password) override;
64 :
65 : #if HAVE_RINGNS
66 : /*void lookupName(const std::string& name, LookupCallback cb) override;
67 : void lookupAddress(const std::string& address, LookupCallback cb) override;*/
68 : void registerName(const std::string& name,
69 : std::string_view scheme, const std::string& password,
70 : RegistrationCallback cb) override;
71 : #endif
72 :
73 : /**
74 : * Change the validity of a certificate. If hash is empty, update all certificates
75 : */
76 : bool setValidity(std::string_view scheme, const std::string& password,
77 : dht::crypto::Identity& device,
78 : const dht::InfoHash& id,
79 : int64_t validity);
80 :
81 : private:
82 : struct DhtLoadContext;
83 : struct AuthContext
84 : {
85 : std::string accountId;
86 : PrivateKey key;
87 : CertRequest request;
88 : std::string deviceName;
89 : std::unique_ptr<ArchiveAccountCredentials> credentials;
90 : std::unique_ptr<DhtLoadContext> dhtContext;
91 : AuthSuccessCallback onSuccess;
92 : AuthFailureCallback onFailure;
93 : };
94 :
95 : void createAccount(AuthContext& ctx);
96 : void migrateAccount(AuthContext& ctx);
97 :
98 : std::pair<std::string, std::shared_ptr<dht::Value>> makeReceipt(
99 : const dht::crypto::Identity& id,
100 : const dht::crypto::Certificate& device,
101 : const std::string& ethAccount);
102 : void updateArchive(AccountArchive& content /*, const ContactList& syncData*/) const;
103 : void saveArchive(AccountArchive& content, std::string_view scheme, const std::string& pwd);
104 : AccountArchive readArchive(std::string_view scheme, const std::string& password) const;
105 : static std::pair<std::vector<uint8_t>, dht::InfoHash> computeKeys(const std::string& password,
106 : const std::string& pin,
107 : bool previous = false);
108 : bool updateCertificates(AccountArchive& archive, dht::crypto::Identity& device);
109 : static bool needsMigration(const dht::crypto::Identity& id);
110 :
111 : void loadFromFile(AuthContext& ctx);
112 : void loadFromDHT(const std::shared_ptr<AuthContext>& ctx);
113 : void onArchiveLoaded(AuthContext& ctx,
114 : AccountArchive&& a);
115 :
116 : OnExportConfig onExportConfig_;
117 : std::string archivePath_;
118 : };
119 :
120 : } // namespace jami
|