| 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 "chrome/common/net/gaia/gaia_auth_fetcher.h" | 5 #include "chrome/common/net/gaia/gaia_auth_fetcher.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 // static | 73 // static |
| 74 const char GaiaAuthFetcher::kTokenAuthFormat[] = | 74 const char GaiaAuthFetcher::kTokenAuthFormat[] = |
| 75 "auth=%s&" | 75 "auth=%s&" |
| 76 "continue=%s&" | 76 "continue=%s&" |
| 77 "source=%s"; | 77 "source=%s"; |
| 78 // static | 78 // static |
| 79 const char GaiaAuthFetcher::kMergeSessionFormat[] = | 79 const char GaiaAuthFetcher::kMergeSessionFormat[] = |
| 80 "uberauth=%s&" | 80 "uberauth=%s&" |
| 81 "continue=%s&" | 81 "continue=%s&" |
| 82 "source=%s"; | 82 "source=%s"; |
| 83 // static |
| 84 const char GaiaAuthFetcher::kUberAuthTokenURLFormat[] = |
| 85 "%s?source=%s&" |
| 86 "issueuberauth=1"; |
| 83 | 87 |
| 84 // static | 88 // static |
| 85 const char GaiaAuthFetcher::kAccountDeletedError[] = "AccountDeleted"; | 89 const char GaiaAuthFetcher::kAccountDeletedError[] = "AccountDeleted"; |
| 86 const char GaiaAuthFetcher::kAccountDeletedErrorCode[] = "adel"; | 90 const char GaiaAuthFetcher::kAccountDeletedErrorCode[] = "adel"; |
| 87 // static | 91 // static |
| 88 const char GaiaAuthFetcher::kAccountDisabledError[] = "AccountDisabled"; | 92 const char GaiaAuthFetcher::kAccountDisabledError[] = "AccountDisabled"; |
| 89 const char GaiaAuthFetcher::kAccountDisabledErrorCode[] = "adis"; | 93 const char GaiaAuthFetcher::kAccountDisabledErrorCode[] = "adis"; |
| 90 // static | 94 // static |
| 91 const char GaiaAuthFetcher::kBadAuthenticationError[] = "BadAuthentication"; | 95 const char GaiaAuthFetcher::kBadAuthenticationError[] = "BadAuthentication"; |
| 92 const char GaiaAuthFetcher::kBadAuthenticationErrorCode[] = "badauth"; | 96 const char GaiaAuthFetcher::kBadAuthenticationErrorCode[] = "badauth"; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 117 const char GaiaAuthFetcher::kAccountTypeGoogle[] = | 121 const char GaiaAuthFetcher::kAccountTypeGoogle[] = |
| 118 "GOOGLE"; | 122 "GOOGLE"; |
| 119 | 123 |
| 120 // static | 124 // static |
| 121 const char GaiaAuthFetcher::kSecondFactor[] = "Info=InvalidSecondFactor"; | 125 const char GaiaAuthFetcher::kSecondFactor[] = "Info=InvalidSecondFactor"; |
| 122 | 126 |
| 123 // static | 127 // static |
| 124 const char GaiaAuthFetcher::kAuthHeaderFormat[] = | 128 const char GaiaAuthFetcher::kAuthHeaderFormat[] = |
| 125 "Authorization: GoogleLogin auth=%s"; | 129 "Authorization: GoogleLogin auth=%s"; |
| 126 // static | 130 // static |
| 131 const char GaiaAuthFetcher::kOAuthHeaderFormat[] = "Authorization: OAuth %s"; |
| 132 // static |
| 127 const char GaiaAuthFetcher::kClientLoginToOAuth2CookiePartSecure[] = "Secure"; | 133 const char GaiaAuthFetcher::kClientLoginToOAuth2CookiePartSecure[] = "Secure"; |
| 128 // static | 134 // static |
| 129 const char GaiaAuthFetcher::kClientLoginToOAuth2CookiePartHttpOnly[] = | 135 const char GaiaAuthFetcher::kClientLoginToOAuth2CookiePartHttpOnly[] = |
| 130 "HttpOnly"; | 136 "HttpOnly"; |
| 131 // static | 137 // static |
| 132 const char GaiaAuthFetcher::kClientLoginToOAuth2CookiePartCodePrefix[] = | 138 const char GaiaAuthFetcher::kClientLoginToOAuth2CookiePartCodePrefix[] = |
| 133 "oauth_code="; | 139 "oauth_code="; |
| 134 // static | 140 // static |
| 135 const int GaiaAuthFetcher::kClientLoginToOAuth2CookiePartCodePrefixLength = | 141 const int GaiaAuthFetcher::kClientLoginToOAuth2CookiePartCodePrefixLength = |
| 136 arraysize(GaiaAuthFetcher::kClientLoginToOAuth2CookiePartCodePrefix) - 1; | 142 arraysize(GaiaAuthFetcher::kClientLoginToOAuth2CookiePartCodePrefix) - 1; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 148 getter_(getter), | 154 getter_(getter), |
| 149 source_(source), | 155 source_(source), |
| 150 client_login_gurl_(GaiaUrls::GetInstance()->client_login_url()), | 156 client_login_gurl_(GaiaUrls::GetInstance()->client_login_url()), |
| 151 issue_auth_token_gurl_(GaiaUrls::GetInstance()->issue_auth_token_url()), | 157 issue_auth_token_gurl_(GaiaUrls::GetInstance()->issue_auth_token_url()), |
| 152 client_login_to_oauth2_gurl_( | 158 client_login_to_oauth2_gurl_( |
| 153 GaiaUrls::GetInstance()->client_login_to_oauth2_url()), | 159 GaiaUrls::GetInstance()->client_login_to_oauth2_url()), |
| 154 oauth2_token_gurl_(GaiaUrls::GetInstance()->oauth2_token_url()), | 160 oauth2_token_gurl_(GaiaUrls::GetInstance()->oauth2_token_url()), |
| 155 get_user_info_gurl_(GaiaUrls::GetInstance()->get_user_info_url()), | 161 get_user_info_gurl_(GaiaUrls::GetInstance()->get_user_info_url()), |
| 156 token_auth_gurl_(GaiaUrls::GetInstance()->token_auth_url()), | 162 token_auth_gurl_(GaiaUrls::GetInstance()->token_auth_url()), |
| 157 merge_session_gurl_(GaiaUrls::GetInstance()->merge_session_url()), | 163 merge_session_gurl_(GaiaUrls::GetInstance()->merge_session_url()), |
| 164 uberauth_token_gurl_(base::StringPrintf(kUberAuthTokenURLFormat, |
| 165 GaiaUrls::GetInstance()->oauth1_login_url().c_str(), source.c_str())), |
| 158 fetch_pending_(false) {} | 166 fetch_pending_(false) {} |
| 159 | 167 |
| 160 GaiaAuthFetcher::~GaiaAuthFetcher() {} | 168 GaiaAuthFetcher::~GaiaAuthFetcher() {} |
| 161 | 169 |
| 162 bool GaiaAuthFetcher::HasPendingFetch() { | 170 bool GaiaAuthFetcher::HasPendingFetch() { |
| 163 return fetch_pending_; | 171 return fetch_pending_; |
| 164 } | 172 } |
| 165 | 173 |
| 166 void GaiaAuthFetcher::CancelRequest() { | 174 void GaiaAuthFetcher::CancelRequest() { |
| 167 fetcher_.reset(); | 175 fetcher_.reset(); |
| 168 fetch_pending_ = false; | 176 fetch_pending_ = false; |
| 169 } | 177 } |
| 170 | 178 |
| 171 // static | 179 // static |
| 172 content::URLFetcher* GaiaAuthFetcher::CreateGaiaFetcher( | 180 content::URLFetcher* GaiaAuthFetcher::CreateGaiaFetcher( |
| 173 net::URLRequestContextGetter* getter, | 181 net::URLRequestContextGetter* getter, |
| 174 const std::string& body, | 182 const std::string& body, |
| 175 const std::string& headers, | 183 const std::string& headers, |
| 176 const GURL& gaia_gurl, | 184 const GURL& gaia_gurl, |
| 177 bool use_cookies, | 185 bool use_cookies, |
| 178 content::URLFetcherDelegate* delegate) { | 186 content::URLFetcherDelegate* delegate) { |
| 179 content::URLFetcher* to_return = content::URLFetcher::Create( | 187 content::URLFetcher* to_return = content::URLFetcher::Create( |
| 180 0, gaia_gurl, content::URLFetcher::POST, delegate); | 188 0, gaia_gurl, |
| 189 body == "" ? content::URLFetcher::GET : content::URLFetcher::POST, |
| 190 delegate); |
| 181 to_return->SetRequestContext(getter); | 191 to_return->SetRequestContext(getter); |
| 182 to_return->SetUploadData("application/x-www-form-urlencoded", body); | 192 to_return->SetUploadData("application/x-www-form-urlencoded", body); |
| 183 | 193 |
| 184 // The Gaia token exchange requests do not require any cookie-based | 194 // The Gaia token exchange requests do not require any cookie-based |
| 185 // identification as part of requests. We suppress sending any cookies to | 195 // identification as part of requests. We suppress sending any cookies to |
| 186 // maintain a separation between the user's browsing and Chrome's internal | 196 // maintain a separation between the user's browsing and Chrome's internal |
| 187 // services. Where such mixing is desired (MergeSession), it will be done | 197 // services. Where such mixing is desired (MergeSession), it will be done |
| 188 // explicitly. | 198 // explicitly. |
| 189 if (!use_cookies) { | 199 if (!use_cookies) { |
| 190 to_return->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 200 to_return->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| (...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 564 request_body_, | 574 request_body_, |
| 565 "", | 575 "", |
| 566 merge_session_gurl_, | 576 merge_session_gurl_, |
| 567 true, | 577 true, |
| 568 this)); | 578 this)); |
| 569 fetch_pending_ = true; | 579 fetch_pending_ = true; |
| 570 fetcher_->Start(); | 580 fetcher_->Start(); |
| 571 } | 581 } |
| 572 | 582 |
| 573 // static | 583 // static |
| 584 void GaiaAuthFetcher::StartUberAuthTokenFetch(const std::string& access_token) { |
| 585 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; |
| 586 |
| 587 DVLOG(1) << "Starting StartUberAuthTokenFetch with access_token=" |
| 588 << access_token; |
| 589 std::string authentication_header = |
| 590 base::StringPrintf(kOAuthHeaderFormat, access_token.c_str()); |
| 591 fetcher_.reset(CreateGaiaFetcher(getter_, |
| 592 "", |
| 593 authentication_header, |
| 594 uberauth_token_gurl_, |
| 595 false, |
| 596 this)); |
| 597 fetch_pending_ = true; |
| 598 fetcher_->Start(); |
| 599 } |
| 600 |
| 601 // static |
| 574 GoogleServiceAuthError GaiaAuthFetcher::GenerateAuthError( | 602 GoogleServiceAuthError GaiaAuthFetcher::GenerateAuthError( |
| 575 const std::string& data, | 603 const std::string& data, |
| 576 const net::URLRequestStatus& status) { | 604 const net::URLRequestStatus& status) { |
| 577 if (!status.is_success()) { | 605 if (!status.is_success()) { |
| 578 if (status.status() == net::URLRequestStatus::CANCELED) { | 606 if (status.status() == net::URLRequestStatus::CANCELED) { |
| 579 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); | 607 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); |
| 580 } else { | 608 } else { |
| 581 DLOG(WARNING) << "Could not reach Google Accounts servers: errno " | 609 DLOG(WARNING) << "Could not reach Google Accounts servers: errno " |
| 582 << status.error(); | 610 << status.error(); |
| 583 return GoogleServiceAuthError::FromConnectionError(status.error()); | 611 return GoogleServiceAuthError::FromConnectionError(status.error()); |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 790 void GaiaAuthFetcher::OnMergeSessionFetched(const std::string& data, | 818 void GaiaAuthFetcher::OnMergeSessionFetched(const std::string& data, |
| 791 const net::URLRequestStatus& status, | 819 const net::URLRequestStatus& status, |
| 792 int response_code) { | 820 int response_code) { |
| 793 if (status.is_success() && response_code == RC_REQUEST_OK) { | 821 if (status.is_success() && response_code == RC_REQUEST_OK) { |
| 794 consumer_->OnMergeSessionSuccess(data); | 822 consumer_->OnMergeSessionSuccess(data); |
| 795 } else { | 823 } else { |
| 796 consumer_->OnMergeSessionFailure(GenerateAuthError(data, status)); | 824 consumer_->OnMergeSessionFailure(GenerateAuthError(data, status)); |
| 797 } | 825 } |
| 798 } | 826 } |
| 799 | 827 |
| 828 void GaiaAuthFetcher::OnUberAuthTokenFetch(const std::string& data, |
| 829 const net::URLRequestStatus& status, |
| 830 int response_code) { |
| 831 if (status.is_success() && response_code == RC_REQUEST_OK) { |
| 832 consumer_->OnUberAuthTokenSuccess(data); |
| 833 } else { |
| 834 consumer_->OnUberAuthTokenFailure(GenerateAuthError(data, status)); |
| 835 } |
| 836 } |
| 837 |
| 800 void GaiaAuthFetcher::OnURLFetchComplete(const content::URLFetcher* source) { | 838 void GaiaAuthFetcher::OnURLFetchComplete(const content::URLFetcher* source) { |
| 801 fetch_pending_ = false; | 839 fetch_pending_ = false; |
| 802 const GURL& url = source->GetURL(); | 840 const GURL& url = source->GetURL(); |
| 803 const net::URLRequestStatus& status = source->GetStatus(); | 841 const net::URLRequestStatus& status = source->GetStatus(); |
| 804 int response_code = source->GetResponseCode(); | 842 int response_code = source->GetResponseCode(); |
| 805 std::string data; | 843 std::string data; |
| 806 source->GetResponseAsString(&data); | 844 source->GetResponseAsString(&data); |
| 807 if (url == client_login_gurl_) { | 845 if (url == client_login_gurl_) { |
| 808 OnClientLoginFetched(data, status, response_code); | 846 OnClientLoginFetched(data, status, response_code); |
| 809 } else if (url == issue_auth_token_gurl_) { | 847 } else if (url == issue_auth_token_gurl_) { |
| 810 OnIssueAuthTokenFetched(data, status, response_code); | 848 OnIssueAuthTokenFetched(data, status, response_code); |
| 811 } else if (url == client_login_to_oauth2_gurl_) { | 849 } else if (url == client_login_to_oauth2_gurl_) { |
| 812 OnClientLoginToOAuth2Fetched( | 850 OnClientLoginToOAuth2Fetched( |
| 813 data, source->GetCookies(), status, response_code); | 851 data, source->GetCookies(), status, response_code); |
| 814 } else if (url == oauth2_token_gurl_) { | 852 } else if (url == oauth2_token_gurl_) { |
| 815 OnOAuth2TokenPairFetched(data, status, response_code); | 853 OnOAuth2TokenPairFetched(data, status, response_code); |
| 816 } else if (url == get_user_info_gurl_) { | 854 } else if (url == get_user_info_gurl_) { |
| 817 OnGetUserInfoFetched(data, status, response_code); | 855 OnGetUserInfoFetched(data, status, response_code); |
| 818 } else if (url == token_auth_gurl_) { | 856 } else if (url == token_auth_gurl_) { |
| 819 OnTokenAuthFetched(data, status, response_code); | 857 OnTokenAuthFetched(data, status, response_code); |
| 820 } else if (url == merge_session_gurl_ || | 858 } else if (url == merge_session_gurl_ || |
| 821 (source && source->GetOriginalURL() == merge_session_gurl_)) { | 859 (source && source->GetOriginalURL() == merge_session_gurl_)) { |
| 822 // MergeSession may redirect, so check the original URL of the fetcher. | 860 // MergeSession may redirect, so check the original URL of the fetcher. |
| 823 OnMergeSessionFetched(data, status, response_code); | 861 OnMergeSessionFetched(data, status, response_code); |
| 862 } else if (url == uberauth_token_gurl_) { |
| 863 OnUberAuthTokenFetch(data, status, response_code); |
| 824 } else { | 864 } else { |
| 825 NOTREACHED(); | 865 NOTREACHED(); |
| 826 } | 866 } |
| 827 } | 867 } |
| 828 | 868 |
| 829 // static | 869 // static |
| 830 bool GaiaAuthFetcher::IsSecondFactorSuccess( | 870 bool GaiaAuthFetcher::IsSecondFactorSuccess( |
| 831 const std::string& alleged_error) { | 871 const std::string& alleged_error) { |
| 832 return alleged_error.find(kSecondFactor) != | 872 return alleged_error.find(kSecondFactor) != |
| 833 std::string::npos; | 873 std::string::npos; |
| 834 } | 874 } |
| OLD | NEW |