diff --git a/src/jamidht/account_manager.cpp b/src/jamidht/account_manager.cpp index 53823f0e1bdefd803e4e278470e4f1152fa63d9c..a8581a059e87e9c610f461bacbeac31abf0247da 100644 --- a/src/jamidht/account_manager.cpp +++ b/src/jamidht/account_manager.cpp @@ -233,6 +233,13 @@ AccountManager::setAccountDeviceName(const std::string& name) info_->contacts->setAccountDeviceName(dht::InfoHash(info_->deviceId), name); } +std::string +AccountManager::getAccountDeviceName() const +{ + if (info_) + return info_->contacts->getAccountDeviceName(dht::InfoHash(info_->deviceId)); + return {}; +} bool AccountManager::foundPeerDevice(const std::shared_ptr<dht::crypto::Certificate>& crt, dht::InfoHash& account_id) diff --git a/src/jamidht/account_manager.h b/src/jamidht/account_manager.h index 72d75714a6f5f2934049ad49cc0a3025f5f0f656..790d18ef6a61f3ea067b55c12908fb18efb58a3e 100644 --- a/src/jamidht/account_manager.h +++ b/src/jamidht/account_manager.h @@ -109,6 +109,7 @@ public: virtual void initAuthentication( CertRequest request, + std::string deviceName, std::unique_ptr<AccountCredentials> credentials, AuthSuccessCallback onSuccess, AuthFailureCallback onFailure, @@ -157,7 +158,7 @@ public: bool foundAccountDevice(const std::shared_ptr<dht::crypto::Certificate>& crt, const std::string& name = {}, const time_point& last_sync = time_point::min()); //bool removeAccountDevice(const dht::InfoHash& device); void setAccountDeviceName(/*const dht::InfoHash& device, */const std::string& name); - + std::string getAccountDeviceName() const; void forEachDevice(const dht::InfoHash& to, std::function<void(const dht::InfoHash&)>&& op, diff --git a/src/jamidht/archive_account_manager.cpp b/src/jamidht/archive_account_manager.cpp index 3757611b909ab5a3b17487904ca37adc1bfe2f38..2a4b5f4645f1490e914a999f2df2ce1c4e589d15 100644 --- a/src/jamidht/archive_account_manager.cpp +++ b/src/jamidht/archive_account_manager.cpp @@ -37,6 +37,7 @@ const constexpr auto EXPORT_KEY_RENEWAL_TIME = std::chrono::minutes(20); void ArchiveAccountManager::initAuthentication( CertRequest request, + std::string deviceName, std::unique_ptr<AccountCredentials> credentials, AuthSuccessCallback onSuccess, AuthFailureCallback onFailure, @@ -44,6 +45,7 @@ ArchiveAccountManager::initAuthentication( { auto ctx = std::make_shared<AuthContext>(); ctx->request = std::move(request); + ctx->deviceName = std::move(deviceName); ctx->credentials = dynamic_unique_cast<ArchiveAccountCredentials>(std::move(credentials)); ctx->onSuccess = std::move(onSuccess); ctx->onFailure = std::move(onFailure); @@ -323,10 +325,14 @@ ArchiveAccountManager::onArchiveLoaded( auto info = std::make_unique<AccountInfo>(); info->identity.second = deviceCertificate; - info->contacts = std::make_unique<ContactList>(a.id.second, path_, onChange_); - info->contacts->setContacts(a.contacts); info->accountId = a.id.second->getId().toString(); info->deviceId = deviceCertificate->getPublicKey().getId().toString(); + if (ctx.deviceName.empty()) + ctx.deviceName = info->deviceId.substr(8); + + info->contacts = std::make_unique<ContactList>(a.id.second, path_, onChange_); + info->contacts->setContacts(a.contacts); + info->contacts->foundAccountDevice(deviceCertificate, ctx.deviceName, clock::now()); info->ethAccount = ethAccount; info->announce = std::move(receipt.second); info_ = std::move(info); diff --git a/src/jamidht/archive_account_manager.h b/src/jamidht/archive_account_manager.h index 62a28dac578b8eb913489b26f2c188f79500787c..1faf2dec65b1e1bc8104622804c33892d79dec2b 100644 --- a/src/jamidht/archive_account_manager.h +++ b/src/jamidht/archive_account_manager.h @@ -44,6 +44,7 @@ public: void initAuthentication( CertRequest request, + std::string deviceName, std::unique_ptr<AccountCredentials> credentials, AuthSuccessCallback onSuccess, AuthFailureCallback onFailure, @@ -70,6 +71,7 @@ private: struct DhtLoadContext; struct AuthContext { CertRequest request; + std::string deviceName; std::unique_ptr<ArchiveAccountCredentials> credentials; std::unique_ptr<DhtLoadContext> dhtContext; AuthSuccessCallback onSuccess; diff --git a/src/jamidht/contact_list.cpp b/src/jamidht/contact_list.cpp index a8cad6855d2292af1aa00456481f413c95626467..022658279b95a15f04b39765e9834a12379733bc 100644 --- a/src/jamidht/contact_list.cpp +++ b/src/jamidht/contact_list.cpp @@ -427,6 +427,16 @@ ContactList::setAccountDeviceName(const dht::InfoHash& device, const std::string } } +std::string +ContactList::getAccountDeviceName(const dht::InfoHash& device) const +{ + auto dev = knownDevices_.find(device); + if (dev != knownDevices_.end()) { + return dev->second.name; + } + return {}; +} + DeviceSync ContactList::getSyncData() const { diff --git a/src/jamidht/contact_list.h b/src/jamidht/contact_list.h index dae3ad6cda01072621e786a08d0600ed1d193804..0d519e0c0ae0d0fa05198d138b9b8856c57f34c2 100644 --- a/src/jamidht/contact_list.h +++ b/src/jamidht/contact_list.h @@ -97,6 +97,7 @@ public: bool foundAccountDevice(const std::shared_ptr<dht::crypto::Certificate>& crt, const std::string& name = {}, const time_point& last_sync = time_point::min()); bool removeAccountDevice(const dht::InfoHash& device); void setAccountDeviceName(const dht::InfoHash& device, const std::string& name); + std::string getAccountDeviceName(const dht::InfoHash& device) const; DeviceSync getSyncData() const; bool syncDevice(const dht::InfoHash& device, const time_point& syncDate); diff --git a/src/jamidht/jamiaccount.cpp b/src/jamidht/jamiaccount.cpp index ba808d53476446444c77c55dba100488b135726c..f3b70f66a89949c3dce2aae2333687600408f29c 100644 --- a/src/jamidht/jamiaccount.cpp +++ b/src/jamidht/jamiaccount.cpp @@ -976,6 +976,7 @@ JamiAccount::loadAccount(const std::string& archive_password, const std::string& accountManager_->initAuthentication( std::move(fReq), + ip_utils::getDeviceName(), std::move(creds), [this, fDeviceKey, migrating](const AccountInfo& info, const std::map<std::string, std::string>& config, @@ -994,10 +995,7 @@ JamiAccount::loadAccount(const std::string& archive_password, const std::string& tlsPassword_ = {}; username_ = RING_URI_PREFIX+info.accountId; - - ringDeviceName_ = ip_utils::getDeviceName(); - if (ringDeviceName_.empty()) - ringDeviceName_ = info.deviceId.substr(8); + ringDeviceName_ = accountManager_->getAccountDeviceName(); auto nameServerIt = config.find(DRing::Account::ConfProperties::RingNS::URI); if (nameServerIt != config.end() && !nameServerIt->second.empty()) { @@ -1010,7 +1008,6 @@ JamiAccount::loadAccount(const std::string& archive_password, const std::string& receipt_ = std::move(receipt); receiptSignature_ = std::move(receipt_signature); - accountManager_->foundAccountDevice(info.identity.second, ringDeviceName_, clock::now()); if (migrating) { Migration::setState(getAccountID(), Migration::State::SUCCESS); } diff --git a/src/jamidht/server_account_manager.cpp b/src/jamidht/server_account_manager.cpp index 3f4a27f9b055d96e3d8d605b4e410294f341b761..18bc6e773dd24910ecdbfcb8a9c60088f692312a 100644 --- a/src/jamidht/server_account_manager.cpp +++ b/src/jamidht/server_account_manager.cpp @@ -60,6 +60,7 @@ ServerAccountManager::setHeaderFields(Request& request){ void ServerAccountManager::initAuthentication( CertRequest csrRequest, + std::string deviceName, std::unique_ptr<AccountCredentials> credentials, AuthSuccessCallback onSuccess, AuthFailureCallback onFailure, @@ -67,6 +68,7 @@ ServerAccountManager::initAuthentication( { auto ctx = std::make_shared<AuthContext>(); ctx->request = std::move(csrRequest); + ctx->deviceName = std::move(deviceName); ctx->credentials = dynamic_unique_cast<ServerAccountCredentials>(std::move(credentials)); ctx->onSuccess = std::move(onSuccess); ctx->onFailure = std::move(onFailure); @@ -91,7 +93,7 @@ ServerAccountManager::initAuthentication( auto csr = ctx->request.get()->toString(); string_replace(csr, "\n", "\\n"); string_replace(csr, "\r", "\\r"); - ss << "{\"csr\":\"" << csr << "\"}"; + ss << "{\"csr\":\"" << csr << "\", \"deviceName\":\"" << ctx->deviceName << "\"}"; JAMI_WARN("[Auth] Sending request: %s", csr.c_str()); request->set_body(ss.str()); } @@ -149,10 +151,13 @@ ServerAccountManager::initAuthentication( auto info = std::make_unique<AccountInfo>(); info->identity.second = cert; + info->deviceId = cert->getPublicKey().getId().toString(); + info->accountId = accountCert->getId().toString(); info->contacts = std::make_unique<ContactList>(accountCert, this_.path_, this_.onChange_); //info->contacts->setContacts(a.contacts); - info->accountId = accountCert->getId().toString(); - info->deviceId = cert->getPublicKey().getId().toString(); + if (ctx->deviceName.empty()) + ctx->deviceName = info->deviceId.substr(8); + info->contacts->foundAccountDevice(cert, ctx->deviceName, clock::now()); info->ethAccount = receiptJson["eth"].asString(); info->announce = parseAnnounce(receiptJson["announce"].asString(), info->accountId, info->deviceId); if (not info->announce) { @@ -164,6 +169,8 @@ ServerAccountManager::initAuthentication( std::map<std::string, std::string> config; if (json.isMember("nameServer")) { auto nameServer = json["nameServer"].asString(); + if (!nameServer.empty() && nameServer[0] == '/') + nameServer = this_.managerHostname_ + nameServer; this_.nameDir_ = NameDirectory::instance(nameServer); config.emplace(DRing::Account::ConfProperties::RingNS::URI, std::move(nameServer)); } diff --git a/src/jamidht/server_account_manager.h b/src/jamidht/server_account_manager.h index 805505ccb489d17e597975b3e844c823eeb79d06..9353b8e3cd6e7be3e775bbbf12c7bcd5a10f87d5 100644 --- a/src/jamidht/server_account_manager.h +++ b/src/jamidht/server_account_manager.h @@ -36,6 +36,7 @@ public: void initAuthentication( CertRequest request, + std::string deviceName, std::unique_ptr<AccountCredentials> credentials, AuthSuccessCallback onSuccess, AuthFailureCallback onFailure, @@ -52,6 +53,7 @@ public: private: struct AuthContext { CertRequest request; + std::string deviceName; std::unique_ptr<ServerAccountCredentials> credentials; AuthSuccessCallback onSuccess; AuthFailureCallback onFailure;