Skip to content

Commit

Permalink
Refactor RunPairedKeyVerification.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 684118776
  • Loading branch information
ftsui authored and copybara-github committed Oct 9, 2024
1 parent 81148a8 commit 08399da
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 21 deletions.
19 changes: 2 additions & 17 deletions sharing/nearby_sharing_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2465,21 +2465,14 @@ void NearbySharingServiceImpl::OnOutgoingConnection(
(context_->GetClock()->Now() - connect_start_time)),
/*referrer_package=*/std::nullopt);

std::optional<std::vector<uint8_t>> token =
nearby_connections_manager_->GetRawAuthenticationToken(
session.endpoint_id());
if (!token.has_value()) {
session.Abort(TransferMetadata::Status::kDeviceAuthenticationFailed);
return;
}
session.RunPairedKeyVerification(
ToProtoOsType(device_info_.GetOsType()),
{
.visibility = settings_->GetVisibility(),
.last_visibility = settings_->GetLastVisibility(),
.last_visibility_time = settings_->GetLastVisibilityTimestamp(),
},
GetCertificateManager(), *token,
GetCertificateManager(),
absl::bind_front(
&NearbySharingServiceImpl::OnOutgoingConnectionKeyVerificationDone,
this, share_target_id));
Expand Down Expand Up @@ -2821,22 +2814,14 @@ void NearbySharingServiceImpl::OnIncomingDecryptedCertificate(
connection->SetDisconnectionListener(
[this, share_target_id]() { OnConnectionDisconnected(share_target_id); });

std::optional<std::vector<uint8_t>> token =
nearby_connections_manager_->GetRawAuthenticationToken(
session.endpoint_id());

if (!token.has_value()) {
session.Abort(TransferMetadata::Status::kDeviceAuthenticationFailed);
return;
}
session.RunPairedKeyVerification(
ToProtoOsType(device_info_.GetOsType()),
{
.visibility = settings_->GetVisibility(),
.last_visibility = settings_->GetLastVisibility(),
.last_visibility_time = settings_->GetLastVisibilityTimestamp(),
},
GetCertificateManager(), *token,
GetCertificateManager(),
absl::bind_front(
&NearbySharingServiceImpl::OnIncomingConnectionKeyVerificationDone,
this, share_target_id));
Expand Down
9 changes: 7 additions & 2 deletions sharing/share_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,16 @@ void ShareSession::RunPairedKeyVerification(
OSType os_type,
const PairedKeyVerificationRunner::VisibilityHistory& visibility_history,
NearbyShareCertificateManager* certificate_manager,
const std::vector<uint8_t>& token,
std::function<void(PairedKeyVerificationRunner::PairedKeyVerificationResult,
OSType)>
callback) {
token_ = TokenToFourDigitString(token);
std::optional<std::vector<uint8_t>> token =
connections_manager_.GetRawAuthenticationToken(endpoint_id());
if (!token.has_value()) {
Abort(TransferMetadata::Status::kDeviceAuthenticationFailed);
return;
}
token_ = TokenToFourDigitString(*token);

key_verification_runner_ = std::make_shared<PairedKeyVerificationRunner>(
&clock_, os_type, IsIncoming(), visibility_history, token, connection_,
Expand Down
1 change: 0 additions & 1 deletion sharing/share_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ class ShareSession {
location::nearby::proto::sharing::OSType os_type,
const PairedKeyVerificationRunner::VisibilityHistory& visibility_history,
NearbyShareCertificateManager* certificate_manager,
const std::vector<uint8_t>& token,
std::function<
void(PairedKeyVerificationRunner::PairedKeyVerificationResult,
location::nearby::proto::sharing::OSType)>
Expand Down
3 changes: 2 additions & 1 deletion sharing/share_session_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ TEST(ShareSessionTest, IncomingRunPairedKeyVerificationSuccess) {
ShareTarget share_target;
share_target.is_incoming = true;
TestShareSession session(std::string(kEndpointId), share_target);
session.connections_manager().SetRawAuthenticationToken(kEndpointId, token);
session.SetOnNewConnectionResult(true);
absl::Time connect_start_time = absl::Now();
EXPECT_TRUE(session.OnConnected(connect_start_time, &connection));
Expand All @@ -185,7 +186,7 @@ TEST(ShareSessionTest, IncomingRunPairedKeyVerificationSuccess) {
proto::DeviceVisibility::DEVICE_VISIBILITY_EVERYONE,
.last_visibility_time = absl::Now(),
},
&certificate_manager, token,
&certificate_manager,
[&notification, &verification_result](
PairedKeyVerificationRunner::PairedKeyVerificationResult result,
location::nearby::proto::sharing::OSType) {
Expand Down

0 comments on commit 08399da

Please sign in to comment.