1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/webauthn/change_pin_controller_impl.h"
#include <memory>
#include <string>
#include <utility>
#include "base/check.h"
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/webauthn/authenticator_request_dialog_model.h"
#include "chrome/browser/webauthn/chrome_authenticator_request_delegate.h"
#include "chrome/browser/webauthn/enclave_manager.h"
#include "chrome/browser/webauthn/enclave_manager_factory.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_user_data.h"
using Step = AuthenticatorRequestDialogModel::Step;
ChangePinControllerImpl::ChangePinControllerImpl(
content::RenderFrameHost* render_frame_host)
: content::DocumentUserData<ChangePinControllerImpl>(render_frame_host) {
Profile* profile =
Profile::FromBrowserContext(render_frame_host->GetBrowserContext());
enclave_manager_ =
EnclaveManagerFactory::GetAsEnclaveManagerForProfile(profile);
model_ =
base::MakeRefCounted<AuthenticatorRequestDialogModel>(render_frame_host);
model_observation_.Observe(model_.get());
}
ChangePinControllerImpl::~ChangePinControllerImpl() {
if (!notify_pin_change_callback_.is_null()) {
std::move(notify_pin_change_callback_).Run(false);
}
}
void ChangePinControllerImpl::IsChangePinFlowAvailable(
PinAvailableCallback callback) {
if (enclave_manager_->is_loaded()) {
NotifyPinAvailability(std::move(callback));
return;
}
enclave_manager_->Load(
base::BindOnce(&ChangePinControllerImpl::NotifyPinAvailability,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void ChangePinControllerImpl::StartChangePin(SuccessCallback callback) {
if (notify_pin_change_callback_) {
std::move(callback).Run(false);
return;
}
notify_pin_change_callback_ = std::move(callback);
model_->SetStep(Step::kGPMReauthForPinReset);
RecordHistogram(ChangePinEvent::kFlowStartedFromSettings);
}
void ChangePinControllerImpl::CancelAuthenticatorRequest() {
// User clicked "Cancel" in the GPM dialog.
Reset(/*success=*/false);
RecordHistogram(ChangePinEvent::kNewPinCancelled);
}
void ChangePinControllerImpl::OnReauthComplete(std::string rapt) {
CHECK_EQ(model_->step(), Step::kGPMReauthForPinReset);
rapt_ = std::move(rapt);
model_->SetStep(Step::kGPMChangePin);
RecordHistogram(ChangePinEvent::kReauthCompleted);
}
void ChangePinControllerImpl::OnRecoverSecurityDomainClosed() {
// User closed the reauth window.
Reset(/*success=*/false);
RecordHistogram(ChangePinEvent::kReauthCancelled);
}
void ChangePinControllerImpl::OnGPMPinEntered(const std::u16string& pin) {
CHECK(rapt_.has_value() && (model_->step() == Step::kGPMChangePin ||
model_->step() == Step::kGPMChangeArbitraryPin));
model_->DisableUiOrShowLoadingDialog();
enclave_manager_->ChangePIN(
base::UTF16ToUTF8(pin), std::move(*rapt_),
base::BindOnce(&ChangePinControllerImpl::OnGpmPinChanged,
weak_ptr_factory_.GetWeakPtr()));
rapt_.reset();
RecordHistogram(ChangePinEvent::kNewPinEntered);
}
void ChangePinControllerImpl::OnGPMPinOptionChanged(bool is_arbitrary) {
CHECK(model_->step() == Step::kGPMChangePin ||
model_->step() == Step::kGPMChangeArbitraryPin);
model_->SetStep(is_arbitrary ? Step::kGPMChangeArbitraryPin
: Step::kGPMChangePin);
}
// static
void ChangePinControllerImpl::RecordHistogram(ChangePinEvent event) {
base::UmaHistogramEnumeration("WebAuthentication.Enclave.ChangePinEvents",
event);
}
void ChangePinControllerImpl::Reset(bool success) {
if (!notify_pin_change_callback_.is_null()) {
std::move(notify_pin_change_callback_).Run(success);
}
rapt_.reset();
model_->SetStep(Step::kNotStarted);
}
void ChangePinControllerImpl::OnGpmPinChanged(bool success) {
if (!success) {
model_->SetStep(Step::kGPMError);
RecordHistogram(ChangePinEvent::kFailed);
return;
}
Reset(/*success=*/true);
RecordHistogram(ChangePinEvent::kCompletedSuccessfully);
}
void ChangePinControllerImpl::NotifyPinAvailability(
PinAvailableCallback callback) {
std::move(callback).Run(enclave_manager_->is_registered() &&
enclave_manager_->is_ready() &&
enclave_manager_->has_wrapped_pin());
}
DOCUMENT_USER_DATA_KEY_IMPL(ChangePinControllerImpl);
|