From 1c455e3488db76d63c657e25d09b4761d3eb63de Mon Sep 17 00:00:00 2001 From: Anay Wadhera Date: Thu, 8 Jun 2023 15:40:22 -0700 Subject: [PATCH] introduce authentication transport function into DeviceProvider PiperOrigin-RevId: 538909999 --- .../v3/connections_device_provider_test.cc | 15 +++++++++ internal/interop/BUILD | 14 ++++++++ internal/interop/authentication_transport.h | 33 +++++++++++++++++++ internal/interop/device_provider.h | 20 +++++++++++ presence/presence_device_provider.h | 7 ++++ 5 files changed, 89 insertions(+) create mode 100644 internal/interop/authentication_transport.h diff --git a/connections/v3/connections_device_provider_test.cc b/connections/v3/connections_device_provider_test.cc index 5915674c..a2050b74 100644 --- a/connections/v3/connections_device_provider_test.cc +++ b/connections/v3/connections_device_provider_test.cc @@ -26,6 +26,11 @@ namespace { constexpr absl::string_view kEndpointId = "ABCD"; constexpr absl::string_view kEndpointInfo = "NC endpoint"; +class MockAuthenticationTransport : public AuthenticationTransport { + MOCK_METHOD(void, WriteMessage, (absl::string_view), (const override)); + MOCK_METHOD(std::string, ReadMessage, (), (const override)); +}; + TEST(ConnectionsDeviceProviderTest, TestProviderWorksTwoArgs) { ConnectionsDeviceProvider provider(kEndpointInfo, {}); auto device = provider.GetLocalDevice(); @@ -46,6 +51,16 @@ TEST(ConnectionsDeviceProviderTest, TestProviderWorksThreeArgs) { EXPECT_EQ(connections_device->GetEndpointId(), kEndpointId); } +TEST(ConnectionsDeviceProviderTest, TestUnknownAuthStatus) { + ConnectionsDeviceProvider provider(kEndpointId, kEndpointInfo, {}); + MockAuthenticationTransport transport; + EXPECT_EQ(provider.AuthenticateConnection(/*local_device=*/nullptr, + /*remote_device=*/nullptr, + AuthenticationRole::kInitiator, + /*shared_secret=*/"", transport), + AuthenticationStatus::kUnknown); +} + } // namespace } // namespace v3 } // namespace connections diff --git a/internal/interop/BUILD b/internal/interop/BUILD index f5423d06..9abb983b 100644 --- a/internal/interop/BUILD +++ b/internal/interop/BUILD @@ -1,3 +1,16 @@ +cc_library( + name = "authentication_transport_interface", + hdrs = [ + "authentication_transport.h", + ], + visibility = [ + "//:__subpackages__", + ], + deps = [ + "@com_google_absl//absl/strings", + ], +) + cc_library( name = "device", hdrs = [ @@ -9,6 +22,7 @@ cc_library( "//presence:__subpackages__", ], deps = [ + ":authentication_transport_interface", "//internal/platform:connection_info", "//internal/platform:types", "@com_google_absl//absl/strings", diff --git a/internal/interop/authentication_transport.h b/internal/interop/authentication_transport.h new file mode 100644 index 00000000..09be8b1c --- /dev/null +++ b/internal/interop/authentication_transport.h @@ -0,0 +1,33 @@ +// 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_INTERNAL_INTEROP_AUTHENTICATION_TRANSPORT_H_ +#define THIRD_PARTY_NEARBY_INTERNAL_INTEROP_AUTHENTICATION_TRANSPORT_H_ + +#include + +#include "absl/strings/string_view.h" + +namespace nearby { + +class AuthenticationTransport { + public: + virtual ~AuthenticationTransport() = default; + virtual void WriteMessage(absl::string_view message) const = 0; + virtual std::string ReadMessage() const = 0; +}; + +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_INTERNAL_INTEROP_AUTHENTICATION_TRANSPORT_H_ diff --git a/internal/interop/device_provider.h b/internal/interop/device_provider.h index f68eb517..bcadd303 100644 --- a/internal/interop/device_provider.h +++ b/internal/interop/device_provider.h @@ -15,10 +15,23 @@ #ifndef THIRD_PARTY_NEARBY_CONNECTIONS_DEVICE_PROVIDER_H_ #define THIRD_PARTY_NEARBY_CONNECTIONS_DEVICE_PROVIDER_H_ +#include "internal/interop/authentication_transport.h" #include "internal/interop/device.h" namespace nearby { +enum class AuthenticationRole { + kUnknown = 0, + kInitiator = 1, + kReceiver = 2, +}; + +enum class AuthenticationStatus { + kUnknown = 0, + kSuccess = 1, + kFailure = 2, +}; + // The base device provider class for use with the Nearby Connections V3 APIs. // This class currently provides a function to get the local device for whatever // client implements it. @@ -27,6 +40,13 @@ class NearbyDeviceProvider { virtual ~NearbyDeviceProvider() = default; const virtual NearbyDevice* GetLocalDevice() = 0; + virtual AuthenticationStatus AuthenticateConnection( + NearbyDevice* local_device, NearbyDevice* remote_device, + AuthenticationRole role, absl::string_view shared_secret, + const AuthenticationTransport& authentication_transport) const { + // We want to check out-of-band by default (Show UKEY2 digits to user). + return AuthenticationStatus::kUnknown; + } }; } // namespace nearby diff --git a/presence/presence_device_provider.h b/presence/presence_device_provider.h index e61a46e7..3558f35f 100644 --- a/presence/presence_device_provider.h +++ b/presence/presence_device_provider.h @@ -28,6 +28,13 @@ class PresenceDeviceProvider : public NearbyDeviceProvider { : device_{metadata} {} const NearbyDevice* GetLocalDevice() override { return &device_; } + AuthenticationStatus AuthenticateConnection( + NearbyDevice* local_device, NearbyDevice* remote_device, + AuthenticationRole role, absl::string_view shared_secret, + const AuthenticationTransport& authentication_transport) const override { + // TODO(b/282027237): Implement. + return AuthenticationStatus::kUnknown; + } void UpdateMetadata(const ::nearby::internal::Metadata& metadata) { device_.SetMetadata(metadata);