Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/autofill/wallet/full_wallet.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/string_number_conversions.h" | |
| 9 #include "base/values.h" | |
| 10 | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 size_t kPanSize = 16; | |
| 15 size_t kBinSize = 6; | |
| 16 size_t kCvnSize = 3; | |
| 17 | |
| 18 } // end anonymous namespace | |
| 19 | |
| 20 namespace wallet { | |
| 21 | |
| 22 FullWallet::FullWallet(int expiration_month, | |
| 23 int expiration_year, | |
| 24 const std::string& iin, | |
| 25 const std::string& encrypted_rest, | |
| 26 Address* billing_address, | |
| 27 Address* shipping_address, | |
| 28 std::vector<std::string> required_actions) | |
| 29 : expiration_month_(expiration_month), | |
| 30 expiration_year_(expiration_year), | |
| 31 iin_(iin), | |
| 32 encrypted_rest_(encrypted_rest), | |
| 33 billing_address_(billing_address), | |
| 34 shipping_address_(shipping_address), | |
| 35 required_actions_(required_actions) {} | |
| 36 | |
| 37 FullWallet::~FullWallet() { | |
| 38 } | |
| 39 | |
| 40 FullWallet* FullWallet::CreateFullWallet(DictionaryValue* dictionary) { | |
| 41 DCHECK(dictionary); | |
| 42 ListValue* required_actions_list; | |
| 43 std::vector<std::string> required_actions; | |
| 44 if (dictionary->GetList("required_action", &required_actions_list)) { | |
| 45 for (size_t i = 0; i < required_actions_list->GetSize(); i++) { | |
| 46 std::string action; | |
| 47 if (required_actions_list->GetString(i, &action)) | |
| 48 required_actions.push_back(action); | |
| 49 } | |
| 50 if (required_actions.size() > 0) { | |
| 51 return new FullWallet(-1, -1, "", "", NULL, NULL, required_actions); | |
| 52 } | |
| 53 } else { | |
| 54 VLOG(1) << "Response from Google wallet missing required actions"; | |
| 55 } | |
| 56 | |
| 57 int expiration_month; | |
| 58 if (!dictionary->GetInteger("expiration_month", &expiration_month)) { | |
| 59 LOG(ERROR) << "Response from Google wallet missing expiration month"; | |
|
Albert Bodenhamer
2012/11/28 23:50:56
We generally prefer DLOG over LOG (and friends) in
ahutter
2012/11/29 20:52:38
Done.
| |
| 60 return NULL; | |
| 61 } | |
| 62 int expiration_year; | |
| 63 if (!dictionary->GetInteger("expiration_year", &expiration_year)) { | |
| 64 LOG(ERROR) << "Response from Google wallet missing expiration year"; | |
| 65 return NULL; | |
| 66 } | |
| 67 std::string iin; | |
| 68 if (!dictionary->GetString("iin", &iin)) { | |
| 69 LOG(ERROR) << "Response from Google wallet missing iin"; | |
| 70 return NULL; | |
| 71 } | |
| 72 std::string encrypted_rest; | |
| 73 if (!dictionary->GetString("rest", &encrypted_rest)) { | |
| 74 LOG(ERROR) << "Response from Google wallet missing rest"; | |
| 75 return NULL; | |
| 76 } | |
| 77 DictionaryValue* billing_address_dict; | |
| 78 if (!dictionary->GetDictionary("billing_address", &billing_address_dict)) { | |
| 79 LOG(ERROR) << "Response from Google wallet missing billing address"; | |
| 80 return NULL; | |
| 81 } | |
| 82 Address* billing_address = | |
| 83 Address::CreateIdedAddress(billing_address_dict); | |
| 84 if (!billing_address) { | |
| 85 LOG(ERROR) << "Response from Google wallet has malformed billing address"; | |
| 86 return NULL; | |
| 87 } | |
| 88 | |
| 89 DictionaryValue* shipping_address_dict; | |
| 90 Address* shipping_address; | |
| 91 if (dictionary->GetDictionary("shipping_address", &shipping_address_dict)) { | |
| 92 shipping_address = | |
| 93 Address::CreateIdedAddress(shipping_address_dict); | |
| 94 } else { | |
| 95 VLOG(1) << "Response from Google wallet missing shipping address"; | |
| 96 } | |
| 97 | |
| 98 return new FullWallet(expiration_month, | |
| 99 expiration_year, | |
| 100 iin, | |
| 101 encrypted_rest, | |
| 102 billing_address, | |
| 103 shipping_address, | |
| 104 required_actions); | |
| 105 } | |
| 106 | |
| 107 bool FullWallet::operator==(const FullWallet& other) const { | |
| 108 if (expiration_month_ != other.expiration_month_) | |
| 109 return false; | |
| 110 if (expiration_year_ != other.expiration_year_) | |
| 111 return false; | |
| 112 if (iin_.compare(other.iin_) != 0) | |
| 113 return false; | |
| 114 if (encrypted_rest_.compare(other.encrypted_rest_) != 0) | |
| 115 return false; | |
| 116 if (billing_address_.get() && other.billing_address_.get()) { | |
| 117 if (*billing_address_.get() != *other.billing_address_.get()) | |
| 118 return false; | |
| 119 } else if (billing_address_.get() || other.billing_address_.get()) { | |
| 120 return false; | |
| 121 } | |
| 122 if (shipping_address_.get() && other.shipping_address_.get()) { | |
| 123 if (*shipping_address_.get() != *other.shipping_address_.get()) | |
| 124 return false; | |
| 125 } else if (shipping_address_.get() || other.shipping_address_.get()) { | |
| 126 return false; | |
| 127 } | |
| 128 if (required_actions_ != other.required_actions_) | |
| 129 return false; | |
| 130 return true; | |
| 131 } | |
| 132 | |
| 133 bool FullWallet::operator!=(const FullWallet& other) const { | |
| 134 return !(*this == other); | |
| 135 } | |
| 136 | |
| 137 const std::string FullWallet::GetPAN(void* otp, size_t length) { | |
| 138 if (cvn_.empty()) | |
| 139 DecryptCardInfo((uint8*) otp, length); | |
| 140 return pan_; | |
| 141 } | |
| 142 | |
| 143 const std::string FullWallet::GetCVN(void* otp, size_t length) { | |
| 144 if (pan_.empty()) | |
| 145 DecryptCardInfo((uint8*) otp, length); | |
| 146 return cvn_; | |
| 147 } | |
| 148 | |
| 149 void FullWallet::DecryptCardInfo(uint8* otp, size_t length) { | |
| 150 std::vector<uint8> operating_data; | |
| 151 if (!base::HexStringToBytes(encrypted_rest_, &operating_data)) { | |
| 152 LOG(ERROR) << "Failed to parse rest"; | |
| 153 return; | |
| 154 } | |
| 155 DCHECK_EQ(length, operating_data.size()); | |
| 156 uint8* result = new uint8[length]; | |
| 157 for (size_t i = 0; i < length; i++) | |
| 158 result[i] = otp[i] ^ operating_data[i]; | |
| 159 std::string hex_decrypted = base::HexEncode(result, length); | |
| 160 delete[] result; | |
| 161 int64 decrypted; | |
| 162 if (!base::HexStringToInt64(hex_decrypted, &decrypted)) { | |
| 163 LOG(ERROR) << "Failed to parse decrypted"; | |
| 164 return; | |
| 165 } | |
| 166 std::string card_info = base::Int64ToString(decrypted); | |
| 167 | |
| 168 size_t padded_length = (kPanSize - kBinSize + kCvnSize); | |
| 169 if (card_info.size() != padded_length) | |
| 170 card_info.insert(card_info.begin(), padded_length - card_info.size(), '0'); | |
| 171 | |
| 172 size_t split = kPanSize - kBinSize; | |
| 173 cvn_ = card_info.substr(split); | |
| 174 pan_ = iin_ + card_info.substr(0, split); | |
| 175 } | |
| 176 | |
| 177 } // end wallet namespace | |
| OLD | NEW |