Commit af1a7d31 authored by Manas Verma's avatar Manas Verma Committed by Commit Bot

[Autofill Auth] Updating PaymentsClient::OptChange to match protos.

Bug: 949269
Change-Id: Ie3365d3b25b7c488c46541eb2119afb05352b3df
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1833043Reviewed-by: default avatarJared Saul <jsaul@google.com>
Commit-Queue: Manas Verma <manasverma@google.com>
Cr-Commit-Position: refs/heads/master@{#702617}
parent cd847ff1
......@@ -99,7 +99,7 @@ void CreditCardFIDOAuthenticator::Register(std::string card_authorization_token,
}
} else {
current_flow_ = OPT_IN_FETCH_CHALLENGE_FLOW;
OptChange(/*opt_in=*/true);
OptChange();
}
}
......@@ -120,7 +120,7 @@ void CreditCardFIDOAuthenticator::Authorize(
void CreditCardFIDOAuthenticator::OptOut() {
current_flow_ = OPT_OUT_FLOW;
card_authorization_token_ = std::string();
OptChange(/*opt_in=*/false);
OptChange();
}
void CreditCardFIDOAuthenticator::IsUserVerifiable(
......@@ -235,12 +235,29 @@ void CreditCardFIDOAuthenticator::MakeCredential(
}
void CreditCardFIDOAuthenticator::OptChange(
bool opt_in,
base::Value authenticator_response) {
payments::PaymentsClient::OptChangeRequestDetails request_details;
request_details.app_locale =
autofill_client_->GetPersonalDataManager()->app_locale();
request_details.opt_in = opt_in;
switch (current_flow_) {
case OPT_IN_WITH_CHALLENGE_FLOW:
case OPT_IN_FETCH_CHALLENGE_FLOW:
request_details.reason =
payments::PaymentsClient::OptChangeRequestDetails::ENABLE_FIDO_AUTH;
break;
case OPT_OUT_FLOW:
request_details.reason =
payments::PaymentsClient::OptChangeRequestDetails::DISABLE_FIDO_AUTH;
break;
case FOLLOWUP_AFTER_CVC_AUTH_FLOW:
request_details.reason = payments::PaymentsClient::
OptChangeRequestDetails::ADD_CARD_FOR_FIDO_AUTH;
break;
default:
NOTREACHED();
break;
}
// If |authenticator_response| is set, that means the user just signed a
// challenge. In which case, if |card_authorization_token_| is not empty, then
......@@ -300,7 +317,7 @@ void CreditCardFIDOAuthenticator::OnDidGetAssertion(
base::Value response = base::Value(base::Value::Type::DICTIONARY);
response.SetKey("fido_assertion_info",
ParseAssertionResponse(std::move(assertion_response)));
OptChange(/*opt_in=*/true, std::move(response));
OptChange(std::move(response));
}
}
......@@ -321,8 +338,7 @@ void CreditCardFIDOAuthenticator::OnDidMakeCredential(
return;
}
OptChange(/*opt_in=*/true,
ParseAttestationResponse(std::move(attestation_response)));
OptChange(ParseAttestationResponse(std::move(attestation_response)));
}
void CreditCardFIDOAuthenticator::OnDidGetOptChangeResult(
......
......@@ -142,8 +142,7 @@ class CreditCardFIDOAuthenticator
PublicKeyCredentialCreationOptionsPtr creation_options);
// Makes a request to payments to either opt-in or opt-out the user.
void OptChange(bool opt_in,
base::Value authenticator_response = base::Value());
void OptChange(base::Value authenticator_response = base::Value());
// The callback invoked from the WebAuthn prompt including the
// |assertion_response|, which will be sent to Google Payments to retrieve
......
......@@ -58,7 +58,7 @@ const char kUnmaskCardRequestFormat[] =
"&s7e_13_cvc=%s";
const char kOptChangeRequestPath[] =
"payments/apis/chromepaymentsservice/autofillauthoptchange";
"payments/apis/chromepaymentsservice/updateautofilluserpreference";
const char kGetUploadDetailsRequestPath[] =
"payments/apis/chromepaymentsservice/getdetailsforsavecard";
......@@ -489,38 +489,63 @@ class OptChangeRequest : public PaymentsRequest {
std::move(chrome_user_context));
}
request_dict.SetKey("opt_in", base::Value(request_details_.opt_in));
std::string reason;
switch (request_details_.reason) {
case PaymentsClient::OptChangeRequestDetails::ENABLE_FIDO_AUTH:
reason = "ENABLE_FIDO_AUTH";
break;
case PaymentsClient::OptChangeRequestDetails::DISABLE_FIDO_AUTH:
reason = "DISABLE_FIDO_AUTH";
break;
case PaymentsClient::OptChangeRequestDetails::ADD_CARD_FOR_FIDO_AUTH:
reason = "ADD_CARD_FOR_FIDO_AUTH";
break;
default:
NOTREACHED();
break;
}
request_dict.SetKey("reason", base::Value(reason));
base::Value fido_authentication_info(base::Value::Type::DICTIONARY);
if (request_details_.fido_authenticator_response.is_dict()) {
request_dict.SetKey(
fido_authentication_info.SetKey(
"fido_authenticator_response",
std::move(request_details_.fido_authenticator_response));
}
if (!request_details_.card_authorization_token.empty()) {
request_dict.SetKey(
fido_authentication_info.SetKey(
"card_authorization_token",
base::Value(request_details_.card_authorization_token));
}
request_dict.SetKey("fido_authentication_info",
std::move(fido_authentication_info));
std::string request_content;
base::JSONWriter::Write(request_dict, &request_content);
VLOG(3) << "autofillauthoptchange request body: " << request_content;
VLOG(3) << "updateautofilluserpreference request body: " << request_content;
return request_content;
}
void ParseResponse(const base::Value& response) override {
const auto* user_is_opted_in =
response.FindKeyOfType("user_is_opted_in", base::Value::Type::BOOLEAN);
if (user_is_opted_in)
response_details_.user_is_opted_in = user_is_opted_in->GetBool();
const auto* fido_authentication_info = response.FindKeyOfType(
"fido_authentication_info", base::Value::Type::DICTIONARY);
if (!fido_authentication_info)
return;
const auto* user_status =
fido_authentication_info->FindStringKey("user_status");
if (user_status && *user_status != "UNKNOWN_USER_STATUS")
response_details_.user_is_opted_in =
(*user_status == "FIDO_AUTH_ENABLED");
const auto* fido_creation_options = response.FindKeyOfType(
const auto* fido_creation_options = fido_authentication_info->FindKeyOfType(
"fido_creation_options", base::Value::Type::DICTIONARY);
if (fido_creation_options)
response_details_.fido_creation_options = fido_creation_options->Clone();
const auto* fido_request_options = response.FindKeyOfType(
const auto* fido_request_options = fido_authentication_info->FindKeyOfType(
"fido_request_options", base::Value::Type::DICTIONARY);
if (fido_request_options)
response_details_.fido_request_options = fido_request_options->Clone();
......@@ -1010,7 +1035,7 @@ PaymentsClient::OptChangeRequestDetails::OptChangeRequestDetails() {}
PaymentsClient::OptChangeRequestDetails::OptChangeRequestDetails(
const OptChangeRequestDetails& other) {
app_locale = other.app_locale;
opt_in = other.opt_in;
reason = other.reason;
fido_authenticator_response = other.fido_authenticator_response.Clone();
card_authorization_token = other.card_authorization_token;
}
......
......@@ -127,9 +127,22 @@ class PaymentsClient {
~OptChangeRequestDetails();
std::string app_locale;
// Set to true if user will be opted-in for FIDO authentication for card
// unmasking. False otherwise.
bool opt_in;
// The reason for making the request.
enum Reason {
// Unknown default.
UNKNOWN_REASON = 0,
// The user wants to enable FIDO authentication for card unmasking.
ENABLE_FIDO_AUTH = 1,
// The user wants to disable FIDO authentication for card unmasking.
DISABLE_FIDO_AUTH = 2,
// The user is authorizing a new card for future FIDO authentication
// unmasking.
ADD_CARD_FOR_FIDO_AUTH = 3,
};
// Reason for the request.
Reason reason;
// Signature required for enrolling user into FIDO authentication for future
// card unmasking.
base::Value fido_authenticator_response;
......
......@@ -211,9 +211,10 @@ class PaymentsClientTest : public testing::Test {
// If |opt_in| is set to true, then opts the user in to use FIDO
// authentication for card unmasking. Otherwise opts the user out.
void StartOptChangeRequest(bool opt_in) {
void StartOptChangeRequest(
PaymentsClient::OptChangeRequestDetails::Reason reason) {
PaymentsClient::OptChangeRequestDetails request_details;
request_details.opt_in = opt_in;
request_details.reason = reason;
client_->OptChange(
request_details,
base::BindOnce(&PaymentsClientTest::OnDidGetOptChangeResult,
......@@ -528,15 +529,19 @@ TEST_F(PaymentsClientTest, UnmaskLogsBlankCvcLength) {
}
TEST_F(PaymentsClientTest, OptInSuccess) {
StartOptChangeRequest(/*opt_in=*/true);
StartOptChangeRequest(
PaymentsClient::OptChangeRequestDetails::ENABLE_FIDO_AUTH);
IssueOAuthToken();
ReturnResponse(net::HTTP_OK, "{ \"user_is_opted_in\": true }");
ReturnResponse(net::HTTP_OK,
"{ \"fido_authentication_info\": { \"user_status\": "
"\"FIDO_AUTH_ENABLED\"}}");
EXPECT_EQ(AutofillClient::SUCCESS, result_);
EXPECT_TRUE(opt_change_response_.user_is_opted_in.value());
}
TEST_F(PaymentsClientTest, OptInServerUnresponsive) {
StartOptChangeRequest(/*opt_in=*/true);
StartOptChangeRequest(
PaymentsClient::OptChangeRequestDetails::ENABLE_FIDO_AUTH);
IssueOAuthToken();
ReturnResponse(net::HTTP_REQUEST_TIMEOUT, "");
EXPECT_EQ(AutofillClient::NETWORK_ERROR, result_);
......@@ -544,19 +549,25 @@ TEST_F(PaymentsClientTest, OptInServerUnresponsive) {
}
TEST_F(PaymentsClientTest, OptOutSuccess) {
StartOptChangeRequest(/*opt_in=*/false);
StartOptChangeRequest(
PaymentsClient::OptChangeRequestDetails::DISABLE_FIDO_AUTH);
IssueOAuthToken();
ReturnResponse(net::HTTP_OK, "{ \"user_is_opted_in\": false }");
ReturnResponse(net::HTTP_OK,
"{ \"fido_authentication_info\": { \"user_status\": "
"\"FIDO_AUTH_DISABLED\"}}");
EXPECT_EQ(AutofillClient::SUCCESS, result_);
EXPECT_FALSE(opt_change_response_.user_is_opted_in.value());
}
TEST_F(PaymentsClientTest, EnrollAttemptReturnsCreationOptions) {
StartOptChangeRequest(/*opt_in=*/true);
StartOptChangeRequest(
PaymentsClient::OptChangeRequestDetails::ENABLE_FIDO_AUTH);
IssueOAuthToken();
ReturnResponse(net::HTTP_OK,
"{ \"user_is_opted_in\": false, \"fido_creation_options\": { "
"\"relying_party_id\": \"google.com\"} }");
"{ \"fido_authentication_info\": { \"user_status\": "
"\"FIDO_AUTH_DISABLED\","
"\"fido_creation_options\": {"
"\"relying_party_id\": \"google.com\"}}}");
EXPECT_EQ(AutofillClient::SUCCESS, result_);
EXPECT_FALSE(opt_change_response_.user_is_opted_in.value());
EXPECT_EQ("google.com",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment