Chromium Code Reviews| 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 | 52 // static |
| 53 const char GaiaAuthFetcher::kTokenAuthFormat[] = | 53 const char GaiaAuthFetcher::kTokenAuthFormat[] = |
| 54 "auth=%s&" | 54 "auth=%s&" |
| 55 "continue=%s&" | 55 "continue=%s&" |
| 56 "source=%s"; | 56 "source=%s"; |
| 57 // static | |
| 58 const char GaiaAuthFetcher::kMergeSessionFormat[] = | |
| 59 "uberauth=%s&" | |
| 60 "continue=%s&" | |
| 61 "source=%s"; | |
| 57 | 62 |
| 58 // static | 63 // static |
| 59 const char GaiaAuthFetcher::kAccountDeletedError[] = "AccountDeleted"; | 64 const char GaiaAuthFetcher::kAccountDeletedError[] = "AccountDeleted"; |
| 60 const char GaiaAuthFetcher::kAccountDeletedErrorCode[] = "adel"; | 65 const char GaiaAuthFetcher::kAccountDeletedErrorCode[] = "adel"; |
| 61 // static | 66 // static |
| 62 const char GaiaAuthFetcher::kAccountDisabledError[] = "AccountDisabled"; | 67 const char GaiaAuthFetcher::kAccountDisabledError[] = "AccountDisabled"; |
| 63 const char GaiaAuthFetcher::kAccountDisabledErrorCode[] = "adis"; | 68 const char GaiaAuthFetcher::kAccountDisabledErrorCode[] = "adis"; |
| 64 // static | 69 // static |
| 65 const char GaiaAuthFetcher::kBadAuthenticationError[] = "BadAuthentication"; | 70 const char GaiaAuthFetcher::kBadAuthenticationError[] = "BadAuthentication"; |
| 66 const char GaiaAuthFetcher::kBadAuthenticationErrorCode[] = "badauth"; | 71 const char GaiaAuthFetcher::kBadAuthenticationErrorCode[] = "badauth"; |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 97 GaiaAuthFetcher::GaiaAuthFetcher(GaiaAuthConsumer* consumer, | 102 GaiaAuthFetcher::GaiaAuthFetcher(GaiaAuthConsumer* consumer, |
| 98 const std::string& source, | 103 const std::string& source, |
| 99 net::URLRequestContextGetter* getter) | 104 net::URLRequestContextGetter* getter) |
| 100 : consumer_(consumer), | 105 : consumer_(consumer), |
| 101 getter_(getter), | 106 getter_(getter), |
| 102 source_(source), | 107 source_(source), |
| 103 client_login_gurl_(GaiaUrls::GetInstance()->client_login_url()), | 108 client_login_gurl_(GaiaUrls::GetInstance()->client_login_url()), |
| 104 issue_auth_token_gurl_(GaiaUrls::GetInstance()->issue_auth_token_url()), | 109 issue_auth_token_gurl_(GaiaUrls::GetInstance()->issue_auth_token_url()), |
| 105 get_user_info_gurl_(GaiaUrls::GetInstance()->get_user_info_url()), | 110 get_user_info_gurl_(GaiaUrls::GetInstance()->get_user_info_url()), |
| 106 token_auth_gurl_(GaiaUrls::GetInstance()->token_auth_url()), | 111 token_auth_gurl_(GaiaUrls::GetInstance()->token_auth_url()), |
| 112 merge_session_gurl_(GaiaUrls::GetInstance()->merge_session_url()), | |
| 107 fetch_pending_(false) {} | 113 fetch_pending_(false) {} |
| 108 | 114 |
| 109 GaiaAuthFetcher::~GaiaAuthFetcher() {} | 115 GaiaAuthFetcher::~GaiaAuthFetcher() {} |
| 110 | 116 |
| 111 bool GaiaAuthFetcher::HasPendingFetch() { | 117 bool GaiaAuthFetcher::HasPendingFetch() { |
| 112 return fetch_pending_; | 118 return fetch_pending_; |
| 113 } | 119 } |
| 114 | 120 |
| 115 void GaiaAuthFetcher::CancelRequest() { | 121 void GaiaAuthFetcher::CancelRequest() { |
| 116 fetcher_.reset(); | 122 fetcher_.reset(); |
| 117 fetch_pending_ = false; | 123 fetch_pending_ = false; |
| 118 } | 124 } |
| 119 | 125 |
| 120 // static | 126 // static |
| 121 URLFetcher* GaiaAuthFetcher::CreateGaiaFetcher( | 127 URLFetcher* GaiaAuthFetcher::CreateGaiaFetcher( |
| 122 net::URLRequestContextGetter* getter, | 128 net::URLRequestContextGetter* getter, |
| 123 const std::string& body, | 129 const std::string& body, |
| 124 const GURL& gaia_gurl, | 130 const GURL& gaia_gurl, |
| 125 URLFetcher::Delegate* delegate) { | 131 URLFetcher::Delegate* delegate) { |
| 132 return CreateGaiaFetcherWithFlags(getter, body, gaia_gurl, | |
| 133 net::LOAD_DO_NOT_SEND_COOKIES, delegate); | |
| 134 } | |
| 135 | |
| 136 // static | |
| 137 URLFetcher* GaiaAuthFetcher::CreateGaiaFetcherWithFlags( | |
| 138 net::URLRequestContextGetter* getter, | |
| 139 const std::string& body, | |
| 140 const GURL& gaia_gurl, | |
| 141 int flags, | |
| 142 URLFetcher::Delegate* delegate) { | |
| 126 | 143 |
| 127 URLFetcher* to_return = | 144 URLFetcher* to_return = |
| 128 URLFetcher::Create(0, | 145 URLFetcher::Create(0, |
| 129 gaia_gurl, | 146 gaia_gurl, |
| 130 URLFetcher::POST, | 147 URLFetcher::POST, |
| 131 delegate); | 148 delegate); |
| 132 to_return->set_request_context(getter); | 149 to_return->set_request_context(getter); |
| 133 to_return->set_load_flags(net::LOAD_DO_NOT_SEND_COOKIES); | 150 to_return->set_load_flags(flags); |
| 134 to_return->set_upload_data("application/x-www-form-urlencoded", body); | 151 to_return->set_upload_data("application/x-www-form-urlencoded", body); |
| 135 return to_return; | 152 return to_return; |
| 136 } | 153 } |
| 137 | 154 |
| 138 // static | 155 // static |
| 139 std::string GaiaAuthFetcher::MakeClientLoginBody( | 156 std::string GaiaAuthFetcher::MakeClientLoginBody( |
| 140 const std::string& username, | 157 const std::string& username, |
| 141 const std::string& password, | 158 const std::string& password, |
| 142 const std::string& source, | 159 const std::string& source, |
| 143 const char* service, | 160 const char* service, |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 206 const std::string& source) { | 223 const std::string& source) { |
| 207 std::string encoded_auth_token = EscapeUrlEncodedData(auth_token, true); | 224 std::string encoded_auth_token = EscapeUrlEncodedData(auth_token, true); |
| 208 std::string encoded_continue_url = EscapeUrlEncodedData(continue_url, true); | 225 std::string encoded_continue_url = EscapeUrlEncodedData(continue_url, true); |
| 209 std::string encoded_source = EscapeUrlEncodedData(source, true); | 226 std::string encoded_source = EscapeUrlEncodedData(source, true); |
| 210 return base::StringPrintf(kTokenAuthFormat, | 227 return base::StringPrintf(kTokenAuthFormat, |
| 211 encoded_auth_token.c_str(), | 228 encoded_auth_token.c_str(), |
| 212 encoded_continue_url.c_str(), | 229 encoded_continue_url.c_str(), |
| 213 encoded_source.c_str()); | 230 encoded_source.c_str()); |
| 214 } | 231 } |
| 215 | 232 |
| 233 // static | |
| 234 std::string GaiaAuthFetcher::MakeMergeSessionBody( | |
| 235 const std::string& auth_token, | |
| 236 const std::string& continue_url, | |
| 237 const std::string& source) { | |
| 238 std::string encoded_auth_token = EscapeUrlEncodedData(auth_token, true); | |
| 239 std::string encoded_continue_url = EscapeUrlEncodedData(continue_url, true); | |
| 240 std::string encoded_source = EscapeUrlEncodedData(source, true); | |
| 241 return base::StringPrintf(kMergeSessionFormat, | |
| 242 encoded_auth_token.c_str(), | |
| 243 encoded_continue_url.c_str(), | |
| 244 encoded_source.c_str()); | |
| 245 } | |
| 246 | |
| 216 // Helper method that extracts tokens from a successful reply. | 247 // Helper method that extracts tokens from a successful reply. |
| 217 // static | 248 // static |
| 218 void GaiaAuthFetcher::ParseClientLoginResponse(const std::string& data, | 249 void GaiaAuthFetcher::ParseClientLoginResponse(const std::string& data, |
| 219 std::string* sid, | 250 std::string* sid, |
| 220 std::string* lsid, | 251 std::string* lsid, |
| 221 std::string* token) { | 252 std::string* token) { |
| 222 using std::vector; | 253 using std::vector; |
| 223 using std::pair; | 254 using std::pair; |
| 224 using std::string; | 255 using std::string; |
| 225 | 256 |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 335 std::string continue_url("http://www.google.com"); | 366 std::string continue_url("http://www.google.com"); |
| 336 request_body_ = MakeTokenAuthBody(auth_token, continue_url, source_); | 367 request_body_ = MakeTokenAuthBody(auth_token, continue_url, source_); |
| 337 fetcher_.reset(CreateGaiaFetcher(getter_, | 368 fetcher_.reset(CreateGaiaFetcher(getter_, |
| 338 request_body_, | 369 request_body_, |
| 339 token_auth_gurl_, | 370 token_auth_gurl_, |
| 340 this)); | 371 this)); |
| 341 fetch_pending_ = true; | 372 fetch_pending_ = true; |
| 342 fetcher_->Start(); | 373 fetcher_->Start(); |
| 343 } | 374 } |
| 344 | 375 |
| 376 void GaiaAuthFetcher::StartMergeSession(const std::string& auth_token) { | |
| 377 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; | |
| 378 | |
| 379 VLOG(1) << "Starting MergeSession with auth_token=" << auth_token; | |
| 380 | |
| 381 // The continue URL is a required parameter of the MergeSession API, but in | |
| 382 // this case we don't actually need or want to navigate to it. Setting it to | |
| 383 // an arbitrary Google URL. | |
| 384 // | |
| 385 // In order for the new session to be merged correctly, the server needs to | |
| 386 // know what sessions already exist in the browser. The fecther needs to be | |
|
Rick Campbell
2011/08/08 21:24:51
TYPO: fetcher
Roger Tawa OOO till Jul 10th
2011/08/09 20:50:49
Done.
| |
| 387 // created such that it sends the cookies with the request, which is | |
| 388 // different from all other requests the fetcher can make. | |
| 389 std::string continue_url("http://www.google.com"); | |
| 390 request_body_ = MakeMergeSessionBody(auth_token, continue_url, source_); | |
| 391 fetcher_.reset(CreateGaiaFetcherWithFlags(getter_, | |
| 392 request_body_, | |
| 393 merge_session_gurl_, | |
| 394 net::LOAD_NORMAL, | |
|
Rick Campbell
2011/08/08 21:24:51
Just looping back to my comment about preferring a
Roger Tawa OOO till Jul 10th
2011/08/09 20:50:49
Done.
| |
| 395 this)); | |
| 396 fetch_pending_ = true; | |
| 397 fetcher_->Start(); | |
| 398 } | |
| 399 | |
| 345 // static | 400 // static |
| 346 GoogleServiceAuthError GaiaAuthFetcher::GenerateAuthError( | 401 GoogleServiceAuthError GaiaAuthFetcher::GenerateAuthError( |
| 347 const std::string& data, | 402 const std::string& data, |
| 348 const net::URLRequestStatus& status) { | 403 const net::URLRequestStatus& status) { |
| 349 if (!status.is_success()) { | 404 if (!status.is_success()) { |
| 350 if (status.status() == net::URLRequestStatus::CANCELED) { | 405 if (status.status() == net::URLRequestStatus::CANCELED) { |
| 351 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); | 406 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); |
| 352 } else { | 407 } else { |
| 353 LOG(WARNING) << "Could not reach Google Accounts servers: errno " | 408 LOG(WARNING) << "Could not reach Google Accounts servers: errno " |
| 354 << status.os_error(); | 409 << status.os_error(); |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 505 void GaiaAuthFetcher::OnTokenAuthFetched(const std::string& data, | 560 void GaiaAuthFetcher::OnTokenAuthFetched(const std::string& data, |
| 506 const net::URLRequestStatus& status, | 561 const net::URLRequestStatus& status, |
| 507 int response_code) { | 562 int response_code) { |
| 508 if (status.is_success() && response_code == RC_REQUEST_OK) { | 563 if (status.is_success() && response_code == RC_REQUEST_OK) { |
| 509 consumer_->OnTokenAuthSuccess(data); | 564 consumer_->OnTokenAuthSuccess(data); |
| 510 } else { | 565 } else { |
| 511 consumer_->OnTokenAuthFailure(GenerateAuthError(data, status)); | 566 consumer_->OnTokenAuthFailure(GenerateAuthError(data, status)); |
| 512 } | 567 } |
| 513 } | 568 } |
| 514 | 569 |
| 570 void GaiaAuthFetcher::OnMergeSessionFetched(const std::string& data, | |
| 571 const net::URLRequestStatus& status, | |
| 572 int response_code) { | |
| 573 if (status.is_success() && response_code == RC_REQUEST_OK) { | |
| 574 consumer_->OnMergeSessionSuccess(data); | |
| 575 } else { | |
| 576 consumer_->OnMergeSessionFailure(GenerateAuthError(data, status)); | |
| 577 } | |
| 578 } | |
| 579 | |
| 515 void GaiaAuthFetcher::OnURLFetchComplete(const URLFetcher* source, | 580 void GaiaAuthFetcher::OnURLFetchComplete(const URLFetcher* source, |
| 516 const GURL& url, | 581 const GURL& url, |
| 517 const net::URLRequestStatus& status, | 582 const net::URLRequestStatus& status, |
| 518 int response_code, | 583 int response_code, |
| 519 const net::ResponseCookies& cookies, | 584 const net::ResponseCookies& cookies, |
| 520 const std::string& data) { | 585 const std::string& data) { |
| 521 fetch_pending_ = false; | 586 fetch_pending_ = false; |
| 522 if (url == client_login_gurl_) { | 587 if (url == client_login_gurl_) { |
| 523 OnClientLoginFetched(data, status, response_code); | 588 OnClientLoginFetched(data, status, response_code); |
| 524 } else if (url == issue_auth_token_gurl_) { | 589 } else if (url == issue_auth_token_gurl_) { |
| 525 OnIssueAuthTokenFetched(data, status, response_code); | 590 OnIssueAuthTokenFetched(data, status, response_code); |
| 526 } else if (url == get_user_info_gurl_) { | 591 } else if (url == get_user_info_gurl_) { |
| 527 OnGetUserInfoFetched(data, status, response_code); | 592 OnGetUserInfoFetched(data, status, response_code); |
| 528 } else if (url == token_auth_gurl_) { | 593 } else if (url == token_auth_gurl_) { |
| 529 OnTokenAuthFetched(data, status, response_code); | 594 OnTokenAuthFetched(data, status, response_code); |
| 595 } else if (url == merge_session_gurl_ || | |
| 596 (source && source->original_url() == merge_session_gurl_)) { | |
| 597 // MergeSession may redirect, so check the original URL of the fetcher. | |
| 598 OnMergeSessionFetched(data, status, response_code); | |
| 530 } else { | 599 } else { |
| 531 NOTREACHED(); | 600 NOTREACHED(); |
| 532 } | 601 } |
| 533 } | 602 } |
| 534 | 603 |
| 535 // static | 604 // static |
| 536 bool GaiaAuthFetcher::IsSecondFactorSuccess( | 605 bool GaiaAuthFetcher::IsSecondFactorSuccess( |
| 537 const std::string& alleged_error) { | 606 const std::string& alleged_error) { |
| 538 return alleged_error.find(kSecondFactor) != | 607 return alleged_error.find(kSecondFactor) != |
| 539 std::string::npos; | 608 std::string::npos; |
| 540 } | 609 } |
| OLD | NEW |