| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/gaia_auth_fetcher.h" | 5 #include "google_apis/gaia/gaia_auth_fetcher.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 client_login_gurl_(GaiaUrls::GetInstance()->client_login_url()), | 179 client_login_gurl_(GaiaUrls::GetInstance()->client_login_url()), |
| 180 issue_auth_token_gurl_(GaiaUrls::GetInstance()->issue_auth_token_url()), | 180 issue_auth_token_gurl_(GaiaUrls::GetInstance()->issue_auth_token_url()), |
| 181 oauth2_token_gurl_(GaiaUrls::GetInstance()->oauth2_token_url()), | 181 oauth2_token_gurl_(GaiaUrls::GetInstance()->oauth2_token_url()), |
| 182 oauth2_revoke_gurl_(GaiaUrls::GetInstance()->oauth2_revoke_url()), | 182 oauth2_revoke_gurl_(GaiaUrls::GetInstance()->oauth2_revoke_url()), |
| 183 get_user_info_gurl_(GaiaUrls::GetInstance()->get_user_info_url()), | 183 get_user_info_gurl_(GaiaUrls::GetInstance()->get_user_info_url()), |
| 184 merge_session_gurl_(GaiaUrls::GetInstance()->merge_session_url()), | 184 merge_session_gurl_(GaiaUrls::GetInstance()->merge_session_url()), |
| 185 uberauth_token_gurl_(GaiaUrls::GetInstance()->oauth1_login_url().Resolve( | 185 uberauth_token_gurl_(GaiaUrls::GetInstance()->oauth1_login_url().Resolve( |
| 186 base::StringPrintf(kUberAuthTokenURLFormat, source.c_str()))), | 186 base::StringPrintf(kUberAuthTokenURLFormat, source.c_str()))), |
| 187 oauth_login_gurl_(GaiaUrls::GetInstance()->oauth1_login_url()), | 187 oauth_login_gurl_(GaiaUrls::GetInstance()->oauth1_login_url()), |
| 188 list_accounts_gurl_(GaiaUrls::GetInstance()->list_accounts_url()), | 188 list_accounts_gurl_(GaiaUrls::GetInstance()->list_accounts_url()), |
| 189 get_check_connection_info_url_( |
| 190 GaiaUrls::GetInstance()->get_check_connection_info_url()), |
| 189 client_login_to_oauth2_gurl_( | 191 client_login_to_oauth2_gurl_( |
| 190 GaiaUrls::GetInstance()->client_login_to_oauth2_url()), | 192 GaiaUrls::GetInstance()->client_login_to_oauth2_url()), |
| 191 fetch_pending_(false) {} | 193 fetch_pending_(false) {} |
| 192 | 194 |
| 193 GaiaAuthFetcher::~GaiaAuthFetcher() {} | 195 GaiaAuthFetcher::~GaiaAuthFetcher() {} |
| 194 | 196 |
| 195 bool GaiaAuthFetcher::HasPendingFetch() { | 197 bool GaiaAuthFetcher::HasPendingFetch() { |
| 196 return fetch_pending_; | 198 return fetch_pending_; |
| 197 } | 199 } |
| 198 | 200 |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 | 344 |
| 343 // static | 345 // static |
| 344 std::string GaiaAuthFetcher::MakeGetUserInfoBody(const std::string& lsid) { | 346 std::string GaiaAuthFetcher::MakeGetUserInfoBody(const std::string& lsid) { |
| 345 std::string encoded_lsid = net::EscapeUrlEncodedData(lsid, true); | 347 std::string encoded_lsid = net::EscapeUrlEncodedData(lsid, true); |
| 346 return base::StringPrintf(kGetUserInfoFormat, encoded_lsid.c_str()); | 348 return base::StringPrintf(kGetUserInfoFormat, encoded_lsid.c_str()); |
| 347 } | 349 } |
| 348 | 350 |
| 349 // static | 351 // static |
| 350 std::string GaiaAuthFetcher::MakeMergeSessionBody( | 352 std::string GaiaAuthFetcher::MakeMergeSessionBody( |
| 351 const std::string& auth_token, | 353 const std::string& auth_token, |
| 354 const std::string& external_cc_result, |
| 352 const std::string& continue_url, | 355 const std::string& continue_url, |
| 353 const std::string& source) { | 356 const std::string& source) { |
| 354 std::string encoded_auth_token = net::EscapeUrlEncodedData(auth_token, true); | 357 std::string encoded_auth_token = net::EscapeUrlEncodedData(auth_token, true); |
| 355 std::string encoded_continue_url = net::EscapeUrlEncodedData(continue_url, | 358 std::string encoded_continue_url = net::EscapeUrlEncodedData(continue_url, |
| 356 true); | 359 true); |
| 357 std::string encoded_source = net::EscapeUrlEncodedData(source, true); | 360 std::string encoded_source = net::EscapeUrlEncodedData(source, true); |
| 358 return base::StringPrintf(kMergeSessionFormat, | 361 std::string result = base::StringPrintf(kMergeSessionFormat, |
| 359 encoded_auth_token.c_str(), | 362 encoded_auth_token.c_str(), |
| 360 encoded_continue_url.c_str(), | 363 encoded_continue_url.c_str(), |
| 361 encoded_source.c_str()); | 364 encoded_source.c_str()); |
| 365 if (!external_cc_result.empty()) { |
| 366 base::StringAppendF(&result, "&externalCcResult=%s", |
| 367 net::EscapeUrlEncodedData( |
| 368 external_cc_result, true).c_str()); |
| 369 } |
| 370 |
| 371 return result; |
| 362 } | 372 } |
| 363 | 373 |
| 364 // static | 374 // static |
| 365 std::string GaiaAuthFetcher::MakeGetAuthCodeHeader( | 375 std::string GaiaAuthFetcher::MakeGetAuthCodeHeader( |
| 366 const std::string& auth_token) { | 376 const std::string& auth_token) { |
| 367 return base::StringPrintf(kAuthHeaderFormat, auth_token.c_str()); | 377 return base::StringPrintf(kAuthHeaderFormat, auth_token.c_str()); |
| 368 } | 378 } |
| 369 | 379 |
| 370 // Helper method that extracts tokens from a successful reply. | 380 // Helper method that extracts tokens from a successful reply. |
| 371 // static | 381 // static |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 615 fetcher_.reset(CreateGaiaFetcher(getter_, | 625 fetcher_.reset(CreateGaiaFetcher(getter_, |
| 616 request_body_, | 626 request_body_, |
| 617 std::string(), | 627 std::string(), |
| 618 get_user_info_gurl_, | 628 get_user_info_gurl_, |
| 619 kLoadFlagsIgnoreCookies, | 629 kLoadFlagsIgnoreCookies, |
| 620 this)); | 630 this)); |
| 621 fetch_pending_ = true; | 631 fetch_pending_ = true; |
| 622 fetcher_->Start(); | 632 fetcher_->Start(); |
| 623 } | 633 } |
| 624 | 634 |
| 625 void GaiaAuthFetcher::StartMergeSession(const std::string& uber_token) { | 635 void GaiaAuthFetcher::StartMergeSession(const std::string& uber_token, |
| 636 const std::string& external_cc_result) { |
| 626 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; | 637 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; |
| 627 | 638 |
| 628 DVLOG(1) << "Starting MergeSession with uber_token=" << uber_token; | 639 DVLOG(1) << "Starting MergeSession with uber_token=" << uber_token; |
| 629 | 640 |
| 630 // The continue URL is a required parameter of the MergeSession API, but in | 641 // The continue URL is a required parameter of the MergeSession API, but in |
| 631 // this case we don't actually need or want to navigate to it. Setting it to | 642 // this case we don't actually need or want to navigate to it. Setting it to |
| 632 // an arbitrary Google URL. | 643 // an arbitrary Google URL. |
| 633 // | 644 // |
| 634 // In order for the new session to be merged correctly, the server needs to | 645 // In order for the new session to be merged correctly, the server needs to |
| 635 // know what sessions already exist in the browser. The fetcher needs to be | 646 // know what sessions already exist in the browser. The fetcher needs to be |
| 636 // created such that it sends the cookies with the request, which is | 647 // created such that it sends the cookies with the request, which is |
| 637 // different from all other requests the fetcher can make. | 648 // different from all other requests the fetcher can make. |
| 638 std::string continue_url("http://www.google.com"); | 649 std::string continue_url("http://www.google.com"); |
| 639 request_body_ = MakeMergeSessionBody(uber_token, continue_url, source_); | 650 request_body_ = MakeMergeSessionBody(uber_token, external_cc_result, |
| 651 continue_url, source_); |
| 640 fetcher_.reset(CreateGaiaFetcher(getter_, | 652 fetcher_.reset(CreateGaiaFetcher(getter_, |
| 641 request_body_, | 653 request_body_, |
| 642 std::string(), | 654 std::string(), |
| 643 merge_session_gurl_, | 655 merge_session_gurl_, |
| 644 net::LOAD_NORMAL, | 656 net::LOAD_NORMAL, |
| 645 this)); | 657 this)); |
| 646 fetch_pending_ = true; | 658 fetch_pending_ = true; |
| 647 fetcher_->Start(); | 659 fetcher_->Start(); |
| 648 } | 660 } |
| 649 | 661 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 688 fetcher_.reset(CreateGaiaFetcher(getter_, | 700 fetcher_.reset(CreateGaiaFetcher(getter_, |
| 689 " ", // To force an HTTP POST. | 701 " ", // To force an HTTP POST. |
| 690 "Origin: https://www.google.com", | 702 "Origin: https://www.google.com", |
| 691 list_accounts_gurl_, | 703 list_accounts_gurl_, |
| 692 net::LOAD_NORMAL, | 704 net::LOAD_NORMAL, |
| 693 this)); | 705 this)); |
| 694 fetch_pending_ = true; | 706 fetch_pending_ = true; |
| 695 fetcher_->Start(); | 707 fetcher_->Start(); |
| 696 } | 708 } |
| 697 | 709 |
| 710 void GaiaAuthFetcher::StartGetCheckConnectionInfo() { |
| 711 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; |
| 712 |
| 713 fetcher_.reset(CreateGaiaFetcher(getter_, |
| 714 std::string(), |
| 715 std::string(), |
| 716 get_check_connection_info_url_, |
| 717 kLoadFlagsIgnoreCookies, |
| 718 this)); |
| 719 fetch_pending_ = true; |
| 720 fetcher_->Start(); |
| 721 } |
| 722 |
| 698 // static | 723 // static |
| 699 GoogleServiceAuthError GaiaAuthFetcher::GenerateAuthError( | 724 GoogleServiceAuthError GaiaAuthFetcher::GenerateAuthError( |
| 700 const std::string& data, | 725 const std::string& data, |
| 701 const net::URLRequestStatus& status) { | 726 const net::URLRequestStatus& status) { |
| 702 if (!status.is_success()) { | 727 if (!status.is_success()) { |
| 703 if (status.status() == net::URLRequestStatus::CANCELED) { | 728 if (status.status() == net::URLRequestStatus::CANCELED) { |
| 704 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); | 729 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); |
| 705 } | 730 } |
| 706 DLOG(WARNING) << "Could not reach Google Accounts servers: errno " | 731 DLOG(WARNING) << "Could not reach Google Accounts servers: errno " |
| 707 << status.error(); | 732 << status.error(); |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 878 std::string lsid; | 903 std::string lsid; |
| 879 std::string token; | 904 std::string token; |
| 880 ParseClientLoginResponse(data, &sid, &lsid, &token); | 905 ParseClientLoginResponse(data, &sid, &lsid, &token); |
| 881 consumer_->OnClientLoginSuccess( | 906 consumer_->OnClientLoginSuccess( |
| 882 GaiaAuthConsumer::ClientLoginResult(sid, lsid, token, data)); | 907 GaiaAuthConsumer::ClientLoginResult(sid, lsid, token, data)); |
| 883 } else { | 908 } else { |
| 884 consumer_->OnClientLoginFailure(GenerateAuthError(data, status)); | 909 consumer_->OnClientLoginFailure(GenerateAuthError(data, status)); |
| 885 } | 910 } |
| 886 } | 911 } |
| 887 | 912 |
| 913 void GaiaAuthFetcher::OnGetCheckConnectionInfoFetched( |
| 914 const std::string& data, |
| 915 const net::URLRequestStatus& status, |
| 916 int response_code) { |
| 917 if (status.is_success() && response_code == net::HTTP_OK) { |
| 918 consumer_->OnGetCheckConnectionInfoSuccess(data); |
| 919 } else { |
| 920 consumer_->OnGetCheckConnectionInfoError(GenerateAuthError(data, status)); |
| 921 } |
| 922 } |
| 923 |
| 888 void GaiaAuthFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | 924 void GaiaAuthFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
| 889 fetch_pending_ = false; | 925 fetch_pending_ = false; |
| 890 // Some of the GAIA requests perform redirects, which results in the final | 926 // Some of the GAIA requests perform redirects, which results in the final |
| 891 // URL of the fetcher not being the original URL requested. Therefore use | 927 // URL of the fetcher not being the original URL requested. Therefore use |
| 892 // the original URL when determining which OnXXX function to call. | 928 // the original URL when determining which OnXXX function to call. |
| 893 const GURL& url = source->GetOriginalURL(); | 929 const GURL& url = source->GetOriginalURL(); |
| 894 const net::URLRequestStatus& status = source->GetStatus(); | 930 const net::URLRequestStatus& status = source->GetStatus(); |
| 895 int response_code = source->GetResponseCode(); | 931 int response_code = source->GetResponseCode(); |
| 896 std::string data; | 932 std::string data; |
| 897 source->GetResponseAsString(&data); | 933 source->GetResponseAsString(&data); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 919 } else if (url == merge_session_gurl_) { | 955 } else if (url == merge_session_gurl_) { |
| 920 OnMergeSessionFetched(data, status, response_code); | 956 OnMergeSessionFetched(data, status, response_code); |
| 921 } else if (url == uberauth_token_gurl_) { | 957 } else if (url == uberauth_token_gurl_) { |
| 922 OnUberAuthTokenFetch(data, status, response_code); | 958 OnUberAuthTokenFetch(data, status, response_code); |
| 923 } else if (url == oauth_login_gurl_) { | 959 } else if (url == oauth_login_gurl_) { |
| 924 OnOAuthLoginFetched(data, status, response_code); | 960 OnOAuthLoginFetched(data, status, response_code); |
| 925 } else if (url == oauth2_revoke_gurl_) { | 961 } else if (url == oauth2_revoke_gurl_) { |
| 926 OnOAuth2RevokeTokenFetched(data, status, response_code); | 962 OnOAuth2RevokeTokenFetched(data, status, response_code); |
| 927 } else if (url == list_accounts_gurl_) { | 963 } else if (url == list_accounts_gurl_) { |
| 928 OnListAccountsFetched(data, status, response_code); | 964 OnListAccountsFetched(data, status, response_code); |
| 965 } else if (url == get_check_connection_info_url_) { |
| 966 OnGetCheckConnectionInfoFetched(data, status, response_code); |
| 929 } else { | 967 } else { |
| 930 NOTREACHED(); | 968 NOTREACHED(); |
| 931 } | 969 } |
| 932 } | 970 } |
| 933 | 971 |
| 934 // static | 972 // static |
| 935 bool GaiaAuthFetcher::IsSecondFactorSuccess( | 973 bool GaiaAuthFetcher::IsSecondFactorSuccess( |
| 936 const std::string& alleged_error) { | 974 const std::string& alleged_error) { |
| 937 return alleged_error.find(kSecondFactor) != | 975 return alleged_error.find(kSecondFactor) != |
| 938 std::string::npos; | 976 std::string::npos; |
| 939 } | 977 } |
| OLD | NEW |