mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Consolidate credential_storage callback functions. Move all callback struct definitions in a central header file compiled as part of internal/platform/implementation:comm library.
PiperOrigin-RevId: 476186596
This commit is contained in:
committed by
Copybara-Service
parent
0e6e22b4db
commit
fd59e9aede
@@ -16,33 +16,40 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "internal/platform/implementation/credential_callbacks.h"
|
||||||
|
|
||||||
namespace location {
|
namespace location {
|
||||||
namespace nearby {
|
namespace nearby {
|
||||||
|
|
||||||
using ::nearby::internal::PrivateCredential;
|
using ::nearby::internal::PrivateCredential;
|
||||||
using ::nearby::internal::PublicCredential;
|
using ::nearby::internal::PublicCredential;
|
||||||
|
using ::nearby::presence::CredentialSelector;
|
||||||
|
using ::nearby::presence::GenerateCredentialsCallback;
|
||||||
|
using ::nearby::presence::GetPrivateCredentialsResultCallback;
|
||||||
|
using ::nearby::presence::GetPublicCredentialsResultCallback;
|
||||||
|
using ::nearby::presence::PublicCredentialType;
|
||||||
|
|
||||||
void CredentialStorageImpl::SaveCredentials(
|
void CredentialStorageImpl::SaveCredentials(
|
||||||
absl::string_view manager_app_id, absl::string_view account_name,
|
absl::string_view manager_app_id, absl::string_view account_name,
|
||||||
const std::vector<PrivateCredential>& private_credentials,
|
const std::vector<PrivateCredential>& private_credentials,
|
||||||
const std::vector<PublicCredential>& public_credentials,
|
const std::vector<PublicCredential>& public_credentials,
|
||||||
api::PublicCredentialType public_credential_type,
|
PublicCredentialType public_credential_type,
|
||||||
api::SaveCredentialsResultCallback callback) {
|
GenerateCredentialsCallback callback) {
|
||||||
return impl_->SaveCredentials(manager_app_id, account_name,
|
return impl_->SaveCredentials(manager_app_id, account_name,
|
||||||
private_credentials, public_credentials,
|
private_credentials, public_credentials,
|
||||||
public_credential_type, std::move(callback));
|
public_credential_type, std::move(callback));
|
||||||
}
|
}
|
||||||
|
|
||||||
void CredentialStorageImpl::GetPrivateCredentials(
|
void CredentialStorageImpl::GetPrivateCredentials(
|
||||||
const api::CredentialSelector& credential_selector,
|
const CredentialSelector& credential_selector,
|
||||||
api::GetPrivateCredentialsResultCallback callback) {
|
GetPrivateCredentialsResultCallback callback) {
|
||||||
return impl_->GetPrivateCredentials(credential_selector, std::move(callback));
|
return impl_->GetPrivateCredentials(credential_selector, std::move(callback));
|
||||||
}
|
}
|
||||||
|
|
||||||
void CredentialStorageImpl::GetPublicCredentials(
|
void CredentialStorageImpl::GetPublicCredentials(
|
||||||
const api::CredentialSelector& credential_selector,
|
const CredentialSelector& credential_selector,
|
||||||
api::PublicCredentialType public_credential_type,
|
PublicCredentialType public_credential_type,
|
||||||
api::GetPublicCredentialsResultCallback callback) {
|
GetPublicCredentialsResultCallback callback) {
|
||||||
return impl_->GetPublicCredentials(
|
return impl_->GetPublicCredentials(
|
||||||
credential_selector, public_credential_type, std::move(callback));
|
credential_selector, public_credential_type, std::move(callback));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,9 +16,11 @@
|
|||||||
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_CREDENTIAL_STORAGE_IMPL_H_
|
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_CREDENTIAL_STORAGE_IMPL_H_
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "absl/strings/string_view.h"
|
#include "absl/strings/string_view.h"
|
||||||
|
#include "internal/platform/implementation/credential_callbacks.h"
|
||||||
#include "internal/platform/implementation/credential_storage.h"
|
#include "internal/platform/implementation/credential_storage.h"
|
||||||
#include "internal/platform/implementation/platform.h"
|
#include "internal/platform/implementation/platform.h"
|
||||||
|
|
||||||
@@ -39,25 +41,26 @@ class CredentialStorageImpl : public api::CredentialStorage {
|
|||||||
CredentialStorageImpl(CredentialStorageImpl&& other) = default;
|
CredentialStorageImpl(CredentialStorageImpl&& other) = default;
|
||||||
CredentialStorageImpl& operator=(CredentialStorageImpl&& other) = default;
|
CredentialStorageImpl& operator=(CredentialStorageImpl&& other) = default;
|
||||||
|
|
||||||
void SaveCredentials(absl::string_view manager_app_id,
|
void SaveCredentials(
|
||||||
absl::string_view account_name,
|
absl::string_view manager_app_id, absl::string_view account_name,
|
||||||
const std::vector<::nearby::internal::PrivateCredential>&
|
const std::vector<::nearby::internal::PrivateCredential>&
|
||||||
private_credentials,
|
private_credentials,
|
||||||
const std::vector<::nearby::internal::PublicCredential>&
|
const std::vector<::nearby::internal::PublicCredential>&
|
||||||
public_credentials,
|
public_credentials,
|
||||||
api::PublicCredentialType public_credential_type,
|
::nearby::presence::PublicCredentialType public_credential_type,
|
||||||
api::SaveCredentialsResultCallback callback) override;
|
::nearby::presence::GenerateCredentialsCallback callback) override;
|
||||||
|
|
||||||
// Used to fetch private creds when broadcasting.
|
// Used to fetch private creds when broadcasting.
|
||||||
void GetPrivateCredentials(
|
void GetPrivateCredentials(
|
||||||
const api::CredentialSelector& credential_selector,
|
const ::nearby::presence::CredentialSelector& credential_selector,
|
||||||
api::GetPrivateCredentialsResultCallback callback) override;
|
::nearby::presence::GetPrivateCredentialsResultCallback callback)
|
||||||
|
override;
|
||||||
|
|
||||||
// Used to fetch remote public creds when scanning.
|
// Used to fetch remote public creds when scanning.
|
||||||
void GetPublicCredentials(
|
void GetPublicCredentials(
|
||||||
const api::CredentialSelector& credential_selector,
|
const ::nearby::presence::CredentialSelector& credential_selector,
|
||||||
api::PublicCredentialType public_credential_type,
|
::nearby::presence::PublicCredentialType public_credential_type,
|
||||||
api::GetPublicCredentialsResultCallback callback) override;
|
::nearby::presence::GetPublicCredentialsResultCallback callback) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<api::CredentialStorage> impl_;
|
std::unique_ptr<api::CredentialStorage> impl_;
|
||||||
|
|||||||
@@ -15,140 +15,277 @@
|
|||||||
#include "internal/platform/credential_storage_impl.h"
|
#include "internal/platform/credential_storage_impl.h"
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
|
#include "absl/strings/string_view.h"
|
||||||
|
#include "internal/platform/implementation/credential_callbacks.h"
|
||||||
#include "internal/proto/credential.pb.h"
|
#include "internal/proto/credential.pb.h"
|
||||||
|
#include "internal/proto/credential.proto.h"
|
||||||
|
|
||||||
namespace location {
|
namespace location {
|
||||||
namespace nearby {
|
namespace nearby {
|
||||||
|
namespace {
|
||||||
|
|
||||||
using ::nearby::internal::PrivateCredential;
|
using ::nearby::internal::PrivateCredential;
|
||||||
using ::nearby::internal::PublicCredential;
|
using ::nearby::internal::PublicCredential;
|
||||||
|
using ::nearby::presence::CredentialOperationStatus;
|
||||||
|
using ::nearby::presence::CredentialSelector;
|
||||||
|
using ::nearby::presence::GenerateCredentialsCallback;
|
||||||
|
using ::nearby::presence::GetPrivateCredentialsResultCallback;
|
||||||
|
using ::nearby::presence::GetPublicCredentialsResultCallback;
|
||||||
|
using ::nearby::presence::PublicCredentialType;
|
||||||
|
|
||||||
|
std::vector<PrivateCredential> BuildDefaultPrivateCreds() {
|
||||||
|
PrivateCredential private_credential;
|
||||||
|
private_credential.set_secret_id("secret_id");
|
||||||
|
std::vector<PrivateCredential> private_credentials;
|
||||||
|
private_credentials.push_back(private_credential);
|
||||||
|
return private_credentials;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<PublicCredential> BuildDefaultPublicCreds() {
|
||||||
|
PublicCredential public_credential;
|
||||||
|
public_credential.set_secret_id("secret_id");
|
||||||
|
std::vector<PublicCredential> public_credentials;
|
||||||
|
public_credentials.push_back(public_credential);
|
||||||
|
return public_credentials;
|
||||||
|
}
|
||||||
|
|
||||||
|
CredentialSelector BuildDefaultCredentialSelector() {
|
||||||
|
constexpr absl::string_view kAppId = "0192";
|
||||||
|
constexpr absl::string_view kAccountName = "test_account";
|
||||||
|
CredentialSelector credential_selector;
|
||||||
|
credential_selector.manager_app_id = kAppId;
|
||||||
|
credential_selector.account_name = kAccountName;
|
||||||
|
return credential_selector;
|
||||||
|
}
|
||||||
|
|
||||||
TEST(CredentialStorageImplTest, CanSaveAndGetPrivateCredentials) {
|
TEST(CredentialStorageImplTest, CanSaveAndGetPrivateCredentials) {
|
||||||
// Define mock parameters
|
std::vector<PrivateCredential> default_private_creds =
|
||||||
std::string manager_app_id{"0192"};
|
BuildDefaultPrivateCreds();
|
||||||
std::string account_name{"test_account"};
|
std::vector<PublicCredential> default_public_creds =
|
||||||
api::CredentialSelector credential_selector;
|
BuildDefaultPublicCreds();
|
||||||
credential_selector.manager_app_id = manager_app_id;
|
std::vector<PrivateCredential> empty_private_creds;
|
||||||
credential_selector.account_name = account_name;
|
std::vector<PublicCredential> empty_public_creds;
|
||||||
api::PublicCredentialType public_credential_type{
|
bool successfull_save = false;
|
||||||
api::PublicCredentialType::kLocalPublicCredential};
|
GenerateCredentialsCallback generate_creds_callback;
|
||||||
// Create mock credentials
|
generate_creds_callback.credentials_generated_cb =
|
||||||
PrivateCredential private_credential;
|
[&successfull_save, &default_public_creds](
|
||||||
PublicCredential public_credential;
|
const std::vector<PublicCredential>& public_creds) {
|
||||||
private_credential.set_secret_id("secret_id");
|
if (public_creds[0].secret_id() ==
|
||||||
public_credential.set_secret_id("secret_id");
|
default_public_creds[0].secret_id()) {
|
||||||
std::vector<PrivateCredential> private_credentials;
|
|
||||||
std::vector<PublicCredential> public_credentials;
|
|
||||||
private_credentials.push_back(private_credential);
|
|
||||||
public_credentials.push_back(public_credential);
|
|
||||||
// Define SaveCredentialsResultCallback
|
|
||||||
bool successfull_save{false};
|
|
||||||
api::SaveCredentialsResultCallback save_creds_callback;
|
|
||||||
save_creds_callback.credentials_saved_cb =
|
|
||||||
[&successfull_save](api::CredentialOperationStatus status) {
|
|
||||||
if (status == api::CredentialOperationStatus::kSucceeded) {
|
|
||||||
successfull_save = true;
|
successfull_save = true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// Define GetPrivateCredentialsResultCallback
|
std::vector<PrivateCredential> retrieved_creds;
|
||||||
bool get_private_cred_succeeded{false};
|
GetPrivateCredentialsResultCallback get_private_creds_callback;
|
||||||
api::GetPrivateCredentialsResultCallback get_private_creds_callback;
|
|
||||||
get_private_creds_callback.credentials_fetched_cb =
|
get_private_creds_callback.credentials_fetched_cb =
|
||||||
[&private_credentials, &get_private_cred_succeeded](
|
[&retrieved_creds](const std::vector<PrivateCredential>& private_creds) {
|
||||||
const std::vector<PrivateCredential> &private_creds) {
|
retrieved_creds = private_creds;
|
||||||
auto private_cred = private_creds[0];
|
|
||||||
auto private_credential = private_credentials[0];
|
|
||||||
if (private_cred.secret_id() == private_credential.secret_id()) {
|
|
||||||
get_private_cred_succeeded = true;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
bool get_private_cred_failed = false;
|
||||||
get_private_creds_callback.get_credentials_failed_cb =
|
get_private_creds_callback.get_credentials_failed_cb =
|
||||||
[&get_private_cred_succeeded](api::CredentialOperationStatus status) {
|
[&get_private_cred_failed](CredentialOperationStatus status) {
|
||||||
if (status == api::CredentialOperationStatus::kFailed) {
|
if (status == CredentialOperationStatus::kFailed) {
|
||||||
get_private_cred_succeeded = false;
|
get_private_cred_failed = true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// Create CredentialStorageImpl object to test SaveCredentials &
|
// Create CredentialStorageImpl object to test SaveCredentials &
|
||||||
// GetPrivateCredentials
|
// GetPrivateCredentials
|
||||||
|
CredentialSelector cred_selector = BuildDefaultCredentialSelector();
|
||||||
|
PublicCredentialType public_credential_type =
|
||||||
|
PublicCredentialType::kLocalPublicCredential;
|
||||||
CredentialStorageImpl creds_storage;
|
CredentialStorageImpl creds_storage;
|
||||||
creds_storage.GetPrivateCredentials(credential_selector,
|
creds_storage.GetPrivateCredentials(cred_selector,
|
||||||
get_private_creds_callback);
|
get_private_creds_callback);
|
||||||
EXPECT_FALSE(get_private_cred_succeeded);
|
EXPECT_TRUE(get_private_cred_failed);
|
||||||
|
creds_storage.SaveCredentials(cred_selector.manager_app_id,
|
||||||
|
cred_selector.account_name, empty_private_creds,
|
||||||
|
empty_public_creds, public_credential_type,
|
||||||
|
generate_creds_callback);
|
||||||
|
EXPECT_FALSE(successfull_save);
|
||||||
creds_storage.SaveCredentials(
|
creds_storage.SaveCredentials(
|
||||||
manager_app_id, account_name, private_credentials, public_credentials,
|
cred_selector.manager_app_id, cred_selector.account_name,
|
||||||
public_credential_type, std::move(save_creds_callback));
|
default_private_creds, empty_public_creds, public_credential_type,
|
||||||
|
generate_creds_callback);
|
||||||
|
EXPECT_FALSE(successfull_save);
|
||||||
|
creds_storage.SaveCredentials(
|
||||||
|
cred_selector.manager_app_id, cred_selector.account_name,
|
||||||
|
default_private_creds, default_public_creds, public_credential_type,
|
||||||
|
std::move(generate_creds_callback));
|
||||||
EXPECT_TRUE(successfull_save);
|
EXPECT_TRUE(successfull_save);
|
||||||
creds_storage.GetPrivateCredentials(credential_selector,
|
creds_storage.GetPrivateCredentials(cred_selector,
|
||||||
std::move(get_private_creds_callback));
|
std::move(get_private_creds_callback));
|
||||||
EXPECT_TRUE(get_private_cred_succeeded);
|
EXPECT_EQ(retrieved_creds.size(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(CredentialStorageImplTest, CanSaveAndGetPublicCredentials) {
|
TEST(CredentialStorageImplTest, CanSaveAndGetPublicCredentials) {
|
||||||
// Define mock parameters
|
std::vector<PrivateCredential> default_private_creds =
|
||||||
std::string manager_app_id{"0192"};
|
BuildDefaultPrivateCreds();
|
||||||
std::string account_name{"test_account"};
|
std::vector<PublicCredential> default_public_creds =
|
||||||
api::CredentialSelector credential_selector;
|
BuildDefaultPublicCreds();
|
||||||
credential_selector.manager_app_id = manager_app_id;
|
std::vector<PrivateCredential> empty_private_creds;
|
||||||
credential_selector.account_name = account_name;
|
std::vector<PublicCredential> empty_public_creds;
|
||||||
api::PublicCredentialType public_credential_type{
|
bool successfull_save = false;
|
||||||
api::PublicCredentialType::kLocalPublicCredential};
|
GenerateCredentialsCallback generate_creds_callback;
|
||||||
// Create mock credentials
|
generate_creds_callback.credentials_generated_cb =
|
||||||
PrivateCredential private_credential;
|
[&successfull_save, &default_public_creds](
|
||||||
PublicCredential public_credential;
|
const std::vector<PublicCredential>& public_creds) {
|
||||||
private_credential.set_secret_id("secret_id");
|
if (public_creds[0].secret_id() ==
|
||||||
public_credential.set_secret_id("secret_id");
|
default_public_creds[0].secret_id()) {
|
||||||
std::vector<PrivateCredential> private_credentials;
|
|
||||||
std::vector<PublicCredential> public_credentials;
|
|
||||||
private_credentials.push_back(private_credential);
|
|
||||||
public_credentials.push_back(public_credential);
|
|
||||||
// Define SaveCredentialsResultCallback
|
|
||||||
bool successfull_save{false};
|
|
||||||
api::SaveCredentialsResultCallback save_creds_callback;
|
|
||||||
save_creds_callback.credentials_saved_cb =
|
|
||||||
[&successfull_save](api::CredentialOperationStatus status) {
|
|
||||||
if (status == api::CredentialOperationStatus::kSucceeded) {
|
|
||||||
successfull_save = true;
|
successfull_save = true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// Define GetPublicCredentialsResultCallback
|
std::vector<PublicCredential> retrieved_creds;
|
||||||
bool get_public_cred_succeeded{false};
|
GetPublicCredentialsResultCallback get_public_creds_callback;
|
||||||
api::GetPublicCredentialsResultCallback get_public_creds_callback;
|
|
||||||
get_public_creds_callback.credentials_fetched_cb =
|
get_public_creds_callback.credentials_fetched_cb =
|
||||||
[&public_credentials, &get_public_cred_succeeded](
|
[&retrieved_creds](const std::vector<PublicCredential>& public_creds) {
|
||||||
const std::vector<PublicCredential> &public_creds) {
|
retrieved_creds = public_creds;
|
||||||
auto public_cred = public_creds[0];
|
|
||||||
auto public_credential = public_credentials[0];
|
|
||||||
if (public_cred.secret_id() == public_credential.secret_id()) {
|
|
||||||
get_public_cred_succeeded = true;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
bool get_public_cred_failed = false;
|
||||||
get_public_creds_callback.get_credentials_failed_cb =
|
get_public_creds_callback.get_credentials_failed_cb =
|
||||||
[&get_public_cred_succeeded](api::CredentialOperationStatus status) {
|
[&get_public_cred_failed](CredentialOperationStatus status) {
|
||||||
if (status == api::CredentialOperationStatus::kFailed) {
|
if (status == CredentialOperationStatus::kFailed) {
|
||||||
get_public_cred_succeeded = false;
|
get_public_cred_failed = true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// Create CredentialStorageImpl object to test SaveCredentials &
|
// Create CredentialStorageImpl object to test SaveCredentials &
|
||||||
// GetPublicCredentials
|
// GetPublicCredentials
|
||||||
|
CredentialSelector cred_selector = BuildDefaultCredentialSelector();
|
||||||
|
PublicCredentialType public_credential_type =
|
||||||
|
PublicCredentialType::kLocalPublicCredential;
|
||||||
CredentialStorageImpl creds_storage;
|
CredentialStorageImpl creds_storage;
|
||||||
creds_storage.GetPublicCredentials(
|
creds_storage.GetPublicCredentials(cred_selector, public_credential_type,
|
||||||
credential_selector, public_credential_type, get_public_creds_callback);
|
get_public_creds_callback);
|
||||||
EXPECT_FALSE(get_public_cred_succeeded);
|
EXPECT_TRUE(get_public_cred_failed);
|
||||||
|
creds_storage.SaveCredentials(cred_selector.manager_app_id,
|
||||||
|
cred_selector.account_name, empty_private_creds,
|
||||||
|
empty_public_creds, public_credential_type,
|
||||||
|
generate_creds_callback);
|
||||||
|
EXPECT_FALSE(successfull_save);
|
||||||
creds_storage.SaveCredentials(
|
creds_storage.SaveCredentials(
|
||||||
manager_app_id, account_name, private_credentials, public_credentials,
|
cred_selector.manager_app_id, cred_selector.account_name,
|
||||||
public_credential_type, std::move(save_creds_callback));
|
default_private_creds, empty_public_creds, public_credential_type,
|
||||||
|
generate_creds_callback);
|
||||||
|
EXPECT_FALSE(successfull_save);
|
||||||
|
creds_storage.SaveCredentials(
|
||||||
|
cred_selector.manager_app_id, cred_selector.account_name,
|
||||||
|
default_private_creds, default_public_creds, public_credential_type,
|
||||||
|
std::move(generate_creds_callback));
|
||||||
EXPECT_TRUE(successfull_save);
|
EXPECT_TRUE(successfull_save);
|
||||||
creds_storage.GetPublicCredentials(credential_selector,
|
creds_storage.GetPublicCredentials(cred_selector, public_credential_type,
|
||||||
public_credential_type,
|
|
||||||
std::move(get_public_creds_callback));
|
std::move(get_public_creds_callback));
|
||||||
EXPECT_TRUE(get_public_cred_succeeded);
|
EXPECT_EQ(retrieved_creds.size(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(CredentialStorageImplTest, OverwritePrivateCredentials) {
|
||||||
|
std::vector<PrivateCredential> default_priv_creds =
|
||||||
|
BuildDefaultPrivateCreds();
|
||||||
|
std::vector<PublicCredential> default_pub_creds = BuildDefaultPublicCreds();
|
||||||
|
PrivateCredential overwrite_priv_cred;
|
||||||
|
overwrite_priv_cred.set_secret_id("overwrite_secret_id");
|
||||||
|
std::vector<PrivateCredential> overwrite_priv_creds;
|
||||||
|
overwrite_priv_creds.push_back(overwrite_priv_cred);
|
||||||
|
std::vector<PublicCredential> generated_creds;
|
||||||
|
GenerateCredentialsCallback generate_creds_callback;
|
||||||
|
generate_creds_callback.credentials_generated_cb =
|
||||||
|
[&generated_creds](const std::vector<PublicCredential>& public_creds) {
|
||||||
|
generated_creds = public_creds;
|
||||||
|
};
|
||||||
|
std::vector<PrivateCredential> retrieved_creds;
|
||||||
|
GetPrivateCredentialsResultCallback get_private_creds_callback;
|
||||||
|
get_private_creds_callback.credentials_fetched_cb =
|
||||||
|
[&retrieved_creds](const std::vector<PrivateCredential>& private_creds) {
|
||||||
|
retrieved_creds = private_creds;
|
||||||
|
};
|
||||||
|
bool get_private_cred_failed = false;
|
||||||
|
get_private_creds_callback.get_credentials_failed_cb =
|
||||||
|
[&get_private_cred_failed](CredentialOperationStatus status) {
|
||||||
|
if (status == CredentialOperationStatus::kFailed) {
|
||||||
|
get_private_cred_failed = true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// Create CredentialStorageImpl object to test SaveCredentials &
|
||||||
|
// GetPrivateCredentials
|
||||||
|
CredentialSelector cred_selector = BuildDefaultCredentialSelector();
|
||||||
|
PublicCredentialType public_credential_type =
|
||||||
|
PublicCredentialType::kLocalPublicCredential;
|
||||||
|
CredentialStorageImpl creds_storage;
|
||||||
|
creds_storage.SaveCredentials(cred_selector.manager_app_id,
|
||||||
|
cred_selector.account_name, default_priv_creds,
|
||||||
|
default_pub_creds, public_credential_type,
|
||||||
|
generate_creds_callback);
|
||||||
|
EXPECT_EQ(generated_creds[0].secret_id(), default_pub_creds[0].secret_id());
|
||||||
|
creds_storage.GetPrivateCredentials(cred_selector,
|
||||||
|
get_private_creds_callback);
|
||||||
|
EXPECT_EQ(retrieved_creds[0].secret_id(), default_priv_creds[0].secret_id());
|
||||||
|
creds_storage.SaveCredentials(
|
||||||
|
cred_selector.manager_app_id, cred_selector.account_name,
|
||||||
|
overwrite_priv_creds, default_pub_creds, public_credential_type,
|
||||||
|
generate_creds_callback);
|
||||||
|
EXPECT_EQ(generated_creds[0].secret_id(), default_pub_creds[0].secret_id());
|
||||||
|
creds_storage.GetPrivateCredentials(cred_selector,
|
||||||
|
get_private_creds_callback);
|
||||||
|
EXPECT_EQ(retrieved_creds[0].secret_id(),
|
||||||
|
overwrite_priv_creds[0].secret_id());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(CredentialStorageImplTest, OverwritePublicCredentials) {
|
||||||
|
std::vector<PrivateCredential> default_priv_creds =
|
||||||
|
BuildDefaultPrivateCreds();
|
||||||
|
std::vector<PublicCredential> default_pub_creds = BuildDefaultPublicCreds();
|
||||||
|
PublicCredential overwrite_pub_cred;
|
||||||
|
overwrite_pub_cred.set_secret_id("overwrite_secret_id");
|
||||||
|
std::vector<PublicCredential> overwrite_pub_creds;
|
||||||
|
overwrite_pub_creds.push_back(overwrite_pub_cred);
|
||||||
|
std::vector<PublicCredential> generated_creds;
|
||||||
|
GenerateCredentialsCallback generate_creds_callback;
|
||||||
|
generate_creds_callback.credentials_generated_cb =
|
||||||
|
[&generated_creds](const std::vector<PublicCredential>& public_creds) {
|
||||||
|
generated_creds = public_creds;
|
||||||
|
};
|
||||||
|
std::vector<PublicCredential> retrieved_creds;
|
||||||
|
GetPublicCredentialsResultCallback get_public_creds_callback;
|
||||||
|
get_public_creds_callback.credentials_fetched_cb =
|
||||||
|
[&retrieved_creds](const std::vector<PublicCredential>& public_creds) {
|
||||||
|
retrieved_creds = public_creds;
|
||||||
|
};
|
||||||
|
bool get_public_cred_failed = false;
|
||||||
|
get_public_creds_callback.get_credentials_failed_cb =
|
||||||
|
[&get_public_cred_failed](CredentialOperationStatus status) {
|
||||||
|
if (status == CredentialOperationStatus::kFailed) {
|
||||||
|
get_public_cred_failed = true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// Create CredentialStorageImpl object to test SaveCredentials &
|
||||||
|
// GetPublicCredentials
|
||||||
|
CredentialSelector cred_selector = BuildDefaultCredentialSelector();
|
||||||
|
PublicCredentialType public_credential_type =
|
||||||
|
PublicCredentialType::kLocalPublicCredential;
|
||||||
|
CredentialStorageImpl creds_storage;
|
||||||
|
creds_storage.SaveCredentials(cred_selector.manager_app_id,
|
||||||
|
cred_selector.account_name, default_priv_creds,
|
||||||
|
default_pub_creds, public_credential_type,
|
||||||
|
generate_creds_callback);
|
||||||
|
EXPECT_EQ(generated_creds[0].secret_id(), default_pub_creds[0].secret_id());
|
||||||
|
creds_storage.GetPublicCredentials(cred_selector, public_credential_type,
|
||||||
|
get_public_creds_callback);
|
||||||
|
EXPECT_EQ(retrieved_creds[0].secret_id(), default_pub_creds[0].secret_id());
|
||||||
|
creds_storage.SaveCredentials(cred_selector.manager_app_id,
|
||||||
|
cred_selector.account_name, default_priv_creds,
|
||||||
|
overwrite_pub_creds, public_credential_type,
|
||||||
|
generate_creds_callback);
|
||||||
|
EXPECT_EQ(generated_creds[0].secret_id(), overwrite_pub_creds[0].secret_id());
|
||||||
|
creds_storage.GetPublicCredentials(cred_selector, public_credential_type,
|
||||||
|
get_public_creds_callback);
|
||||||
|
EXPECT_EQ(retrieved_creds[0].secret_id(), overwrite_pub_creds[0].secret_id());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
} // namespace nearby
|
} // namespace nearby
|
||||||
} // namespace location
|
} // namespace location
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ cc_library(
|
|||||||
"ble_v2.h",
|
"ble_v2.h",
|
||||||
"bluetooth_adapter.h",
|
"bluetooth_adapter.h",
|
||||||
"bluetooth_classic.h",
|
"bluetooth_classic.h",
|
||||||
|
"credential_callbacks.h",
|
||||||
"credential_storage.h",
|
"credential_storage.h",
|
||||||
"server_sync.h",
|
"server_sync.h",
|
||||||
"wifi.h",
|
"wifi.h",
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
// Copyright 2020 Google LLC
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_CREDENTIAL_CALLBACKS_H_
|
||||||
|
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_CREDENTIAL_CALLBACKS_H_
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "absl/strings/string_view.h"
|
||||||
|
#include "internal/proto/credential.pb.h"
|
||||||
|
|
||||||
|
namespace nearby {
|
||||||
|
namespace presence {
|
||||||
|
|
||||||
|
enum class CredentialOperationStatus {
|
||||||
|
kUnknown = 0,
|
||||||
|
kFailed = 1,
|
||||||
|
kSucceeded = 2,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct CredentialSelector {
|
||||||
|
std::string manager_app_id;
|
||||||
|
std::string account_name;
|
||||||
|
::nearby::internal::IdentityType identity_type;
|
||||||
|
};
|
||||||
|
|
||||||
|
enum PublicCredentialType {
|
||||||
|
kLocalPublicCredential = 1,
|
||||||
|
kRemotePublicCredential = 2,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct GenerateCredentialsCallback {
|
||||||
|
std::function<void(std::vector<nearby::internal::PublicCredential>)>
|
||||||
|
credentials_generated_cb;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct UpdateRemotePublicCredentialsCallback {
|
||||||
|
std::function<void(CredentialOperationStatus)> credentials_updated_cb;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct GetPrivateCredentialsResultCallback {
|
||||||
|
std::function<void(std::vector<::nearby::internal::PrivateCredential>)>
|
||||||
|
credentials_fetched_cb;
|
||||||
|
std::function<void(CredentialOperationStatus)> get_credentials_failed_cb;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct GetPublicCredentialsResultCallback {
|
||||||
|
std::function<void(std::vector<::nearby::internal::PublicCredential>)>
|
||||||
|
credentials_fetched_cb;
|
||||||
|
std::function<void(CredentialOperationStatus)> get_credentials_failed_cb;
|
||||||
|
};
|
||||||
|
} // namespace presence
|
||||||
|
} // namespace nearby
|
||||||
|
|
||||||
|
#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_CREDENTIAL_CALLBACKS_H_
|
||||||
@@ -20,45 +20,13 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "absl/strings/string_view.h"
|
#include "absl/strings/string_view.h"
|
||||||
|
#include "internal/platform/implementation/credential_callbacks.h"
|
||||||
#include "internal/proto/credential.pb.h"
|
#include "internal/proto/credential.pb.h"
|
||||||
|
|
||||||
namespace location {
|
namespace location {
|
||||||
namespace nearby {
|
namespace nearby {
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
enum class CredentialOperationStatus {
|
|
||||||
kUnknown = 0,
|
|
||||||
kFailed = 1,
|
|
||||||
kSucceeded = 2,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct CredentialSelector {
|
|
||||||
std::string manager_app_id;
|
|
||||||
std::string account_name;
|
|
||||||
::nearby::internal::IdentityType identity_type;
|
|
||||||
};
|
|
||||||
|
|
||||||
enum PublicCredentialType {
|
|
||||||
kLocalPublicCredential = 1,
|
|
||||||
kRemotePublicCredential = 2,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct SaveCredentialsResultCallback {
|
|
||||||
std::function<void(CredentialOperationStatus)> credentials_saved_cb;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct GetPrivateCredentialsResultCallback {
|
|
||||||
std::function<void(std::vector<::nearby::internal::PrivateCredential>)>
|
|
||||||
credentials_fetched_cb;
|
|
||||||
std::function<void(CredentialOperationStatus)> get_credentials_failed_cb;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct GetPublicCredentialsResultCallback {
|
|
||||||
std::function<void(std::vector<::nearby::internal::PublicCredential>)>
|
|
||||||
credentials_fetched_cb;
|
|
||||||
std::function<void(CredentialOperationStatus)> get_credentials_failed_cb;
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This class specifies the virtual functions for native platforms to implement.
|
* This class specifies the virtual functions for native platforms to implement.
|
||||||
*/
|
*/
|
||||||
@@ -76,19 +44,19 @@ class CredentialStorage {
|
|||||||
private_credentials,
|
private_credentials,
|
||||||
const std::vector<::nearby::internal::PublicCredential>&
|
const std::vector<::nearby::internal::PublicCredential>&
|
||||||
public_credentials,
|
public_credentials,
|
||||||
PublicCredentialType public_credential_type,
|
::nearby::presence::PublicCredentialType public_credential_type,
|
||||||
SaveCredentialsResultCallback callback) = 0;
|
::nearby::presence::GenerateCredentialsCallback callback) = 0;
|
||||||
|
|
||||||
// Used to fetch private creds when broadcasting.
|
// Used to fetch private creds when broadcasting.
|
||||||
virtual void GetPrivateCredentials(
|
virtual void GetPrivateCredentials(
|
||||||
const CredentialSelector& credential_selector,
|
const ::nearby::presence::CredentialSelector& credential_selector,
|
||||||
GetPrivateCredentialsResultCallback callback) = 0;
|
::nearby::presence::GetPrivateCredentialsResultCallback callback) = 0;
|
||||||
|
|
||||||
// Used to fetch remote public creds when scanning.
|
// Used to fetch remote public creds when scanning.
|
||||||
virtual void GetPublicCredentials(
|
virtual void GetPublicCredentials(
|
||||||
const CredentialSelector& credential_selector,
|
const ::nearby::presence::CredentialSelector& credential_selector,
|
||||||
PublicCredentialType public_credential_type,
|
::nearby::presence::PublicCredentialType public_credential_type,
|
||||||
GetPublicCredentialsResultCallback callback) = 0;
|
::nearby::presence::GetPublicCredentialsResultCallback callback) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace api
|
} // namespace api
|
||||||
|
|||||||
@@ -32,47 +32,71 @@ void CredentialStorageImpl::SaveCredentials(
|
|||||||
absl::string_view manager_app_id, absl::string_view account_name,
|
absl::string_view manager_app_id, absl::string_view account_name,
|
||||||
const std::vector<PrivateCredential>& private_credentials,
|
const std::vector<PrivateCredential>& private_credentials,
|
||||||
const std::vector<PublicCredential>& public_credentials,
|
const std::vector<PublicCredential>& public_credentials,
|
||||||
api::PublicCredentialType public_credential_type,
|
::nearby::presence::PublicCredentialType public_credential_type,
|
||||||
api::SaveCredentialsResultCallback callback) {
|
::nearby::presence::GenerateCredentialsCallback callback) {
|
||||||
NEARBY_LOGS(INFO) << "G3 Save Private Credentials for account: "
|
NEARBY_LOGS(INFO) << "G3 Save Private Credentials for account: "
|
||||||
<< account_name;
|
<< account_name << "], manager app ID:[" << manager_app_id
|
||||||
auto private_key_value = std::make_pair(
|
<< "]";
|
||||||
std::make_pair(manager_app_id, account_name), private_credentials);
|
if (private_credentials.empty()) {
|
||||||
auto private_res = private_credentials_map_.insert(private_key_value);
|
NEARBY_LOGS(INFO) << "There are no Private Credentials for account: "
|
||||||
if (!private_res.second) {
|
<< account_name << "], manager app ID:[" << manager_app_id
|
||||||
NEARBY_LOGS(WARNING)
|
<< "]";
|
||||||
<< "Credentials already saved in map. Overriding previous creds!";
|
return;
|
||||||
private_credentials_map_[std::make_pair(manager_app_id, account_name)] =
|
}
|
||||||
private_credentials;
|
{
|
||||||
|
absl::MutexLock lock(&private_mutex_);
|
||||||
|
auto private_key_value = std::make_pair(
|
||||||
|
std::make_pair(manager_app_id, account_name), private_credentials);
|
||||||
|
auto private_result = private_credentials_map_.insert(private_key_value);
|
||||||
|
if (!private_result.second) {
|
||||||
|
NEARBY_LOGS(WARNING)
|
||||||
|
<< "Credentials already saved in map. Overwriting previous creds!";
|
||||||
|
private_credentials_map_[std::make_pair(manager_app_id, account_name)] =
|
||||||
|
private_credentials;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
NEARBY_LOGS(INFO) << "G3 Save Public Credentials for account: "
|
NEARBY_LOGS(INFO) << "G3 Save Public Credentials for account: "
|
||||||
<< account_name;
|
<< account_name << "], manager app ID:[" << manager_app_id
|
||||||
auto public_key_value = std::make_pair(
|
<< "]";
|
||||||
std::make_tuple(manager_app_id, account_name, public_credential_type),
|
if (public_credentials.empty()) {
|
||||||
public_credentials);
|
NEARBY_LOGS(INFO) << "There are no Public Credentials for account: "
|
||||||
auto public_res = public_credentials_map_.insert(public_key_value);
|
<< account_name << "], manager app ID:[" << manager_app_id
|
||||||
if (!public_res.second) {
|
<< "]";
|
||||||
NEARBY_LOGS(WARNING)
|
return;
|
||||||
<< "Credentials already saved in map. Overriding previous creds!";
|
|
||||||
public_credentials_map_[std::make_tuple(manager_app_id, account_name,
|
|
||||||
public_credential_type)] =
|
|
||||||
public_credentials;
|
|
||||||
}
|
}
|
||||||
callback.credentials_saved_cb(api::CredentialOperationStatus::kSucceeded);
|
{
|
||||||
|
absl::MutexLock lock(&public_mutex_);
|
||||||
|
auto public_key_value = std::make_pair(
|
||||||
|
std::make_tuple(manager_app_id, account_name, public_credential_type),
|
||||||
|
public_credentials);
|
||||||
|
auto public_result = public_credentials_map_.insert(public_key_value);
|
||||||
|
if (!public_result.second) {
|
||||||
|
NEARBY_LOGS(WARNING)
|
||||||
|
<< "Credentials already saved in map. Overwriting previous creds!";
|
||||||
|
public_credentials_map_[std::make_tuple(manager_app_id, account_name,
|
||||||
|
public_credential_type)] =
|
||||||
|
public_credentials;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
callback.credentials_generated_cb(public_credentials);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CredentialStorageImpl::GetPrivateCredentials(
|
void CredentialStorageImpl::GetPrivateCredentials(
|
||||||
const api::CredentialSelector& credential_selector,
|
const ::nearby::presence::CredentialSelector& credential_selector,
|
||||||
api::GetPrivateCredentialsResultCallback callback) {
|
::nearby::presence::GetPrivateCredentialsResultCallback callback) {
|
||||||
NEARBY_LOGS(INFO) << "G3 Get Private Credentials for account: "
|
NEARBY_LOGS(INFO) << "G3 Get Private Credentials for account: "
|
||||||
<< credential_selector.account_name;
|
<< credential_selector.account_name << "], manager app ID:["
|
||||||
|
<< credential_selector.manager_app_id << "]";
|
||||||
|
absl::MutexLock lock(&private_mutex_);
|
||||||
auto key = std::make_pair(credential_selector.manager_app_id,
|
auto key = std::make_pair(credential_selector.manager_app_id,
|
||||||
credential_selector.account_name);
|
credential_selector.account_name);
|
||||||
if (private_credentials_map_.find(key) == private_credentials_map_.end()) {
|
if (private_credentials_map_.find(key) == private_credentials_map_.end()) {
|
||||||
NEARBY_LOGS(WARNING) << "There are no Private Credentials stored for key:"
|
NEARBY_LOGS(WARNING) << "There are no Private Credentials stored for key:"
|
||||||
<< std::get<0>(key) << ", " << std::get<1>(key);
|
<< std::get<0>(key) << ", " << std::get<1>(key);
|
||||||
callback.get_credentials_failed_cb(api::CredentialOperationStatus::kFailed);
|
callback.get_credentials_failed_cb(
|
||||||
|
::nearby::presence::CredentialOperationStatus::kFailed);
|
||||||
} else {
|
} else {
|
||||||
std::vector<PrivateCredential> private_credentials =
|
std::vector<PrivateCredential> private_credentials =
|
||||||
private_credentials_map_[key];
|
private_credentials_map_[key];
|
||||||
@@ -81,11 +105,13 @@ void CredentialStorageImpl::GetPrivateCredentials(
|
|||||||
}
|
}
|
||||||
|
|
||||||
void CredentialStorageImpl::GetPublicCredentials(
|
void CredentialStorageImpl::GetPublicCredentials(
|
||||||
const api::CredentialSelector& credential_selector,
|
const ::nearby::presence::CredentialSelector& credential_selector,
|
||||||
api::PublicCredentialType public_credential_type,
|
::nearby::presence::PublicCredentialType public_credential_type,
|
||||||
api::GetPublicCredentialsResultCallback callback) {
|
::nearby::presence::GetPublicCredentialsResultCallback callback) {
|
||||||
NEARBY_LOGS(INFO) << "G3 Get Public Credentials for account: "
|
NEARBY_LOGS(INFO) << "G3 Get Public Credentials for account: "
|
||||||
<< credential_selector.account_name;
|
<< credential_selector.account_name << "], manager app ID:["
|
||||||
|
<< credential_selector.manager_app_id << "]";
|
||||||
|
absl::MutexLock lock(&public_mutex_);
|
||||||
auto key =
|
auto key =
|
||||||
std::make_tuple(credential_selector.manager_app_id,
|
std::make_tuple(credential_selector.manager_app_id,
|
||||||
credential_selector.account_name, public_credential_type);
|
credential_selector.account_name, public_credential_type);
|
||||||
@@ -93,7 +119,8 @@ void CredentialStorageImpl::GetPublicCredentials(
|
|||||||
NEARBY_LOGS(WARNING) << "There are no Public Credentials stored for key:"
|
NEARBY_LOGS(WARNING) << "There are no Public Credentials stored for key:"
|
||||||
<< std::get<0>(key) << ", " << std::get<1>(key) << ", "
|
<< std::get<0>(key) << ", " << std::get<1>(key) << ", "
|
||||||
<< std::get<2>(key);
|
<< std::get<2>(key);
|
||||||
callback.get_credentials_failed_cb(api::CredentialOperationStatus::kFailed);
|
callback.get_credentials_failed_cb(
|
||||||
|
::nearby::presence::CredentialOperationStatus::kFailed);
|
||||||
} else {
|
} else {
|
||||||
std::vector<PublicCredential> public_credentials =
|
std::vector<PublicCredential> public_credentials =
|
||||||
public_credentials_map_[key];
|
public_credentials_map_[key];
|
||||||
|
|||||||
@@ -22,6 +22,8 @@
|
|||||||
|
|
||||||
#include "absl/container/flat_hash_map.h"
|
#include "absl/container/flat_hash_map.h"
|
||||||
#include "absl/strings/string_view.h"
|
#include "absl/strings/string_view.h"
|
||||||
|
#include "absl/synchronization/mutex.h"
|
||||||
|
#include "internal/platform/implementation/credential_callbacks.h"
|
||||||
#include "internal/platform/implementation/credential_storage.h"
|
#include "internal/platform/implementation/credential_storage.h"
|
||||||
#include "internal/proto/credential.proto.h"
|
#include "internal/proto/credential.proto.h"
|
||||||
|
|
||||||
@@ -40,34 +42,37 @@ class CredentialStorageImpl : public api::CredentialStorage {
|
|||||||
~CredentialStorageImpl() override = default;
|
~CredentialStorageImpl() override = default;
|
||||||
|
|
||||||
// Used to save private and public credentials.
|
// Used to save private and public credentials.
|
||||||
void SaveCredentials(absl::string_view manager_app_id,
|
void SaveCredentials(
|
||||||
absl::string_view account_name,
|
absl::string_view manager_app_id, absl::string_view account_name,
|
||||||
const std::vector<::nearby::internal::PrivateCredential>&
|
const std::vector<::nearby::internal::PrivateCredential>&
|
||||||
private_credentials,
|
private_credentials,
|
||||||
const std::vector<::nearby::internal::PublicCredential>&
|
const std::vector<::nearby::internal::PublicCredential>&
|
||||||
public_credentials,
|
public_credentials,
|
||||||
api::PublicCredentialType public_credential_type,
|
::nearby::presence::PublicCredentialType public_credential_type,
|
||||||
api::SaveCredentialsResultCallback callback) override;
|
::nearby::presence::GenerateCredentialsCallback callback) override;
|
||||||
|
|
||||||
// Used to fetch private creds when broadcasting.
|
// Used to fetch private creds when broadcasting.
|
||||||
void GetPrivateCredentials(
|
void GetPrivateCredentials(
|
||||||
const api::CredentialSelector& credential_selector,
|
const ::nearby::presence::CredentialSelector& credential_selector,
|
||||||
api::GetPrivateCredentialsResultCallback callback) override;
|
::nearby::presence::GetPrivateCredentialsResultCallback callback)
|
||||||
|
override;
|
||||||
|
|
||||||
// Used to fetch remote public creds when scanning.
|
// Used to fetch remote public creds when scanning.
|
||||||
void GetPublicCredentials(
|
void GetPublicCredentials(
|
||||||
const api::CredentialSelector& credential_selector,
|
const ::nearby::presence::CredentialSelector& credential_selector,
|
||||||
api::PublicCredentialType public_credential_type,
|
::nearby::presence::PublicCredentialType public_credential_type,
|
||||||
api::GetPublicCredentialsResultCallback callback) override;
|
::nearby::presence::GetPublicCredentialsResultCallback callback) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
absl::flat_hash_map<std::pair<absl::string_view, absl::string_view>,
|
absl::flat_hash_map<std::pair<absl::string_view, absl::string_view>,
|
||||||
std::vector<::nearby::internal::PrivateCredential>>
|
std::vector<::nearby::internal::PrivateCredential>>
|
||||||
private_credentials_map_;
|
private_credentials_map_;
|
||||||
absl::flat_hash_map<std::tuple<absl::string_view, absl::string_view,
|
absl::flat_hash_map<std::tuple<absl::string_view, absl::string_view,
|
||||||
api::PublicCredentialType>,
|
::nearby::presence::PublicCredentialType>,
|
||||||
std::vector<::nearby::internal::PublicCredential>>
|
std::vector<::nearby::internal::PublicCredential>>
|
||||||
public_credentials_map_;
|
public_credentials_map_;
|
||||||
|
absl::Mutex private_mutex_;
|
||||||
|
absl::Mutex public_mutex_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace g3
|
} // namespace g3
|
||||||
|
|||||||
@@ -21,22 +21,12 @@
|
|||||||
|
|
||||||
#include "absl/status/statusor.h"
|
#include "absl/status/statusor.h"
|
||||||
#include "absl/strings/string_view.h"
|
#include "absl/strings/string_view.h"
|
||||||
#include "internal/platform/implementation/credential_storage.h"
|
#include "internal/platform/implementation/credential_callbacks.h"
|
||||||
#include "internal/proto/credential.pb.h"
|
#include "internal/proto/credential.pb.h"
|
||||||
|
|
||||||
namespace nearby {
|
namespace nearby {
|
||||||
namespace presence {
|
namespace presence {
|
||||||
|
|
||||||
struct GenerateCredentialsCallback {
|
|
||||||
std::function<void(std::vector<nearby::internal::PublicCredential>)>
|
|
||||||
credentials_generated_cb;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct UpdateRemotePublicCredentialsCallback {
|
|
||||||
std::function<void(location::nearby::api::CredentialOperationStatus)>
|
|
||||||
credentials_updated_cb;
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The instance of CredentialManager is owned by {@code ServiceControllerImpl}.
|
* The instance of CredentialManager is owned by {@code ServiceControllerImpl}.
|
||||||
* Helping service controller to manage local credentials and coordinate with
|
* Helping service controller to manage local credentials and coordinate with
|
||||||
@@ -68,13 +58,14 @@ class CredentialManager {
|
|||||||
|
|
||||||
// Used to fetch private creds when broadcasting.
|
// Used to fetch private creds when broadcasting.
|
||||||
virtual void GetPrivateCredentials(
|
virtual void GetPrivateCredentials(
|
||||||
location::nearby::api::CredentialSelector credential_selector,
|
CredentialSelector credential_selector,
|
||||||
location::nearby::api::GetPrivateCredentialsResultCallback callback) = 0;
|
GetPrivateCredentialsResultCallback callback) = 0;
|
||||||
|
|
||||||
// Used to fetch remote public creds when scanning.
|
// Used to fetch remote public creds when scanning.
|
||||||
virtual void GetPublicCredentials(
|
virtual void GetPublicCredentials(
|
||||||
location::nearby::api::CredentialSelector credential_selector,
|
CredentialSelector credential_selector,
|
||||||
location::nearby::api::GetPublicCredentialsResultCallback callback) = 0;
|
PublicCredentialType public_credential_type,
|
||||||
|
GetPublicCredentialsResultCallback callback) = 0;
|
||||||
|
|
||||||
// Decrypts the device metadata from a public credential.
|
// Decrypts the device metadata from a public credential.
|
||||||
// Returns an empty string if decryption fails.
|
// Returns an empty string if decryption fails.
|
||||||
|
|||||||
@@ -32,9 +32,6 @@ namespace presence {
|
|||||||
namespace {
|
namespace {
|
||||||
using ::location::nearby::Base64Utils;
|
using ::location::nearby::Base64Utils;
|
||||||
using ::location::nearby::Crypto;
|
using ::location::nearby::Crypto;
|
||||||
using ::location::nearby::api::CredentialOperationStatus;
|
|
||||||
using ::location::nearby::api::PublicCredentialType;
|
|
||||||
using ::location::nearby::api::SaveCredentialsResultCallback;
|
|
||||||
using ::nearby::internal::DeviceMetadata;
|
using ::nearby::internal::DeviceMetadata;
|
||||||
using ::nearby::internal::IdentityType;
|
using ::nearby::internal::IdentityType;
|
||||||
using ::nearby::internal::PrivateCredential;
|
using ::nearby::internal::PrivateCredential;
|
||||||
@@ -71,25 +68,11 @@ void CredentialManagerImpl::GenerateCredentials(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto save_creds_lambda = [&public_credentials, &credentials_generated_cb](
|
|
||||||
CredentialOperationStatus status) {
|
|
||||||
if (status == CredentialOperationStatus::kSucceeded) {
|
|
||||||
credentials_generated_cb.credentials_generated_cb(public_credentials);
|
|
||||||
} else {
|
|
||||||
NEARBY_LOGS(ERROR) << "Fails to save generated credentials";
|
|
||||||
credentials_generated_cb.credentials_generated_cb(
|
|
||||||
std::vector<PublicCredential>());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
SaveCredentialsResultCallback save_creds_cb;
|
|
||||||
save_creds_cb.credentials_saved_cb = save_creds_lambda;
|
|
||||||
|
|
||||||
// Create credential_storage object and invoke SaveCredentials.
|
// Create credential_storage object and invoke SaveCredentials.
|
||||||
credential_storage_ptr_->SaveCredentials(
|
credential_storage_ptr_->SaveCredentials(
|
||||||
manager_app_id, device_metadata.account_name(), private_credentials,
|
manager_app_id, device_metadata.account_name(), private_credentials,
|
||||||
public_credentials, PublicCredentialType::kLocalPublicCredential,
|
public_credentials, PublicCredentialType::kLocalPublicCredential,
|
||||||
save_creds_cb);
|
std::move(credentials_generated_cb));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<std::unique_ptr<PrivateCredential>, std::unique_ptr<PublicCredential>>
|
std::pair<std::unique_ptr<PrivateCredential>, std::unique_ptr<PublicCredential>>
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
#include "absl/status/statusor.h"
|
#include "absl/status/statusor.h"
|
||||||
#include "absl/strings/string_view.h"
|
#include "absl/strings/string_view.h"
|
||||||
#include "internal/platform/credential_storage_impl.h"
|
#include "internal/platform/credential_storage_impl.h"
|
||||||
|
#include "internal/platform/implementation/credential_callbacks.h"
|
||||||
#include "internal/proto/credential.pb.h"
|
#include "internal/proto/credential.pb.h"
|
||||||
#include "presence/implementation/credential_manager.h"
|
#include "presence/implementation/credential_manager.h"
|
||||||
|
|
||||||
@@ -66,15 +67,14 @@ class CredentialManagerImpl : public CredentialManager {
|
|||||||
UpdateRemotePublicCredentialsCallback credentials_updated_cb) override{};
|
UpdateRemotePublicCredentialsCallback credentials_updated_cb) override{};
|
||||||
|
|
||||||
void GetPrivateCredentials(
|
void GetPrivateCredentials(
|
||||||
location::nearby::api::CredentialSelector credential_selector,
|
CredentialSelector credential_selector,
|
||||||
location::nearby::api::GetPrivateCredentialsResultCallback callback)
|
GetPrivateCredentialsResultCallback callback) override{};
|
||||||
override {}
|
|
||||||
|
|
||||||
// Used to fetch remote public creds when scanning.
|
// Used to fetch remote public creds when scanning.
|
||||||
void GetPublicCredentials(
|
void GetPublicCredentials(
|
||||||
location::nearby::api::CredentialSelector credential_selector,
|
CredentialSelector credential_selector,
|
||||||
location::nearby::api::GetPublicCredentialsResultCallback callback)
|
PublicCredentialType public_credential_type,
|
||||||
override {}
|
GetPublicCredentialsResultCallback callback) override{};
|
||||||
|
|
||||||
std::string DecryptDeviceMetadata(
|
std::string DecryptDeviceMetadata(
|
||||||
std::string device_metadata_encryption_key, std::string authenticity_key,
|
std::string device_metadata_encryption_key, std::string authenticity_key,
|
||||||
|
|||||||
@@ -23,17 +23,16 @@
|
|||||||
#include "protobuf-matchers/protocol-buffer-matchers.h"
|
#include "protobuf-matchers/protocol-buffer-matchers.h"
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
#include "internal/platform/credential_storage_impl.h"
|
#include "internal/platform/credential_storage_impl.h"
|
||||||
#include "internal/platform/implementation/credential_storage.h"
|
|
||||||
#include "internal/platform/implementation/crypto.h"
|
#include "internal/platform/implementation/crypto.h"
|
||||||
#include "internal/proto/credential.pb.h"
|
#include "internal/proto/credential.pb.h"
|
||||||
|
#include "internal/proto/credential.proto.h"
|
||||||
|
|
||||||
namespace nearby {
|
namespace nearby {
|
||||||
namespace presence {
|
namespace presence {
|
||||||
namespace {
|
namespace {
|
||||||
using ::location::nearby::Crypto;
|
using ::location::nearby::Crypto;
|
||||||
using ::location::nearby::api::PublicCredentialType;
|
|
||||||
using ::location::nearby::api::SaveCredentialsResultCallback;
|
|
||||||
using ::nearby::internal::DeviceMetadata;
|
using ::nearby::internal::DeviceMetadata;
|
||||||
|
using ::nearby::internal::IdentityType;
|
||||||
using ::nearby::internal::PrivateCredential;
|
using ::nearby::internal::PrivateCredential;
|
||||||
using ::nearby::internal::PublicCredential;
|
using ::nearby::internal::PublicCredential;
|
||||||
using ::nearby::internal::IdentityType::IDENTITY_TYPE_PRIVATE;
|
using ::nearby::internal::IdentityType::IDENTITY_TYPE_PRIVATE;
|
||||||
@@ -63,7 +62,7 @@ class CredentialManagerImplTest : public ::testing::Test {
|
|||||||
const std::vector<::nearby::internal::PublicCredential>&
|
const std::vector<::nearby::internal::PublicCredential>&
|
||||||
public_credentials,
|
public_credentials,
|
||||||
PublicCredentialType public_credential_type,
|
PublicCredentialType public_credential_type,
|
||||||
SaveCredentialsResultCallback callback),
|
GenerateCredentialsCallback callback),
|
||||||
(override));
|
(override));
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -75,7 +74,6 @@ class CredentialManagerImplTest : public ::testing::Test {
|
|||||||
std::unique_ptr<MockCredentialStorage> mock_credential_storage_ptr_;
|
std::unique_ptr<MockCredentialStorage> mock_credential_storage_ptr_;
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO(b/241926454): Make sure CredentialManager builds with Github.
|
|
||||||
TEST(CredentialManagerImpl, CreateOneCredentialSuccessfully) {
|
TEST(CredentialManagerImpl, CreateOneCredentialSuccessfully) {
|
||||||
DeviceMetadata device_metadata = CreateTestDeviceMetadata();
|
DeviceMetadata device_metadata = CreateTestDeviceMetadata();
|
||||||
|
|
||||||
@@ -141,7 +139,7 @@ TEST(CredentialManagerImpl, GenerateCredentialsSuccessfully) {
|
|||||||
|
|
||||||
credentials_generated_cb.credentials_generated_cb =
|
credentials_generated_cb.credentials_generated_cb =
|
||||||
create_creds_callback_lambda;
|
create_creds_callback_lambda;
|
||||||
std::vector<internal::IdentityType> identityTypes{IDENTITY_TYPE_PRIVATE};
|
std::vector<IdentityType> identityTypes{IDENTITY_TYPE_PRIVATE};
|
||||||
|
|
||||||
credential_manager.GenerateCredentials(
|
credential_manager.GenerateCredentials(
|
||||||
device_metadata,
|
device_metadata,
|
||||||
@@ -171,23 +169,19 @@ TEST(CredentialManagerImpl, GenerateCredentialsSuccessfullyButStoreFailed) {
|
|||||||
const std::vector<PrivateCredential>& private_credentials,
|
const std::vector<PrivateCredential>& private_credentials,
|
||||||
const std::vector<PublicCredential>& public_credentials,
|
const std::vector<PublicCredential>& public_credentials,
|
||||||
PublicCredentialType public_credential_type,
|
PublicCredentialType public_credential_type,
|
||||||
SaveCredentialsResultCallback callback) {
|
GenerateCredentialsCallback callback) {
|
||||||
callback.credentials_saved_cb(
|
// Do nothing! Testing failed SaveCredentials call.
|
||||||
location::nearby::api::CredentialOperationStatus::kFailed);
|
|
||||||
}));
|
}));
|
||||||
CredentialManagerImpl credential_manager(std::move(credential_storage_ptr));
|
CredentialManagerImpl credential_manager(std::move(credential_storage_ptr));
|
||||||
|
|
||||||
GenerateCredentialsCallback credentials_generated_cb;
|
GenerateCredentialsCallback credentials_generated_cb;
|
||||||
std::vector<nearby::internal::PublicCredential> publicCredentials;
|
std::vector<nearby::internal::PublicCredential> publicCredentials;
|
||||||
auto create_creds_callback_lambda =
|
credentials_generated_cb.credentials_generated_cb =
|
||||||
[&publicCredentials](
|
[&publicCredentials](
|
||||||
std::vector<nearby::internal::PublicCredential> credentials) {
|
std::vector<nearby::internal::PublicCredential> credentials) {
|
||||||
publicCredentials = credentials;
|
publicCredentials = credentials;
|
||||||
};
|
};
|
||||||
|
std::vector<IdentityType> identityTypes{IDENTITY_TYPE_PRIVATE};
|
||||||
credentials_generated_cb.credentials_generated_cb =
|
|
||||||
create_creds_callback_lambda;
|
|
||||||
std::vector<internal::IdentityType> identityTypes{IDENTITY_TYPE_PRIVATE};
|
|
||||||
|
|
||||||
credential_manager.GenerateCredentials(
|
credential_manager.GenerateCredentials(
|
||||||
device_metadata,
|
device_metadata,
|
||||||
|
|||||||
Reference in New Issue
Block a user