Skip to content

Commit

Permalink
webauthn: replace FidoAuthenticator::IsFooAuthenticator with an enum.
Browse files Browse the repository at this point in the history
We've grown multiple virtual calls to ask whether an authenticator is a
specific type. Rather than add another, this change introduces
`GetType`, which subsumes them all.

Change-Id: I2edddd084e2aa39ec2e95065a3f6766c735c0110
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3554482
Auto-Submit: Adam Langley <agl@chromium.org>
Reviewed-by: Martin Kreichgauer <martinkr@google.com>
Commit-Queue: Martin Kreichgauer <martinkr@google.com>
Cr-Commit-Position: refs/heads/main@{#987118}
  • Loading branch information
Adam Langley authored and Chromium LUCI CQ committed Mar 30, 2022
1 parent 81af487 commit 413e555
Show file tree
Hide file tree
Showing 17 changed files with 44 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,8 @@ void AuthenticatorRequestDialogModel::AddAuthenticator(
const device::FidoAuthenticator& authenticator) {
if (!authenticator.AuthenticatorTransport()) {
#if BUILDFLAG(IS_WIN)
DCHECK(authenticator.IsWinNativeApiAuthenticator());
DCHECK_EQ(authenticator.GetType(),
device::FidoAuthenticator::Type::kWinNative);
#endif // BUILDFLAG(IS_WIN)
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ void ChromeAuthenticatorRequestDelegate::ShouldReturnAttestation(
}

#if BUILDFLAG(IS_WIN)
if (authenticator->IsWinNativeApiAuthenticator() &&
if (authenticator->GetType() == device::FidoAuthenticator::Type::kWinNative &&
static_cast<const device::WinWebAuthnApiAuthenticator*>(authenticator)
->ShowsPrivacyNotice()) {
// The OS' native API includes an attestation prompt.
Expand Down
6 changes: 4 additions & 2 deletions content/browser/webauth/authenticator_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,8 @@ void AuthenticatorCommon::OnRegisterResponse(

#if BUILDFLAG(IS_WIN)
GetWebAuthenticationDelegate()->OperationSucceeded(
GetBrowserContext(), authenticator->IsWinNativeApiAuthenticator());
GetBrowserContext(), authenticator->GetType() ==
device::FidoAuthenticator::Type::kWinNative);
#endif

absl::optional<device::FidoTransportProtocol> transport =
Expand Down Expand Up @@ -1533,7 +1534,8 @@ void AuthenticatorCommon::OnSignResponse(

#if BUILDFLAG(IS_WIN)
GetWebAuthenticationDelegate()->OperationSucceeded(
GetBrowserContext(), authenticator->IsWinNativeApiAuthenticator());
GetBrowserContext(),
authenticator->GetType() == device::FidoAuthenticator::Type::kWinNative);
#endif

// Show an account picker for requests with empty allow lists.
Expand Down
8 changes: 4 additions & 4 deletions device/fido/cros/authenticator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ ChromeOSAuthenticator::ChromeOSAuthenticator(

ChromeOSAuthenticator::~ChromeOSAuthenticator() {}

FidoAuthenticator::Type ChromeOSAuthenticator::GetType() const {
return Type::kChromeOS;
}

std::string ChromeOSAuthenticator::GetId() const {
return "ChromeOSAuthenticator";
}
Expand Down Expand Up @@ -421,10 +425,6 @@ bool ChromeOSAuthenticator::RequiresBlePairingPin() const {
return false;
}

bool ChromeOSAuthenticator::IsChromeOSAuthenticator() const {
return true;
}

base::WeakPtr<FidoAuthenticator> ChromeOSAuthenticator::GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
Expand Down
3 changes: 1 addition & 2 deletions device/fido/cros/authenticator.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class COMPONENT_EXPORT(DEVICE_FIDO) ChromeOSAuthenticator
GetAssertionCallback callback) override;
void GetNextAssertion(GetAssertionCallback callback) override {}
void Cancel() override;
Type GetType() const override;
std::string GetId() const override;
const absl::optional<AuthenticatorSupportedOptions>& Options() const override;

Expand All @@ -86,8 +87,6 @@ class COMPONENT_EXPORT(DEVICE_FIDO) ChromeOSAuthenticator
bool IsPaired() const override;
bool RequiresBlePairingPin() const override;

bool IsChromeOSAuthenticator() const override;

void GetTouch(base::OnceClosure callback) override {}
base::WeakPtr<FidoAuthenticator> GetWeakPtr() override;

Expand Down
4 changes: 4 additions & 0 deletions device/fido/fido_authenticator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ void FidoAuthenticator::Reset(ResetCallback callback) {
absl::nullopt);
}

FidoAuthenticator::Type FidoAuthenticator::GetType() const {
return Type::kOther;
}

std::string FidoAuthenticator::GetDisplayName() const {
return GetId();
}
Expand Down
19 changes: 10 additions & 9 deletions device/fido/fido_authenticator.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,16 @@ class COMPONENT_EXPORT(DEVICE_FIDO) FidoAuthenticator {
// stored resident keys and any configured PIN.
virtual void Reset(ResetCallback callback);
virtual void Cancel() = 0;

enum class Type {
kWinNative, // i.e. webauthn.dll
kTouchID, // the Chrome-native Touch ID integration on macOS
kChromeOS, // the platform authenticator on Chrome OS
kOther,
};
// GetType returns the type of the authenticator.
virtual Type GetType() const;

// GetId returns a unique string representing this device. This string should
// be distinct from all other devices concurrently discovered.
virtual std::string GetId() const = 0;
Expand All @@ -286,15 +296,6 @@ class COMPONENT_EXPORT(DEVICE_FIDO) FidoAuthenticator {
virtual bool IsInPairingMode() const = 0;
virtual bool IsPaired() const = 0;
virtual bool RequiresBlePairingPin() const = 0;
#if BUILDFLAG(IS_WIN)
virtual bool IsWinNativeApiAuthenticator() const = 0;
#endif // BUILDFLAG(IS_WIN)
#if BUILDFLAG(IS_MAC)
virtual bool IsTouchIdAuthenticator() const = 0;
#endif // BUILDFLAG(IS_MAC)
#if BUILDFLAG(IS_CHROMEOS_ASH)
virtual bool IsChromeOSAuthenticator() const = 0;
#endif
virtual base::WeakPtr<FidoAuthenticator> GetWeakPtr() = 0;
};

Expand Down
18 changes: 0 additions & 18 deletions device/fido/fido_device_authenticator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1108,24 +1108,6 @@ bool FidoDeviceAuthenticator::RequiresBlePairingPin() const {
return device_->RequiresBlePairingPin();
}

#if BUILDFLAG(IS_WIN)
bool FidoDeviceAuthenticator::IsWinNativeApiAuthenticator() const {
return false;
}
#endif // BUILDFLAG(IS_WIN)

#if BUILDFLAG(IS_MAC)
bool FidoDeviceAuthenticator::IsTouchIdAuthenticator() const {
return false;
}
#endif // BUILDFLAG(IS_MAC)

#if BUILDFLAG(IS_CHROMEOS_ASH)
bool FidoDeviceAuthenticator::IsChromeOSAuthenticator() const {
return false;
}
#endif // BUILDFLAG(IS_CHROMEOS_ASH)

void FidoDeviceAuthenticator::SetTaskForTesting(
std::unique_ptr<FidoTask> task) {
task_ = std::move(task);
Expand Down
9 changes: 0 additions & 9 deletions device/fido/fido_device_authenticator.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,6 @@ class COMPONENT_EXPORT(DEVICE_FIDO) FidoDeviceAuthenticator
bool IsInPairingMode() const override;
bool IsPaired() const override;
bool RequiresBlePairingPin() const override;
#if BUILDFLAG(IS_WIN)
bool IsWinNativeApiAuthenticator() const override;
#endif // BUILDFLAG(IS_WIN)
#if BUILDFLAG(IS_MAC)
bool IsTouchIdAuthenticator() const override;
#endif // BUILDFLAG(IS_MAC)
#if BUILDFLAG(IS_CHROMEOS_ASH)
bool IsChromeOSAuthenticator() const override;
#endif // BUILDFLAG(IS_CHROMEOS_ASH)
base::WeakPtr<FidoAuthenticator> GetWeakPtr() override;

FidoDevice* device() { return device_.get(); }
Expand Down
2 changes: 1 addition & 1 deletion device/fido/fido_request_handler_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ void FidoRequestHandlerBase::AuthenticatorAdded(
}

#if BUILDFLAG(IS_WIN)
if (authenticator->IsWinNativeApiAuthenticator()) {
if (authenticator->GetType() == FidoAuthenticator::Type::kWinNative) {
DCHECK(transport_availability_info_.has_win_native_api_authenticator);
transport_availability_info_.win_native_api_authenticator_id =
authenticator->GetId();
Expand Down
5 changes: 2 additions & 3 deletions device/fido/get_assertion_handler_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -852,9 +852,8 @@ TEST(GetAssertionRequestHandlerWinTest, TestWinUsbDiscovery) {

ASSERT_FALSE(cb.was_called());
EXPECT_EQ(handler->AuthenticatorsForTesting().size(), 1u);
EXPECT_EQ(handler->AuthenticatorsForTesting()
.begin()
->second->IsWinNativeApiAuthenticator(),
EXPECT_EQ(handler->AuthenticatorsForTesting().begin()->second->GetType() ==
FidoAuthenticator::Type::kWinNative,
enable_api);
}
}
Expand Down
4 changes: 2 additions & 2 deletions device/fido/get_assertion_request_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ void GetAssertionRequestHandler::GetPlatformCredentialStatus(

#if BUILDFLAG(IS_MAC)
// In tests the platform authenticator may be a virtual device.
if (!platform_authenticator->IsTouchIdAuthenticator()) {
if (platform_authenticator->GetType() != FidoAuthenticator::Type::kTouchID) {
FidoRequestHandlerBase::GetPlatformCredentialStatus(platform_authenticator);
return;
}
Expand Down Expand Up @@ -573,7 +573,7 @@ void GetAssertionRequestHandler::HandleResponse(
}

#if BUILDFLAG(IS_WIN)
if (authenticator->IsWinNativeApiAuthenticator()) {
if (authenticator->GetType() == FidoAuthenticator::Type::kWinNative) {
state_ = State::kFinished;
CancelActiveAuthenticators(authenticator->GetId());
if (status != CtapDeviceResponseCode::kSuccess) {
Expand Down
2 changes: 1 addition & 1 deletion device/fido/mac/authenticator.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ class COMPONENT_EXPORT(DEVICE_FIDO) TouchIdAuthenticator
GetAssertionCallback callback) override;
void GetNextAssertion(GetAssertionCallback callback) override;
void Cancel() override;
Type GetType() const override;
std::string GetId() const override;
const absl::optional<AuthenticatorSupportedOptions>& Options() const override;
absl::optional<FidoTransportProtocol> AuthenticatorTransport() const override;
bool IsInPairingMode() const override;
bool IsPaired() const override;
bool RequiresBlePairingPin() const override;
bool IsTouchIdAuthenticator() const override;
void GetTouch(base::OnceClosure callback) override;
base::WeakPtr<FidoAuthenticator> GetWeakPtr() override;

Expand Down
8 changes: 4 additions & 4 deletions device/fido/mac/authenticator.mm
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ new TouchIdAuthenticator(std::move(config.keychain_access_group),
operation_.reset();
}

FidoAuthenticator::Type TouchIdAuthenticator::GetType() const {
return Type::kTouchID;
}

std::string TouchIdAuthenticator::GetId() const {
return "TouchIdAuthenticator";
}
Expand Down Expand Up @@ -199,10 +203,6 @@ AuthenticatorSupportedOptions TouchIdAuthenticatorOptions() {
return false;
}

bool TouchIdAuthenticator::IsTouchIdAuthenticator() const {
return true;
}

void TouchIdAuthenticator::GetTouch(base::OnceClosure callback) {
NOTREACHED();
std::move(callback).Run();
Expand Down
6 changes: 3 additions & 3 deletions device/fido/make_credential_request_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ void MakeCredentialRequestHandler::DispatchRequest(
// If the Windows API cannot handle a request, just reject the request
// outright. There are no other authenticators to attempt, so calling
// GetTouch() would not make sense.
if (authenticator->IsWinNativeApiAuthenticator()) {
if (authenticator->GetType() == FidoAuthenticator::Type::kWinNative) {
HandleInapplicableAuthenticator(authenticator, post_touch_status);
return;
}
Expand Down Expand Up @@ -721,7 +721,7 @@ void MakeCredentialRequestHandler::HandleResponse(
}

#if BUILDFLAG(IS_WIN)
if (authenticator->IsWinNativeApiAuthenticator()) {
if (authenticator->GetType() == FidoAuthenticator::Type::kWinNative) {
state_ = State::kFinished;
if (status != CtapDeviceResponseCode::kSuccess) {
std::move(completion_callback_)
Expand Down Expand Up @@ -961,7 +961,7 @@ void MakeCredentialRequestHandler::SpecializeRequestForAuthenticator(
request->resident_key_required =
#if BUILDFLAG(IS_WIN)
// Windows does not yet support rk=preferred.
!authenticator->IsWinNativeApiAuthenticator() &&
authenticator->GetType() != FidoAuthenticator::Type::kWinNative &&
#endif
auth_options && auth_options->supports_resident_key &&
!authenticator->DiscoverableCredentialStorageFull() &&
Expand Down
8 changes: 4 additions & 4 deletions device/fido/win/authenticator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ void WinWebAuthnApiAuthenticator::Cancel() {
win_api_->CancelCurrentOperation(&cancellation_id_);
}

FidoAuthenticator::Type WinWebAuthnApiAuthenticator::GetType() const {
return Type::kWinNative;
}

std::string WinWebAuthnApiAuthenticator::GetId() const {
return "WinWebAuthnApiAuthenticator";
}
Expand All @@ -190,10 +194,6 @@ WinWebAuthnApiAuthenticator::AuthenticatorTransport() const {
return absl::nullopt;
}

bool WinWebAuthnApiAuthenticator::IsWinNativeApiAuthenticator() const {
return true;
}

bool WinWebAuthnApiAuthenticator::SupportsCredProtectExtension() const {
return win_api_->Version() >= WEBAUTHN_API_VERSION_2;
}
Expand Down
2 changes: 1 addition & 1 deletion device/fido/win/authenticator.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class COMPONENT_EXPORT(DEVICE_FIDO) WinWebAuthnApiAuthenticator
GetAssertionCallback callback) override;
void GetTouch(base::OnceClosure callback) override;
void Cancel() override;
Type GetType() const override;
std::string GetId() const override;
bool IsInPairingMode() const override;
bool IsPaired() const override;
Expand All @@ -76,7 +77,6 @@ class COMPONENT_EXPORT(DEVICE_FIDO) WinWebAuthnApiAuthenticator
bool SupportsCredBlobOfSize(size_t num_bytes) const override;
const absl::optional<AuthenticatorSupportedOptions>& Options() const override;
absl::optional<FidoTransportProtocol> AuthenticatorTransport() const override;
bool IsWinNativeApiAuthenticator() const override;
base::WeakPtr<FidoAuthenticator> GetWeakPtr() override;

void MakeCredentialDone(
Expand Down

0 comments on commit 413e555

Please sign in to comment.