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