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 137 138 139 140
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/trusted_vault/trusted_vault_throttling_connection_impl.h"
#include <cstddef>
#include <memory>
#include "base/memory/ptr_util.h"
#include "base/time/clock.h"
#include "base/time/default_clock.h"
#include "components/trusted_vault/proto_time_conversion.h"
#include "components/trusted_vault/securebox.h"
namespace trusted_vault {
TrustedVaultThrottlingConnectionImpl::TrustedVaultThrottlingConnectionImpl(
std::unique_ptr<TrustedVaultConnection> delegate,
raw_ptr<StandaloneTrustedVaultStorage> storage)
: TrustedVaultThrottlingConnectionImpl(std::move(delegate),
storage,
base::DefaultClock::GetInstance()) {}
TrustedVaultThrottlingConnectionImpl::TrustedVaultThrottlingConnectionImpl(
std::unique_ptr<TrustedVaultConnection> delegate,
raw_ptr<StandaloneTrustedVaultStorage> storage,
raw_ptr<base::Clock> clock)
: delegate_(std::move(delegate)), storage_(storage), clock_(clock) {
CHECK(delegate_);
CHECK(storage_);
CHECK(clock_);
}
// static
std::unique_ptr<TrustedVaultThrottlingConnectionImpl>
TrustedVaultThrottlingConnectionImpl::CreateForTesting(
std::unique_ptr<TrustedVaultConnection> delegate,
raw_ptr<StandaloneTrustedVaultStorage> storage,
raw_ptr<base::Clock> clock) {
return base::WrapUnique(new TrustedVaultThrottlingConnectionImpl(
std::move(delegate), storage, clock));
}
TrustedVaultThrottlingConnectionImpl::~TrustedVaultThrottlingConnectionImpl() =
default;
bool TrustedVaultThrottlingConnectionImpl::AreRequestsThrottled(
const CoreAccountInfo& account_info) {
auto* per_user_vault = storage_->FindUserVault(account_info.gaia);
CHECK(per_user_vault);
const base::Time current_time = clock_->Now();
base::Time last_failed_request_time = ProtoTimeToTime(
per_user_vault->last_failed_request_millis_since_unix_epoch());
// Fix |last_failed_request_time| if it's set to the future.
if (last_failed_request_time > current_time) {
// Immediately unthrottle, but don't write new state to the file.
last_failed_request_time = base::Time();
}
return last_failed_request_time + kThrottlingDuration > current_time;
}
void TrustedVaultThrottlingConnectionImpl::RecordFailedRequestForThrottling(
const CoreAccountInfo& account_info) {
auto* per_user_vault = storage_->FindUserVault(account_info.gaia);
CHECK(per_user_vault);
per_user_vault->set_last_failed_request_millis_since_unix_epoch(
TimeToProtoTime(clock_->Now()));
storage_->WriteDataToDisk();
}
std::unique_ptr<TrustedVaultConnection::Request>
TrustedVaultThrottlingConnectionImpl::RegisterAuthenticationFactor(
const CoreAccountInfo& account_info,
const MemberKeysSource& member_keys_source,
const SecureBoxPublicKey& authentication_factor_public_key,
AuthenticationFactorTypeAndRegistrationParams
authentication_factor_type_and_registration_params,
RegisterAuthenticationFactorCallback callback) {
return delegate_->RegisterAuthenticationFactor(
account_info, member_keys_source, authentication_factor_public_key,
authentication_factor_type_and_registration_params, std::move(callback));
}
std::unique_ptr<TrustedVaultConnection::Request>
TrustedVaultThrottlingConnectionImpl::RegisterLocalDeviceWithoutKeys(
const CoreAccountInfo& account_info,
const SecureBoxPublicKey& device_public_key,
RegisterAuthenticationFactorCallback callback) {
return delegate_->RegisterLocalDeviceWithoutKeys(
account_info, device_public_key, std::move(callback));
}
std::unique_ptr<TrustedVaultConnection::Request>
TrustedVaultThrottlingConnectionImpl::DownloadNewKeys(
const CoreAccountInfo& account_info,
const TrustedVaultKeyAndVersion& last_trusted_vault_key_and_version,
std::unique_ptr<SecureBoxKeyPair> device_key_pair,
DownloadNewKeysCallback callback) {
return delegate_->DownloadNewKeys(
account_info, last_trusted_vault_key_and_version,
std::move(device_key_pair), std::move(callback));
}
std::unique_ptr<TrustedVaultConnection::Request>
TrustedVaultThrottlingConnectionImpl::DownloadIsRecoverabilityDegraded(
const CoreAccountInfo& account_info,
IsRecoverabilityDegradedCallback callback) {
return delegate_->DownloadIsRecoverabilityDegraded(account_info,
std::move(callback));
}
std::unique_ptr<TrustedVaultConnection::Request>
TrustedVaultThrottlingConnectionImpl::
DownloadAuthenticationFactorsRegistrationState(
const CoreAccountInfo& account_info,
DownloadAuthenticationFactorsRegistrationStateCallback callback,
base::RepeatingClosure keep_alive_callback) {
return delegate_->DownloadAuthenticationFactorsRegistrationState(
account_info, std::move(callback), keep_alive_callback);
}
std::unique_ptr<TrustedVaultConnection::Request>
TrustedVaultThrottlingConnectionImpl::
DownloadAuthenticationFactorsRegistrationState(
const CoreAccountInfo& account_info,
std::set<trusted_vault_pb::SecurityDomainMember_MemberType>
recovery_factor_filter,
DownloadAuthenticationFactorsRegistrationStateCallback callback,
base::RepeatingClosure keep_alive_callback) {
return delegate_->DownloadAuthenticationFactorsRegistrationState(
account_info, recovery_factor_filter, std::move(callback),
keep_alive_callback);
}
} // namespace trusted_vault
|