Ring Daemon 16.0.0
Loading...
Searching...
No Matches
archive_account_manager.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 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"
21
22#include <dhtnet/multiplexed_socket.h>
23#include <memory>
24
25namespace jami {
26
27// used for status codes on DeviceAuthStateChanged
29 INIT = 0,
31 CONNECTING = 2,
33 IN_PROGRESS = 4,
34 DONE = 5,
35};
36
38{
39public:
40 using OnExportConfig = std::function<std::map<std::string, std::string>()>;
41
42 ArchiveAccountManager(const std::string& accountId,
43 const std::filesystem::path& path,
45 std::string archivePath,
46 const std::string& nameServer)
47 : AccountManager(accountId, path, nameServer)
48 , onExportConfig_(std::move(onExportConfig))
49 , archivePath_(std::move(archivePath))
50 {}
51
53 {
55 std::vector<std::string> dhtBootstrap;
56 dht::crypto::Identity updateIdentity;
57 };
58
59 void initAuthentication(PrivateKey request,
60 std::string deviceName,
61 std::unique_ptr<AccountCredentials> credentials,
62 AuthSuccessCallback onSuccess,
63 AuthFailureCallback onFailure,
64 const OnChangeCallback& onChange) override;
65
66 void startSync(const OnNewDeviceCb&,
67 const OnDeviceAnnouncedCb& dcb = {},
68 bool publishPresence = true) override;
69
70 bool changePassword(const std::string& password_old, const std::string& password_new) override;
71 virtual std::vector<uint8_t> getPasswordKey(const std::string& /*password*/) override;
72
73 void syncDevices() override;
74
75 int32_t addDevice(const std::string& uri,
76 std::string_view auth_scheme,
77 AuthChannelHandler*) override;
78 bool cancelAddDevice(uint32_t token) override;
79 bool confirmAddDevice(uint32_t token) override;
80
81 bool revokeDevice(const std::string& device,
82 std::string_view scheme,
83 const std::string& password,
84 RevokeDeviceCallback) override;
85 bool exportArchive(const std::string& destinationPath,
86 std::string_view scheme,
87 const std::string& password);
88 bool isPasswordValid(const std::string& password) override;
89
90 bool provideAccountAuthentication(const std::string& credentialsFromUser,
91 const std::string& scheme);
92
93#if HAVE_RINGNS
94 void registerName(const std::string& name,
95 std::string_view scheme,
96 const std::string& password,
97 RegistrationCallback cb) override;
98#endif
99
103 bool setValidity(std::string_view scheme,
104 const std::string& password,
105 dht::crypto::Identity& device,
106 const dht::InfoHash& id,
108
109 // for linking devices
110 void onAuthReady(const std::string& deviceId, std::shared_ptr<dhtnet::ChannelSocket> channel);
111
112private:
113 struct DhtLoadContext;
114 struct DeviceContextBase;
115 struct AddDeviceContext;
116 struct LinkDeviceContext;
117 struct AuthContext
118 {
119 std::mutex mutex;
120 std::string accountId;
121 uint32_t token;
122 PrivateKey key;
123 CertRequest request;
124 std::string deviceName;
125 std::unique_ptr<ArchiveAccountCredentials> credentials;
126 std::unique_ptr<DhtLoadContext> dhtContext;
127 std::shared_ptr<LinkDeviceContext> linkDevCtx; // data for NEW dev
128 std::unique_ptr<AddDeviceContext> addDeviceCtx; // data for OLD dev
129 AuthSuccessCallback onSuccess;
130 AuthFailureCallback onFailure;
131 std::unique_ptr<asio::steady_timer> timeout;
132 bool canceled {false};
133 };
134 struct DecodingContext;
135 struct AuthMsg;
136 struct DeviceAuthInfo;
137 std::shared_ptr<AuthContext> authCtx_;
138
139 void createAccount(AuthContext& ctx);
140 void migrateAccount(AuthContext& ctx);
141
142 std::pair<std::string, std::shared_ptr<dht::Value>> makeReceipt(
143 const dht::crypto::Identity& id,
144 const dht::crypto::Certificate& device,
145 const std::string& ethAccount);
146 void updateArchive(AccountArchive& content /*, const ContactList& syncData*/) const;
147 void saveArchive(AccountArchive& content, std::string_view scheme, const std::string& pwd);
148 AccountArchive readArchive(std::string_view scheme, const std::string& password) const;
149 static std::pair<std::vector<uint8_t>, dht::InfoHash> computeKeys(const std::string& password,
150 const std::string& pin,
151 bool previous = false);
152 bool updateCertificates(AccountArchive& archive, dht::crypto::Identity& device);
153 static bool needsMigration(const std::string& accountId, const dht::crypto::Identity& id);
154
155 void loadFromFile(AuthContext& ctx);
156
157 // for linking devices
158 void startLoadArchiveFromDevice(const std::shared_ptr<AuthContext>& ctx);
159
160 bool doAddDevice(std::string_view scheme,
161 const std::shared_ptr<AuthContext>& ctx,
162 const std::shared_ptr<dhtnet::ChannelSocket>& channel);
163
164 void loadFromDHT(const std::shared_ptr<AuthContext>& ctx);
165 void onArchiveLoaded(AuthContext& ctx, AccountArchive&& a, bool isLinkDevProtocol);
166
167 inline std::weak_ptr<ArchiveAccountManager> weak()
168 {
169 return std::static_pointer_cast<ArchiveAccountManager>(shared_from_this());
170 }
171
172 OnExportConfig onExportConfig_;
173 std::string archivePath_;
174};
175
176} // namespace jami
NameDirectory::RegistrationCallback RegistrationCallback
std::function< void(const std::shared_ptr< dht::crypto::Certificate > &)> OnNewDeviceCb
virtual void registerName(const std::string &name, std::string_view scheme, const std::string &password, RegistrationCallback cb)=0
std::function< void()> OnDeviceAnnouncedCb
std::shared_future< std::shared_ptr< dht::crypto::PrivateKey > > PrivateKey
std::function< void(RevokeDeviceResult)> RevokeDeviceCallback
std::function< void(AuthError error, const std::string &message)> AuthFailureCallback
std::future< std::unique_ptr< dht::crypto::CertificateRequest > > CertRequest
std::function< void(const AccountInfo &info, const std::map< std::string, std::string > &config, std::string &&receipt, std::vector< uint8_t > &&receipt_signature)> AuthSuccessCallback
virtual std::vector< uint8_t > getPasswordKey(const std::string &) override
bool exportArchive(const std::string &destinationPath, std::string_view scheme, const std::string &password)
bool cancelAddDevice(uint32_t token) override
bool provideAccountAuthentication(const std::string &credentialsFromUser, const std::string &scheme)
int32_t addDevice(const std::string &uri, std::string_view auth_scheme, AuthChannelHandler *) override
Initiates the process of adding a new device to the account.
std::function< std::map< std::string, std::string >()> OnExportConfig
void initAuthentication(PrivateKey request, std::string deviceName, std::unique_ptr< AccountCredentials > credentials, AuthSuccessCallback onSuccess, AuthFailureCallback onFailure, const OnChangeCallback &onChange) override
bool revokeDevice(const std::string &device, std::string_view scheme, const std::string &password, RevokeDeviceCallback) override
bool confirmAddDevice(uint32_t token) override
bool setValidity(std::string_view scheme, const std::string &password, dht::crypto::Identity &device, const dht::InfoHash &id, int64_t validity)
Change the validity of a certificate.
ArchiveAccountManager(const std::string &accountId, const std::filesystem::path &path, OnExportConfig &&onExportConfig, std::string archivePath, const std::string &nameServer)
void startSync(const OnNewDeviceCb &, const OnDeviceAnnouncedCb &dcb={}, bool publishPresence=true) override
bool isPasswordValid(const std::string &password) override
void onAuthReady(const std::string &deviceId, std::shared_ptr< dhtnet::ChannelSocket > channel)
bool changePassword(const std::string &password_old, const std::string &password_new) override
Manages channels for syncing informations.
void emitSignal(Args... args)
Definition ring_signal.h:64