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/memory/scoped_ptr.h" | |
| 9 #include "base/string_number_conversions.h" | |
| 10 #include "base/values.h" | |
| 11 | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 size_t pan_size = 16; | |
| 16 size_t bin_size = 6; | |
| 17 size_t cvn_size = 3; | |
| 18 | |
| 19 } // end anonymous namespace | |
| 20 | |
| 21 namespace wallet { | |
| 22 | |
| 23 FullWallet::FullWallet(int expiration_month, | |
| 24 int expiration_year, | |
| 25 const std::string& iin, | |
| 26 const std::string& rest, | |
| 27 Address* billing_address, | |
| 28 Address* shipping_address, | |
| 29 std::vector<std::string> required_actions) | |
| 30 : expiration_month_(expiration_month), | |
| 31 expiration_year_(expiration_year), | |
| 32 iin_(iin), | |
| 33 rest_(rest), | |
| 34 billing_address_(billing_address), | |
| 35 shipping_address_(shipping_address), | |
| 36 required_actions_(required_actions) {} | |
| 37 | |
| 38 FullWallet::~FullWallet() { | |
| 39 } | |
| 40 | |
| 41 scoped_ptr<FullWallet> FullWallet::FromDictionary(DictionaryValue* dictionary) { | |
|
Dane Wallinga
2012/11/16 01:27:25
return raw pointer, change name or add comment to
ahutter
2012/11/16 19:46:41
Done.
| |
| 42 DCHECK(dictionary); | |
| 43 ListValue* required_actions_list; | |
| 44 std::vector<std::string> required_actions; | |
| 45 if (dictionary->GetList("required_action", &required_actions_list)) { | |
| 46 for (size_t i = 0; i < required_actions_list->GetSize(); i++) { | |
| 47 std::string action; | |
| 48 if (required_actions_list->GetString(i, &action)) | |
| 49 required_actions.push_back(action); | |
| 50 } | |
| 51 if (required_actions.size() > 0) { | |
| 52 return scoped_ptr<FullWallet>(new FullWallet(-1, | |
| 53 -1, | |
| 54 "", | |
| 55 "", | |
| 56 NULL, | |
| 57 NULL, | |
| 58 required_actions)); | |
| 59 } | |
| 60 } else { | |
| 61 VLOG(1) << "Response from Google wallet missing required actions"; | |
| 62 } | |
| 63 | |
| 64 int expiration_month; | |
| 65 if (!dictionary->GetInteger("expiration_month", &expiration_month)) { | |
| 66 LOG(ERROR) << "Response from Google wallet missing expiration month"; | |
| 67 return scoped_ptr<FullWallet>(); | |
| 68 } | |
| 69 int expiration_year; | |
| 70 if (!dictionary->GetInteger("expiration_year", &expiration_year)) { | |
| 71 LOG(ERROR) << "Response from Google wallet missing expiration year"; | |
| 72 return scoped_ptr<FullWallet>(); | |
| 73 } | |
| 74 std::string iin; | |
| 75 if (!dictionary->GetString("iin", &iin)) { | |
| 76 LOG(ERROR) << "Response from Google wallet missing iin"; | |
| 77 return scoped_ptr<FullWallet>(); | |
| 78 } | |
| 79 std::string rest; | |
| 80 if (!dictionary->GetString("rest", &rest)) { | |
| 81 LOG(ERROR) << "Response from Google wallet missing rest"; | |
| 82 return scoped_ptr<FullWallet>(); | |
| 83 } | |
| 84 DictionaryValue* billing_address_dict; | |
| 85 if (!dictionary->GetDictionary("billing_address", &billing_address_dict)) { | |
| 86 LOG(ERROR) << "Response from Google wallet missing billing address"; | |
| 87 return scoped_ptr<FullWallet>(); | |
| 88 } | |
| 89 scoped_ptr<Address> billing_address = | |
| 90 Address::FromWalletDictionary(billing_address_dict); | |
| 91 if (!billing_address.get()) { | |
| 92 LOG(ERROR) << "Response from Google wallet has malformed billing address"; | |
| 93 return scoped_ptr<FullWallet>(); | |
| 94 } | |
| 95 | |
| 96 DictionaryValue* shipping_address_dict; | |
| 97 scoped_ptr<Address> shipping_address(NULL); | |
| 98 if (dictionary->GetDictionary("shipping_address", &shipping_address_dict)) { | |
| 99 shipping_address = Address::FromWalletDictionary(shipping_address_dict); | |
| 100 } else { | |
| 101 VLOG(1) << "Response from Google wallet missing shipping address"; | |
| 102 } | |
| 103 | |
| 104 return scoped_ptr<FullWallet>(new FullWallet(expiration_month, | |
| 105 expiration_year, | |
| 106 iin, | |
| 107 rest, | |
| 108 billing_address.release(), | |
| 109 shipping_address.release(), | |
| 110 required_actions)); | |
| 111 } | |
| 112 | |
| 113 bool FullWallet::operator==(const FullWallet& other) const { | |
| 114 if (expiration_month_ != other.expiration_month_) | |
| 115 return false; | |
| 116 if (expiration_year_ != other.expiration_year_) | |
| 117 return false; | |
| 118 if (iin_.compare(other.iin_) != 0) | |
| 119 return false; | |
| 120 if (rest_.compare(other.rest_) != 0) | |
| 121 return false; | |
| 122 if (billing_address_.get() && other.billing_address_.get()) { | |
| 123 if (*billing_address_.get() != *other.billing_address_.get()) | |
| 124 return false; | |
| 125 } else if (billing_address_.get() || other.billing_address_.get()) { | |
| 126 return false; | |
| 127 } | |
| 128 if (shipping_address_.get() && other.shipping_address_.get()) { | |
| 129 if (*shipping_address_.get() != *other.shipping_address_.get()) | |
| 130 return false; | |
| 131 } else if (shipping_address_.get() || other.shipping_address_.get()) { | |
| 132 return false; | |
| 133 } | |
| 134 if (required_actions_ != other.required_actions_) | |
| 135 return false; | |
| 136 return true; | |
| 137 } | |
| 138 | |
| 139 bool FullWallet::operator!=(const FullWallet& other) const { | |
| 140 return !(*this == other); | |
| 141 } | |
| 142 | |
| 143 std::ostream& operator<<(std::ostream& o, | |
| 144 const FullWallet& full_wallet) { | |
| 145 o << "Expiration month : " << full_wallet.expiration_month_ << std::endl; | |
| 146 o << "Expiration year : " << full_wallet.expiration_year_ << std::endl; | |
| 147 o << "Iin : " << full_wallet.iin_ << std::endl; | |
| 148 o << "Rest : " << full_wallet.rest_ << std::endl; | |
| 149 o << "Billing address : " << full_wallet.billing_address_ << std::endl; | |
| 150 o << "Shipping address : " << full_wallet.shipping_address_ << std::endl; | |
| 151 // o << "Required actions : " << full_wallet.required_actions_ << std::endl; | |
| 152 return o; | |
| 153 } | |
| 154 | |
| 155 const std::string FullWallet::GetPAN(void* otp, size_t length) const { | |
| 156 std::vector<uint8> operating_data; | |
| 157 if (!base::HexStringToBytes(rest_, &operating_data)) { | |
| 158 LOG(ERROR) << "Failed to parse rest"; | |
| 159 return ""; | |
| 160 } | |
| 161 CHECK_EQ(length, operating_data.size()); | |
| 162 char* result = new char[length]; | |
| 163 char* otp_thing = (char*) otp; | |
| 164 for (size_t i = 0; i < length; i++) | |
| 165 result[i] = static_cast<uint8>(otp_thing[i]) ^ operating_data[i]; | |
| 166 std::string hex_decrypted = base::HexEncode(result, length); | |
| 167 delete[] result; | |
| 168 int64 decrypted; | |
| 169 if (!base::HexStringToInt64(hex_decrypted, &decrypted)) { | |
| 170 LOG(ERROR) << "Failed to parse decrypted"; | |
| 171 return ""; | |
| 172 } | |
| 173 std::string card_info = base::Int64ToString(decrypted); | |
| 174 | |
| 175 size_t padded_length = (pan_size - bin_size + cvn_size); | |
| 176 if (card_info.size() != padded_length) | |
| 177 card_info.insert(card_info.begin(), padded_length - card_info.size(), '0'); | |
| 178 | |
| 179 size_t split = pan_size - bin_size; | |
| 180 return card_info.substr(0, split); | |
| 181 } | |
| 182 | |
| 183 const std::string FullWallet::GetCVN(void* otp, size_t length) const { | |
| 184 std::vector<uint8> operating_data; | |
| 185 if (!base::HexStringToBytes(rest_, &operating_data)) { | |
| 186 LOG(ERROR) << "Failed to parse rest"; | |
| 187 return ""; | |
| 188 } | |
| 189 CHECK_EQ(length, operating_data.size()); | |
| 190 char* result = new char[length]; | |
| 191 char* otp_thing = (char*) otp; | |
| 192 for (size_t i = 0; i < length; i++) | |
| 193 result[i] = static_cast<uint8>(otp_thing[i]) ^ operating_data[i]; | |
| 194 std::string hex_decrypted = base::HexEncode(result, length); | |
| 195 delete[] result; | |
| 196 int64 decrypted; | |
| 197 if (!base::HexStringToInt64(hex_decrypted, &decrypted)) { | |
| 198 LOG(ERROR) << "Failed to parse decrypted"; | |
| 199 return ""; | |
| 200 } | |
| 201 std::string card_info = base::Int64ToString(decrypted); | |
| 202 | |
| 203 size_t padded_length = (pan_size - bin_size + cvn_size); | |
| 204 if (card_info.size() != padded_length) | |
| 205 card_info.insert(card_info.begin(), padded_length - card_info.size(), '0'); | |
| 206 | |
| 207 size_t split = pan_size - bin_size; | |
| 208 return card_info.substr(split); | |
| 209 } | |
| 210 | |
| 211 } // end wallet namespace | |
| OLD | NEW |