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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
|
// Copyright 2020 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/payments/content/secure_payment_confirmation_app.h"
#include <cstdint>
#include <optional>
#include <utility>
#include <vector>
#include "base/base64.h"
#include "base/base64url.h"
#include "base/check.h"
#include "base/containers/flat_tree.h"
#include "base/containers/to_vector.h"
#include "base/feature_list.h"
#include "base/json/json_writer.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/payments/content/browser_binding/browser_bound_key.h"
#include "components/payments/content/browser_binding/passkey_browser_binder.h"
#include "components/payments/content/payment_request_spec.h"
#include "components/payments/core/error_strings.h"
#include "components/payments/core/features.h"
#include "components/payments/core/method_strings.h"
#include "components/payments/core/payer_data.h"
#include "components/payments/core/payments_experimental_features.h"
#include "components/webauthn/core/browser/internal_authenticator.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_features.h"
#include "crypto/sha2.h"
#include "device/fido/fido_transport_protocol.h"
#include "device/fido/fido_types.h"
#include "device/fido/public_key_credential_descriptor.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/webauthn/authenticator.mojom.h"
#include "url/url_constants.h"
namespace payments {
namespace {
static constexpr int kDefaultTimeoutMinutes = 3;
// Records UMA metric for the system prompt result.
void RecordSystemPromptResult(
const SecurePaymentConfirmationSystemPromptResult result) {
base::UmaHistogramEnumeration(
"PaymentRequest.SecurePaymentConfirmation.Funnel.SystemPromptResult",
result);
}
#if BUILDFLAG(IS_ANDROID)
const device::PublicKeyCredentialParams::CredentialInfo
kDefaultBrowserBoundKeyCredentialParameters[] = {
{device::CredentialType::kPublicKey,
base::strict_cast<int32_t>(device::CoseAlgorithmIdentifier::kEs256)},
{device::CredentialType::kPublicKey,
base::strict_cast<int32_t>(device::CoseAlgorithmIdentifier::kRs256)}};
#endif // BUILDFLAG(IS_ANDROID)
} // namespace
SecurePaymentConfirmationApp::SecurePaymentConfirmationApp(
content::WebContents* web_contents_to_observe,
const std::string& effective_relying_party_identity,
const std::u16string& payment_instrument_label,
std::unique_ptr<SkBitmap> payment_instrument_icon,
std::vector<uint8_t> credential_id,
std::unique_ptr<PasskeyBrowserBinder> passkey_browser_binder,
const url::Origin& merchant_origin,
base::WeakPtr<PaymentRequestSpec> spec,
mojom::SecurePaymentConfirmationRequestPtr request,
std::unique_ptr<webauthn::InternalAuthenticator> authenticator,
const std::u16string& network_label,
std::unique_ptr<SkBitmap> network_icon,
const std::u16string& issuer_label,
std::unique_ptr<SkBitmap> issuer_icon)
: PaymentApp(/*icon_resource_id=*/0, PaymentApp::Type::INTERNAL),
content::WebContentsObserver(web_contents_to_observe),
authenticator_frame_routing_id_(
authenticator ? authenticator->GetRenderFrameHost()->GetGlobalId()
: content::GlobalRenderFrameHostId()),
effective_relying_party_identity_(effective_relying_party_identity),
payment_instrument_label_(payment_instrument_label),
payment_instrument_icon_(std::move(payment_instrument_icon)),
credential_id_(std::move(credential_id)),
merchant_origin_(merchant_origin),
spec_(spec),
request_(std::move(request)),
authenticator_(std::move(authenticator)),
passkey_browser_binder_(std::move(passkey_browser_binder)),
network_label_(network_label),
network_icon_(std::move(network_icon)),
issuer_label_(issuer_label),
issuer_icon_(std::move(issuer_icon)) {
app_method_names_.insert(methods::kSecurePaymentConfirmation);
}
SecurePaymentConfirmationApp::~SecurePaymentConfirmationApp() = default;
void SecurePaymentConfirmationApp::InvokePaymentApp(
base::WeakPtr<Delegate> delegate) {
if (!authenticator_ || !spec_)
return;
DCHECK(spec_->IsInitialized());
auto options = blink::mojom::PublicKeyCredentialRequestOptions::New();
options->relying_party_id = effective_relying_party_identity_;
options->timeout = request_->timeout.has_value()
? request_->timeout.value()
: base::Minutes(kDefaultTimeoutMinutes);
options->user_verification = device::UserVerificationRequirement::kRequired;
std::vector<device::PublicKeyCredentialDescriptor> credentials;
options->extensions =
!request_->extensions
? blink::mojom::AuthenticationExtensionsClientInputs::New()
: request_->extensions.Clone();
if (base::FeatureList::IsEnabled(
::features::kSecurePaymentConfirmationDebug)) {
options->user_verification =
device::UserVerificationRequirement::kPreferred;
// The `device::PublicKeyCredentialDescriptor` constructor with 2 parameters
// enables authentication through all protocols.
credentials.emplace_back(device::CredentialType::kPublicKey,
credential_id_);
} else {
// Enable authentication only through internal authenticators by default.
credentials.emplace_back(device::CredentialType::kPublicKey, credential_id_,
base::flat_set<device::FidoTransportProtocol>{
device::FidoTransportProtocol::kInternal});
}
options->allow_credentials = std::move(credentials);
options->challenge = request_->challenge;
std::optional<std::vector<uint8_t>> browser_bound_public_key = std::nullopt;
#if BUILDFLAG(IS_ANDROID)
if (passkey_browser_binder_) {
std::vector<device::PublicKeyCredentialParams::CredentialInfo>
credential_parameters = request_->browser_bound_pub_key_cred_params;
if (credential_parameters.empty()) {
credential_parameters =
base::ToVector(kDefaultBrowserBoundKeyCredentialParameters);
}
auto on_get_browser_bound_key_callback = base::BindOnce(
&SecurePaymentConfirmationApp::OnGetBrowserBoundKey,
weak_ptr_factory_.GetWeakPtr(), delegate, std::move(options));
if (web_contents()->GetBrowserContext()->IsOffTheRecord()) {
passkey_browser_binder_->GetBoundKeyForPasskey(
credential_id_, effective_relying_party_identity_,
std::move(on_get_browser_bound_key_callback));
} else {
passkey_browser_binder_->GetOrCreateBoundKeyForPasskey(
credential_id_, effective_relying_party_identity_,
credential_parameters, std::move(on_get_browser_bound_key_callback));
}
} else {
OnGetBrowserBoundKey(delegate, std::move(options),
/*browser_bound_key=*/nullptr);
}
#else // BUILDFLAG(IS_ANDROID))
OnGetBrowserBoundKey(delegate, std::move(options),
/*browser_bound_key=*/nullptr);
#endif // BUILDFLAG(IS_ANDROID))
}
bool SecurePaymentConfirmationApp::IsCompleteForPayment() const {
return true;
}
bool SecurePaymentConfirmationApp::CanPreselect() const {
return true;
}
std::u16string SecurePaymentConfirmationApp::GetMissingInfoLabel() const {
NOTREACHED();
}
bool SecurePaymentConfirmationApp::HasEnrolledInstrument() const {
// If the fallback feature is disabled, the factory should only create this
// app if the authenticator and credentials were available. Therefore, this
// function can always return true with the fallback feature disabled.
return (authenticator_ && !credential_id_.empty()) ||
!PaymentsExperimentalFeatures::IsEnabled(
features::kSecurePaymentConfirmationFallback);
}
bool SecurePaymentConfirmationApp::NeedsInstallation() const {
return false;
}
std::string SecurePaymentConfirmationApp::GetId() const {
if (credential_id_.empty()) {
CHECK(PaymentsExperimentalFeatures::IsEnabled(
features::kSecurePaymentConfirmationFallback));
// Since there is no credential_id_ in the fallback flow, we still must
// return a non-empty app ID.
return "spc";
} else {
return base::Base64Encode(credential_id_);
}
}
std::u16string SecurePaymentConfirmationApp::GetLabel() const {
return payment_instrument_label_;
}
std::u16string SecurePaymentConfirmationApp::GetSublabel() const {
return std::u16string();
}
const SkBitmap* SecurePaymentConfirmationApp::icon_bitmap() const {
return payment_instrument_icon_.get();
}
const SkBitmap* SecurePaymentConfirmationApp::issuer_bitmap() const {
return issuer_icon_.get();
}
const SkBitmap* SecurePaymentConfirmationApp::network_bitmap() const {
return network_icon_.get();
}
bool SecurePaymentConfirmationApp::IsValidForModifier(
const std::string& method) const {
bool is_valid = false;
IsValidForPaymentMethodIdentifier(method, &is_valid);
return is_valid;
}
base::WeakPtr<PaymentApp> SecurePaymentConfirmationApp::AsWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
bool SecurePaymentConfirmationApp::HandlesShippingAddress() const {
return false;
}
bool SecurePaymentConfirmationApp::HandlesPayerName() const {
return false;
}
bool SecurePaymentConfirmationApp::HandlesPayerEmail() const {
return false;
}
bool SecurePaymentConfirmationApp::HandlesPayerPhone() const {
return false;
}
bool SecurePaymentConfirmationApp::IsWaitingForPaymentDetailsUpdate() const {
return false;
}
void SecurePaymentConfirmationApp::UpdateWith(
mojom::PaymentRequestDetailsUpdatePtr details_update) {
NOTREACHED();
}
void SecurePaymentConfirmationApp::OnPaymentDetailsNotUpdated() {
NOTREACHED();
}
void SecurePaymentConfirmationApp::AbortPaymentApp(
base::OnceCallback<void(bool)> abort_callback) {
std::move(abort_callback).Run(/*abort_success=*/false);
}
mojom::PaymentResponsePtr
SecurePaymentConfirmationApp::SetAppSpecificResponseFields(
mojom::PaymentResponsePtr response) const {
blink::mojom::GetAssertionAuthenticatorResponsePtr assertion_response =
blink::mojom::GetAssertionAuthenticatorResponse::New(
response_->info.Clone(), response_->authenticator_attachment,
response_->signature, response_->user_handle,
response_->extensions.Clone());
#if BUILDFLAG(IS_ANDROID)
if (base::FeatureList::IsEnabled(
blink::features::kSecurePaymentConfirmationBrowserBoundKeys) &&
assertion_response->extensions->payment.is_null()) {
assertion_response->extensions->payment =
blink::mojom::AuthenticationExtensionsPaymentResponse::New();
}
if (browser_bound_key_) {
assertion_response->extensions->payment->browser_bound_signature =
browser_bound_key_->Sign(response_->info->client_data_json);
}
#endif // BUILDFLAG(IS_ANDROID)
response->get_assertion_authenticator_response =
std::move(assertion_response);
return response;
}
void SecurePaymentConfirmationApp::RenderFrameDeleted(
content::RenderFrameHost* render_frame_host) {
if (content::RenderFrameHost::FromID(authenticator_frame_routing_id_) ==
render_frame_host) {
// The authenticator requires to be deleted before the render frame.
authenticator_.reset();
}
}
PasskeyBrowserBinder*
SecurePaymentConfirmationApp::GetPasskeyBrowserBinderForTesting() {
return passkey_browser_binder_.get();
}
void SecurePaymentConfirmationApp::OnGetBrowserBoundKey(
base::WeakPtr<Delegate> delegate,
blink::mojom::PublicKeyCredentialRequestOptionsPtr options,
std::unique_ptr<BrowserBoundKey> browser_bound_key) {
browser_bound_key_ = std::move(browser_bound_key);
std::optional<std::vector<uint8_t>> browser_bound_public_key = std::nullopt;
if (browser_bound_key_) {
browser_bound_public_key = browser_bound_key_->GetPublicKeyAsCoseKey();
}
// TODO(crbug.com/40225659): The 'showOptOut' flag status must also be signed
// in the assertion, so that the verifier can check that the caller offered
// the experience if desired.
// TODO(crbug.com/333945861): The network and issuer information must also be
// signed in the assertion, so that the verifier can check that the caller
// passed the correct information.
authenticator_->SetPaymentOptions(blink::mojom::PaymentOptions::New(
spec_->GetTotal(/*selected_app=*/this)->amount.Clone(),
request_->instrument.Clone(), request_->payee_name,
request_->payee_origin, std::move(browser_bound_public_key)));
authenticator_->GetAssertion(
std::move(options),
base::BindOnce(&SecurePaymentConfirmationApp::OnGetAssertion,
weak_ptr_factory_.GetWeakPtr(), delegate));
}
void SecurePaymentConfirmationApp::OnGetAssertion(
base::WeakPtr<Delegate> delegate,
blink::mojom::AuthenticatorStatus status,
blink::mojom::GetAssertionAuthenticatorResponsePtr response,
blink::mojom::WebAuthnDOMExceptionDetailsPtr dom_exception_details) {
if (!delegate)
return;
if (status != blink::mojom::AuthenticatorStatus::SUCCESS || !response) {
delegate->OnInstrumentDetailsError(
errors::kWebAuthnOperationTimedOutOrNotAllowed);
RecordSystemPromptResult(
SecurePaymentConfirmationSystemPromptResult::kCanceled);
return;
}
RecordSystemPromptResult(
SecurePaymentConfirmationSystemPromptResult::kAccepted);
response_ = std::move(response);
delegate->OnInstrumentDetailsReady(methods::kSecurePaymentConfirmation, "{}",
PayerData());
}
} // namespace payments
|