[Nearby Presence] Expose API's to fetch local credentials

Expose APIs to fetch local credentials from the CredentialManager, which will be used to fetch a local credential to be used for authentication in follow up CL's. See go/cros-nearby-presence-np-nc-authentication  for details.

PiperOrigin-RevId: 601781066
This commit is contained in:
Juliet Levesque
2024-01-26 09:04:16 -08:00
committed by Copybara-Service
parent d1a6913017
commit aaa1bea130
10 changed files with 266 additions and 16 deletions
+1 -1
View File
@@ -34,9 +34,9 @@ cc_library(
"//internal/platform:types",
"//internal/proto:metadata_cc_proto",
"//presence/implementation:internal", # build_cleaner: keep
"//presence/implementation/mediums",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/types:variant",
],
)
+36 -2
View File
@@ -1,3 +1,5 @@
load("@bazel_skylib//lib:selects.bzl", "selects")
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -12,7 +14,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load("@bazel_skylib//lib:selects.bzl", "selects")
licenses(["notice"])
@@ -136,6 +137,7 @@ cc_library(
srcs = [
],
hdrs = [
"mock_credential_manager.h",
"mock_service_controller.h",
],
visibility = [
@@ -143,8 +145,9 @@ cc_library(
],
deps = [
":internal",
"//presence",
"//internal/platform/implementation:comm",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/strings:string_view",
"@com_google_googletest//:gtest_main",
],
)
@@ -352,3 +355,34 @@ cc_test(
],
}),
)
cc_test(
name = "service_controller_impl_test",
size = "small",
srcs = ["service_controller_impl_test.cc"],
deps = [
":internal",
":internal_test",
"//internal/platform:comm",
"//internal/platform:test_util",
"//internal/platform:types",
"//internal/platform/implementation:comm",
"//internal/platform/implementation:types",
"//internal/proto:credential_cc_proto",
"//net/proto2/contrib/parse_proto:testing",
"//presence/implementation/mediums",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/time",
"@com_google_googletest//:gtest_main",
] + select({
"@platforms//os:windows": [
"//internal/platform/implementation/windows",
],
"//conditions:default": [
"//internal/platform/implementation/g3",
],
}),
)
+1 -1
View File
@@ -23,7 +23,7 @@ cc_library(
"mediums.h",
],
visibility = [
"//presence/implementation:__subpackages__",
"//presence:__subpackages__",
],
deps = [
"//internal/platform:comm",
@@ -0,0 +1,85 @@
// Copyright 2023 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_PRESENCE_IMPLEMENTATION_MOCK_CREDENTIAL_MANAGER_H_
#define THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_MOCK_CREDENTIAL_MANAGER_H_
#include <string>
#include <vector>
#include "gmock/gmock.h"
#include "absl/strings/string_view.h"
#include "internal/platform/implementation/credential_callbacks.h"
#include "presence/implementation/credential_manager.h"
namespace nearby {
namespace presence {
class MockCredentialManager : public CredentialManager {
public:
MOCK_METHOD(
void, GenerateCredentials,
(const nearby::internal::Metadata& metadata,
absl::string_view manager_app_id,
const std::vector<nearby::internal::IdentityType>& identity_types,
int credential_life_cycle_days, int contiguous_copy_of_credentials,
GenerateCredentialsResultCallback credentials_generated_cb),
(override));
MOCK_METHOD(void, UpdateRemotePublicCredentials,
(absl::string_view manager_app_id, absl::string_view account_name,
const std::vector<nearby::internal::SharedCredential>&
remote_public_creds,
UpdateRemotePublicCredentialsCallback credentials_updated_cb),
(override));
MOCK_METHOD(void, UpdateLocalCredential,
(const CredentialSelector& credential_selector,
nearby::internal::LocalCredential credential,
SaveCredentialsResultCallback result_callback),
(override));
MOCK_METHOD(void, GetLocalCredentials,
(const CredentialSelector& credential_selector,
GetLocalCredentialsResultCallback callback),
(override));
MOCK_METHOD(void, GetPublicCredentials,
(const CredentialSelector& credential_selector,
PublicCredentialType public_credential_type,
GetPublicCredentialsResultCallback callback),
(override));
MOCK_METHOD(SubscriberId, SubscribeForPublicCredentials,
(const CredentialSelector& credential_selector,
PublicCredentialType public_credential_type,
GetPublicCredentialsResultCallback callback),
(override));
MOCK_METHOD(void, UnsubscribeFromPublicCredentials, (SubscriberId id),
(override));
MOCK_METHOD(std::string, DecryptMetadata,
(absl::string_view metadata_encryption_key,
absl::string_view key_seed, absl::string_view metadata_string),
(override));
MOCK_METHOD(
void, SetLocalDeviceMetadata,
(const ::nearby::internal::Metadata& metadata, bool regen_credentials,
absl::string_view manager_app_id,
const std::vector<nearby::internal::IdentityType>& identity_types,
int credential_life_cycle_days, int contiguous_copy_of_credentials,
GenerateCredentialsResultCallback credentials_generated_cb),
(override));
MOCK_METHOD(::nearby::internal::Metadata, GetLocalDeviceMetadata, (),
(override));
};
} // namespace presence
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_MOCK_CREDENTIAL_MANAGER_H_
@@ -19,6 +19,7 @@
#include <vector>
#include "absl/status/statusor.h"
#include "internal/platform/implementation/credential_callbacks.h"
#include "internal/proto/metadata.pb.h"
#include "presence/broadcast_request.h"
#include "presence/data_types.h"
@@ -57,6 +58,9 @@ class ServiceController {
const std::vector<nearby::internal::SharedCredential>&
remote_public_creds,
UpdateRemotePublicCredentialsCallback credentials_updated_cb) = 0;
virtual void GetLocalCredentials(
const CredentialSelector& credential_selector,
GetLocalCredentialsResultCallback callback) = 0;
};
} // namespace presence
@@ -18,7 +18,9 @@
#include <vector>
#include "absl/status/statusor.h"
#include "internal/platform/implementation/credential_callbacks.h"
#include "presence/data_types.h"
#include "presence/implementation/credential_manager.h"
namespace nearby {
namespace presence {
@@ -70,5 +72,12 @@ void ServiceControllerImpl::UpdateRemotePublicCredentials(
std::move(credentials_updated_cb));
}
void ServiceControllerImpl::GetLocalCredentials(
const CredentialSelector& credential_selector,
GetLocalCredentialsResultCallback callback) {
credential_manager_.GetLocalCredentials(credential_selector,
std::move(callback));
}
} // namespace presence
} // namespace nearby
@@ -20,9 +20,10 @@
#include <utility>
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "internal/proto/metadata.pb.h"
#include "presence/implementation/broadcast_manager.h"
#include "presence/implementation/credential_manager_impl.h"
#include "presence/implementation/credential_manager.h"
#include "presence/implementation/mediums/mediums.h"
#include "presence/implementation/scan_manager.h"
#include "presence/implementation/service_controller.h"
@@ -39,6 +40,14 @@ class ServiceControllerImpl : public ServiceController {
public:
using SingleThreadExecutor = ::nearby::SingleThreadExecutor;
ServiceControllerImpl(SingleThreadExecutor* executor,
CredentialManager* credential_manager,
ScanManager* scan_manager,
BroadcastManager* broadcast_manager)
: executor_(*executor),
credential_manager_(*credential_manager),
scan_manager_(*scan_manager),
broadcast_manager_(*broadcast_manager) {}
~ServiceControllerImpl() override { executor_.Shutdown(); }
absl::StatusOr<ScanSessionId> StartScan(ScanRequest scan_request,
@@ -65,24 +74,21 @@ class ServiceControllerImpl : public ServiceController {
const std::vector<nearby::internal::SharedCredential>&
remote_public_creds,
UpdateRemotePublicCredentialsCallback credentials_updated_cb) override;
void GetLocalCredentials(const CredentialSelector& credential_selector,
GetLocalCredentialsResultCallback callback) override;
SingleThreadExecutor& GetBackgroundExecutor() { return executor_; }
// Gives tests access to mediums.
Mediums& GetMediums() { return mediums_; }
private:
SingleThreadExecutor executor_;
void NotifyStartCallbackStatus(BroadcastSessionId id, absl::Status status);
void RunOnServiceControllerThread(absl::string_view name, Runnable runnable) {
executor_.Execute(std::string(name), std::move(runnable));
}
Mediums mediums_; // NOLINT: further impl will use it.
CredentialManagerImpl credential_manager_{
&executor_}; // NOLINT: further impl will use it.
ScanManager scan_manager_{mediums_, credential_manager_,
executor_}; // NOLINT: further impl will use it.
BroadcastManager broadcast_manager_{mediums_, credential_manager_, executor_};
SingleThreadExecutor& executor_;
CredentialManager& credential_manager_;
ScanManager& scan_manager_;
BroadcastManager& broadcast_manager_;
};
} // namespace presence
@@ -0,0 +1,102 @@
// Copyright 2023 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.
#include "presence/implementation/service_controller_impl.h"
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "internal/platform/implementation/credential_callbacks.h"
#include "internal/platform/single_thread_executor.h"
#include "internal/proto/credential.pb.h"
#include "presence/implementation/broadcast_manager.h"
#include "presence/implementation/mediums/mediums.h"
#include "presence/implementation/mock_credential_manager.h"
#include "presence/implementation/scan_manager.h"
namespace nearby {
namespace presence {
namespace {
constexpr absl::string_view kManagerAppId = "TEST_MANAGER_APP";
constexpr absl::string_view kAccountName = "test account";
constexpr absl::string_view kSecretId1 = "1111111";
constexpr absl::string_view kSecretId2 = "2222222";
constexpr absl::string_view kSecretId3 = "3333333";
CredentialSelector BuildDefaultCredentialSelector() {
CredentialSelector credential_selector;
credential_selector.manager_app_id = std::string(kManagerAppId);
credential_selector.account_name = std::string(kAccountName);
credential_selector.identity_type =
::nearby::internal::IdentityType::IDENTITY_TYPE_PRIVATE;
return credential_selector;
}
std::vector<internal::LocalCredential> BuildLocalCredentials() {
internal::LocalCredential local_credential1;
local_credential1.set_secret_id(kSecretId1);
internal::LocalCredential local_credential2;
local_credential2.set_secret_id(kSecretId2);
internal::LocalCredential local_credential3;
local_credential3.set_secret_id(kSecretId3);
return {local_credential1, local_credential2, local_credential3};
}
TEST(ServiceControllerImplTest, GetLocalCredentials) {
auto mock_credential_manager = std::make_unique<MockCredentialManager>();
EXPECT_CALL(*mock_credential_manager.get(), GetLocalCredentials)
.WillOnce([&](const CredentialSelector& credential_selector,
GetLocalCredentialsResultCallback callback) {
callback.credentials_fetched_cb(BuildLocalCredentials());
});
Mediums mediums;
SingleThreadExecutor executor;
ScanManager scan_manager{mediums, *mock_credential_manager, executor};
BroadcastManager broadcast_manager{mediums, *mock_credential_manager,
executor};
auto service_controller = std::make_unique<ServiceControllerImpl>(
&executor, mock_credential_manager.get(), &scan_manager,
&broadcast_manager);
CredentialSelector credential_selector = BuildDefaultCredentialSelector();
absl::StatusOr<std::vector<::nearby::internal::LocalCredential>>
private_credentials;
service_controller->GetLocalCredentials(
credential_selector,
{.credentials_fetched_cb =
[&](absl::StatusOr<std::vector<::nearby::internal::LocalCredential>>
credentials) {
private_credentials = std::move(credentials);
}});
EXPECT_OK(private_credentials);
ASSERT_EQ(3u, private_credentials->size());
ASSERT_EQ(private_credentials->at(0).secret_id(), kSecretId1);
ASSERT_EQ(private_credentials->at(1).secret_id(), kSecretId2);
ASSERT_EQ(private_credentials->at(2).secret_id(), kSecretId3);
}
} // namespace
} // namespace presence
} // namespace nearby
+2 -1
View File
@@ -27,7 +27,8 @@ namespace nearby {
namespace presence {
PresenceServiceImpl::PresenceServiceImpl() {
service_controller_ = std::make_unique<ServiceControllerImpl>();
service_controller_ = std::make_unique<ServiceControllerImpl>(
&executor_, &credential_manager_, &scan_manager_, &broadcast_manager_);
provider_ = std::make_unique<PresenceDeviceProvider>(
service_controller_->GetLocalDeviceMetadata());
}
+9
View File
@@ -22,6 +22,10 @@
#include "internal/platform/borrowable.h"
#include "internal/proto/metadata.pb.h"
#include "presence/data_types.h"
#include "presence/implementation/broadcast_manager.h"
#include "presence/implementation/credential_manager_impl.h"
#include "presence/implementation/mediums/mediums.h"
#include "presence/implementation/scan_manager.h"
#include "presence/implementation/service_controller.h"
#include "presence/presence_client.h"
#include "presence/presence_device_provider.h"
@@ -77,6 +81,11 @@ class PresenceServiceImpl : public PresenceService {
UpdateRemotePublicCredentialsCallback credentials_updated_cb) override;
private:
SingleThreadExecutor executor_;
Mediums mediums_;
CredentialManagerImpl credential_manager_{&executor_};
ScanManager scan_manager_{mediums_, credential_manager_, executor_};
BroadcastManager broadcast_manager_{mediums_, credential_manager_, executor_};
std::unique_ptr<ServiceController> service_controller_;
::nearby::Lender<PresenceService*> lender_{this};
std::unique_ptr<PresenceDeviceProvider> provider_;