| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 "logincaptcha=%s"; | 42 "logincaptcha=%s"; |
| 43 // static | 43 // static |
| 44 const char GaiaAuthFetcher::kIssueAuthTokenFormat[] = | 44 const char GaiaAuthFetcher::kIssueAuthTokenFormat[] = |
| 45 "SID=%s&" | 45 "SID=%s&" |
| 46 "LSID=%s&" | 46 "LSID=%s&" |
| 47 "service=%s&" | 47 "service=%s&" |
| 48 "Session=%s"; | 48 "Session=%s"; |
| 49 // static | 49 // static |
| 50 const char GaiaAuthFetcher::kGetUserInfoFormat[] = | 50 const char GaiaAuthFetcher::kGetUserInfoFormat[] = |
| 51 "LSID=%s"; | 51 "LSID=%s"; |
| 52 // static |
| 53 const char GaiaAuthFetcher::kTokenAuthFormat[] = |
| 54 "auth=%s&" |
| 55 "continue=%s&" |
| 56 "source=%s"; |
| 52 | 57 |
| 53 // static | 58 // static |
| 54 const char GaiaAuthFetcher::kAccountDeletedError[] = "AccountDeleted"; | 59 const char GaiaAuthFetcher::kAccountDeletedError[] = "AccountDeleted"; |
| 55 // static | 60 // static |
| 56 const char GaiaAuthFetcher::kAccountDisabledError[] = "AccountDisabled"; | 61 const char GaiaAuthFetcher::kAccountDisabledError[] = "AccountDisabled"; |
| 57 // static | 62 // static |
| 58 const char GaiaAuthFetcher::kBadAuthenticationError[] = "BadAuthentication"; | 63 const char GaiaAuthFetcher::kBadAuthenticationError[] = "BadAuthentication"; |
| 59 // static | 64 // static |
| 60 const char GaiaAuthFetcher::kCaptchaError[] = "CaptchaRequired"; | 65 const char GaiaAuthFetcher::kCaptchaError[] = "CaptchaRequired"; |
| 61 // static | 66 // static |
| (...skipping 23 matching lines...) Expand all Loading... |
| 85 | 90 |
| 86 GaiaAuthFetcher::GaiaAuthFetcher(GaiaAuthConsumer* consumer, | 91 GaiaAuthFetcher::GaiaAuthFetcher(GaiaAuthConsumer* consumer, |
| 87 const std::string& source, | 92 const std::string& source, |
| 88 net::URLRequestContextGetter* getter) | 93 net::URLRequestContextGetter* getter) |
| 89 : consumer_(consumer), | 94 : consumer_(consumer), |
| 90 getter_(getter), | 95 getter_(getter), |
| 91 source_(source), | 96 source_(source), |
| 92 client_login_gurl_(GaiaUrls::GetInstance()->client_login_url()), | 97 client_login_gurl_(GaiaUrls::GetInstance()->client_login_url()), |
| 93 issue_auth_token_gurl_(GaiaUrls::GetInstance()->issue_auth_token_url()), | 98 issue_auth_token_gurl_(GaiaUrls::GetInstance()->issue_auth_token_url()), |
| 94 get_user_info_gurl_(GaiaUrls::GetInstance()->get_user_info_url()), | 99 get_user_info_gurl_(GaiaUrls::GetInstance()->get_user_info_url()), |
| 100 token_auth_gurl_(GaiaUrls::GetInstance()->token_auth_url()), |
| 95 fetch_pending_(false) {} | 101 fetch_pending_(false) {} |
| 96 | 102 |
| 97 GaiaAuthFetcher::~GaiaAuthFetcher() {} | 103 GaiaAuthFetcher::~GaiaAuthFetcher() {} |
| 98 | 104 |
| 99 bool GaiaAuthFetcher::HasPendingFetch() { | 105 bool GaiaAuthFetcher::HasPendingFetch() { |
| 100 return fetch_pending_; | 106 return fetch_pending_; |
| 101 } | 107 } |
| 102 | 108 |
| 103 void GaiaAuthFetcher::CancelRequest() { | 109 void GaiaAuthFetcher::CancelRequest() { |
| 104 fetcher_.reset(); | 110 fetcher_.reset(); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 service, | 187 service, |
| 182 session ? "true" : "false"); | 188 session ? "true" : "false"); |
| 183 } | 189 } |
| 184 | 190 |
| 185 // static | 191 // static |
| 186 std::string GaiaAuthFetcher::MakeGetUserInfoBody(const std::string& lsid) { | 192 std::string GaiaAuthFetcher::MakeGetUserInfoBody(const std::string& lsid) { |
| 187 std::string encoded_lsid = EscapeUrlEncodedData(lsid, true); | 193 std::string encoded_lsid = EscapeUrlEncodedData(lsid, true); |
| 188 return base::StringPrintf(kGetUserInfoFormat, encoded_lsid.c_str()); | 194 return base::StringPrintf(kGetUserInfoFormat, encoded_lsid.c_str()); |
| 189 } | 195 } |
| 190 | 196 |
| 197 // static |
| 198 std::string GaiaAuthFetcher::MakeTokenAuthBody(const std::string& auth_token, |
| 199 const std::string& continue_url, |
| 200 const std::string& source) { |
| 201 std::string encoded_auth_token = EscapeUrlEncodedData(auth_token, true); |
| 202 std::string encoded_continue_url = EscapeUrlEncodedData(continue_url, true); |
| 203 std::string encoded_source = EscapeUrlEncodedData(source, true); |
| 204 return base::StringPrintf(kTokenAuthFormat, |
| 205 encoded_auth_token.c_str(), |
| 206 encoded_continue_url.c_str(), |
| 207 encoded_source.c_str()); |
| 208 } |
| 209 |
| 191 // Helper method that extracts tokens from a successful reply. | 210 // Helper method that extracts tokens from a successful reply. |
| 192 // static | 211 // static |
| 193 void GaiaAuthFetcher::ParseClientLoginResponse(const std::string& data, | 212 void GaiaAuthFetcher::ParseClientLoginResponse(const std::string& data, |
| 194 std::string* sid, | 213 std::string* sid, |
| 195 std::string* lsid, | 214 std::string* lsid, |
| 196 std::string* token) { | 215 std::string* token) { |
| 197 using std::vector; | 216 using std::vector; |
| 198 using std::pair; | 217 using std::pair; |
| 199 using std::string; | 218 using std::string; |
| 200 | 219 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 request_body_ = MakeGetUserInfoBody(lsid); | 311 request_body_ = MakeGetUserInfoBody(lsid); |
| 293 fetcher_.reset(CreateGaiaFetcher(getter_, | 312 fetcher_.reset(CreateGaiaFetcher(getter_, |
| 294 request_body_, | 313 request_body_, |
| 295 get_user_info_gurl_, | 314 get_user_info_gurl_, |
| 296 this)); | 315 this)); |
| 297 fetch_pending_ = true; | 316 fetch_pending_ = true; |
| 298 requested_info_key_ = info_key; | 317 requested_info_key_ = info_key; |
| 299 fetcher_->Start(); | 318 fetcher_->Start(); |
| 300 } | 319 } |
| 301 | 320 |
| 321 void GaiaAuthFetcher::StartTokenAuth(const std::string& auth_token) { |
| 322 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; |
| 323 |
| 324 VLOG(1) << "Starting TokenAuth with auth_token=" << auth_token; |
| 325 |
| 326 // The continue URL is a required parameter of the TokenAuth API, but in this |
| 327 // case we don't actually need or want to navigate to it. Setting it to |
| 328 // an arbitrary Google URL. |
| 329 std::string continue_url("http://www.google.com"); |
| 330 request_body_ = MakeTokenAuthBody(auth_token, continue_url, source_); |
| 331 fetcher_.reset(CreateGaiaFetcher(getter_, |
| 332 request_body_, |
| 333 token_auth_gurl_, |
| 334 this)); |
| 335 fetch_pending_ = true; |
| 336 fetcher_->Start(); |
| 337 } |
| 338 |
| 302 // static | 339 // static |
| 303 GoogleServiceAuthError GaiaAuthFetcher::GenerateAuthError( | 340 GoogleServiceAuthError GaiaAuthFetcher::GenerateAuthError( |
| 304 const std::string& data, | 341 const std::string& data, |
| 305 const net::URLRequestStatus& status) { | 342 const net::URLRequestStatus& status) { |
| 306 if (!status.is_success()) { | 343 if (!status.is_success()) { |
| 307 if (status.status() == net::URLRequestStatus::CANCELED) { | 344 if (status.status() == net::URLRequestStatus::CANCELED) { |
| 308 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); | 345 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); |
| 309 } else { | 346 } else { |
| 310 LOG(WARNING) << "Could not reach Google Accounts servers: errno " | 347 LOG(WARNING) << "Could not reach Google Accounts servers: errno " |
| 311 << status.os_error(); | 348 << status.os_error(); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 consumer_->OnGetUserInfoSuccess(i->first, i->second); | 436 consumer_->OnGetUserInfoSuccess(i->first, i->second); |
| 400 return; | 437 return; |
| 401 } | 438 } |
| 402 } | 439 } |
| 403 consumer_->OnGetUserInfoKeyNotFound(requested_info_key_); | 440 consumer_->OnGetUserInfoKeyNotFound(requested_info_key_); |
| 404 } else { | 441 } else { |
| 405 consumer_->OnGetUserInfoFailure(GenerateAuthError(data, status)); | 442 consumer_->OnGetUserInfoFailure(GenerateAuthError(data, status)); |
| 406 } | 443 } |
| 407 } | 444 } |
| 408 | 445 |
| 446 void GaiaAuthFetcher::OnTokenAuthFetched(const std::string& data, |
| 447 const net::URLRequestStatus& status, |
| 448 int response_code) { |
| 449 if (status.is_success() && response_code == RC_REQUEST_OK) { |
| 450 consumer_->OnTokenAuthSuccess(data); |
| 451 } else { |
| 452 consumer_->OnTokenAuthFailure(GenerateAuthError(data, status)); |
| 453 } |
| 454 } |
| 455 |
| 409 void GaiaAuthFetcher::OnURLFetchComplete(const URLFetcher* source, | 456 void GaiaAuthFetcher::OnURLFetchComplete(const URLFetcher* source, |
| 410 const GURL& url, | 457 const GURL& url, |
| 411 const net::URLRequestStatus& status, | 458 const net::URLRequestStatus& status, |
| 412 int response_code, | 459 int response_code, |
| 413 const net::ResponseCookies& cookies, | 460 const net::ResponseCookies& cookies, |
| 414 const std::string& data) { | 461 const std::string& data) { |
| 415 fetch_pending_ = false; | 462 fetch_pending_ = false; |
| 416 if (url == client_login_gurl_) { | 463 if (url == client_login_gurl_) { |
| 417 OnClientLoginFetched(data, status, response_code); | 464 OnClientLoginFetched(data, status, response_code); |
| 418 } else if (url == issue_auth_token_gurl_) { | 465 } else if (url == issue_auth_token_gurl_) { |
| 419 OnIssueAuthTokenFetched(data, status, response_code); | 466 OnIssueAuthTokenFetched(data, status, response_code); |
| 420 } else if (url == get_user_info_gurl_) { | 467 } else if (url == get_user_info_gurl_) { |
| 421 OnGetUserInfoFetched(data, status, response_code); | 468 OnGetUserInfoFetched(data, status, response_code); |
| 469 } else if (url == token_auth_gurl_) { |
| 470 OnTokenAuthFetched(data, status, response_code); |
| 422 } else { | 471 } else { |
| 423 NOTREACHED(); | 472 NOTREACHED(); |
| 424 } | 473 } |
| 425 } | 474 } |
| 426 | 475 |
| 427 // static | 476 // static |
| 428 bool GaiaAuthFetcher::IsSecondFactorSuccess( | 477 bool GaiaAuthFetcher::IsSecondFactorSuccess( |
| 429 const std::string& alleged_error) { | 478 const std::string& alleged_error) { |
| 430 return alleged_error.find(kSecondFactor) != | 479 return alleged_error.find(kSecondFactor) != |
| 431 std::string::npos; | 480 std::string::npos; |
| 432 } | 481 } |
| OLD | NEW |