Commit 51b28b64 authored by Austin Tankiang's avatar Austin Tankiang Committed by Commit Bot

Convert Authenticator to BindOnce/OnceCallback

Bug: 1007662
Change-Id: I31c5de09e2e0e18daf316b519a86cdabdb50be11
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2375005Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Commit-Queue: Austin Tankiang <austinct@chromium.org>
Cr-Commit-Position: refs/heads/master@{#801688}
parent 04c880cb
...@@ -39,10 +39,10 @@ class Authenticator { ...@@ -39,10 +39,10 @@ class Authenticator {
// If the authentication protocol succeeds, then |secure_context| will be // If the authentication protocol succeeds, then |secure_context| will be
// contain the SecureContext used to securely exchange messages. Otherwise, it // contain the SecureContext used to securely exchange messages. Otherwise, it
// will be null if the protocol fails. // will be null if the protocol fails.
typedef base::Callback<void(Result result, typedef base::OnceCallback<
std::unique_ptr<SecureContext> secure_context)> void(Result result, std::unique_ptr<SecureContext> secure_context)>
AuthenticationCallback; AuthenticationCallback;
virtual void Authenticate(const AuthenticationCallback& callback) = 0; virtual void Authenticate(AuthenticationCallback callback) = 0;
}; };
} // namespace secure_channel } // namespace secure_channel
......
...@@ -71,15 +71,15 @@ DeviceToDeviceAuthenticator::~DeviceToDeviceAuthenticator() { ...@@ -71,15 +71,15 @@ DeviceToDeviceAuthenticator::~DeviceToDeviceAuthenticator() {
} }
void DeviceToDeviceAuthenticator::Authenticate( void DeviceToDeviceAuthenticator::Authenticate(
const AuthenticationCallback& callback) { AuthenticationCallback callback) {
if (state_ != State::NOT_STARTED) { if (state_ != State::NOT_STARTED) {
PA_LOG(ERROR) PA_LOG(ERROR)
<< "Authenticator was already used. Do not reuse this instance!"; << "Authenticator was already used. Do not reuse this instance!";
callback.Run(Result::FAILURE, nullptr); std::move(callback).Run(Result::FAILURE, nullptr);
return; return;
} }
callback_ = callback; callback_ = std::move(callback);
if (!connection_->IsConnected()) { if (!connection_->IsConnected()) {
Fail("Not connected to remote device", Result::DISCONNECTED); Fail("Not connected to remote device", Result::DISCONNECTED);
return; return;
...@@ -90,8 +90,8 @@ void DeviceToDeviceAuthenticator::Authenticate( ...@@ -90,8 +90,8 @@ void DeviceToDeviceAuthenticator::Authenticate(
// Generate a key-pair for this individual session. // Generate a key-pair for this individual session.
state_ = State::GENERATING_SESSION_KEYS; state_ = State::GENERATING_SESSION_KEYS;
secure_message_delegate_->GenerateKeyPair( secure_message_delegate_->GenerateKeyPair(
base::Bind(&DeviceToDeviceAuthenticator::OnKeyPairGenerated, base::BindOnce(&DeviceToDeviceAuthenticator::OnKeyPairGenerated,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
} }
void DeviceToDeviceAuthenticator::OnKeyPairGenerated( void DeviceToDeviceAuthenticator::OnKeyPairGenerated(
...@@ -109,8 +109,8 @@ void DeviceToDeviceAuthenticator::OnKeyPairGenerated( ...@@ -109,8 +109,8 @@ void DeviceToDeviceAuthenticator::OnKeyPairGenerated(
helper_->CreateHelloMessage( helper_->CreateHelloMessage(
public_key, connection_->remote_device().persistent_symmetric_key(), public_key, connection_->remote_device().persistent_symmetric_key(),
secure_message_delegate_.get(), secure_message_delegate_.get(),
base::Bind(&DeviceToDeviceAuthenticator::OnHelloMessageCreated, base::BindOnce(&DeviceToDeviceAuthenticator::OnHelloMessageCreated,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
} }
std::unique_ptr<base::OneShotTimer> DeviceToDeviceAuthenticator::CreateTimer() { std::unique_ptr<base::OneShotTimer> DeviceToDeviceAuthenticator::CreateTimer() {
...@@ -163,8 +163,8 @@ void DeviceToDeviceAuthenticator::OnResponderAuthValidated( ...@@ -163,8 +163,8 @@ void DeviceToDeviceAuthenticator::OnResponderAuthValidated(
helper_->CreateInitiatorAuthMessage( helper_->CreateInitiatorAuthMessage(
session_keys_, connection_->remote_device().persistent_symmetric_key(), session_keys_, connection_->remote_device().persistent_symmetric_key(),
responder_auth_message_, secure_message_delegate_.get(), responder_auth_message_, secure_message_delegate_.get(),
base::Bind(&DeviceToDeviceAuthenticator::OnInitiatorAuthCreated, base::BindOnce(&DeviceToDeviceAuthenticator::OnInitiatorAuthCreated,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
} }
void DeviceToDeviceAuthenticator::OnInitiatorAuthCreated( void DeviceToDeviceAuthenticator::OnInitiatorAuthCreated(
...@@ -192,7 +192,7 @@ void DeviceToDeviceAuthenticator::Fail(const std::string& error_message, ...@@ -192,7 +192,7 @@ void DeviceToDeviceAuthenticator::Fail(const std::string& error_message,
weak_ptr_factory_.InvalidateWeakPtrs(); weak_ptr_factory_.InvalidateWeakPtrs();
connection_->RemoveObserver(this); connection_->RemoveObserver(this);
timer_.reset(); timer_.reset();
callback_.Run(result, nullptr); std::move(callback_).Run(result, nullptr);
} }
void DeviceToDeviceAuthenticator::Succeed() { void DeviceToDeviceAuthenticator::Succeed() {
...@@ -203,7 +203,7 @@ void DeviceToDeviceAuthenticator::Succeed() { ...@@ -203,7 +203,7 @@ void DeviceToDeviceAuthenticator::Succeed() {
state_ = State::AUTHENTICATION_SUCCESS; state_ = State::AUTHENTICATION_SUCCESS;
connection_->RemoveObserver(this); connection_->RemoveObserver(this);
callback_.Run( std::move(callback_).Run(
Result::SUCCESS, Result::SUCCESS,
std::make_unique<DeviceToDeviceSecureContext>( std::make_unique<DeviceToDeviceSecureContext>(
std::move(secure_message_delegate_), session_keys_, std::move(secure_message_delegate_), session_keys_,
...@@ -240,8 +240,8 @@ void DeviceToDeviceAuthenticator::OnMessageReceived( ...@@ -240,8 +240,8 @@ void DeviceToDeviceAuthenticator::OnMessageReceived(
connection_->remote_device().persistent_symmetric_key(), connection_->remote_device().persistent_symmetric_key(),
local_session_private_key_, hello_message_, local_session_private_key_, hello_message_,
secure_message_delegate_.get(), secure_message_delegate_.get(),
base::Bind(&DeviceToDeviceAuthenticator::OnResponderAuthValidated, base::BindOnce(&DeviceToDeviceAuthenticator::OnResponderAuthValidated,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
} else { } else {
Fail("Unexpected message received"); Fail("Unexpected message received");
} }
......
...@@ -80,7 +80,7 @@ class DeviceToDeviceAuthenticator : public Authenticator, ...@@ -80,7 +80,7 @@ class DeviceToDeviceAuthenticator : public Authenticator,
~DeviceToDeviceAuthenticator() override; ~DeviceToDeviceAuthenticator() override;
// Authenticator: // Authenticator:
void Authenticate(const AuthenticationCallback& callback) override; void Authenticate(AuthenticationCallback callback) override;
protected: protected:
// Creates a base::OneShotTimer instance. Exposed for testing. // Creates a base::OneShotTimer instance. Exposed for testing.
......
...@@ -163,13 +163,13 @@ class SecureChannelDeviceToDeviceAuthenticatorTest : public testing::Test { ...@@ -163,13 +163,13 @@ class SecureChannelDeviceToDeviceAuthenticatorTest : public testing::Test {
secure_message_delegate_->DeriveKey( secure_message_delegate_->DeriveKey(
remote_session_private_key_, local_session_public_key_, remote_session_private_key_, local_session_public_key_,
base::Bind(&SaveStringResult, &session_symmetric_key_)); base::BindOnce(&SaveStringResult, &session_symmetric_key_));
} }
// Begins authentication, and returns the [Hello] message sent from the local // Begins authentication, and returns the [Hello] message sent from the local
// device to the remote device. // device to the remote device.
std::string BeginAuthentication() { std::string BeginAuthentication() {
authenticator_.Authenticate(base::Bind( authenticator_.Authenticate(base::BindOnce(
&SecureChannelDeviceToDeviceAuthenticatorTest::OnAuthenticationResult, &SecureChannelDeviceToDeviceAuthenticatorTest::OnAuthenticationResult,
base::Unretained(this))); base::Unretained(this)));
...@@ -182,8 +182,8 @@ class SecureChannelDeviceToDeviceAuthenticatorTest : public testing::Test { ...@@ -182,8 +182,8 @@ class SecureChannelDeviceToDeviceAuthenticatorTest : public testing::Test {
DeviceToDeviceResponderOperations::ValidateHelloMessage( DeviceToDeviceResponderOperations::ValidateHelloMessage(
hello_message, remote_device_.persistent_symmetric_key(), hello_message, remote_device_.persistent_symmetric_key(),
secure_message_delegate_, secure_message_delegate_,
base::Bind(&SaveValidateHelloMessageResult, &validated, base::BindOnce(&SaveValidateHelloMessageResult, &validated,
&local_session_public_key)); &local_session_public_key));
EXPECT_TRUE(validated); EXPECT_TRUE(validated);
EXPECT_EQ(local_session_public_key_, local_session_public_key); EXPECT_EQ(local_session_public_key_, local_session_public_key);
...@@ -202,7 +202,7 @@ class SecureChannelDeviceToDeviceAuthenticatorTest : public testing::Test { ...@@ -202,7 +202,7 @@ class SecureChannelDeviceToDeviceAuthenticatorTest : public testing::Test {
hello_message, remote_session_public_key_, remote_session_private_key_, hello_message, remote_session_public_key_, remote_session_private_key_,
remote_device_private_key, remote_device_.persistent_symmetric_key(), remote_device_private_key, remote_device_.persistent_symmetric_key(),
secure_message_delegate_, secure_message_delegate_,
base::Bind(&SaveStringResult, &responder_auth_message)); base::BindOnce(&SaveStringResult, &responder_auth_message));
EXPECT_FALSE(responder_auth_message.empty()); EXPECT_FALSE(responder_auth_message.empty());
WireMessage wire_message(responder_auth_message, WireMessage wire_message(responder_auth_message,
...@@ -264,7 +264,7 @@ TEST_F(SecureChannelDeviceToDeviceAuthenticatorTest, AuthenticateSucceeds) { ...@@ -264,7 +264,7 @@ TEST_F(SecureChannelDeviceToDeviceAuthenticatorTest, AuthenticateSucceeds) {
initiator_auth, SessionKeys(session_symmetric_key_), initiator_auth, SessionKeys(session_symmetric_key_),
remote_device_.persistent_symmetric_key(), responder_auth_message, remote_device_.persistent_symmetric_key(), responder_auth_message,
secure_message_delegate_, secure_message_delegate_,
base::Bind(&SaveBooleanResult, &initiator_auth_validated)); base::BindOnce(&SaveBooleanResult, &initiator_auth_validated));
ASSERT_TRUE(initiator_auth_validated); ASSERT_TRUE(initiator_auth_validated);
} }
...@@ -304,7 +304,7 @@ TEST_F(SecureChannelDeviceToDeviceAuthenticatorTest, NotConnectedInitially) { ...@@ -304,7 +304,7 @@ TEST_F(SecureChannelDeviceToDeviceAuthenticatorTest, NotConnectedInitially) {
connection_.Disconnect(); connection_.Disconnect();
EXPECT_CALL(*this, EXPECT_CALL(*this,
OnAuthenticationResultProxy(Authenticator::Result::DISCONNECTED)); OnAuthenticationResultProxy(Authenticator::Result::DISCONNECTED));
authenticator_.Authenticate(base::Bind( authenticator_.Authenticate(base::BindOnce(
&SecureChannelDeviceToDeviceAuthenticatorTest::OnAuthenticationResult, &SecureChannelDeviceToDeviceAuthenticatorTest::OnAuthenticationResult,
base::Unretained(this))); base::Unretained(this)));
EXPECT_FALSE(secure_context_); EXPECT_FALSE(secure_context_);
...@@ -314,7 +314,7 @@ TEST_F(SecureChannelDeviceToDeviceAuthenticatorTest, FailToSendHello) { ...@@ -314,7 +314,7 @@ TEST_F(SecureChannelDeviceToDeviceAuthenticatorTest, FailToSendHello) {
connection_.set_connection_blocked(true); connection_.set_connection_blocked(true);
EXPECT_CALL(*this, EXPECT_CALL(*this,
OnAuthenticationResultProxy(Authenticator::Result::FAILURE)); OnAuthenticationResultProxy(Authenticator::Result::FAILURE));
authenticator_.Authenticate(base::Bind( authenticator_.Authenticate(base::BindOnce(
&SecureChannelDeviceToDeviceAuthenticatorTest::OnAuthenticationResult, &SecureChannelDeviceToDeviceAuthenticatorTest::OnAuthenticationResult,
base::Unretained(this))); base::Unretained(this)));
EXPECT_FALSE(secure_context_); EXPECT_FALSE(secure_context_);
......
...@@ -15,8 +15,8 @@ FakeAuthenticator::FakeAuthenticator() {} ...@@ -15,8 +15,8 @@ FakeAuthenticator::FakeAuthenticator() {}
FakeAuthenticator::~FakeAuthenticator() {} FakeAuthenticator::~FakeAuthenticator() {}
void FakeAuthenticator::Authenticate( void FakeAuthenticator::Authenticate(
const Authenticator::AuthenticationCallback& callback) { Authenticator::AuthenticationCallback callback) {
last_callback_ = callback; last_callback_ = std::move(callback);
} }
} // namespace secure_channel } // namespace secure_channel
......
...@@ -20,9 +20,9 @@ class FakeAuthenticator : public Authenticator { ...@@ -20,9 +20,9 @@ class FakeAuthenticator : public Authenticator {
~FakeAuthenticator() override; ~FakeAuthenticator() override;
// Authenticator: // Authenticator:
void Authenticate(const AuthenticationCallback& callback) override; void Authenticate(AuthenticationCallback callback) override;
AuthenticationCallback last_callback() { return last_callback_; } AuthenticationCallback last_callback() { return std::move(last_callback_); }
private: private:
AuthenticationCallback last_callback_; AuthenticationCallback last_callback_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment