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 "chrome/common/net/gaia/gaia_auth_fetcher.h" | 5 #include "chrome/common/net/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> |
11 | 11 |
12 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
13 #include "base/string_split.h" | 13 #include "base/string_split.h" |
14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
15 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "chrome/common/net/gaia/gaia_auth_consumer.h" | 17 #include "chrome/common/net/gaia/gaia_auth_consumer.h" |
18 #include "chrome/common/net/gaia/gaia_constants.h" | 18 #include "chrome/common/net/gaia/gaia_constants.h" |
19 #include "chrome/common/net/gaia/gaia_urls.h" | 19 #include "chrome/common/net/gaia/gaia_urls.h" |
20 #include "chrome/common/net/gaia/google_service_auth_error.h" | 20 #include "chrome/common/net/gaia/google_service_auth_error.h" |
21 #include "content/public/common/url_fetcher.h" | 21 #include "content/public/common/url_fetcher.h" |
22 #include "net/base/escape.h" | 22 #include "net/base/escape.h" |
23 #include "net/base/load_flags.h" | 23 #include "net/base/load_flags.h" |
24 #include "net/http/http_status_code.h" | 24 #include "net/http/http_status_code.h" |
25 #include "net/url_request/url_request_context_getter.h" | 25 #include "net/url_request/url_request_context_getter.h" |
26 #include "net/url_request/url_request_status.h" | 26 #include "net/url_request/url_request_status.h" |
27 | 27 |
28 namespace { | 28 namespace { |
| 29 const int kLoadFlagsIgnoreCookies = net::LOAD_DO_NOT_SEND_COOKIES | |
| 30 net::LOAD_DO_NOT_SAVE_COOKIES; |
| 31 |
29 static bool CookiePartsContains(const std::vector<std::string>& parts, | 32 static bool CookiePartsContains(const std::vector<std::string>& parts, |
30 const char* part) { | 33 const char* part) { |
31 return std::find(parts.begin(), parts.end(), part) != parts.end(); | 34 return std::find(parts.begin(), parts.end(), part) != parts.end(); |
32 } | 35 } |
33 } // namespace | 36 } // namespace |
34 | 37 |
35 // TODO(chron): Add sourceless version of this formatter. | 38 // TODO(chron): Add sourceless version of this formatter. |
36 // static | 39 // static |
37 const char GaiaAuthFetcher::kClientLoginFormat[] = | 40 const char GaiaAuthFetcher::kClientLoginFormat[] = |
38 "Email=%s&" | 41 "Email=%s&" |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 const int GaiaAuthFetcher::kClientLoginToOAuth2CookiePartCodePrefixLength = | 144 const int GaiaAuthFetcher::kClientLoginToOAuth2CookiePartCodePrefixLength = |
142 arraysize(GaiaAuthFetcher::kClientLoginToOAuth2CookiePartCodePrefix) - 1; | 145 arraysize(GaiaAuthFetcher::kClientLoginToOAuth2CookiePartCodePrefix) - 1; |
143 // static | 146 // static |
144 const char GaiaAuthFetcher::kOAuth2RefreshTokenKey[] = "refresh_token"; | 147 const char GaiaAuthFetcher::kOAuth2RefreshTokenKey[] = "refresh_token"; |
145 // static | 148 // static |
146 const char GaiaAuthFetcher::kOAuth2AccessTokenKey[] = "access_token"; | 149 const char GaiaAuthFetcher::kOAuth2AccessTokenKey[] = "access_token"; |
147 // static | 150 // static |
148 const char GaiaAuthFetcher::kOAuth2ExpiresInKey[] = "expires_in"; | 151 const char GaiaAuthFetcher::kOAuth2ExpiresInKey[] = "expires_in"; |
149 | 152 |
150 GaiaAuthFetcher::GaiaAuthFetcher(GaiaAuthConsumer* consumer, | 153 GaiaAuthFetcher::GaiaAuthFetcher(GaiaAuthConsumer* consumer, |
151 const std::string& source, | 154 const std::string& source, |
152 net::URLRequestContextGetter* getter) | 155 net::URLRequestContextGetter* getter) |
153 : consumer_(consumer), | 156 : consumer_(consumer), |
154 getter_(getter), | 157 getter_(getter), |
155 source_(source), | 158 source_(source), |
156 client_login_gurl_(GaiaUrls::GetInstance()->client_login_url()), | 159 client_login_gurl_(GaiaUrls::GetInstance()->client_login_url()), |
157 issue_auth_token_gurl_(GaiaUrls::GetInstance()->issue_auth_token_url()), | 160 issue_auth_token_gurl_(GaiaUrls::GetInstance()->issue_auth_token_url()), |
158 client_login_to_oauth2_gurl_( | 161 client_login_to_oauth2_gurl_( |
159 GaiaUrls::GetInstance()->client_login_to_oauth2_url()), | 162 GaiaUrls::GetInstance()->client_login_to_oauth2_url()), |
160 oauth2_token_gurl_(GaiaUrls::GetInstance()->oauth2_token_url()), | 163 oauth2_token_gurl_(GaiaUrls::GetInstance()->oauth2_token_url()), |
161 get_user_info_gurl_(GaiaUrls::GetInstance()->get_user_info_url()), | 164 get_user_info_gurl_(GaiaUrls::GetInstance()->get_user_info_url()), |
162 token_auth_gurl_(GaiaUrls::GetInstance()->token_auth_url()), | 165 token_auth_gurl_(GaiaUrls::GetInstance()->token_auth_url()), |
(...skipping 12 matching lines...) Expand all Loading... |
175 fetcher_.reset(); | 178 fetcher_.reset(); |
176 fetch_pending_ = false; | 179 fetch_pending_ = false; |
177 } | 180 } |
178 | 181 |
179 // static | 182 // static |
180 content::URLFetcher* GaiaAuthFetcher::CreateGaiaFetcher( | 183 content::URLFetcher* GaiaAuthFetcher::CreateGaiaFetcher( |
181 net::URLRequestContextGetter* getter, | 184 net::URLRequestContextGetter* getter, |
182 const std::string& body, | 185 const std::string& body, |
183 const std::string& headers, | 186 const std::string& headers, |
184 const GURL& gaia_gurl, | 187 const GURL& gaia_gurl, |
185 bool use_cookies, | 188 int load_flags, |
186 content::URLFetcherDelegate* delegate) { | 189 content::URLFetcherDelegate* delegate) { |
187 content::URLFetcher* to_return = content::URLFetcher::Create( | 190 content::URLFetcher* to_return = content::URLFetcher::Create( |
188 0, gaia_gurl, | 191 0, gaia_gurl, |
189 body == "" ? content::URLFetcher::GET : content::URLFetcher::POST, | 192 body == "" ? content::URLFetcher::GET : content::URLFetcher::POST, |
190 delegate); | 193 delegate); |
191 to_return->SetRequestContext(getter); | 194 to_return->SetRequestContext(getter); |
192 to_return->SetUploadData("application/x-www-form-urlencoded", body); | 195 to_return->SetUploadData("application/x-www-form-urlencoded", body); |
193 | 196 |
194 // The Gaia token exchange requests do not require any cookie-based | 197 // The Gaia token exchange requests do not require any cookie-based |
195 // identification as part of requests. We suppress sending any cookies to | 198 // identification as part of requests. We suppress sending any cookies to |
196 // maintain a separation between the user's browsing and Chrome's internal | 199 // maintain a separation between the user's browsing and Chrome's internal |
197 // services. Where such mixing is desired (MergeSession), it will be done | 200 // services. Where such mixing is desired (MergeSession), it will be done |
198 // explicitly. | 201 // explicitly. |
199 if (!use_cookies) { | 202 to_return->SetLoadFlags(load_flags); |
200 to_return->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 203 |
201 net::LOAD_DO_NOT_SAVE_COOKIES); | |
202 } | |
203 if (!headers.empty()) | 204 if (!headers.empty()) |
204 to_return->SetExtraRequestHeaders(headers); | 205 to_return->SetExtraRequestHeaders(headers); |
205 | 206 |
206 return to_return; | 207 return to_return; |
207 } | 208 } |
208 | 209 |
209 // static | 210 // static |
210 std::string GaiaAuthFetcher::MakeClientLoginBody( | 211 std::string GaiaAuthFetcher::MakeClientLoginBody( |
211 const std::string& username, | 212 const std::string& username, |
212 const std::string& password, | 213 const std::string& password, |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 password, | 472 password, |
472 source_, | 473 source_, |
473 service, | 474 service, |
474 login_token, | 475 login_token, |
475 login_captcha, | 476 login_captcha, |
476 allow_hosted_accounts); | 477 allow_hosted_accounts); |
477 fetcher_.reset(CreateGaiaFetcher(getter_, | 478 fetcher_.reset(CreateGaiaFetcher(getter_, |
478 request_body_, | 479 request_body_, |
479 "", | 480 "", |
480 client_login_gurl_, | 481 client_login_gurl_, |
481 false, | 482 kLoadFlagsIgnoreCookies, |
482 this)); | 483 this)); |
483 fetch_pending_ = true; | 484 fetch_pending_ = true; |
484 fetcher_->Start(); | 485 fetcher_->Start(); |
485 } | 486 } |
486 | 487 |
487 void GaiaAuthFetcher::StartIssueAuthToken(const std::string& sid, | 488 void GaiaAuthFetcher::StartIssueAuthToken(const std::string& sid, |
488 const std::string& lsid, | 489 const std::string& lsid, |
489 const char* const service) { | 490 const char* const service) { |
490 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; | 491 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; |
491 | 492 |
492 DVLOG(1) << "Starting IssueAuthToken for: " << service; | 493 DVLOG(1) << "Starting IssueAuthToken for: " << service; |
493 requested_service_ = service; | 494 requested_service_ = service; |
494 request_body_ = MakeIssueAuthTokenBody(sid, lsid, service); | 495 request_body_ = MakeIssueAuthTokenBody(sid, lsid, service); |
495 fetcher_.reset(CreateGaiaFetcher(getter_, | 496 fetcher_.reset(CreateGaiaFetcher(getter_, |
496 request_body_, | 497 request_body_, |
497 "", | 498 "", |
498 issue_auth_token_gurl_, | 499 issue_auth_token_gurl_, |
499 false, | 500 kLoadFlagsIgnoreCookies, |
500 this)); | 501 this)); |
501 fetch_pending_ = true; | 502 fetch_pending_ = true; |
502 fetcher_->Start(); | 503 fetcher_->Start(); |
503 } | 504 } |
504 | 505 |
505 void GaiaAuthFetcher::StartOAuthLoginTokenFetch( | 506 void GaiaAuthFetcher::StartOAuthLoginTokenFetch( |
506 const std::string& auth_token) { | 507 const std::string& auth_token) { |
507 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; | 508 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; |
508 | 509 |
509 DVLOG(1) << "Starting OAuth login token fetch"; | 510 DVLOG(1) << "Starting OAuth login token fetch"; |
510 request_body_ = MakeGetAuthCodeBody(); | 511 request_body_ = MakeGetAuthCodeBody(); |
| 512 |
| 513 // If no auth_token is given, then make sure to use the cookie jar with this |
| 514 // request. Otherwise the token contains all the necessary information and |
| 515 // the cookie jar should not be touched. |
| 516 int load_flags = net::LOAD_NORMAL; |
| 517 std::string auth_code_header = ""; |
| 518 |
| 519 if (!auth_token.empty()) { |
| 520 load_flags = kLoadFlagsIgnoreCookies; |
| 521 auth_code_header = MakeGetAuthCodeHeader(auth_token); |
| 522 } |
| 523 |
511 fetcher_.reset(CreateGaiaFetcher(getter_, | 524 fetcher_.reset(CreateGaiaFetcher(getter_, |
512 request_body_, | 525 request_body_, |
513 MakeGetAuthCodeHeader(auth_token), | 526 auth_code_header, |
514 client_login_to_oauth2_gurl_, | 527 client_login_to_oauth2_gurl_, |
515 false, | 528 load_flags, |
516 this)); | 529 this)); |
517 fetch_pending_ = true; | 530 fetch_pending_ = true; |
518 fetcher_->Start(); | 531 fetcher_->Start(); |
519 } | 532 } |
520 | 533 |
521 void GaiaAuthFetcher::StartGetUserInfo(const std::string& lsid) { | 534 void GaiaAuthFetcher::StartGetUserInfo(const std::string& lsid) { |
522 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; | 535 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; |
523 | 536 |
524 DVLOG(1) << "Starting GetUserInfo for lsid=" << lsid; | 537 DVLOG(1) << "Starting GetUserInfo for lsid=" << lsid; |
525 request_body_ = MakeGetUserInfoBody(lsid); | 538 request_body_ = MakeGetUserInfoBody(lsid); |
526 fetcher_.reset(CreateGaiaFetcher(getter_, | 539 fetcher_.reset(CreateGaiaFetcher(getter_, |
527 request_body_, | 540 request_body_, |
528 "", | 541 "", |
529 get_user_info_gurl_, | 542 get_user_info_gurl_, |
530 false, | 543 kLoadFlagsIgnoreCookies, |
531 this)); | 544 this)); |
532 fetch_pending_ = true; | 545 fetch_pending_ = true; |
533 fetcher_->Start(); | 546 fetcher_->Start(); |
534 } | 547 } |
535 | 548 |
536 void GaiaAuthFetcher::StartTokenAuth(const std::string& auth_token) { | 549 void GaiaAuthFetcher::StartTokenAuth(const std::string& auth_token) { |
537 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; | 550 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; |
538 | 551 |
539 DVLOG(1) << "Starting TokenAuth with auth_token=" << auth_token; | 552 DVLOG(1) << "Starting TokenAuth with auth_token=" << auth_token; |
540 | 553 |
541 // The continue URL is a required parameter of the TokenAuth API, but in this | 554 // The continue URL is a required parameter of the TokenAuth API, but in this |
542 // case we don't actually need or want to navigate to it. Setting it to | 555 // case we don't actually need or want to navigate to it. Setting it to |
543 // an arbitrary Google URL. | 556 // an arbitrary Google URL. |
544 std::string continue_url("http://www.google.com"); | 557 std::string continue_url("http://www.google.com"); |
545 request_body_ = MakeTokenAuthBody(auth_token, continue_url, source_); | 558 request_body_ = MakeTokenAuthBody(auth_token, continue_url, source_); |
546 fetcher_.reset(CreateGaiaFetcher(getter_, | 559 fetcher_.reset(CreateGaiaFetcher(getter_, |
547 request_body_, | 560 request_body_, |
548 "", | 561 "", |
549 token_auth_gurl_, | 562 token_auth_gurl_, |
550 false, | 563 kLoadFlagsIgnoreCookies, |
551 this)); | 564 this)); |
552 fetch_pending_ = true; | 565 fetch_pending_ = true; |
553 fetcher_->Start(); | 566 fetcher_->Start(); |
554 } | 567 } |
555 | 568 |
556 void GaiaAuthFetcher::StartMergeSession(const std::string& auth_token) { | 569 void GaiaAuthFetcher::StartMergeSession(const std::string& auth_token) { |
557 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; | 570 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; |
558 | 571 |
559 DVLOG(1) << "Starting MergeSession with auth_token=" << auth_token; | 572 DVLOG(1) << "Starting MergeSession with auth_token=" << auth_token; |
560 | 573 |
561 // The continue URL is a required parameter of the MergeSession API, but in | 574 // The continue URL is a required parameter of the MergeSession API, but in |
562 // this case we don't actually need or want to navigate to it. Setting it to | 575 // this case we don't actually need or want to navigate to it. Setting it to |
563 // an arbitrary Google URL. | 576 // an arbitrary Google URL. |
564 // | 577 // |
565 // In order for the new session to be merged correctly, the server needs to | 578 // In order for the new session to be merged correctly, the server needs to |
566 // know what sessions already exist in the browser. The fetcher needs to be | 579 // know what sessions already exist in the browser. The fetcher needs to be |
567 // created such that it sends the cookies with the request, which is | 580 // created such that it sends the cookies with the request, which is |
568 // different from all other requests the fetcher can make. | 581 // different from all other requests the fetcher can make. |
569 std::string continue_url("http://www.google.com"); | 582 std::string continue_url("http://www.google.com"); |
570 request_body_ = MakeMergeSessionBody(auth_token, continue_url, source_); | 583 request_body_ = MakeMergeSessionBody(auth_token, continue_url, source_); |
571 fetcher_.reset(CreateGaiaFetcher(getter_, | 584 fetcher_.reset(CreateGaiaFetcher(getter_, |
572 request_body_, | 585 request_body_, |
573 "", | 586 "", |
574 merge_session_gurl_, | 587 merge_session_gurl_, |
575 true, | 588 net::LOAD_NORMAL, |
576 this)); | 589 this)); |
577 fetch_pending_ = true; | 590 fetch_pending_ = true; |
578 fetcher_->Start(); | 591 fetcher_->Start(); |
579 } | 592 } |
580 | 593 |
581 // static | 594 // static |
582 void GaiaAuthFetcher::StartUberAuthTokenFetch(const std::string& access_token) { | 595 void GaiaAuthFetcher::StartUberAuthTokenFetch(const std::string& access_token) { |
583 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; | 596 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; |
584 | 597 |
585 DVLOG(1) << "Starting StartUberAuthTokenFetch with access_token=" | 598 DVLOG(1) << "Starting StartUberAuthTokenFetch with access_token=" |
586 << access_token; | 599 << access_token; |
587 std::string authentication_header = | 600 std::string authentication_header = |
588 base::StringPrintf(kOAuthHeaderFormat, access_token.c_str()); | 601 base::StringPrintf(kOAuthHeaderFormat, access_token.c_str()); |
589 fetcher_.reset(CreateGaiaFetcher(getter_, | 602 fetcher_.reset(CreateGaiaFetcher(getter_, |
590 "", | 603 "", |
591 authentication_header, | 604 authentication_header, |
592 uberauth_token_gurl_, | 605 uberauth_token_gurl_, |
593 false, | 606 kLoadFlagsIgnoreCookies, |
594 this)); | 607 this)); |
595 fetch_pending_ = true; | 608 fetch_pending_ = true; |
596 fetcher_->Start(); | 609 fetcher_->Start(); |
597 } | 610 } |
598 | 611 |
599 // static | 612 // static |
600 GoogleServiceAuthError GaiaAuthFetcher::GenerateAuthError( | 613 GoogleServiceAuthError GaiaAuthFetcher::GenerateAuthError( |
601 const std::string& data, | 614 const std::string& data, |
602 const net::URLRequestStatus& status) { | 615 const net::URLRequestStatus& status) { |
603 if (!status.is_success()) { | 616 if (!status.is_success()) { |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
748 | 761 |
749 void GaiaAuthFetcher::StartOAuth2TokenPairFetch(const std::string& auth_code) { | 762 void GaiaAuthFetcher::StartOAuth2TokenPairFetch(const std::string& auth_code) { |
750 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; | 763 DCHECK(!fetch_pending_) << "Tried to fetch two things at once!"; |
751 | 764 |
752 DVLOG(1) << "Starting OAuth token pair fetch"; | 765 DVLOG(1) << "Starting OAuth token pair fetch"; |
753 request_body_ = MakeGetTokenPairBody(auth_code); | 766 request_body_ = MakeGetTokenPairBody(auth_code); |
754 fetcher_.reset(CreateGaiaFetcher(getter_, | 767 fetcher_.reset(CreateGaiaFetcher(getter_, |
755 request_body_, | 768 request_body_, |
756 "", | 769 "", |
757 oauth2_token_gurl_, | 770 oauth2_token_gurl_, |
758 false, | 771 kLoadFlagsIgnoreCookies, |
759 this)); | 772 this)); |
760 fetch_pending_ = true; | 773 fetch_pending_ = true; |
761 fetcher_->Start(); | 774 fetcher_->Start(); |
762 } | 775 } |
763 | 776 |
764 void GaiaAuthFetcher::OnOAuth2TokenPairFetched( | 777 void GaiaAuthFetcher::OnOAuth2TokenPairFetched( |
765 const std::string& data, | 778 const std::string& data, |
766 const net::URLRequestStatus& status, | 779 const net::URLRequestStatus& status, |
767 int response_code) { | 780 int response_code) { |
768 if (status.is_success() && response_code == net::HTTP_OK) { | 781 if (status.is_success() && response_code == net::HTTP_OK) { |
(...skipping 21 matching lines...) Expand all Loading... |
790 std::vector<std::pair<std::string, std::string> >::iterator i; | 803 std::vector<std::pair<std::string, std::string> >::iterator i; |
791 for (i = tokens.begin(); i != tokens.end(); ++i) { | 804 for (i = tokens.begin(); i != tokens.end(); ++i) { |
792 matches[i->first] = i->second; | 805 matches[i->first] = i->second; |
793 } | 806 } |
794 consumer_->OnGetUserInfoSuccess(matches); | 807 consumer_->OnGetUserInfoSuccess(matches); |
795 } else { | 808 } else { |
796 consumer_->OnGetUserInfoFailure(GenerateAuthError(data, status)); | 809 consumer_->OnGetUserInfoFailure(GenerateAuthError(data, status)); |
797 } | 810 } |
798 } | 811 } |
799 | 812 |
800 void GaiaAuthFetcher::OnTokenAuthFetched(const std::string& data, | 813 void GaiaAuthFetcher::OnTokenAuthFetched(const net::ResponseCookies& cookies, |
| 814 const std::string& data, |
801 const net::URLRequestStatus& status, | 815 const net::URLRequestStatus& status, |
802 int response_code) { | 816 int response_code) { |
803 if (status.is_success() && response_code == net::HTTP_OK) { | 817 if (status.is_success() && response_code == net::HTTP_OK) { |
804 consumer_->OnTokenAuthSuccess(data); | 818 consumer_->OnTokenAuthSuccess(cookies, data); |
805 } else { | 819 } else { |
806 consumer_->OnTokenAuthFailure(GenerateAuthError(data, status)); | 820 consumer_->OnTokenAuthFailure(GenerateAuthError(data, status)); |
807 } | 821 } |
808 } | 822 } |
809 | 823 |
810 void GaiaAuthFetcher::OnMergeSessionFetched(const std::string& data, | 824 void GaiaAuthFetcher::OnMergeSessionFetched(const std::string& data, |
811 const net::URLRequestStatus& status, | 825 const net::URLRequestStatus& status, |
812 int response_code) { | 826 int response_code) { |
813 if (status.is_success() && response_code == net::HTTP_OK) { | 827 if (status.is_success() && response_code == net::HTTP_OK) { |
814 consumer_->OnMergeSessionSuccess(data); | 828 consumer_->OnMergeSessionSuccess(data); |
815 } else { | 829 } else { |
816 consumer_->OnMergeSessionFailure(GenerateAuthError(data, status)); | 830 consumer_->OnMergeSessionFailure(GenerateAuthError(data, status)); |
817 } | 831 } |
818 } | 832 } |
819 | 833 |
820 void GaiaAuthFetcher::OnUberAuthTokenFetch(const std::string& data, | 834 void GaiaAuthFetcher::OnUberAuthTokenFetch(const std::string& data, |
821 const net::URLRequestStatus& status, | 835 const net::URLRequestStatus& status, |
822 int response_code) { | 836 int response_code) { |
823 if (status.is_success() && response_code == net::HTTP_OK) { | 837 if (status.is_success() && response_code == net::HTTP_OK) { |
824 consumer_->OnUberAuthTokenSuccess(data); | 838 consumer_->OnUberAuthTokenSuccess(data); |
825 } else { | 839 } else { |
826 consumer_->OnUberAuthTokenFailure(GenerateAuthError(data, status)); | 840 consumer_->OnUberAuthTokenFailure(GenerateAuthError(data, status)); |
827 } | 841 } |
828 } | 842 } |
829 | 843 |
830 void GaiaAuthFetcher::OnURLFetchComplete(const content::URLFetcher* source) { | 844 void GaiaAuthFetcher::OnURLFetchComplete(const content::URLFetcher* source) { |
831 fetch_pending_ = false; | 845 fetch_pending_ = false; |
832 const GURL& url = source->GetURL(); | 846 // Some of the GAIA requests perform redirects, which results in the final |
| 847 // URL of the fetcher not being the original URL requested. Therefore use |
| 848 // the original URL when determining which OnXXX function to call. |
| 849 const GURL& url = source->GetOriginalURL(); |
833 const net::URLRequestStatus& status = source->GetStatus(); | 850 const net::URLRequestStatus& status = source->GetStatus(); |
834 int response_code = source->GetResponseCode(); | 851 int response_code = source->GetResponseCode(); |
835 std::string data; | 852 std::string data; |
836 source->GetResponseAsString(&data); | 853 source->GetResponseAsString(&data); |
837 if (url == client_login_gurl_) { | 854 if (url == client_login_gurl_) { |
838 OnClientLoginFetched(data, status, response_code); | 855 OnClientLoginFetched(data, status, response_code); |
839 } else if (url == issue_auth_token_gurl_) { | 856 } else if (url == issue_auth_token_gurl_) { |
840 OnIssueAuthTokenFetched(data, status, response_code); | 857 OnIssueAuthTokenFetched(data, status, response_code); |
841 } else if (url == client_login_to_oauth2_gurl_) { | 858 } else if (url == client_login_to_oauth2_gurl_) { |
842 OnClientLoginToOAuth2Fetched( | 859 OnClientLoginToOAuth2Fetched( |
843 data, source->GetCookies(), status, response_code); | 860 data, source->GetCookies(), status, response_code); |
844 } else if (url == oauth2_token_gurl_) { | 861 } else if (url == oauth2_token_gurl_) { |
845 OnOAuth2TokenPairFetched(data, status, response_code); | 862 OnOAuth2TokenPairFetched(data, status, response_code); |
846 } else if (url == get_user_info_gurl_) { | 863 } else if (url == get_user_info_gurl_) { |
847 OnGetUserInfoFetched(data, status, response_code); | 864 OnGetUserInfoFetched(data, status, response_code); |
848 } else if (url == token_auth_gurl_) { | 865 } else if (url == token_auth_gurl_) { |
849 OnTokenAuthFetched(data, status, response_code); | 866 OnTokenAuthFetched(source->GetCookies(), data, status, response_code); |
850 } else if (url == merge_session_gurl_ || | 867 } else if (url == merge_session_gurl_) { |
851 (source && source->GetOriginalURL() == merge_session_gurl_)) { | |
852 // MergeSession may redirect, so check the original URL of the fetcher. | |
853 OnMergeSessionFetched(data, status, response_code); | 868 OnMergeSessionFetched(data, status, response_code); |
854 } else if (url == uberauth_token_gurl_) { | 869 } else if (url == uberauth_token_gurl_) { |
855 OnUberAuthTokenFetch(data, status, response_code); | 870 OnUberAuthTokenFetch(data, status, response_code); |
856 } else { | 871 } else { |
857 NOTREACHED(); | 872 NOTREACHED(); |
858 } | 873 } |
859 } | 874 } |
860 | 875 |
861 // static | 876 // static |
862 bool GaiaAuthFetcher::IsSecondFactorSuccess( | 877 bool GaiaAuthFetcher::IsSecondFactorSuccess( |
863 const std::string& alleged_error) { | 878 const std::string& alleged_error) { |
864 return alleged_error.find(kSecondFactor) != | 879 return alleged_error.find(kSecondFactor) != |
865 std::string::npos; | 880 std::string::npos; |
866 } | 881 } |
OLD | NEW |