OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "google_apis/gaia/fake_gaia.h" | 5 #include "google_apis/gaia/fake_gaia.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 : expires_in(3600) {} | 129 : expires_in(3600) {} |
130 | 130 |
131 FakeGaia::AccessTokenInfo::~AccessTokenInfo() {} | 131 FakeGaia::AccessTokenInfo::~AccessTokenInfo() {} |
132 | 132 |
133 FakeGaia::MergeSessionParams::MergeSessionParams() { | 133 FakeGaia::MergeSessionParams::MergeSessionParams() { |
134 } | 134 } |
135 | 135 |
136 FakeGaia::MergeSessionParams::~MergeSessionParams() { | 136 FakeGaia::MergeSessionParams::~MergeSessionParams() { |
137 } | 137 } |
138 | 138 |
139 void FakeGaia::MergeSessionParams::Update(const MergeSessionParams& update) { | |
140 auto maybe_update_field = | |
achuithb
2015/05/28 00:18:57
The number of obscure C++ features used in this sn
dzhioev (left Google)
2015/05/28 02:21:42
Done =).
| |
141 [this, &update](std::string MergeSessionParams::*field) { | |
142 if (!(update.*field).empty()) | |
143 this->*field = update.*field; | |
144 }; | |
145 maybe_update_field(&MergeSessionParams::auth_sid_cookie); | |
146 maybe_update_field(&MergeSessionParams::auth_lsid_cookie); | |
147 maybe_update_field(&MergeSessionParams::auth_code); | |
148 maybe_update_field(&MergeSessionParams::refresh_token); | |
149 maybe_update_field(&MergeSessionParams::access_token); | |
150 maybe_update_field(&MergeSessionParams::gaia_uber_token); | |
151 maybe_update_field(&MergeSessionParams::session_sid_cookie); | |
152 maybe_update_field(&MergeSessionParams::session_lsid_cookie); | |
153 maybe_update_field(&MergeSessionParams::email); | |
154 } | |
155 | |
139 FakeGaia::FakeGaia() : issue_oauth_code_cookie_(false) { | 156 FakeGaia::FakeGaia() : issue_oauth_code_cookie_(false) { |
140 base::FilePath source_root_dir; | 157 base::FilePath source_root_dir; |
141 PathService::Get(base::DIR_SOURCE_ROOT, &source_root_dir); | 158 PathService::Get(base::DIR_SOURCE_ROOT, &source_root_dir); |
142 CHECK(base::ReadFileToString( | 159 CHECK(base::ReadFileToString( |
143 source_root_dir.Append(base::FilePath(kServiceLogin)), | 160 source_root_dir.Append(base::FilePath(kServiceLogin)), |
144 &service_login_response_)); | 161 &service_login_response_)); |
145 CHECK(base::ReadFileToString( | 162 CHECK(base::ReadFileToString( |
146 source_root_dir.Append(base::FilePath(kEmbeddedSetupChromeos)), | 163 source_root_dir.Append(base::FilePath(kEmbeddedSetupChromeos)), |
147 &embedded_setup_chromeos_response_)); | 164 &embedded_setup_chromeos_response_)); |
148 } | 165 } |
(...skipping 15 matching lines...) Expand all Loading... | |
164 params.session_lsid_cookie = kTestSessionLSIDCookie; | 181 params.session_lsid_cookie = kTestSessionLSIDCookie; |
165 params.email = email; | 182 params.email = email; |
166 SetMergeSessionParams(params); | 183 SetMergeSessionParams(params); |
167 } | 184 } |
168 | 185 |
169 void FakeGaia::SetMergeSessionParams( | 186 void FakeGaia::SetMergeSessionParams( |
170 const MergeSessionParams& params) { | 187 const MergeSessionParams& params) { |
171 merge_session_params_ = params; | 188 merge_session_params_ = params; |
172 } | 189 } |
173 | 190 |
191 void FakeGaia::UpdateMergeSessionParams(const MergeSessionParams& params) { | |
192 merge_session_params_.Update(params); | |
193 } | |
194 | |
174 void FakeGaia::MapEmailToGaiaId(const std::string& email, | 195 void FakeGaia::MapEmailToGaiaId(const std::string& email, |
175 const std::string& gaia_id) { | 196 const std::string& gaia_id) { |
176 DCHECK(!email.empty()); | 197 DCHECK(!email.empty()); |
177 DCHECK(!gaia_id.empty()); | 198 DCHECK(!gaia_id.empty()); |
178 email_to_gaia_id_map_[email] = gaia_id; | 199 email_to_gaia_id_map_[email] = gaia_id; |
179 } | 200 } |
180 | 201 |
181 std::string FakeGaia::GetGaiaIdOfEmail(const std::string& email) const { | 202 std::string FakeGaia::GetGaiaIdOfEmail(const std::string& email) const { |
182 DCHECK(!email.empty()); | 203 DCHECK(!email.empty()); |
183 auto it = email_to_gaia_id_map_.find(email); | 204 const auto it = email_to_gaia_id_map_.find(email); |
184 return it == email_to_gaia_id_map_.end() ? std::string(kDefaultGaiaId) : | 205 return it == email_to_gaia_id_map_.end() ? std::string(kDefaultGaiaId) : |
185 it->second; | 206 it->second; |
186 } | 207 } |
187 | 208 |
188 void FakeGaia::AddGoogleAccountsSigninHeader( | 209 void FakeGaia::AddGoogleAccountsSigninHeader( |
189 net::test_server::BasicHttpResponse* http_response, | 210 net::test_server::BasicHttpResponse* http_response, |
190 const std::string& email) const { | 211 const std::string& email) const { |
191 DCHECK(!email.empty()); | 212 DCHECK(!email.empty()); |
192 http_response->AddCustomHeader("google-accounts-signin", | 213 http_response->AddCustomHeader("google-accounts-signin", |
193 base::StringPrintf( | 214 base::StringPrintf( |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
299 // static | 320 // static |
300 bool FakeGaia::GetQueryParameter(const std::string& query, | 321 bool FakeGaia::GetQueryParameter(const std::string& query, |
301 const std::string& key, | 322 const std::string& key, |
302 std::string* value) { | 323 std::string* value) { |
303 // Name and scheme actually don't matter, but are required to get a valid URL | 324 // Name and scheme actually don't matter, but are required to get a valid URL |
304 // for parsing. | 325 // for parsing. |
305 GURL query_url("http://localhost?" + query); | 326 GURL query_url("http://localhost?" + query); |
306 return net::GetValueForKeyInQuery(query_url, key, value); | 327 return net::GetValueForKeyInQuery(query_url, key, value); |
307 } | 328 } |
308 | 329 |
330 std::string FakeGaia::GetDeviceIdByRefreshToken( | |
331 const std::string& refresh_token) const { | |
332 auto it = refresh_token_to_device_id_map_.find(refresh_token); | |
333 return it != refresh_token_to_device_id_map_.end() ? it->second | |
334 : std::string(); | |
335 } | |
336 | |
337 void FakeGaia::SetRefreshTokenToDeviceIdMap( | |
338 const RefreshTokenToDeviceIdMap& refresh_token_to_device_id_map) { | |
339 refresh_token_to_device_id_map_ = refresh_token_to_device_id_map; | |
340 } | |
341 | |
309 void FakeGaia::HandleMergeSession(const HttpRequest& request, | 342 void FakeGaia::HandleMergeSession(const HttpRequest& request, |
310 BasicHttpResponse* http_response) { | 343 BasicHttpResponse* http_response) { |
311 http_response->set_code(net::HTTP_UNAUTHORIZED); | 344 http_response->set_code(net::HTTP_UNAUTHORIZED); |
312 if (merge_session_params_.session_sid_cookie.empty() || | 345 if (merge_session_params_.session_sid_cookie.empty() || |
313 merge_session_params_.session_lsid_cookie.empty()) { | 346 merge_session_params_.session_lsid_cookie.empty()) { |
314 http_response->set_code(net::HTTP_BAD_REQUEST); | 347 http_response->set_code(net::HTTP_BAD_REQUEST); |
315 return; | 348 return; |
316 } | 349 } |
317 | 350 |
318 std::string uber_token; | 351 std::string uber_token; |
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
595 | 628 |
596 if (grant_type == "authorization_code") { | 629 if (grant_type == "authorization_code") { |
597 std::string auth_code; | 630 std::string auth_code; |
598 if (!GetQueryParameter(request.content, "code", &auth_code) || | 631 if (!GetQueryParameter(request.content, "code", &auth_code) || |
599 auth_code != merge_session_params_.auth_code) { | 632 auth_code != merge_session_params_.auth_code) { |
600 http_response->set_code(net::HTTP_BAD_REQUEST); | 633 http_response->set_code(net::HTTP_BAD_REQUEST); |
601 LOG(ERROR) << "No 'code' param in /o/oauth2/token"; | 634 LOG(ERROR) << "No 'code' param in /o/oauth2/token"; |
602 return; | 635 return; |
603 } | 636 } |
604 | 637 |
638 std::string device_id; | |
639 if (GetQueryParameter(request.content, "device_id", &device_id)) { | |
640 std::string device_type; | |
641 if (!GetQueryParameter(request.content, "device_type", &device_type)) { | |
642 http_response->set_code(net::HTTP_BAD_REQUEST); | |
643 LOG(ERROR) << "'device_type' should be set if 'device_id' is set."; | |
644 return; | |
645 } | |
646 if (device_type != "chrome") { | |
647 http_response->set_code(net::HTTP_BAD_REQUEST); | |
648 LOG(ERROR) << "'device_type' is not 'chrome'."; | |
649 return; | |
650 } | |
651 } | |
652 | |
605 base::DictionaryValue response_dict; | 653 base::DictionaryValue response_dict; |
606 response_dict.SetString("refresh_token", | 654 response_dict.SetString("refresh_token", |
607 merge_session_params_.refresh_token); | 655 merge_session_params_.refresh_token); |
656 if (!device_id.empty()) | |
657 refresh_token_to_device_id_map_[merge_session_params_.refresh_token] = | |
658 device_id; | |
608 response_dict.SetString("access_token", | 659 response_dict.SetString("access_token", |
609 merge_session_params_.access_token); | 660 merge_session_params_.access_token); |
610 response_dict.SetInteger("expires_in", 3600); | 661 response_dict.SetInteger("expires_in", 3600); |
611 FormatJSONResponse(response_dict, http_response); | 662 FormatJSONResponse(response_dict, http_response); |
612 return; | 663 return; |
613 } | 664 } |
614 | 665 |
615 std::string scope; | 666 std::string scope; |
616 GetQueryParameter(request.content, "scope", &scope); | 667 GetQueryParameter(request.content, "scope", &scope); |
617 | 668 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
712 if (token_info) { | 763 if (token_info) { |
713 base::DictionaryValue response_dict; | 764 base::DictionaryValue response_dict; |
714 response_dict.SetString("id", GetGaiaIdOfEmail(token_info->email)); | 765 response_dict.SetString("id", GetGaiaIdOfEmail(token_info->email)); |
715 response_dict.SetString("email", token_info->email); | 766 response_dict.SetString("email", token_info->email); |
716 response_dict.SetString("verified_email", token_info->email); | 767 response_dict.SetString("verified_email", token_info->email); |
717 FormatJSONResponse(response_dict, http_response); | 768 FormatJSONResponse(response_dict, http_response); |
718 } else { | 769 } else { |
719 http_response->set_code(net::HTTP_BAD_REQUEST); | 770 http_response->set_code(net::HTTP_BAD_REQUEST); |
720 } | 771 } |
721 } | 772 } |
OLD | NEW |