Add initial impl for UpdateRemotePublicCredentials in CredentialManager.

PiperOrigin-RevId: 491458184
This commit is contained in:
Hai Shang
2022-11-28 15:24:26 -08:00
committed by Copybara-Service
parent 0cb5b004e4
commit 6871b9d5a6
5 changed files with 65 additions and 12 deletions
@@ -35,16 +35,19 @@ void CredentialStorageImpl::SaveCredentials(
const std::vector<PublicCredential>& 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);
+1
View File
@@ -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",
@@ -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<nearby::internal::PublicCredential>& 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<nearby::internal::PublicCredential> creds) {
if (!creds.empty()) {
credentials_updated_cb.credentials_updated_cb(
CredentialOperationStatus::kSucceeded);
} else {
credentials_updated_cb.credentials_updated_cb(
CredentialOperationStatus::kFailed);
}
},
});
}
std::pair<PrivateCredential, PublicCredential>
CredentialManagerImpl::CreatePrivateCredential(
const DeviceMetadata& device_metadata, IdentityType identity_type,
@@ -64,7 +64,7 @@ class CredentialManagerImpl : public CredentialManager {
absl::string_view manager_app_id, absl::string_view account_name,
const std::vector<nearby::internal::PublicCredential>&
remote_public_creds,
UpdateRemotePublicCredentialsCallback credentials_updated_cb) override{};
UpdateRemotePublicCredentialsCallback credentials_updated_cb) override;
void GetPrivateCredentials(
const CredentialSelector& credential_selector,
@@ -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<nearby::internal::PublicCredential> 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<PrivateCredential> private_credentials;
auto get_credentials_fetched_cb =