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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 const char kTestAuthCode[] = "fake-auth-code"; | 44 const char kTestAuthCode[] = "fake-auth-code"; |
45 const char kTestGaiaUberToken[] = "fake-uber-token"; | 45 const char kTestGaiaUberToken[] = "fake-uber-token"; |
46 const char kTestAuthLoginAccessToken[] = "fake-access-token"; | 46 const char kTestAuthLoginAccessToken[] = "fake-access-token"; |
47 const char kTestRefreshToken[] = "fake-refresh-token"; | 47 const char kTestRefreshToken[] = "fake-refresh-token"; |
48 const char kTestSessionSIDCookie[] = "fake-session-SID-cookie"; | 48 const char kTestSessionSIDCookie[] = "fake-session-SID-cookie"; |
49 const char kTestSessionLSIDCookie[] = "fake-session-LSID-cookie"; | 49 const char kTestSessionLSIDCookie[] = "fake-session-LSID-cookie"; |
50 const char kTestOAuthLoginSID[] = "fake-oauth-SID-cookie"; | 50 const char kTestOAuthLoginSID[] = "fake-oauth-SID-cookie"; |
51 const char kTestOAuthLoginLSID[] = "fake-oauth-LSID-cookie"; | 51 const char kTestOAuthLoginLSID[] = "fake-oauth-LSID-cookie"; |
52 const char kTestOAuthLoginAuthCode[] = "fake-oauth-auth-code"; | 52 const char kTestOAuthLoginAuthCode[] = "fake-oauth-auth-code"; |
53 | 53 |
54 const char kDefaultGaiaId[] ="12345"; | |
55 | |
56 const base::FilePath::CharType kServiceLogin[] = | 54 const base::FilePath::CharType kServiceLogin[] = |
57 FILE_PATH_LITERAL("google_apis/test/service_login.html"); | 55 FILE_PATH_LITERAL("google_apis/test/service_login.html"); |
58 | 56 |
59 // OAuth2 Authentication header value prefix. | 57 // OAuth2 Authentication header value prefix. |
60 const char kAuthHeaderBearer[] = "Bearer "; | 58 const char kAuthHeaderBearer[] = "Bearer "; |
61 const char kAuthHeaderOAuth[] = "OAuth "; | 59 const char kAuthHeaderOAuth[] = "OAuth "; |
62 | 60 |
63 const char kListAccountsResponseFormat[] = | 61 const char kListAccountsResponseFormat[] = |
64 "[\"gaia.l.a.r\",[[\"gaia.l.a\",1,\"\",\"%s\",\"\",1,1,0]]]"; | 62 "[\"gaia.l.a.r\",[[\"gaia.l.a\",1,\"\",\"%s\",\"\",1,1,0]]]"; |
65 | 63 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 params.session_lsid_cookie = kTestSessionLSIDCookie; | 156 params.session_lsid_cookie = kTestSessionLSIDCookie; |
159 params.email = email; | 157 params.email = email; |
160 SetMergeSessionParams(params); | 158 SetMergeSessionParams(params); |
161 } | 159 } |
162 | 160 |
163 void FakeGaia::SetMergeSessionParams( | 161 void FakeGaia::SetMergeSessionParams( |
164 const MergeSessionParams& params) { | 162 const MergeSessionParams& params) { |
165 merge_session_params_ = params; | 163 merge_session_params_ = params; |
166 } | 164 } |
167 | 165 |
168 void FakeGaia::MapEmailToGaiaId(const std::string& email, | |
169 const std::string& gaia_id) { | |
170 DCHECK(!email.empty()); | |
171 DCHECK(!gaia_id.empty()); | |
172 email_to_gaia_id_map_[email] = gaia_id; | |
173 } | |
174 | |
175 std::string FakeGaia::GetGaiaIdOfEmail(const std::string& email) const { | |
176 DCHECK(!email.empty()); | |
177 auto it = email_to_gaia_id_map_.find(email); | |
178 return it == email_to_gaia_id_map_.end() ? std::string(kDefaultGaiaId) : | |
179 it->second; | |
180 } | |
181 | |
182 void FakeGaia::AddGoogleAccountsSigninHeader( | |
183 net::test_server::BasicHttpResponse* http_response, | |
184 const std::string& email) const { | |
185 DCHECK(!email.empty()); | |
186 http_response->AddCustomHeader("google-accounts-signin", | |
187 base::StringPrintf( | |
188 "email=\"%s\", obfuscatedid=\"%s\", sessionindex=0", | |
189 email.c_str(), GetGaiaIdOfEmail(email).c_str())); | |
190 } | |
191 | |
192 void FakeGaia::Initialize() { | 166 void FakeGaia::Initialize() { |
193 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); | 167 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); |
194 // Handles /MergeSession GAIA call. | 168 // Handles /MergeSession GAIA call. |
195 REGISTER_RESPONSE_HANDLER( | 169 REGISTER_RESPONSE_HANDLER( |
196 gaia_urls->merge_session_url(), HandleMergeSession); | 170 gaia_urls->merge_session_url(), HandleMergeSession); |
197 | 171 |
198 // Handles /o/oauth2/programmatic_auth GAIA call. | 172 // Handles /o/oauth2/programmatic_auth GAIA call. |
199 REGISTER_RESPONSE_HANDLER( | 173 REGISTER_RESPONSE_HANDLER( |
200 gaia_urls->client_login_to_oauth2_url(), HandleProgramaticAuth); | 174 gaia_urls->client_login_to_oauth2_url(), HandleProgramaticAuth); |
201 | 175 |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 | 408 |
435 void FakeGaia::HandleServiceLoginAuth(const HttpRequest& request, | 409 void FakeGaia::HandleServiceLoginAuth(const HttpRequest& request, |
436 BasicHttpResponse* http_response) { | 410 BasicHttpResponse* http_response) { |
437 std::string continue_url = | 411 std::string continue_url = |
438 GaiaUrls::GetInstance()->service_login_url().spec(); | 412 GaiaUrls::GetInstance()->service_login_url().spec(); |
439 GetQueryParameter(request.content, "continue", &continue_url); | 413 GetQueryParameter(request.content, "continue", &continue_url); |
440 | 414 |
441 std::string redirect_url = continue_url; | 415 std::string redirect_url = continue_url; |
442 | 416 |
443 std::string email; | 417 std::string email; |
444 const bool is_saml = | 418 if (GetQueryParameter(request.content, "Email", &email) && |
445 GetQueryParameter(request.content, "Email", &email) && | 419 saml_account_idp_map_.find(email) != saml_account_idp_map_.end()) { |
446 saml_account_idp_map_.find(email) != saml_account_idp_map_.end(); | |
447 | |
448 if (is_saml) { | |
449 GURL url(saml_account_idp_map_[email]); | 420 GURL url(saml_account_idp_map_[email]); |
450 url = net::AppendQueryParameter(url, "SAMLRequest", "fake_request"); | 421 url = net::AppendQueryParameter(url, "SAMLRequest", "fake_request"); |
451 url = net::AppendQueryParameter(url, "RelayState", continue_url); | 422 url = net::AppendQueryParameter(url, "RelayState", continue_url); |
452 redirect_url = url.spec(); | 423 redirect_url = url.spec(); |
453 http_response->AddCustomHeader("Google-Accounts-SAML", "Start"); | 424 http_response->AddCustomHeader("Google-Accounts-SAML", "Start"); |
454 } else if (!merge_session_params_.auth_sid_cookie.empty() && | 425 } else if (!merge_session_params_.auth_sid_cookie.empty() && |
455 !merge_session_params_.auth_lsid_cookie.empty()) { | 426 !merge_session_params_.auth_lsid_cookie.empty()) { |
456 SetCookies(http_response, | 427 SetCookies(http_response, |
457 merge_session_params_.auth_sid_cookie, | 428 merge_session_params_.auth_sid_cookie, |
458 merge_session_params_.auth_lsid_cookie); | 429 merge_session_params_.auth_lsid_cookie); |
459 } | 430 } |
460 | 431 |
461 http_response->set_code(net::HTTP_TEMPORARY_REDIRECT); | 432 http_response->set_code(net::HTTP_TEMPORARY_REDIRECT); |
462 http_response->AddCustomHeader("Location", redirect_url); | 433 http_response->AddCustomHeader("Location", redirect_url); |
463 | 434 http_response->AddCustomHeader("google-accounts-signin", |
464 // SAML sign-ins complete in HandleSSO(). | 435 base::StringPrintf("email=\"%s\", sessionindex=0", email.c_str())); |
465 if (is_saml) | |
466 return; | |
467 | |
468 AddGoogleAccountsSigninHeader(http_response, email); | |
469 } | 436 } |
470 | 437 |
471 void FakeGaia::HandleSSO(const HttpRequest& request, | 438 void FakeGaia::HandleSSO(const HttpRequest& request, |
472 BasicHttpResponse* http_response) { | 439 BasicHttpResponse* http_response) { |
473 if (!merge_session_params_.auth_sid_cookie.empty() && | 440 if (!merge_session_params_.auth_sid_cookie.empty() && |
474 !merge_session_params_.auth_lsid_cookie.empty()) { | 441 !merge_session_params_.auth_lsid_cookie.empty()) { |
475 SetCookies(http_response, | 442 SetCookies(http_response, |
476 merge_session_params_.auth_sid_cookie, | 443 merge_session_params_.auth_sid_cookie, |
477 merge_session_params_.auth_lsid_cookie); | 444 merge_session_params_.auth_lsid_cookie); |
478 } | 445 } |
479 std::string relay_state; | 446 std::string relay_state; |
480 GetQueryParameter(request.content, "RelayState", &relay_state); | 447 GetQueryParameter(request.content, "RelayState", &relay_state); |
481 std::string redirect_url = relay_state; | 448 std::string redirect_url = relay_state; |
482 http_response->set_code(net::HTTP_TEMPORARY_REDIRECT); | 449 http_response->set_code(net::HTTP_TEMPORARY_REDIRECT); |
483 http_response->AddCustomHeader("Location", redirect_url); | 450 http_response->AddCustomHeader("Location", redirect_url); |
484 http_response->AddCustomHeader("Google-Accounts-SAML", "End"); | 451 http_response->AddCustomHeader("Google-Accounts-SAML", "End"); |
485 | |
486 if (!merge_session_params_.email.empty()) | |
487 AddGoogleAccountsSigninHeader(http_response, merge_session_params_.email); | |
488 } | 452 } |
489 | 453 |
490 void FakeGaia::HandleAuthToken(const HttpRequest& request, | 454 void FakeGaia::HandleAuthToken(const HttpRequest& request, |
491 BasicHttpResponse* http_response) { | 455 BasicHttpResponse* http_response) { |
492 std::string scope; | 456 std::string scope; |
493 GetQueryParameter(request.content, "scope", &scope); | 457 GetQueryParameter(request.content, "scope", &scope); |
494 | 458 |
495 std::string grant_type; | 459 std::string grant_type; |
496 if (!GetQueryParameter(request.content, "grant_type", &grant_type)) { | 460 if (!GetQueryParameter(request.content, "grant_type", &grant_type)) { |
497 http_response->set_code(net::HTTP_BAD_REQUEST); | 461 http_response->set_code(net::HTTP_BAD_REQUEST); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
609 } | 573 } |
610 | 574 |
611 void FakeGaia::HandleGetUserInfo(const HttpRequest& request, | 575 void FakeGaia::HandleGetUserInfo(const HttpRequest& request, |
612 BasicHttpResponse* http_response) { | 576 BasicHttpResponse* http_response) { |
613 http_response->set_content(base::StringPrintf( | 577 http_response->set_content(base::StringPrintf( |
614 "email=%s\ndisplayEmail=%s", | 578 "email=%s\ndisplayEmail=%s", |
615 merge_session_params_.email.c_str(), | 579 merge_session_params_.email.c_str(), |
616 merge_session_params_.email.c_str())); | 580 merge_session_params_.email.c_str())); |
617 http_response->set_code(net::HTTP_OK); | 581 http_response->set_code(net::HTTP_OK); |
618 } | 582 } |
619 | |
OLD | NEW |