| 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/browser/net/gaia/gaia_oauth_fetcher.h" | 5 #include "chrome/browser/net/gaia/gaia_oauth_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 "https://www.google.com/accounts/OAuthLogin"; | 47 "https://www.google.com/accounts/OAuthLogin"; |
| 48 | 48 |
| 49 static const char kUserInfoUrl[] = | 49 static const char kUserInfoUrl[] = |
| 50 "https://www.googleapis.com/oauth2/v1/userinfo"; | 50 "https://www.googleapis.com/oauth2/v1/userinfo"; |
| 51 | 51 |
| 52 static const char kOAuthTokenCookie[] = "oauth_token"; | 52 static const char kOAuthTokenCookie[] = "oauth_token"; |
| 53 | 53 |
| 54 GaiaOAuthFetcher::GaiaOAuthFetcher(GaiaOAuthConsumer* consumer, | 54 GaiaOAuthFetcher::GaiaOAuthFetcher(GaiaOAuthConsumer* consumer, |
| 55 net::URLRequestContextGetter* getter, | 55 net::URLRequestContextGetter* getter, |
| 56 Profile* profile, | 56 Profile* profile, |
| 57 const std::string& service_name, |
| 57 const std::string& service_scope) | 58 const std::string& service_scope) |
| 58 : consumer_(consumer), | 59 : consumer_(consumer), |
| 59 getter_(getter), | 60 getter_(getter), |
| 60 profile_(profile), | 61 profile_(profile), |
| 62 popup_(NULL), |
| 63 service_name_(service_name), |
| 61 service_scope_(service_scope), | 64 service_scope_(service_scope), |
| 62 popup_(NULL), | |
| 63 fetch_pending_(false), | 65 fetch_pending_(false), |
| 64 auto_fetch_limit_(ALL_OAUTH_STEPS) {} | 66 auto_fetch_limit_(ALL_OAUTH_STEPS) {} |
| 65 | 67 |
| 66 GaiaOAuthFetcher::~GaiaOAuthFetcher() {} | 68 GaiaOAuthFetcher::~GaiaOAuthFetcher() {} |
| 67 | 69 |
| 68 bool GaiaOAuthFetcher::HasPendingFetch() { | 70 bool GaiaOAuthFetcher::HasPendingFetch() { |
| 69 return fetch_pending_; | 71 return fetch_pending_; |
| 70 } | 72 } |
| 71 | 73 |
| 72 void GaiaOAuthFetcher::CancelRequest() { | 74 void GaiaOAuthFetcher::CancelRequest() { |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 base::SplitStringIntoKeyValuePairs(data, '=', '&', &tokens); | 198 base::SplitStringIntoKeyValuePairs(data, '=', '&', &tokens); |
| 197 for (vector<pair<string, string> >::iterator i = tokens.begin(); | 199 for (vector<pair<string, string> >::iterator i = tokens.begin(); |
| 198 i != tokens.end(); ++i) { | 200 i != tokens.end(); ++i) { |
| 199 if (i->first == "oauth_token") { | 201 if (i->first == "oauth_token") { |
| 200 std::string decoded; | 202 std::string decoded; |
| 201 if (OAuthRequestSigner::Decode(i->second, &decoded)) | 203 if (OAuthRequestSigner::Decode(i->second, &decoded)) |
| 202 token->assign(decoded); | 204 token->assign(decoded); |
| 203 } | 205 } |
| 204 } | 206 } |
| 205 } | 207 } |
| 208 |
| 206 // Helper method that extracts tokens from a successful reply. | 209 // Helper method that extracts tokens from a successful reply. |
| 207 // static | 210 // static |
| 208 void GaiaOAuthFetcher::ParseOAuthLoginResponse( | 211 void GaiaOAuthFetcher::ParseOAuthLoginResponse( |
| 209 const std::string& data, | 212 const std::string& data, |
| 210 std::string* sid, | 213 std::string* sid, |
| 211 std::string* lsid, | 214 std::string* lsid, |
| 212 std::string* auth) { | 215 std::string* auth) { |
| 213 using std::vector; | 216 using std::vector; |
| 214 using std::pair; | 217 using std::pair; |
| 215 using std::string; | 218 using std::string; |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 false, | 401 false, |
| 399 this)); | 402 this)); |
| 400 fetch_pending_ = true; | 403 fetch_pending_ = true; |
| 401 fetcher_->Start(); | 404 fetcher_->Start(); |
| 402 } | 405 } |
| 403 | 406 |
| 404 void GaiaOAuthFetcher::StartOAuthWrapBridge( | 407 void GaiaOAuthFetcher::StartOAuthWrapBridge( |
| 405 const std::string& oauth1_access_token, | 408 const std::string& oauth1_access_token, |
| 406 const std::string& oauth1_access_token_secret, | 409 const std::string& oauth1_access_token_secret, |
| 407 const std::string& wrap_token_duration, | 410 const std::string& wrap_token_duration, |
| 408 const std::string& oauth2_scope) { | 411 const std::string& service_name, |
| 412 const std::string& service_scope) { |
| 409 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; | 413 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; |
| 410 | 414 |
| 411 std::string combined_oauth2_scope = oauth2_scope + " " + | 415 VLOG(1) << "Starting OAuthWrapBridge for: " << service_name; |
| 416 std::string combined_scope = service_scope + " " + |
| 412 kOAuthWrapBridgeUserInfoScope; | 417 kOAuthWrapBridgeUserInfoScope; |
| 418 service_name_ = service_name; |
| 413 | 419 |
| 414 // Must outlive fetcher_. | 420 // Must outlive fetcher_. |
| 415 request_body_ = MakeOAuthWrapBridgeBody( | 421 request_body_ = MakeOAuthWrapBridgeBody( |
| 416 oauth1_access_token, | 422 oauth1_access_token, |
| 417 oauth1_access_token_secret, | 423 oauth1_access_token_secret, |
| 418 wrap_token_duration, | 424 wrap_token_duration, |
| 419 combined_oauth2_scope); | 425 combined_scope); |
| 420 | 426 |
| 421 request_headers_ = ""; | 427 request_headers_ = ""; |
| 422 fetcher_.reset(CreateGaiaFetcher(getter_, | 428 fetcher_.reset(CreateGaiaFetcher(getter_, |
| 423 GURL(kOAuthWrapBridgeUrl), | 429 GURL(kOAuthWrapBridgeUrl), |
| 424 request_body_, | 430 request_body_, |
| 425 request_headers_, | 431 request_headers_, |
| 426 false, | 432 false, |
| 427 this)); | 433 this)); |
| 428 fetch_pending_ = true; | 434 fetch_pending_ = true; |
| 429 fetcher_->Start(); | 435 fetcher_->Start(); |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 const std::string& data, | 587 const std::string& data, |
| 582 const net::URLRequestStatus& status, | 588 const net::URLRequestStatus& status, |
| 583 int response_code) { | 589 int response_code) { |
| 584 if (status.is_success() && response_code == RC_REQUEST_OK) { | 590 if (status.is_success() && response_code == RC_REQUEST_OK) { |
| 585 VLOG(1) << "OAuth1 access token fetched."; | 591 VLOG(1) << "OAuth1 access token fetched."; |
| 586 std::string secret; | 592 std::string secret; |
| 587 std::string token; | 593 std::string token; |
| 588 ParseOAuthGetAccessTokenResponse(data, &token, &secret); | 594 ParseOAuthGetAccessTokenResponse(data, &token, &secret); |
| 589 consumer_->OnOAuthGetAccessTokenSuccess(token, secret); | 595 consumer_->OnOAuthGetAccessTokenSuccess(token, secret); |
| 590 if (ShouldAutoFetch(OAUTH2_SERVICE_ACCESS_TOKEN)) | 596 if (ShouldAutoFetch(OAUTH2_SERVICE_ACCESS_TOKEN)) |
| 591 StartOAuthWrapBridge(token, secret, "3600", service_scope_); | 597 StartOAuthWrapBridge( |
| 598 token, secret, "3600", service_name_, service_scope_); |
| 592 } else { | 599 } else { |
| 593 consumer_->OnOAuthGetAccessTokenFailure(GenerateAuthError(data, status)); | 600 consumer_->OnOAuthGetAccessTokenFailure(GenerateAuthError(data, status)); |
| 594 } | 601 } |
| 595 } | 602 } |
| 596 | 603 |
| 597 void GaiaOAuthFetcher::OnOAuthWrapBridgeFetched( | 604 void GaiaOAuthFetcher::OnOAuthWrapBridgeFetched( |
| 598 const std::string& data, | 605 const std::string& data, |
| 599 const net::URLRequestStatus& status, | 606 const net::URLRequestStatus& status, |
| 600 int response_code) { | 607 int response_code) { |
| 601 if (status.is_success() && response_code == RC_REQUEST_OK) { | 608 if (status.is_success() && response_code == RC_REQUEST_OK) { |
| 602 VLOG(1) << "OAuth2 access token fetched."; | 609 VLOG(1) << "OAuth2 access token fetched."; |
| 603 std::string token; | 610 std::string token; |
| 604 std::string expires_in; | 611 std::string expires_in; |
| 605 ParseOAuthWrapBridgeResponse(data, &token, &expires_in); | 612 ParseOAuthWrapBridgeResponse(data, &token, &expires_in); |
| 606 consumer_->OnOAuthWrapBridgeSuccess(token, expires_in); | 613 consumer_->OnOAuthWrapBridgeSuccess(service_name_, token, expires_in); |
| 607 if (ShouldAutoFetch(USER_INFO)) | 614 if (ShouldAutoFetch(USER_INFO)) |
| 608 StartUserInfo(token); | 615 StartUserInfo(token); |
| 609 } else { | 616 } else { |
| 610 consumer_->OnOAuthWrapBridgeFailure(GenerateAuthError(data, status)); | 617 consumer_->OnOAuthWrapBridgeFailure(GenerateAuthError(data, status)); |
| 611 } | 618 } |
| 612 } | 619 } |
| 613 | 620 |
| 614 void GaiaOAuthFetcher::OnUserInfoFetched( | 621 void GaiaOAuthFetcher::OnUserInfoFetched( |
| 615 const std::string& data, | 622 const std::string& data, |
| 616 const net::URLRequestStatus& status, | 623 const net::URLRequestStatus& status, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 645 } else if (url.spec() == kUserInfoUrl) { | 652 } else if (url.spec() == kUserInfoUrl) { |
| 646 OnUserInfoFetched(data, status, response_code); | 653 OnUserInfoFetched(data, status, response_code); |
| 647 } else { | 654 } else { |
| 648 NOTREACHED(); | 655 NOTREACHED(); |
| 649 } | 656 } |
| 650 } | 657 } |
| 651 | 658 |
| 652 bool GaiaOAuthFetcher::ShouldAutoFetch(AutoFetchLimit fetch_step) { | 659 bool GaiaOAuthFetcher::ShouldAutoFetch(AutoFetchLimit fetch_step) { |
| 653 return fetch_step <= auto_fetch_limit_; | 660 return fetch_step <= auto_fetch_limit_; |
| 654 } | 661 } |
| OLD | NEW |