| 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 const char GaiaAuthFetcher::kClientLoginToOAuth2BodyFormat[] = | 84 const char GaiaAuthFetcher::kClientLoginToOAuth2BodyFormat[] = |
| 85 "scope=%s&client_id=%s"; | 85 "scope=%s&client_id=%s"; |
| 86 // static | 86 // static |
| 87 const char GaiaAuthFetcher::kOAuth2CodeToTokenPairBodyFormat[] = | 87 const char GaiaAuthFetcher::kOAuth2CodeToTokenPairBodyFormat[] = |
| 88 "scope=%s&" | 88 "scope=%s&" |
| 89 "grant_type=authorization_code&" | 89 "grant_type=authorization_code&" |
| 90 "client_id=%s&" | 90 "client_id=%s&" |
| 91 "client_secret=%s&" | 91 "client_secret=%s&" |
| 92 "code=%s"; | 92 "code=%s"; |
| 93 // static | 93 // static |
| 94 const char GaiaAuthFetcher::kOAuth2RevokeTokenBodyFormat[] = |
| 95 "token=%s"; |
| 96 // static |
| 94 const char GaiaAuthFetcher::kGetUserInfoFormat[] = | 97 const char GaiaAuthFetcher::kGetUserInfoFormat[] = |
| 95 "LSID=%s"; | 98 "LSID=%s"; |
| 96 // static | 99 // static |
| 97 const char GaiaAuthFetcher::kMergeSessionFormat[] = | 100 const char GaiaAuthFetcher::kMergeSessionFormat[] = |
| 98 "uberauth=%s&" | 101 "uberauth=%s&" |
| 99 "continue=%s&" | 102 "continue=%s&" |
| 100 "source=%s"; | 103 "source=%s"; |
| 101 // static | 104 // static |
| 102 const char GaiaAuthFetcher::kUberAuthTokenURLFormat[] = | 105 const char GaiaAuthFetcher::kUberAuthTokenURLFormat[] = |
| 103 "%s?source=%s&" | 106 "%s?source=%s&" |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 | 176 |
| 174 GaiaAuthFetcher::GaiaAuthFetcher(GaiaAuthConsumer* consumer, | 177 GaiaAuthFetcher::GaiaAuthFetcher(GaiaAuthConsumer* consumer, |
| 175 const std::string& source, | 178 const std::string& source, |
| 176 net::URLRequestContextGetter* getter) | 179 net::URLRequestContextGetter* getter) |
| 177 : consumer_(consumer), | 180 : consumer_(consumer), |
| 178 getter_(getter), | 181 getter_(getter), |
| 179 source_(source), | 182 source_(source), |
| 180 client_login_gurl_(GaiaUrls::GetInstance()->client_login_url()), | 183 client_login_gurl_(GaiaUrls::GetInstance()->client_login_url()), |
| 181 issue_auth_token_gurl_(GaiaUrls::GetInstance()->issue_auth_token_url()), | 184 issue_auth_token_gurl_(GaiaUrls::GetInstance()->issue_auth_token_url()), |
| 182 oauth2_token_gurl_(GaiaUrls::GetInstance()->oauth2_token_url()), | 185 oauth2_token_gurl_(GaiaUrls::GetInstance()->oauth2_token_url()), |
| 186 oauth2_revoke_gurl_(GaiaUrls::GetInstance()->oauth2_revoke_url()), |
| 183 get_user_info_gurl_(GaiaUrls::GetInstance()->get_user_info_url()), | 187 get_user_info_gurl_(GaiaUrls::GetInstance()->get_user_info_url()), |
| 184 merge_session_gurl_(GaiaUrls::GetInstance()->merge_session_url()), | 188 merge_session_gurl_(GaiaUrls::GetInstance()->merge_session_url()), |
| 185 uberauth_token_gurl_(base::StringPrintf(kUberAuthTokenURLFormat, | 189 uberauth_token_gurl_(base::StringPrintf(kUberAuthTokenURLFormat, |
| 186 GaiaUrls::GetInstance()->oauth1_login_url().c_str(), source.c_str())), | 190 GaiaUrls::GetInstance()->oauth1_login_url().c_str(), source.c_str())), |
| 187 client_oauth_gurl_(GaiaUrls::GetInstance()->client_oauth_url()), | 191 client_oauth_gurl_(GaiaUrls::GetInstance()->client_oauth_url()), |
| 188 oauth_login_gurl_(GaiaUrls::GetInstance()->oauth1_login_url()), | 192 oauth_login_gurl_(GaiaUrls::GetInstance()->oauth1_login_url()), |
| 189 client_login_to_oauth2_gurl_( | 193 client_login_to_oauth2_gurl_( |
| 190 GaiaUrls::GetInstance()->client_login_to_oauth2_url()), | 194 GaiaUrls::GetInstance()->client_login_to_oauth2_url()), |
| 191 fetch_pending_(false) {} | 195 fetch_pending_(false) {} |
| 192 | 196 |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), true); | 326 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), true); |
| 323 std::string encoded_auth_code = net::EscapeUrlEncodedData(auth_code, true); | 327 std::string encoded_auth_code = net::EscapeUrlEncodedData(auth_code, true); |
| 324 return base::StringPrintf(kOAuth2CodeToTokenPairBodyFormat, | 328 return base::StringPrintf(kOAuth2CodeToTokenPairBodyFormat, |
| 325 encoded_scope.c_str(), | 329 encoded_scope.c_str(), |
| 326 encoded_client_id.c_str(), | 330 encoded_client_id.c_str(), |
| 327 encoded_client_secret.c_str(), | 331 encoded_client_secret.c_str(), |
| 328 encoded_auth_code.c_str()); | 332 encoded_auth_code.c_str()); |
| 329 } | 333 } |
| 330 | 334 |
| 331 // static | 335 // static |
| 336 std::string GaiaAuthFetcher::MakeRevokeTokenBody( |
| 337 const std::string& auth_token) { |
| 338 return base::StringPrintf(kOAuth2RevokeTokenBodyFormat, auth_token.c_str()); |
| 339 } |
| 340 |
| 341 // static |
| 332 std::string GaiaAuthFetcher::MakeGetUserInfoBody(const std::string& lsid) { | 342 std::string GaiaAuthFetcher::MakeGetUserInfoBody(const std::string& lsid) { |
| 333 std::string encoded_lsid = net::EscapeUrlEncodedData(lsid, true); | 343 std::string encoded_lsid = net::EscapeUrlEncodedData(lsid, true); |
| 334 return base::StringPrintf(kGetUserInfoFormat, encoded_lsid.c_str()); | 344 return base::StringPrintf(kGetUserInfoFormat, encoded_lsid.c_str()); |
| 335 } | 345 } |
| 336 | 346 |
| 337 // static | 347 // static |
| 338 std::string GaiaAuthFetcher::MakeMergeSessionBody( | 348 std::string GaiaAuthFetcher::MakeMergeSessionBody( |
| 339 const std::string& auth_token, | 349 const std::string& auth_token, |
| 340 const std::string& continue_url, | 350 const std::string& continue_url, |
| 341 const std::string& source) { | 351 const std::string& source) { |
| (...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 645 fetcher_.reset(CreateGaiaFetcher(getter_, | 655 fetcher_.reset(CreateGaiaFetcher(getter_, |
| 646 request_body_, | 656 request_body_, |
| 647 MakeGetAuthCodeHeader(auth_token), | 657 MakeGetAuthCodeHeader(auth_token), |
| 648 client_login_to_oauth2_gurl_, | 658 client_login_to_oauth2_gurl_, |
| 649 kLoadFlagsIgnoreCookies, | 659 kLoadFlagsIgnoreCookies, |
| 650 this)); | 660 this)); |
| 651 fetch_pending_ = true; | 661 fetch_pending_ = true; |
| 652 fetcher_->Start(); | 662 fetcher_->Start(); |
| 653 } | 663 } |
| 654 | 664 |
| 665 void GaiaAuthFetcher::StartRevokeOAuth2Token(const std::string& auth_token) { |
| 666 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; |
| 667 |
| 668 DVLOG(1) << "Starting OAuth2 token revocation"; |
| 669 request_body_ = MakeRevokeTokenBody(auth_token); |
| 670 fetcher_.reset(CreateGaiaFetcher(getter_, |
| 671 request_body_, |
| 672 "", |
| 673 oauth2_revoke_gurl_, |
| 674 kLoadFlagsIgnoreCookies, |
| 675 this)); |
| 676 fetch_pending_ = true; |
| 677 fetcher_->Start(); |
| 678 } |
| 679 |
| 655 void GaiaAuthFetcher::StartCookieForOAuthLoginTokenExchange( | 680 void GaiaAuthFetcher::StartCookieForOAuthLoginTokenExchange( |
| 656 const std::string& session_index) { | 681 const std::string& session_index) { |
| 657 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; | 682 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; |
| 658 | 683 |
| 659 DVLOG(1) << "Starting OAuth login token fetch with cookie jar"; | 684 DVLOG(1) << "Starting OAuth login token fetch with cookie jar"; |
| 660 request_body_ = MakeGetAuthCodeBody(); | 685 request_body_ = MakeGetAuthCodeBody(); |
| 661 | 686 |
| 662 std::string url = GaiaUrls::GetInstance()->client_login_to_oauth2_url(); | 687 std::string url = GaiaUrls::GetInstance()->client_login_to_oauth2_url(); |
| 663 if (!session_index.empty()) | 688 if (!session_index.empty()) |
| 664 url += "?authuser=" + session_index; | 689 url += "?authuser=" + session_index; |
| (...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 983 | 1008 |
| 984 if (success) { | 1009 if (success) { |
| 985 consumer_->OnClientOAuthSuccess( | 1010 consumer_->OnClientOAuthSuccess( |
| 986 GaiaAuthConsumer::ClientOAuthResult(refresh_token, access_token, | 1011 GaiaAuthConsumer::ClientOAuthResult(refresh_token, access_token, |
| 987 expires_in_secs)); | 1012 expires_in_secs)); |
| 988 } else { | 1013 } else { |
| 989 consumer_->OnClientOAuthFailure(GenerateAuthError(data, status)); | 1014 consumer_->OnClientOAuthFailure(GenerateAuthError(data, status)); |
| 990 } | 1015 } |
| 991 } | 1016 } |
| 992 | 1017 |
| 1018 void GaiaAuthFetcher::OnOAuth2RevokeTokenFetched( |
| 1019 const std::string& data, |
| 1020 const net::URLRequestStatus& status, |
| 1021 int response_code) { |
| 1022 consumer_->OnOAuth2RevokeTokenCompleted(); |
| 1023 } |
| 1024 |
| 993 void GaiaAuthFetcher::OnGetUserInfoFetched( | 1025 void GaiaAuthFetcher::OnGetUserInfoFetched( |
| 994 const std::string& data, | 1026 const std::string& data, |
| 995 const net::URLRequestStatus& status, | 1027 const net::URLRequestStatus& status, |
| 996 int response_code) { | 1028 int response_code) { |
| 997 if (status.is_success() && response_code == net::HTTP_OK) { | 1029 if (status.is_success() && response_code == net::HTTP_OK) { |
| 998 std::vector<std::pair<std::string, std::string> > tokens; | 1030 std::vector<std::pair<std::string, std::string> > tokens; |
| 999 UserInfoMap matches; | 1031 UserInfoMap matches; |
| 1000 base::SplitStringIntoKeyValuePairs(data, '=', '\n', &tokens); | 1032 base::SplitStringIntoKeyValuePairs(data, '=', '\n', &tokens); |
| 1001 std::vector<std::pair<std::string, std::string> >::iterator i; | 1033 std::vector<std::pair<std::string, std::string> >::iterator i; |
| 1002 for (i = tokens.begin(); i != tokens.end(); ++i) { | 1034 for (i = tokens.begin(); i != tokens.end(); ++i) { |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1111 } else if (url == get_user_info_gurl_) { | 1143 } else if (url == get_user_info_gurl_) { |
| 1112 OnGetUserInfoFetched(data, status, response_code); | 1144 OnGetUserInfoFetched(data, status, response_code); |
| 1113 } else if (url == merge_session_gurl_) { | 1145 } else if (url == merge_session_gurl_) { |
| 1114 OnMergeSessionFetched(data, status, response_code); | 1146 OnMergeSessionFetched(data, status, response_code); |
| 1115 } else if (url == uberauth_token_gurl_) { | 1147 } else if (url == uberauth_token_gurl_) { |
| 1116 OnUberAuthTokenFetch(data, status, response_code); | 1148 OnUberAuthTokenFetch(data, status, response_code); |
| 1117 } else if (url == client_oauth_gurl_) { | 1149 } else if (url == client_oauth_gurl_) { |
| 1118 OnClientOAuthFetched(data, status, response_code); | 1150 OnClientOAuthFetched(data, status, response_code); |
| 1119 } else if (url == oauth_login_gurl_) { | 1151 } else if (url == oauth_login_gurl_) { |
| 1120 OnOAuthLoginFetched(data, status, response_code); | 1152 OnOAuthLoginFetched(data, status, response_code); |
| 1153 } else if (url == oauth2_revoke_gurl_) { |
| 1154 OnOAuth2RevokeTokenFetched(data, status, response_code); |
| 1121 } else { | 1155 } else { |
| 1122 NOTREACHED(); | 1156 NOTREACHED(); |
| 1123 } | 1157 } |
| 1124 } | 1158 } |
| 1125 | 1159 |
| 1126 // static | 1160 // static |
| 1127 bool GaiaAuthFetcher::IsSecondFactorSuccess( | 1161 bool GaiaAuthFetcher::IsSecondFactorSuccess( |
| 1128 const std::string& alleged_error) { | 1162 const std::string& alleged_error) { |
| 1129 return alleged_error.find(kSecondFactor) != | 1163 return alleged_error.find(kSecondFactor) != |
| 1130 std::string::npos; | 1164 std::string::npos; |
| 1131 } | 1165 } |
| OLD | NEW |