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 23 matching lines...) Expand all Loading... |
34 url.path(), base::Bind(&FakeGaia::method, base::Unretained(this)))) | 34 url.path(), base::Bind(&FakeGaia::method, base::Unretained(this)))) |
35 | 35 |
36 #define REGISTER_PATH_RESPONSE_HANDLER(path, method) \ | 36 #define REGISTER_PATH_RESPONSE_HANDLER(path, method) \ |
37 request_handlers_.insert(std::make_pair( \ | 37 request_handlers_.insert(std::make_pair( \ |
38 path, base::Bind(&FakeGaia::method, base::Unretained(this)))) | 38 path, base::Bind(&FakeGaia::method, base::Unretained(this)))) |
39 | 39 |
40 using namespace net::test_server; | 40 using namespace net::test_server; |
41 | 41 |
42 namespace { | 42 namespace { |
43 | 43 |
| 44 const char kTestAuthSIDCookie[] = "fake-auth-SID-cookie"; |
| 45 const char kTestAuthLSIDCookie[] = "fake-auth-LSID-cookie"; |
| 46 const char kTestAuthCode[] = "fake-auth-code"; |
| 47 const char kTestGaiaUberToken[] = "fake-uber-token"; |
| 48 const char kTestAuthLoginAccessToken[] = "fake-access-token"; |
| 49 const char kTestRefreshToken[] = "fake-refresh-token"; |
| 50 const char kTestSessionSIDCookie[] = "fake-session-SID-cookie"; |
| 51 const char kTestSessionLSIDCookie[] = "fake-session-LSID-cookie"; |
| 52 const char kTestOAuthLoginSID[] = "fake-oauth-SID-cookie"; |
| 53 const char kTestOAuthLoginLSID[] = "fake-oauth-LSID-cookie"; |
| 54 const char kTestOAuthLoginAuthCode[] = "fake-oauth-auth-code"; |
| 55 |
44 const base::FilePath::CharType kServiceLogin[] = | 56 const base::FilePath::CharType kServiceLogin[] = |
45 FILE_PATH_LITERAL("google_apis/test/service_login.html"); | 57 FILE_PATH_LITERAL("google_apis/test/service_login.html"); |
46 | 58 |
47 // OAuth2 Authentication header value prefix. | 59 // OAuth2 Authentication header value prefix. |
48 const char kAuthHeaderBearer[] = "Bearer "; | 60 const char kAuthHeaderBearer[] = "Bearer "; |
49 const char kAuthHeaderOAuth[] = "OAuth "; | 61 const char kAuthHeaderOAuth[] = "OAuth "; |
50 | 62 |
51 const char kListAccountsResponseFormat[] = | 63 const char kListAccountsResponseFormat[] = |
52 "[\"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]]]"; |
53 | 65 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 FakeGaia::FakeGaia() { | 136 FakeGaia::FakeGaia() { |
125 base::FilePath source_root_dir; | 137 base::FilePath source_root_dir; |
126 PathService::Get(base::DIR_SOURCE_ROOT, &source_root_dir); | 138 PathService::Get(base::DIR_SOURCE_ROOT, &source_root_dir); |
127 CHECK(base::ReadFileToString( | 139 CHECK(base::ReadFileToString( |
128 source_root_dir.Append(base::FilePath(kServiceLogin)), | 140 source_root_dir.Append(base::FilePath(kServiceLogin)), |
129 &service_login_response_)); | 141 &service_login_response_)); |
130 } | 142 } |
131 | 143 |
132 FakeGaia::~FakeGaia() {} | 144 FakeGaia::~FakeGaia() {} |
133 | 145 |
| 146 void FakeGaia::SetFakeMergeSessionParamsForEmail(const std::string& email) { |
| 147 FakeGaia::MergeSessionParams params; |
| 148 params.auth_sid_cookie = kTestAuthSIDCookie; |
| 149 params.auth_lsid_cookie = kTestAuthLSIDCookie; |
| 150 params.auth_code = kTestAuthCode; |
| 151 params.refresh_token = kTestRefreshToken; |
| 152 params.access_token = kTestAuthLoginAccessToken; |
| 153 params.gaia_uber_token = kTestGaiaUberToken; |
| 154 params.session_sid_cookie = kTestSessionSIDCookie; |
| 155 params.session_lsid_cookie = kTestSessionLSIDCookie; |
| 156 params.email = email; |
| 157 SetMergeSessionParams(params); |
| 158 } |
| 159 |
134 void FakeGaia::SetMergeSessionParams( | 160 void FakeGaia::SetMergeSessionParams( |
135 const MergeSessionParams& params) { | 161 const MergeSessionParams& params) { |
136 merge_session_params_ = params; | 162 merge_session_params_ = params; |
137 } | 163 } |
138 | 164 |
139 void FakeGaia::Initialize() { | 165 void FakeGaia::Initialize() { |
140 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); | 166 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); |
141 // Handles /MergeSession GAIA call. | 167 // Handles /MergeSession GAIA call. |
142 REGISTER_RESPONSE_HANDLER( | 168 REGISTER_RESPONSE_HANDLER( |
143 gaia_urls->merge_session_url(), HandleMergeSession); | 169 gaia_urls->merge_session_url(), HandleMergeSession); |
(...skipping 25 matching lines...) Expand all Loading... |
169 REGISTER_RESPONSE_HANDLER( | 195 REGISTER_RESPONSE_HANDLER( |
170 gaia_urls->oauth2_token_info_url(), HandleTokenInfo); | 196 gaia_urls->oauth2_token_info_url(), HandleTokenInfo); |
171 | 197 |
172 // Handles /oauth2/v2/IssueToken GAIA call. | 198 // Handles /oauth2/v2/IssueToken GAIA call. |
173 REGISTER_RESPONSE_HANDLER( | 199 REGISTER_RESPONSE_HANDLER( |
174 gaia_urls->oauth2_issue_token_url(), HandleIssueToken); | 200 gaia_urls->oauth2_issue_token_url(), HandleIssueToken); |
175 | 201 |
176 // Handles /ListAccounts GAIA call. | 202 // Handles /ListAccounts GAIA call. |
177 REGISTER_RESPONSE_HANDLER( | 203 REGISTER_RESPONSE_HANDLER( |
178 gaia_urls->list_accounts_url(), HandleListAccounts); | 204 gaia_urls->list_accounts_url(), HandleListAccounts); |
| 205 |
| 206 // Handles /GetUserInfo GAIA call. |
| 207 REGISTER_RESPONSE_HANDLER( |
| 208 gaia_urls->get_user_info_url(), HandleGetUserInfo); |
179 } | 209 } |
180 | 210 |
181 scoped_ptr<HttpResponse> FakeGaia::HandleRequest(const HttpRequest& request) { | 211 scoped_ptr<HttpResponse> FakeGaia::HandleRequest(const HttpRequest& request) { |
182 // The scheme and host of the URL is actually not important but required to | 212 // The scheme and host of the URL is actually not important but required to |
183 // get a valid GURL in order to parse |request.relative_url|. | 213 // get a valid GURL in order to parse |request.relative_url|. |
184 GURL request_url = GURL("http://localhost").Resolve(request.relative_url); | 214 GURL request_url = GURL("http://localhost").Resolve(request.relative_url); |
185 std::string request_path = request_url.path(); | 215 std::string request_path = request_url.path(); |
186 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse()); | 216 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse()); |
187 RequestHandlerMap::iterator iter = request_handlers_.find(request_path); | 217 RequestHandlerMap::iterator iter = request_handlers_.find(request_path); |
188 if (iter != request_handlers_.end()) { | 218 if (iter != request_handlers_.end()) { |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 | 368 |
339 void FakeGaia::HandleOAuthLogin(const HttpRequest& request, | 369 void FakeGaia::HandleOAuthLogin(const HttpRequest& request, |
340 BasicHttpResponse* http_response) { | 370 BasicHttpResponse* http_response) { |
341 http_response->set_code(net::HTTP_UNAUTHORIZED); | 371 http_response->set_code(net::HTTP_UNAUTHORIZED); |
342 if (merge_session_params_.gaia_uber_token.empty()) { | 372 if (merge_session_params_.gaia_uber_token.empty()) { |
343 http_response->set_code(net::HTTP_FORBIDDEN); | 373 http_response->set_code(net::HTTP_FORBIDDEN); |
344 return; | 374 return; |
345 } | 375 } |
346 | 376 |
347 std::string access_token; | 377 std::string access_token; |
348 if (!GetAccessToken(request, kAuthHeaderOAuth, &access_token)) { | 378 if (!GetAccessToken(request, kAuthHeaderBearer, &access_token)) { |
349 LOG(ERROR) << "/OAuthLogin missing access token in the header"; | 379 LOG(ERROR) << "/OAuthLogin missing access token in the header"; |
350 return; | 380 return; |
351 } | 381 } |
352 | 382 |
353 GURL request_url = GURL("http://localhost").Resolve(request.relative_url); | 383 GURL request_url = GURL("http://localhost").Resolve(request.relative_url); |
354 std::string request_query = request_url.query(); | 384 std::string request_query = request_url.query(); |
355 | 385 |
356 std::string source; | 386 std::string source; |
357 if (!GetQueryParameter(request_query, "source", &source)) { | 387 if (!GetQueryParameter(request_query, "source", &source) && |
| 388 !GetQueryParameter(request.content, "source", &source)) { |
358 LOG(ERROR) << "Missing 'source' param in /OAuthLogin call"; | 389 LOG(ERROR) << "Missing 'source' param in /OAuthLogin call"; |
359 return; | 390 return; |
360 } | 391 } |
361 | 392 |
362 std::string issue_uberauth; | 393 std::string issue_uberauth; |
363 if (GetQueryParameter(request_query, "issueuberauth", &issue_uberauth) && | 394 if (GetQueryParameter(request_query, "issueuberauth", &issue_uberauth) && |
364 issue_uberauth == "1") { | 395 issue_uberauth == "1") { |
365 http_response->set_content(merge_session_params_.gaia_uber_token); | 396 http_response->set_content(merge_session_params_.gaia_uber_token); |
366 http_response->set_code(net::HTTP_OK); | 397 http_response->set_code(net::HTTP_OK); |
367 // Issue GAIA uber token. | 398 // Issue GAIA uber token. |
368 } else { | 399 } else { |
369 LOG(FATAL) << "/OAuthLogin for SID/LSID is not supported"; | 400 http_response->set_content(base::StringPrintf( |
| 401 "SID=%s\nLSID=%s\nAuth=%s", |
| 402 kTestOAuthLoginSID, kTestOAuthLoginLSID, kTestOAuthLoginAuthCode)); |
| 403 http_response->set_code(net::HTTP_OK); |
370 } | 404 } |
371 } | 405 } |
372 | 406 |
373 void FakeGaia::HandleServiceLoginAuth(const HttpRequest& request, | 407 void FakeGaia::HandleServiceLoginAuth(const HttpRequest& request, |
374 BasicHttpResponse* http_response) { | 408 BasicHttpResponse* http_response) { |
375 std::string continue_url = | 409 std::string continue_url = |
376 GaiaUrls::GetInstance()->service_login_url().spec(); | 410 GaiaUrls::GetInstance()->service_login_url().spec(); |
377 GetQueryParameter(request.content, "continue", &continue_url); | 411 GetQueryParameter(request.content, "continue", &continue_url); |
378 | 412 |
379 std::string redirect_url = continue_url; | 413 std::string redirect_url = continue_url; |
380 | 414 |
381 std::string email; | 415 std::string email; |
382 if (GetQueryParameter(request.content, "Email", &email) && | 416 if (GetQueryParameter(request.content, "Email", &email) && |
383 saml_account_idp_map_.find(email) != saml_account_idp_map_.end()) { | 417 saml_account_idp_map_.find(email) != saml_account_idp_map_.end()) { |
384 GURL url(saml_account_idp_map_[email]); | 418 GURL url(saml_account_idp_map_[email]); |
385 url = net::AppendQueryParameter(url, "SAMLRequest", "fake_request"); | 419 url = net::AppendQueryParameter(url, "SAMLRequest", "fake_request"); |
386 url = net::AppendQueryParameter(url, "RelayState", continue_url); | 420 url = net::AppendQueryParameter(url, "RelayState", continue_url); |
387 redirect_url = url.spec(); | 421 redirect_url = url.spec(); |
388 } else if (!merge_session_params_.auth_sid_cookie.empty() && | 422 } else if (!merge_session_params_.auth_sid_cookie.empty() && |
389 !merge_session_params_.auth_lsid_cookie.empty()) { | 423 !merge_session_params_.auth_lsid_cookie.empty()) { |
390 SetCookies(http_response, | 424 SetCookies(http_response, |
391 merge_session_params_.auth_sid_cookie, | 425 merge_session_params_.auth_sid_cookie, |
392 merge_session_params_.auth_lsid_cookie); | 426 merge_session_params_.auth_lsid_cookie); |
393 } | 427 } |
394 | 428 |
395 http_response->set_code(net::HTTP_TEMPORARY_REDIRECT); | 429 http_response->set_code(net::HTTP_TEMPORARY_REDIRECT); |
396 http_response->AddCustomHeader("Location", redirect_url); | 430 http_response->AddCustomHeader("Location", redirect_url); |
| 431 http_response->AddCustomHeader("google-accounts-signin", |
| 432 base::StringPrintf("email=\"%s\", sessionindex=0", email.c_str())); |
397 } | 433 } |
398 | 434 |
399 void FakeGaia::HandleSSO(const HttpRequest& request, | 435 void FakeGaia::HandleSSO(const HttpRequest& request, |
400 BasicHttpResponse* http_response) { | 436 BasicHttpResponse* http_response) { |
401 if (!merge_session_params_.auth_sid_cookie.empty() && | 437 if (!merge_session_params_.auth_sid_cookie.empty() && |
402 !merge_session_params_.auth_lsid_cookie.empty()) { | 438 !merge_session_params_.auth_lsid_cookie.empty()) { |
403 SetCookies(http_response, | 439 SetCookies(http_response, |
404 merge_session_params_.auth_sid_cookie, | 440 merge_session_params_.auth_sid_cookie, |
405 merge_session_params_.auth_lsid_cookie); | 441 merge_session_params_.auth_lsid_cookie); |
406 } | 442 } |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 base::IntToString(token_info->expires_in)); | 555 base::IntToString(token_info->expires_in)); |
520 response_dict.SetString("token", token_info->token); | 556 response_dict.SetString("token", token_info->token); |
521 FormatJSONResponse(response_dict, http_response); | 557 FormatJSONResponse(response_dict, http_response); |
522 return; | 558 return; |
523 } | 559 } |
524 } | 560 } |
525 http_response->set_code(net::HTTP_BAD_REQUEST); | 561 http_response->set_code(net::HTTP_BAD_REQUEST); |
526 } | 562 } |
527 | 563 |
528 void FakeGaia::HandleListAccounts(const HttpRequest& request, | 564 void FakeGaia::HandleListAccounts(const HttpRequest& request, |
529 BasicHttpResponse* http_response) { | 565 BasicHttpResponse* http_response) { |
530 http_response->set_content(base::StringPrintf( | 566 http_response->set_content(base::StringPrintf( |
531 kListAccountsResponseFormat, merge_session_params_.email.c_str())); | 567 kListAccountsResponseFormat, merge_session_params_.email.c_str())); |
532 http_response->set_code(net::HTTP_OK); | 568 http_response->set_code(net::HTTP_OK); |
533 } | 569 } |
| 570 |
| 571 void FakeGaia::HandleGetUserInfo(const HttpRequest& request, |
| 572 BasicHttpResponse* http_response) { |
| 573 http_response->set_content(base::StringPrintf( |
| 574 "email=%s\ndisplayEmail=%s", |
| 575 merge_session_params_.email.c_str(), |
| 576 merge_session_params_.email.c_str())); |
| 577 http_response->set_code(net::HTTP_OK); |
| 578 } |
OLD | NEW |