From 6871b9d5a6c5bf74bc1e8f893a4cc4ddef1b6491 Mon Sep 17 00:00:00 2001 From: Hai Shang Date: Mon, 28 Nov 2022 15:22:35 -0800 Subject: [PATCH] Add initial impl for UpdateRemotePublicCredentials in CredentialManager. PiperOrigin-RevId: 491458184 --- .../g3/credential_storage_impl.cc | 24 ++++++++-------- presence/implementation/BUILD | 1 + .../implementation/credential_manager_impl.cc | 22 +++++++++++++++ .../implementation/credential_manager_impl.h | 2 +- .../credential_manager_impl_test.cc | 28 +++++++++++++++++++ 5 files changed, 65 insertions(+), 12 deletions(-) diff --git a/internal/platform/implementation/g3/credential_storage_impl.cc b/internal/platform/implementation/g3/credential_storage_impl.cc index 9dc72db5..b34eb367 100644 --- a/internal/platform/implementation/g3/credential_storage_impl.cc +++ b/internal/platform/implementation/g3/credential_storage_impl.cc @@ -35,16 +35,19 @@ void CredentialStorageImpl::SaveCredentials( const std::vector& public_credentials, PublicCredentialType public_credential_type, ::nearby::presence::GenerateCredentialsCallback callback) { - NEARBY_LOGS(INFO) << "G3 Save Private Credentials for account: " - << account_name << "], manager app ID:[" << manager_app_id - << "]"; + if (private_credentials.empty() && public_credentials.empty()) { + NEARBY_LOGS(INFO) << "G3 Save Credentials but seeing private and public " + "both empty, skipping"; + return; + } if (private_credentials.empty()) { NEARBY_LOGS(INFO) << "There are no Private Credentials for account: " << account_name << "], manager app ID:[" << manager_app_id << "]"; - return; - } - { + } else { + NEARBY_LOGS(INFO) << "G3 Save Private Credentials for account: " + << account_name << "], manager app ID:[" << manager_app_id + << "]"; absl::MutexLock lock(&private_mutex_); PrivateCredentialKey key = CreatePrivateCredentialKey(manager_app_id, account_name); @@ -57,16 +60,15 @@ void CredentialStorageImpl::SaveCredentials( } } - NEARBY_LOGS(INFO) << "G3 Save Public Credentials for account: " - << account_name << "], manager app ID:[" << manager_app_id - << "]"; if (public_credentials.empty()) { NEARBY_LOGS(INFO) << "There are no Public Credentials for account: " << account_name << "], manager app ID:[" << manager_app_id << "]"; return; - } - { + } else { + NEARBY_LOGS(INFO) << "G3 Save Public Credentials for account: " + << account_name << "], manager app ID:[" << manager_app_id + << "]"; absl::MutexLock lock(&public_mutex_); PublicCredentialKey key = CreatePublicCredentialKey( manager_app_id, account_name, public_credential_type); diff --git a/presence/implementation/BUILD b/presence/implementation/BUILD index 1dcbbd7b..7e08eff9 100644 --- a/presence/implementation/BUILD +++ b/presence/implementation/BUILD @@ -239,6 +239,7 @@ cc_test( deps = [ ":internal", "//internal/platform:comm", + "//internal/platform:types", "//internal/platform/implementation:types", "//internal/platform/implementation/g3", # build_cleaner: keep "//internal/proto:credential_cc_proto", diff --git a/presence/implementation/credential_manager_impl.cc b/presence/implementation/credential_manager_impl.cc index 9e9624b7..9cfcf630 100644 --- a/presence/implementation/credential_manager_impl.cc +++ b/presence/implementation/credential_manager_impl.cc @@ -88,6 +88,28 @@ void CredentialManagerImpl::GenerateCredentials( std::move(credentials_generated_cb)); } +void CredentialManagerImpl::UpdateRemotePublicCredentials( + absl::string_view manager_app_id, absl::string_view account_name, + const std::vector& remote_public_creds, + UpdateRemotePublicCredentialsCallback credentials_updated_cb) { + credential_storage_ptr_->SaveCredentials( + manager_app_id, account_name, /* private_credentials */ {}, + remote_public_creds, PublicCredentialType::kRemotePublicCredential, + GenerateCredentialsCallback{ + .credentials_generated_cb = + [credentials_updated_cb = std::move(credentials_updated_cb)]( + std::vector creds) { + if (!creds.empty()) { + credentials_updated_cb.credentials_updated_cb( + CredentialOperationStatus::kSucceeded); + } else { + credentials_updated_cb.credentials_updated_cb( + CredentialOperationStatus::kFailed); + } + }, + }); +} + std::pair CredentialManagerImpl::CreatePrivateCredential( const DeviceMetadata& device_metadata, IdentityType identity_type, diff --git a/presence/implementation/credential_manager_impl.h b/presence/implementation/credential_manager_impl.h index 03938e92..e73a6b32 100644 --- a/presence/implementation/credential_manager_impl.h +++ b/presence/implementation/credential_manager_impl.h @@ -64,7 +64,7 @@ class CredentialManagerImpl : public CredentialManager { absl::string_view manager_app_id, absl::string_view account_name, const std::vector& remote_public_creds, - UpdateRemotePublicCredentialsCallback credentials_updated_cb) override{}; + UpdateRemotePublicCredentialsCallback credentials_updated_cb) override; void GetPrivateCredentials( const CredentialSelector& credential_selector, diff --git a/presence/implementation/credential_manager_impl_test.cc b/presence/implementation/credential_manager_impl_test.cc index 60a3e34a..dae78805 100644 --- a/presence/implementation/credential_manager_impl_test.cc +++ b/presence/implementation/credential_manager_impl_test.cc @@ -24,6 +24,7 @@ #include "protobuf-matchers/protocol-buffer-matchers.h" #include "gtest/gtest.h" #include "absl/status/status.h" +#include "internal/platform/count_down_latch.h" #include "internal/platform/credential_storage_impl.h" #include "internal/platform/implementation/crypto.h" #include "internal/proto/credential.pb.h" @@ -217,6 +218,33 @@ TEST(CredentialManagerImpl, GenerateCredentialsSuccessfullyButStoreFailed) { EXPECT_TRUE(publicCredentials.empty()); } +TEST(CredentialManagerImpl, UpdateRemotePublicCredentialsSuccessfully) { + nearby::internal::PublicCredential public_credential_for_test; + public_credential_for_test.set_identity_type( + nearby::internal::IdentityType::IDENTITY_TYPE_TRUSTED); + std::vector publicCredentials{ + {public_credential_for_test}}; + + location::nearby::CountDownLatch updated_latch(1); + UpdateRemotePublicCredentialsCallback update_credentials_cb{ + .credentials_updated_cb = + [&updated_latch](CredentialOperationStatus status) { + if (status == CredentialOperationStatus::kSucceeded) { + updated_latch.CountDown(); + } + }, + }; + + CredentialManagerImpl credential_manager; + + credential_manager.UpdateRemotePublicCredentials( + /* manager_app_id= */ "TEST_MANAGER_APP", + /* account_name= */ "test_account", publicCredentials, + update_credentials_cb); + + EXPECT_TRUE(updated_latch.Await().Ok()); +} + TEST(CredentialManagerImpl, GetPrivateCredentialsFailed) { std::vector private_credentials; auto get_credentials_fetched_cb =