| 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 30 matching lines...) Expand all Loading... |
| 41 "logincaptcha=%s"; | 41 "logincaptcha=%s"; |
| 42 // static | 42 // static |
| 43 const char GaiaAuthFetcher::kIssueAuthTokenFormat[] = | 43 const char GaiaAuthFetcher::kIssueAuthTokenFormat[] = |
| 44 "SID=%s&" | 44 "SID=%s&" |
| 45 "LSID=%s&" | 45 "LSID=%s&" |
| 46 "service=%s&" | 46 "service=%s&" |
| 47 "Session=%s"; | 47 "Session=%s"; |
| 48 // static | 48 // static |
| 49 const char GaiaAuthFetcher::kGetUserInfoFormat[] = | 49 const char GaiaAuthFetcher::kGetUserInfoFormat[] = |
| 50 "LSID=%s"; | 50 "LSID=%s"; |
| 51 // static |
| 52 const char GaiaAuthFetcher::kTokenAuthFormat[] = |
| 53 "auth=%s&" |
| 54 "continue=%s&" |
| 55 "source=%s"; |
| 51 | 56 |
| 52 // static | 57 // static |
| 53 const char GaiaAuthFetcher::kAccountDeletedError[] = "AccountDeleted"; | 58 const char GaiaAuthFetcher::kAccountDeletedError[] = "AccountDeleted"; |
| 54 // static | 59 // static |
| 55 const char GaiaAuthFetcher::kAccountDisabledError[] = "AccountDisabled"; | 60 const char GaiaAuthFetcher::kAccountDisabledError[] = "AccountDisabled"; |
| 56 // static | 61 // static |
| 57 const char GaiaAuthFetcher::kBadAuthenticationError[] = "BadAuthentication"; | 62 const char GaiaAuthFetcher::kBadAuthenticationError[] = "BadAuthentication"; |
| 58 // static | 63 // static |
| 59 const char GaiaAuthFetcher::kCaptchaError[] = "CaptchaRequired"; | 64 const char GaiaAuthFetcher::kCaptchaError[] = "CaptchaRequired"; |
| 60 // static | 65 // static |
| (...skipping 25 matching lines...) Expand all Loading... |
| 86 const char GaiaAuthFetcher::kSecondFactor[] = "Info=InvalidSecondFactor"; | 91 const char GaiaAuthFetcher::kSecondFactor[] = "Info=InvalidSecondFactor"; |
| 87 | 92 |
| 88 // TODO(chron): These urls are also in auth_response_handler.h. | 93 // TODO(chron): These urls are also in auth_response_handler.h. |
| 89 // The URLs for different calls in the Google Accounts programmatic login API. | 94 // The URLs for different calls in the Google Accounts programmatic login API. |
| 90 const char GaiaAuthFetcher::kClientLoginUrl[] = | 95 const char GaiaAuthFetcher::kClientLoginUrl[] = |
| 91 "https://www.google.com/accounts/ClientLogin"; | 96 "https://www.google.com/accounts/ClientLogin"; |
| 92 const char GaiaAuthFetcher::kIssueAuthTokenUrl[] = | 97 const char GaiaAuthFetcher::kIssueAuthTokenUrl[] = |
| 93 "https://www.google.com/accounts/IssueAuthToken"; | 98 "https://www.google.com/accounts/IssueAuthToken"; |
| 94 const char GaiaAuthFetcher::kGetUserInfoUrl[] = | 99 const char GaiaAuthFetcher::kGetUserInfoUrl[] = |
| 95 "https://www.google.com/accounts/GetUserInfo"; | 100 "https://www.google.com/accounts/GetUserInfo"; |
| 101 const char GaiaAuthFetcher::kTokenAuthUrl[] = |
| 102 "https://www.google.com/accounts/TokenAuth"; |
| 96 | 103 |
| 97 GaiaAuthFetcher::GaiaAuthFetcher(GaiaAuthConsumer* consumer, | 104 GaiaAuthFetcher::GaiaAuthFetcher(GaiaAuthConsumer* consumer, |
| 98 const std::string& source, | 105 const std::string& source, |
| 99 net::URLRequestContextGetter* getter) | 106 net::URLRequestContextGetter* getter) |
| 100 : consumer_(consumer), | 107 : consumer_(consumer), |
| 101 getter_(getter), | 108 getter_(getter), |
| 102 source_(source), | 109 source_(source), |
| 103 client_login_gurl_(kClientLoginUrl), | 110 client_login_gurl_(kClientLoginUrl), |
| 104 issue_auth_token_gurl_(kIssueAuthTokenUrl), | 111 issue_auth_token_gurl_(kIssueAuthTokenUrl), |
| 105 get_user_info_gurl_(kGetUserInfoUrl), | 112 get_user_info_gurl_(kGetUserInfoUrl), |
| 113 token_auth_gurl_(kTokenAuthUrl), |
| 106 fetch_pending_(false) {} | 114 fetch_pending_(false) {} |
| 107 | 115 |
| 108 GaiaAuthFetcher::~GaiaAuthFetcher() {} | 116 GaiaAuthFetcher::~GaiaAuthFetcher() {} |
| 109 | 117 |
| 110 bool GaiaAuthFetcher::HasPendingFetch() { | 118 bool GaiaAuthFetcher::HasPendingFetch() { |
| 111 return fetch_pending_; | 119 return fetch_pending_; |
| 112 } | 120 } |
| 113 | 121 |
| 114 void GaiaAuthFetcher::CancelRequest() { | 122 void GaiaAuthFetcher::CancelRequest() { |
| 115 fetcher_.reset(); | 123 fetcher_.reset(); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 service, | 201 service, |
| 194 session ? "true" : "false"); | 202 session ? "true" : "false"); |
| 195 } | 203 } |
| 196 | 204 |
| 197 // static | 205 // static |
| 198 std::string GaiaAuthFetcher::MakeGetUserInfoBody(const std::string& lsid) { | 206 std::string GaiaAuthFetcher::MakeGetUserInfoBody(const std::string& lsid) { |
| 199 std::string encoded_lsid = EscapeUrlEncodedData(lsid); | 207 std::string encoded_lsid = EscapeUrlEncodedData(lsid); |
| 200 return base::StringPrintf(kGetUserInfoFormat, encoded_lsid.c_str()); | 208 return base::StringPrintf(kGetUserInfoFormat, encoded_lsid.c_str()); |
| 201 } | 209 } |
| 202 | 210 |
| 211 // static |
| 212 std::string GaiaAuthFetcher::MakeTokenAuthBody(const std::string& auth_token, |
| 213 const std::string& continue_url, |
| 214 const std::string& source) { |
| 215 std::string encoded_auth_token = EscapeUrlEncodedData(auth_token); |
| 216 std::string encoded_continue_url = EscapeUrlEncodedData(continue_url); |
| 217 std::string encoded_source = EscapeUrlEncodedData(source); |
| 218 return base::StringPrintf(kTokenAuthFormat, |
| 219 encoded_auth_token.c_str(), |
| 220 encoded_continue_url.c_str(), |
| 221 encoded_source.c_str()); |
| 222 } |
| 223 |
| 203 // Helper method that extracts tokens from a successful reply. | 224 // Helper method that extracts tokens from a successful reply. |
| 204 // static | 225 // static |
| 205 void GaiaAuthFetcher::ParseClientLoginResponse(const std::string& data, | 226 void GaiaAuthFetcher::ParseClientLoginResponse(const std::string& data, |
| 206 std::string* sid, | 227 std::string* sid, |
| 207 std::string* lsid, | 228 std::string* lsid, |
| 208 std::string* token) { | 229 std::string* token) { |
| 209 using std::vector; | 230 using std::vector; |
| 210 using std::pair; | 231 using std::pair; |
| 211 using std::string; | 232 using std::string; |
| 212 | 233 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 request_body_ = MakeGetUserInfoBody(lsid); | 326 request_body_ = MakeGetUserInfoBody(lsid); |
| 306 fetcher_.reset(CreateGaiaFetcher(getter_, | 327 fetcher_.reset(CreateGaiaFetcher(getter_, |
| 307 request_body_, | 328 request_body_, |
| 308 get_user_info_gurl_, | 329 get_user_info_gurl_, |
| 309 this)); | 330 this)); |
| 310 fetch_pending_ = true; | 331 fetch_pending_ = true; |
| 311 requested_info_key_ = info_key; | 332 requested_info_key_ = info_key; |
| 312 fetcher_->Start(); | 333 fetcher_->Start(); |
| 313 } | 334 } |
| 314 | 335 |
| 336 void GaiaAuthFetcher::StartTokenAuth(const std::string& auth_token) { |
| 337 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; |
| 338 |
| 339 VLOG(1) << "Starting TokenAuth with auth_token=" << auth_token; |
| 340 |
| 341 // The continue URL is a required parameter of the TokenAuth API, but in this |
| 342 // case we don't actually need or want to navigate to it. Setting it to |
| 343 // an arbitrary Google URL. |
| 344 std::string continue_url("http://www.google.com"); |
| 345 request_body_ = MakeTokenAuthBody(auth_token, continue_url, source_); |
| 346 fetcher_.reset(CreateGaiaFetcher(getter_, |
| 347 request_body_, |
| 348 token_auth_gurl_, |
| 349 this)); |
| 350 fetch_pending_ = true; |
| 351 fetcher_->Start(); |
| 352 } |
| 353 |
| 315 // static | 354 // static |
| 316 GoogleServiceAuthError GaiaAuthFetcher::GenerateAuthError( | 355 GoogleServiceAuthError GaiaAuthFetcher::GenerateAuthError( |
| 317 const std::string& data, | 356 const std::string& data, |
| 318 const net::URLRequestStatus& status) { | 357 const net::URLRequestStatus& status) { |
| 319 if (!status.is_success()) { | 358 if (!status.is_success()) { |
| 320 if (status.status() == net::URLRequestStatus::CANCELED) { | 359 if (status.status() == net::URLRequestStatus::CANCELED) { |
| 321 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); | 360 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); |
| 322 } else { | 361 } else { |
| 323 LOG(WARNING) << "Could not reach Google Accounts servers: errno " | 362 LOG(WARNING) << "Could not reach Google Accounts servers: errno " |
| 324 << status.os_error(); | 363 << status.os_error(); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 consumer_->OnGetUserInfoSuccess(i->first, i->second); | 450 consumer_->OnGetUserInfoSuccess(i->first, i->second); |
| 412 return; | 451 return; |
| 413 } | 452 } |
| 414 } | 453 } |
| 415 consumer_->OnGetUserInfoKeyNotFound(requested_info_key_); | 454 consumer_->OnGetUserInfoKeyNotFound(requested_info_key_); |
| 416 } else { | 455 } else { |
| 417 consumer_->OnGetUserInfoFailure(GenerateAuthError(data, status)); | 456 consumer_->OnGetUserInfoFailure(GenerateAuthError(data, status)); |
| 418 } | 457 } |
| 419 } | 458 } |
| 420 | 459 |
| 460 void GaiaAuthFetcher::OnTokenAuthFetched(const std::string& data, |
| 461 const net::URLRequestStatus& status, |
| 462 int response_code) { |
| 463 if (status.is_success() && response_code == RC_REQUEST_OK) { |
| 464 consumer_->OnTokenAuthSuccess(data); |
| 465 } else { |
| 466 consumer_->OnTokenAuthFailure(GenerateAuthError(data, status)); |
| 467 } |
| 468 } |
| 469 |
| 421 void GaiaAuthFetcher::OnURLFetchComplete(const URLFetcher* source, | 470 void GaiaAuthFetcher::OnURLFetchComplete(const URLFetcher* source, |
| 422 const GURL& url, | 471 const GURL& url, |
| 423 const net::URLRequestStatus& status, | 472 const net::URLRequestStatus& status, |
| 424 int response_code, | 473 int response_code, |
| 425 const net::ResponseCookies& cookies, | 474 const net::ResponseCookies& cookies, |
| 426 const std::string& data) { | 475 const std::string& data) { |
| 427 fetch_pending_ = false; | 476 fetch_pending_ = false; |
| 428 if (url == client_login_gurl_) { | 477 if (url == client_login_gurl_) { |
| 429 OnClientLoginFetched(data, status, response_code); | 478 OnClientLoginFetched(data, status, response_code); |
| 430 } else if (url == issue_auth_token_gurl_) { | 479 } else if (url == issue_auth_token_gurl_) { |
| 431 OnIssueAuthTokenFetched(data, status, response_code); | 480 OnIssueAuthTokenFetched(data, status, response_code); |
| 432 } else if (url == get_user_info_gurl_) { | 481 } else if (url == get_user_info_gurl_) { |
| 433 OnGetUserInfoFetched(data, status, response_code); | 482 OnGetUserInfoFetched(data, status, response_code); |
| 483 } else if (url == token_auth_gurl_) { |
| 484 OnTokenAuthFetched(data, status, response_code); |
| 434 } else { | 485 } else { |
| 435 NOTREACHED(); | 486 NOTREACHED(); |
| 436 } | 487 } |
| 437 } | 488 } |
| 438 | 489 |
| 439 // static | 490 // static |
| 440 bool GaiaAuthFetcher::IsSecondFactorSuccess( | 491 bool GaiaAuthFetcher::IsSecondFactorSuccess( |
| 441 const std::string& alleged_error) { | 492 const std::string& alleged_error) { |
| 442 return alleged_error.find(kSecondFactor) != | 493 return alleged_error.find(kSecondFactor) != |
| 443 std::string::npos; | 494 std::string::npos; |
| 444 } | 495 } |
| OLD | NEW |