OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/autofill/content/browser/wallet/wallet_service_url.h" | 5 #include "components/autofill/content/browser/wallet/wallet_service_url.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
11 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
16 #include "components/autofill/core/common/autofill_switches.h" | 16 #include "components/autofill/core/common/autofill_switches.h" |
17 #include "content/public/common/content_switches.h" | 17 #include "content/public/common/content_switches.h" |
18 #include "content/public/common/url_constants.h" | 18 #include "content/public/common/url_constants.h" |
19 #include "google_apis/gaia/gaia_urls.h" | 19 #include "google_apis/gaia/gaia_urls.h" |
20 #include "net/base/url_util.h" | 20 #include "net/base/url_util.h" |
21 #include "url/gurl.h" | 21 #include "url/gurl.h" |
22 | 22 |
23 namespace autofill { | 23 namespace autofill { |
24 namespace { | 24 namespace { |
25 | 25 |
26 const char kProdWalletServiceUrl[] = "https://wallet.google.com/"; | 26 const char kProdWalletServiceUrl[] = "https://wallet.google.com/"; |
27 | 27 |
28 const char kSandboxWalletServiceUrl[] = | |
29 "https://wallet-web.sandbox.google.com/"; | |
30 | |
31 const char kSandboxWalletSecureServiceUrl[] = | 28 const char kSandboxWalletSecureServiceUrl[] = |
32 "https://wallet-web.sandbox.google.com/"; | 29 "https://wallet-web.sandbox.google.com/"; |
33 | 30 |
34 bool IsWalletProductionEnabled() { | 31 bool IsWalletProductionEnabled() { |
35 // If the command line flag exists, it takes precedence. | 32 // If the command line flag exists, it takes precedence. |
36 const base::CommandLine* command_line = | 33 const base::CommandLine* command_line = |
37 base::CommandLine::ForCurrentProcess(); | 34 base::CommandLine::ForCurrentProcess(); |
38 std::string sandbox_enabled( | 35 std::string sandbox_enabled( |
39 command_line->GetSwitchValueASCII(switches::kWalletServiceUseSandbox)); | 36 command_line->GetSwitchValueASCII(switches::kWalletServiceUseSandbox)); |
40 if (!sandbox_enabled.empty()) | 37 if (!sandbox_enabled.empty()) |
41 return sandbox_enabled != "1"; | 38 return sandbox_enabled != "1"; |
42 | 39 |
43 // Default to sandbox when --reduce-security-for-testing is passed to allow | 40 // Default to sandbox when --reduce-security-for-testing is passed to allow |
44 // rAc on http:// pages. | 41 // rAc on http:// pages. |
45 if (command_line->HasSwitch(::switches::kReduceSecurityForTesting)) | 42 if (command_line->HasSwitch(::switches::kReduceSecurityForTesting)) |
46 return false; | 43 return false; |
47 | 44 |
48 #if defined(ENABLE_PROD_WALLET_SERVICE) | 45 #if defined(ENABLE_PROD_WALLET_SERVICE) |
49 return true; | 46 return true; |
50 #else | 47 #else |
51 return false; | 48 return false; |
52 #endif | 49 #endif |
53 } | 50 } |
54 | 51 |
55 GURL GetWalletHostUrl() { | |
56 const base::CommandLine& command_line = | |
57 *base::CommandLine::ForCurrentProcess(); | |
58 std::string wallet_service_hostname = | |
59 command_line.GetSwitchValueASCII(switches::kWalletServiceUrl); | |
60 if (!wallet_service_hostname.empty()) | |
61 return GURL(wallet_service_hostname); | |
62 if (IsWalletProductionEnabled()) | |
63 return GURL(kProdWalletServiceUrl); | |
64 return GURL(kSandboxWalletServiceUrl); | |
65 } | |
66 | |
67 GURL GetBaseWalletUrl(size_t user_index) { | |
68 std::string path = base::StringPrintf("online/v2/u/%" PRIuS "/", user_index); | |
69 return GetWalletHostUrl().Resolve(path); | |
70 } | |
71 | |
72 GURL GetBaseAutocheckoutUrl(size_t user_index) { | |
73 return GetBaseWalletUrl(user_index).Resolve("wallet/autocheckout/v1/"); | |
74 } | |
75 | |
76 GURL GetBaseSecureUrl() { | 52 GURL GetBaseSecureUrl() { |
77 const base::CommandLine& command_line = | 53 const base::CommandLine& command_line = |
78 *base::CommandLine::ForCurrentProcess(); | 54 *base::CommandLine::ForCurrentProcess(); |
79 std::string wallet_secure_url = | 55 std::string wallet_secure_url = |
80 command_line.GetSwitchValueASCII(switches::kWalletSecureServiceUrl); | 56 command_line.GetSwitchValueASCII(switches::kWalletSecureServiceUrl); |
81 if (!wallet_secure_url.empty()) | 57 if (!wallet_secure_url.empty()) |
82 return GURL(wallet_secure_url); | 58 return GURL(wallet_secure_url); |
83 if (IsWalletProductionEnabled()) | 59 if (IsWalletProductionEnabled()) |
84 return GURL(kProdWalletServiceUrl); | 60 return GURL(kProdWalletServiceUrl); |
85 return GURL(kSandboxWalletSecureServiceUrl); | 61 return GURL(kSandboxWalletSecureServiceUrl); |
86 } | 62 } |
87 | 63 |
88 GURL GetBaseEncryptedFrontendUrl(size_t user_index) { | |
89 const base::CommandLine& command_line = | |
90 *base::CommandLine::ForCurrentProcess(); | |
91 GURL base_url = IsWalletProductionEnabled() || | |
92 command_line.HasSwitch(switches::kWalletServiceUrl) ? | |
93 GetWalletHostUrl() : GetBaseSecureUrl(); | |
94 std::string path = | |
95 base::StringPrintf("online-secure/v2/u/%" PRIuS "/autocheckout/v1/", | |
96 user_index); | |
97 return base_url.Resolve(path); | |
98 } | |
99 | |
100 } // namespace | 64 } // namespace |
101 | 65 |
102 namespace wallet { | 66 namespace wallet { |
103 | 67 |
104 GURL GetGetWalletItemsUrl(size_t user_index) { | |
105 return GetBaseAutocheckoutUrl(user_index).Resolve("getWalletItemsJwtless"); | |
106 } | |
107 | |
108 GURL GetGetFullWalletUrl(size_t user_index) { | |
109 return GetBaseEncryptedFrontendUrl(user_index) | |
110 .Resolve("getFullWalletJwtless?s7e=otp"); | |
111 } | |
112 | |
113 GURL GetManageInstrumentsUrl(size_t user_index) { | 68 GURL GetManageInstrumentsUrl(size_t user_index) { |
114 std::string path = | 69 std::string path = |
115 base::StringPrintf("manage/w/%" PRIuS "/paymentMethods", user_index); | 70 base::StringPrintf("manage/w/%" PRIuS "/paymentMethods", user_index); |
116 return GetBaseSecureUrl().Resolve(path); | 71 return GetBaseSecureUrl().Resolve(path); |
117 } | 72 } |
118 | 73 |
119 GURL GetManageAddressesUrl(size_t user_index) { | 74 GURL GetManageAddressesUrl(size_t user_index) { |
120 std::string path = | 75 std::string path = |
121 base::StringPrintf("manage/w/%" PRIuS "/settings/addresses", user_index); | 76 base::StringPrintf("manage/w/%" PRIuS "/settings/addresses", user_index); |
122 return GetBaseSecureUrl().Resolve(path); | 77 return GetBaseSecureUrl().Resolve(path); |
123 } | 78 } |
124 | 79 |
125 GURL GetPrivacyNoticeUrl() { | |
126 return GetWalletHostUrl().Resolve("legaldocument?family=0.privacynotice"); | |
127 } | |
128 | |
129 GURL GetAcceptLegalDocumentsUrl(size_t user_index) { | |
130 return GetBaseAutocheckoutUrl(user_index).Resolve("acceptLegalDocument"); | |
131 } | |
132 | |
133 GURL GetAuthenticateInstrumentUrl(size_t user_index) { | |
134 return GetBaseEncryptedFrontendUrl(user_index) | |
135 .Resolve("authenticateInstrument?s7e=cvn"); | |
136 } | |
137 | |
138 GURL GetSaveToWalletNoEscrowUrl(size_t user_index) { | |
139 return GetBaseAutocheckoutUrl(user_index).Resolve("saveToWallet"); | |
140 } | |
141 | |
142 GURL GetSaveToWalletUrl(size_t user_index) { | |
143 return GetBaseEncryptedFrontendUrl(user_index) | |
144 .Resolve("saveToWallet?s7e=card_number%3Bcvn"); | |
145 } | |
146 | |
147 GURL GetPassiveAuthUrl(size_t user_index) { | |
148 return GetBaseWalletUrl(user_index) | |
149 .Resolve("passiveauth?isChromePayments=true"); | |
150 } | |
151 | |
152 GURL GetAddAccountUrl() { | |
153 GURL url(GaiaUrls::GetInstance()->add_account_url()); | |
154 url = net::AppendQueryParameter(url, "nui", "1"); | |
155 // Prevents promos from showing (see http://crbug.com/235227). | |
156 url = net::AppendQueryParameter(url, "sarp", "1"); | |
157 url = net::AppendQueryParameter(url, | |
158 "continue", | |
159 GetSignInContinueUrl().spec()); | |
160 return url; | |
161 } | |
162 | |
163 GURL GetSignInUrl(size_t user_index) { | |
164 GURL url(GaiaUrls::GetInstance()->service_login_url()); | |
165 url = net::AppendQueryParameter( | |
166 url, "authuser", base::SizeTToString(user_index)); | |
167 // Prevents promos from showing (see http://crbug.com/235227). | |
168 url = net::AppendQueryParameter(url, "sarp", "1"); | |
169 url = | |
170 net::AppendQueryParameter(url, "continue", GetSignInContinueUrl().spec()); | |
171 return url; | |
172 } | |
173 | |
174 // The continue url portion of the sign-in URL. This URL is used as a milestone | |
175 // to determine that the sign-in process is finished. It has to be a Google | |
176 // domain, use https://, and do almost nothing, but otherwise it's not too | |
177 // important what the URL actually is: it's not important that this URL has the | |
178 // ability to generate a gdToken. | |
179 GURL GetSignInContinueUrl() { | |
180 return GetPassiveAuthUrl(0); | |
181 } | |
182 | |
183 bool IsSignInContinueUrl(const GURL& url, size_t* user_index) { | |
184 GURL final_url = GetSignInContinueUrl(); | |
185 if (url.scheme() != final_url.scheme() || | |
186 url.host() != final_url.host() || | |
187 url.path() != final_url.path()) { | |
188 return false; | |
189 } | |
190 | |
191 *user_index = 0; | |
192 std::string query_str = url.query(); | |
193 url::Component query(0, query_str.length()); | |
194 url::Component key, value; | |
195 const char kUserIndexKey[] = "authuser"; | |
196 while (url::ExtractQueryKeyValue(query_str.c_str(), &query, &key, &value)) { | |
197 if (key.is_nonempty() && | |
198 query_str.substr(key.begin, key.len) == kUserIndexKey) { | |
199 base::StringToSizeT(query_str.substr(value.begin, value.len), user_index); | |
200 break; | |
201 } | |
202 } | |
203 | |
204 return true; | |
205 } | |
206 | |
207 bool IsSignInRelatedUrl(const GURL& url) { | |
208 size_t unused; | |
209 // Origins are host names which are canonicalized, so case-sensitive is OK. | |
210 GURL origin = url.GetOrigin(); | |
211 return origin == GetAddAccountUrl().GetOrigin() || | |
212 base::StartsWith(origin.host(), "accounts.", | |
213 base::CompareCase::SENSITIVE) || | |
214 IsSignInContinueUrl(url, &unused); | |
215 } | |
216 | |
217 bool IsUsingProd() { | |
218 return GetWalletHostUrl() == GURL(kProdWalletServiceUrl); | |
219 } | |
220 | |
221 } // namespace wallet | 80 } // namespace wallet |
222 } // namespace autofill | 81 } // namespace autofill |
OLD | NEW |