summaryrefslogtreecommitdiff
path: root/chromium/components/autofill/core/browser/payments/credit_card_cvc_authenticator.cc
blob: 3f22e722569a59931401dde24f406722df95af0c (plain)
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/autofill/core/browser/payments/credit_card_cvc_authenticator.h"

#include <memory>
#include <string>

#include "build/build_config.h"
#include "components/autofill/core/browser/autofill_client.h"
#include "components/autofill/core/browser/data_model/credit_card.h"
#include "components/autofill/core/browser/payments/full_card_request.h"

namespace autofill {

CreditCardCVCAuthenticator::CVCAuthenticationResponse::
    CVCAuthenticationResponse() {}
CreditCardCVCAuthenticator::CVCAuthenticationResponse::
    ~CVCAuthenticationResponse() {}

CreditCardCVCAuthenticator::CreditCardCVCAuthenticator(AutofillClient* client)
    : client_(client) {}

CreditCardCVCAuthenticator::~CreditCardCVCAuthenticator() {}

void CreditCardCVCAuthenticator::Authenticate(
    const CreditCard* card,
    base::WeakPtr<Requester> requester,
    PersonalDataManager* personal_data_manager) {
  requester_ = requester;
  if (!card) {
    return OnFullCardRequestFailed(
        payments::FullCardRequest::FailureType::GENERIC_FAILURE);
  }
  full_card_request_ = std::make_unique<payments::FullCardRequest>(
      client_, client_->GetPaymentsClient(), personal_data_manager);

  absl::optional<GURL> last_committed_url_origin;
  if (card->record_type() == CreditCard::VIRTUAL_CARD &&
      client_->GetLastCommittedURL().is_valid()) {
    last_committed_url_origin =
        client_->GetLastCommittedURL().DeprecatedGetOriginAsURL();
  }

  full_card_request_->GetFullCard(
      *card, AutofillClient::UnmaskCardReason::kAutofill,
      weak_ptr_factory_.GetWeakPtr(), weak_ptr_factory_.GetWeakPtr(),
      last_committed_url_origin);
}

void CreditCardCVCAuthenticator::OnFullCardRequestSucceeded(
    const payments::FullCardRequest& full_card_request,
    const CreditCard& card,
    const std::u16string& cvc) {
  payments::PaymentsClient::UnmaskResponseDetails response =
      full_card_request.unmask_response_details();
  requester_->OnCVCAuthenticationComplete(
      CVCAuthenticationResponse()
          .with_did_succeed(true)
          .with_card(&card)
          .with_cvc(cvc)
          .with_creation_options(std::move(response.fido_creation_options))
          .with_request_options(std::move(response.fido_request_options))
          .with_card_authorization_token(response.card_authorization_token));
}

void CreditCardCVCAuthenticator::OnFullCardRequestFailed(
    payments::FullCardRequest::FailureType failure_type) {
  requester_->OnCVCAuthenticationComplete(
      CVCAuthenticationResponse().with_did_succeed(false));
}

void CreditCardCVCAuthenticator::ShowUnmaskPrompt(
    const CreditCard& card,
    AutofillClient::UnmaskCardReason reason,
    base::WeakPtr<CardUnmaskDelegate> delegate) {
  client_->ShowUnmaskPrompt(card, reason, delegate);
}

void CreditCardCVCAuthenticator::OnUnmaskVerificationResult(
    AutofillClient::PaymentsRpcResult result) {
  client_->OnUnmaskVerificationResult(result);
}

#if defined(OS_ANDROID)
bool CreditCardCVCAuthenticator::ShouldOfferFidoAuth() const {
  return requester_ && requester_->ShouldOfferFidoAuth();
}

bool CreditCardCVCAuthenticator::UserOptedInToFidoFromSettingsPageOnMobile()
    const {
  return requester_ && requester_->UserOptedInToFidoFromSettingsPageOnMobile();
}
#endif

payments::FullCardRequest* CreditCardCVCAuthenticator::GetFullCardRequest() {
  // TODO(crbug.com/951669): iOS and Android clients should use
  // CreditCardAccessManager to retrieve cards from payments instead of calling
  // this function directly.
  if (!full_card_request_) {
    full_card_request_ = std::make_unique<payments::FullCardRequest>(
        client_, client_->GetPaymentsClient(),
        client_->GetPersonalDataManager());
  }
  return full_card_request_.get();
}

base::WeakPtr<payments::FullCardRequest::UIDelegate>
CreditCardCVCAuthenticator::GetAsFullCardRequestUIDelegate() {
  return weak_ptr_factory_.GetWeakPtr();
}

}  // namespace autofill