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
3 // found in the LICENSE file.
5 #include "google_apis/gaia/gaia_auth_fetcher.h"
12 #include "base/json/json_reader.h"
13 #include "base/json/json_writer.h"
14 #include "base/string_split.h"
15 #include "base/string_util.h"
16 #include "base/stringprintf.h"
17 #include "base/values.h"
18 #include "google_apis/gaia/gaia_auth_consumer.h"
19 #include "google_apis/gaia/gaia_constants.h"
20 #include "google_apis/gaia/gaia_urls.h"
21 #include "google_apis/gaia/google_service_auth_error.h"
22 #include "net/base/escape.h"
23 #include "net/base/load_flags.h"
24 #include "net/http/http_status_code.h"
25 #include "net/url_request/url_fetcher.h"
26 #include "net/url_request/url_request_context_getter.h"
27 #include "net/url_request/url_request_status.h"
30 const int kLoadFlagsIgnoreCookies
= net::LOAD_DO_NOT_SEND_COOKIES
|
31 net::LOAD_DO_NOT_SAVE_COOKIES
;
33 static bool CookiePartsContains(const std::vector
<std::string
>& parts
,
35 return std::find(parts
.begin(), parts
.end(), part
) != parts
.end();
38 bool ExtractOAuth2TokenPairResponse(DictionaryValue
* dict
,
39 std::string
* refresh_token
,
40 std::string
* access_token
,
41 int* expires_in_secs
) {
42 DCHECK(refresh_token
);
44 DCHECK(expires_in_secs
);
46 if (!dict
->GetStringWithoutPathExpansion("refresh_token", refresh_token
) ||
47 !dict
->GetStringWithoutPathExpansion("access_token", access_token
) ||
48 !dict
->GetIntegerWithoutPathExpansion("expires_in", expires_in_secs
)) {
57 // TODO(chron): Add sourceless version of this formatter.
59 const char GaiaAuthFetcher::kClientLoginFormat
[] =
62 "PersistentCookie=%s&"
67 const char GaiaAuthFetcher::kClientLoginCaptchaFormat
[] =
70 "PersistentCookie=%s&"
77 const char GaiaAuthFetcher::kIssueAuthTokenFormat
[] =
83 const char GaiaAuthFetcher::kClientLoginToOAuth2BodyFormat
[] =
84 "scope=%s&client_id=%s";
86 const char GaiaAuthFetcher::kOAuth2CodeToTokenPairBodyFormat
[] =
88 "grant_type=authorization_code&"
93 const char GaiaAuthFetcher::kGetUserInfoFormat
[] =
96 const char GaiaAuthFetcher::kMergeSessionFormat
[] =
101 const char GaiaAuthFetcher::kUberAuthTokenURLFormat
[] =
105 const char GaiaAuthFetcher::kOAuthLoginFormat
[] = "service=%s&source=%s";
108 const char GaiaAuthFetcher::kAccountDeletedError
[] = "AccountDeleted";
109 const char GaiaAuthFetcher::kAccountDeletedErrorCode
[] = "adel";
111 const char GaiaAuthFetcher::kAccountDisabledError
[] = "AccountDisabled";
112 const char GaiaAuthFetcher::kAccountDisabledErrorCode
[] = "adis";
114 const char GaiaAuthFetcher::kBadAuthenticationError
[] = "BadAuthentication";
115 const char GaiaAuthFetcher::kBadAuthenticationErrorCode
[] = "badauth";
117 const char GaiaAuthFetcher::kCaptchaError
[] = "CaptchaRequired";
118 const char GaiaAuthFetcher::kCaptchaErrorCode
[] = "cr";
120 const char GaiaAuthFetcher::kServiceUnavailableError
[] =
121 "ServiceUnavailable";
122 const char GaiaAuthFetcher::kServiceUnavailableErrorCode
[] =
125 const char GaiaAuthFetcher::kErrorParam
[] = "Error";
127 const char GaiaAuthFetcher::kErrorUrlParam
[] = "Url";
129 const char GaiaAuthFetcher::kCaptchaUrlParam
[] = "CaptchaUrl";
131 const char GaiaAuthFetcher::kCaptchaTokenParam
[] = "CaptchaToken";
134 const char GaiaAuthFetcher::kNeedsAdditional
[] = "NeedsAdditional";
136 const char GaiaAuthFetcher::kCaptcha
[] = "Captcha";
138 const char GaiaAuthFetcher::kTwoFactor
[] = "TwoStep";
141 const char GaiaAuthFetcher::kCookiePersistence
[] = "true";
143 // TODO(johnnyg): When hosted accounts are supported by sync,
144 // we can always use "HOSTED_OR_GOOGLE"
145 const char GaiaAuthFetcher::kAccountTypeHostedOrGoogle
[] =
147 const char GaiaAuthFetcher::kAccountTypeGoogle
[] =
151 const char GaiaAuthFetcher::kSecondFactor
[] = "Info=InvalidSecondFactor";
154 const char GaiaAuthFetcher::kAuthHeaderFormat
[] =
155 "Authorization: GoogleLogin auth=%s";
157 const char GaiaAuthFetcher::kOAuthHeaderFormat
[] = "Authorization: OAuth %s";
159 const char GaiaAuthFetcher::kClientLoginToOAuth2CookiePartSecure
[] = "Secure";
161 const char GaiaAuthFetcher::kClientLoginToOAuth2CookiePartHttpOnly
[] =
164 const char GaiaAuthFetcher::kClientLoginToOAuth2CookiePartCodePrefix
[] =
167 const int GaiaAuthFetcher::kClientLoginToOAuth2CookiePartCodePrefixLength
=
168 arraysize(GaiaAuthFetcher::kClientLoginToOAuth2CookiePartCodePrefix
) - 1;
170 GaiaAuthFetcher::GaiaAuthFetcher(GaiaAuthConsumer
* consumer
,
171 const std::string
& source
,
172 net::URLRequestContextGetter
* getter
)
173 : consumer_(consumer
),
176 client_login_gurl_(GaiaUrls::GetInstance()->client_login_url()),
177 issue_auth_token_gurl_(GaiaUrls::GetInstance()->issue_auth_token_url()),
178 oauth2_token_gurl_(GaiaUrls::GetInstance()->oauth2_token_url()),
179 get_user_info_gurl_(GaiaUrls::GetInstance()->get_user_info_url()),
180 merge_session_gurl_(GaiaUrls::GetInstance()->merge_session_url()),
181 uberauth_token_gurl_(base::StringPrintf(kUberAuthTokenURLFormat
,
182 GaiaUrls::GetInstance()->oauth1_login_url().c_str(), source
.c_str())),
183 client_oauth_gurl_(GaiaUrls::GetInstance()->client_oauth_url()),
184 oauth_login_gurl_(GaiaUrls::GetInstance()->oauth1_login_url()),
185 client_login_to_oauth2_gurl_(
186 GaiaUrls::GetInstance()->client_login_to_oauth2_url()),
187 fetch_pending_(false) {}
189 GaiaAuthFetcher::~GaiaAuthFetcher() {}
191 bool GaiaAuthFetcher::HasPendingFetch() {
192 return fetch_pending_
;
195 void GaiaAuthFetcher::CancelRequest() {
197 fetch_pending_
= false;
201 net::URLFetcher
* GaiaAuthFetcher::CreateGaiaFetcher(
202 net::URLRequestContextGetter
* getter
,
203 const std::string
& body
,
204 const std::string
& headers
,
205 const GURL
& gaia_gurl
,
207 net::URLFetcherDelegate
* delegate
) {
208 net::URLFetcher
* to_return
= net::URLFetcher::Create(
210 body
== "" ? net::URLFetcher::GET
: net::URLFetcher::POST
,
212 to_return
->SetRequestContext(getter
);
213 to_return
->SetUploadData("application/x-www-form-urlencoded", body
);
215 DVLOG(2) << "Gaia fetcher URL: " << gaia_gurl
.spec();
216 DVLOG(2) << "Gaia fetcher headers: " << headers
;
217 DVLOG(2) << "Gaia fetcher body: " << body
;
219 // The Gaia token exchange requests do not require any cookie-based
220 // identification as part of requests. We suppress sending any cookies to
221 // maintain a separation between the user's browsing and Chrome's internal
222 // services. Where such mixing is desired (MergeSession), it will be done
224 to_return
->SetLoadFlags(load_flags
);
226 if (!headers
.empty())
227 to_return
->SetExtraRequestHeaders(headers
);
233 std::string
GaiaAuthFetcher::MakeClientLoginBody(
234 const std::string
& username
,
235 const std::string
& password
,
236 const std::string
& source
,
238 const std::string
& login_token
,
239 const std::string
& login_captcha
,
240 HostedAccountsSetting allow_hosted_accounts
) {
241 std::string encoded_username
= net::EscapeUrlEncodedData(username
, true);
242 std::string encoded_password
= net::EscapeUrlEncodedData(password
, true);
243 std::string encoded_login_token
= net::EscapeUrlEncodedData(login_token
,
245 std::string encoded_login_captcha
= net::EscapeUrlEncodedData(login_captcha
,
248 const char* account_type
= allow_hosted_accounts
== HostedAccountsAllowed
?
249 kAccountTypeHostedOrGoogle
:
252 if (login_token
.empty() || login_captcha
.empty()) {
253 return base::StringPrintf(kClientLoginFormat
,
254 encoded_username
.c_str(),
255 encoded_password
.c_str(),
262 return base::StringPrintf(kClientLoginCaptchaFormat
,
263 encoded_username
.c_str(),
264 encoded_password
.c_str(),
269 encoded_login_token
.c_str(),
270 encoded_login_captcha
.c_str());
274 std::string
GaiaAuthFetcher::MakeIssueAuthTokenBody(
275 const std::string
& sid
,
276 const std::string
& lsid
,
277 const char* const service
) {
278 std::string encoded_sid
= net::EscapeUrlEncodedData(sid
, true);
279 std::string encoded_lsid
= net::EscapeUrlEncodedData(lsid
, true);
281 // All tokens should be session tokens except the gaia auth token.
283 if (!strcmp(service
, GaiaConstants::kGaiaService
))
286 return base::StringPrintf(kIssueAuthTokenFormat
,
288 encoded_lsid
.c_str(),
290 session
? "true" : "false");
294 std::string
GaiaAuthFetcher::MakeGetAuthCodeBody() {
295 std::string encoded_scope
= net::EscapeUrlEncodedData(
296 GaiaUrls::GetInstance()->oauth1_login_scope(), true);
297 std::string encoded_client_id
= net::EscapeUrlEncodedData(
298 GaiaUrls::GetInstance()->oauth2_chrome_client_id(), true);
299 return StringPrintf(kClientLoginToOAuth2BodyFormat
,
300 encoded_scope
.c_str(),
301 encoded_client_id
.c_str());
305 std::string
GaiaAuthFetcher::MakeGetTokenPairBody(
306 const std::string
& auth_code
) {
307 std::string encoded_scope
= net::EscapeUrlEncodedData(
308 GaiaUrls::GetInstance()->oauth1_login_scope(), true);
309 std::string encoded_client_id
= net::EscapeUrlEncodedData(
310 GaiaUrls::GetInstance()->oauth2_chrome_client_id(), true);
311 std::string encoded_client_secret
= net::EscapeUrlEncodedData(
312 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), true);
313 std::string encoded_auth_code
= net::EscapeUrlEncodedData(auth_code
, true);
314 return StringPrintf(kOAuth2CodeToTokenPairBodyFormat
,
315 encoded_scope
.c_str(),
316 encoded_client_id
.c_str(),
317 encoded_client_secret
.c_str(),
318 encoded_auth_code
.c_str());
322 std::string
GaiaAuthFetcher::MakeGetUserInfoBody(const std::string
& lsid
) {
323 std::string encoded_lsid
= net::EscapeUrlEncodedData(lsid
, true);
324 return base::StringPrintf(kGetUserInfoFormat
, encoded_lsid
.c_str());
328 std::string
GaiaAuthFetcher::MakeMergeSessionBody(
329 const std::string
& auth_token
,
330 const std::string
& continue_url
,
331 const std::string
& source
) {
332 std::string encoded_auth_token
= net::EscapeUrlEncodedData(auth_token
, true);
333 std::string encoded_continue_url
= net::EscapeUrlEncodedData(continue_url
,
335 std::string encoded_source
= net::EscapeUrlEncodedData(source
, true);
336 return base::StringPrintf(kMergeSessionFormat
,
337 encoded_auth_token
.c_str(),
338 encoded_continue_url
.c_str(),
339 encoded_source
.c_str());
343 std::string
GaiaAuthFetcher::MakeGetAuthCodeHeader(
344 const std::string
& auth_token
) {
345 return StringPrintf(kAuthHeaderFormat
, auth_token
.c_str());
348 // Helper method that extracts tokens from a successful reply.
350 void GaiaAuthFetcher::ParseClientLoginResponse(const std::string
& data
,
353 std::string
* token
) {
358 vector
<pair
<string
, string
> > tokens
;
359 base::SplitStringIntoKeyValuePairs(data
, '=', '\n', &tokens
);
360 for (vector
<pair
<string
, string
> >::iterator i
= tokens
.begin();
361 i
!= tokens
.end(); ++i
) {
362 if (i
->first
== "SID") {
363 sid
->assign(i
->second
);
364 } else if (i
->first
== "LSID") {
365 lsid
->assign(i
->second
);
366 } else if (i
->first
== "Auth") {
367 token
->assign(i
->second
);
373 std::string
GaiaAuthFetcher::MakeClientOAuthBody(
374 const std::string
& username
,
375 const std::string
& password
,
376 const std::vector
<std::string
>& scopes
,
377 const std::string
& persistent_id
,
378 const std::string
& friendly_name
,
379 const std::string
& locale
) {
380 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue
);
381 dict
->SetString(GaiaConstants::kClientOAuthEmailKey
, username
);
382 dict
->SetString(GaiaConstants::kClientOAuthPasswordKey
, password
);
384 scoped_ptr
<base::ListValue
> scope_list(new base::ListValue
);
385 for (size_t i
= 0; i
< scopes
.size(); ++i
)
386 scope_list
->Append(base::Value::CreateStringValue(scopes
[i
]));
387 dict
->Set(GaiaConstants::kClientOAuthScopesKey
, scope_list
.release());
389 dict
->SetString(GaiaConstants::kClientOAuthOAuth2ClientIdKey
,
390 GaiaUrls::GetInstance()->oauth2_chrome_client_id());
391 // crbug.com/129600: use a less generic friendly name.
392 dict
->SetString(GaiaConstants::kClientOAuthFriendlyDeviceNameKey
,
395 scoped_ptr
<base::ListValue
> accepts_challenge_list(new base::ListValue
);
396 accepts_challenge_list
->Append(base::Value::CreateStringValue(kCaptcha
));
397 accepts_challenge_list
->Append(base::Value::CreateStringValue(kTwoFactor
));
398 dict
->Set(GaiaConstants::kClientOAuthAcceptsChallengesKey
,
399 accepts_challenge_list
.release());
401 dict
->SetString(GaiaConstants::kClientOAuthLocaleKey
, locale
);
402 // Chrome presently does not not support a web-fallback for ClientOAuth,
403 // but need to hardcode an arbitrary one here since the endpoint expects it.
404 dict
->SetString(GaiaConstants::kClientOAuthFallbackNameKey
, "GetOAuth2Token");
406 std::string json_string
;
407 base::JSONWriter::Write(dict
.get(), &json_string
);
412 std::string
GaiaAuthFetcher::MakeClientOAuthChallengeResponseBody(
413 const std::string
& name
,
414 const std::string
& token
,
415 const std::string
& solution
) {
416 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue
);
417 std::string field_name
= name
== kTwoFactor
? "otp" : "solution";
419 scoped_ptr
<base::DictionaryValue
> challenge_reply(new base::DictionaryValue
);
420 challenge_reply
->SetString(GaiaConstants::kClientOAuthNameKey
, name
);
421 challenge_reply
->SetString(GaiaConstants::kClientOAuthChallengeTokenKey
,
423 challenge_reply
->SetString(field_name
, solution
);
424 dict
->Set(GaiaConstants::kClientOAuthchallengeReplyKey
,
425 challenge_reply
.release());
427 std::string json_string
;
428 base::JSONWriter::Write(dict
.get(), &json_string
);
433 std::string
GaiaAuthFetcher::MakeOAuthLoginBody(const std::string
& service
,
434 const std::string
& source
) {
435 std::string encoded_service
= net::EscapeUrlEncodedData(service
, true);
436 std::string encoded_source
= net::EscapeUrlEncodedData(source
, true);
437 return StringPrintf(kOAuthLoginFormat
, encoded_service
.c_str(),
438 encoded_source
.c_str());
442 void GaiaAuthFetcher::ParseClientLoginFailure(const std::string
& data
,
444 std::string
* error_url
,
445 std::string
* captcha_url
,
446 std::string
* captcha_token
) {
451 vector
<pair
<string
, string
> > tokens
;
452 base::SplitStringIntoKeyValuePairs(data
, '=', '\n', &tokens
);
453 for (vector
<pair
<string
, string
> >::iterator i
= tokens
.begin();
454 i
!= tokens
.end(); ++i
) {
455 if (i
->first
== kErrorParam
) {
456 error
->assign(i
->second
);
457 } else if (i
->first
== kErrorUrlParam
) {
458 error_url
->assign(i
->second
);
459 } else if (i
->first
== kCaptchaUrlParam
) {
460 captcha_url
->assign(i
->second
);
461 } else if (i
->first
== kCaptchaTokenParam
) {
462 captcha_token
->assign(i
->second
);
468 bool GaiaAuthFetcher::ParseClientLoginToOAuth2Response(
469 const net::ResponseCookies
& cookies
,
470 std::string
* auth_code
) {
472 net::ResponseCookies::const_iterator iter
;
473 for (iter
= cookies
.begin(); iter
!= cookies
.end(); ++iter
) {
474 if (ParseClientLoginToOAuth2Cookie(*iter
, auth_code
))
481 bool GaiaAuthFetcher::ParseClientLoginToOAuth2Cookie(const std::string
& cookie
,
482 std::string
* auth_code
) {
483 std::vector
<std::string
> parts
;
484 base::SplitString(cookie
, ';', &parts
);
485 // Per documentation, the cookie should have Secure and HttpOnly.
486 if (!CookiePartsContains(parts
, kClientLoginToOAuth2CookiePartSecure
) ||
487 !CookiePartsContains(parts
, kClientLoginToOAuth2CookiePartHttpOnly
)) {
491 std::vector
<std::string
>::const_iterator iter
;
492 for (iter
= parts
.begin(); iter
!= parts
.end(); ++iter
) {
493 const std::string
& part
= *iter
;
495 part
, kClientLoginToOAuth2CookiePartCodePrefix
, false)) {
496 auth_code
->assign(part
.substr(
497 kClientLoginToOAuth2CookiePartCodePrefixLength
));
505 GoogleServiceAuthError
506 GaiaAuthFetcher::GenerateClientOAuthError(const std::string
& data
,
507 const net::URLRequestStatus
& status
) {
508 scoped_ptr
<base::Value
> value(base::JSONReader::Read(data
));
509 if (!value
.get() || value
->GetType() != base::Value::TYPE_DICTIONARY
)
510 return GenerateAuthError(data
, status
);
511 DictionaryValue
* dict
= static_cast<DictionaryValue
*>(value
.get());
514 if (!dict
->GetStringWithoutPathExpansion("cause", &cause
))
515 return GoogleServiceAuthError::FromClientOAuthError(data
);
517 if (cause
!= kNeedsAdditional
)
518 return GoogleServiceAuthError::FromClientOAuthError(data
);
520 DictionaryValue
* challenge
;
521 if (!dict
->GetDictionaryWithoutPathExpansion("challenge", &challenge
))
522 return GoogleServiceAuthError::FromClientOAuthError(data
);
525 if (!challenge
->GetStringWithoutPathExpansion("name", &name
))
526 return GoogleServiceAuthError::FromClientOAuthError(data
);
528 if (name
== kCaptcha
) {
530 std::string audio_url
;
531 std::string image_url
;
534 if (!challenge
->GetStringWithoutPathExpansion("challenge_token", &token
) ||
535 !challenge
->GetStringWithoutPathExpansion("audio_url", &audio_url
) ||
536 !challenge
->GetStringWithoutPathExpansion("image_url", &image_url
) ||
537 !challenge
->GetIntegerWithoutPathExpansion("image_width",
539 !challenge
->GetIntegerWithoutPathExpansion("image_height",
541 return GoogleServiceAuthError::FromClientOAuthError(data
);
543 return GoogleServiceAuthError::FromCaptchaChallenge(token
, GURL(audio_url
),
547 } else if (name
== kTwoFactor
) {
549 std::string prompt_text
;
550 std::string alternate_text
;
553 // The protocol doc says these are required, but in practice they are not
554 // returned. So only a missing challenge token will cause an error here.
555 challenge
->GetStringWithoutPathExpansion("prompt_text", &prompt_text
);
556 challenge
->GetStringWithoutPathExpansion("alternate_text", &alternate_text
);
557 challenge
->GetIntegerWithoutPathExpansion("field_length", &field_length
);
558 if (!challenge
->GetStringWithoutPathExpansion("challenge_token", &token
))
559 return GoogleServiceAuthError::FromClientOAuthError(data
);
561 return GoogleServiceAuthError::FromSecondFactorChallenge(token
, prompt_text
,
566 return GoogleServiceAuthError::FromClientOAuthError(data
);
569 void GaiaAuthFetcher::StartClientLogin(
570 const std::string
& username
,
571 const std::string
& password
,
572 const char* const service
,
573 const std::string
& login_token
,
574 const std::string
& login_captcha
,
575 HostedAccountsSetting allow_hosted_accounts
) {
577 DCHECK(!fetch_pending_
) << "Tried to fetch two things at once!";
579 // This class is thread agnostic, so be sure to call this only on the
580 // same thread each time.
581 DVLOG(1) << "Starting new ClientLogin fetch for:" << username
;
583 // Must outlive fetcher_.
584 request_body_
= MakeClientLoginBody(username
,
590 allow_hosted_accounts
);
591 fetcher_
.reset(CreateGaiaFetcher(getter_
,
595 kLoadFlagsIgnoreCookies
,
597 fetch_pending_
= true;
601 void GaiaAuthFetcher::StartIssueAuthToken(const std::string
& sid
,
602 const std::string
& lsid
,
603 const char* const service
) {
604 DCHECK(!fetch_pending_
) << "Tried to fetch two things at once!";
606 DVLOG(1) << "Starting IssueAuthToken for: " << service
;
607 requested_service_
= service
;
608 request_body_
= MakeIssueAuthTokenBody(sid
, lsid
, service
);
609 fetcher_
.reset(CreateGaiaFetcher(getter_
,
612 issue_auth_token_gurl_
,
613 kLoadFlagsIgnoreCookies
,
615 fetch_pending_
= true;
619 void GaiaAuthFetcher::StartLsoForOAuthLoginTokenExchange(
620 const std::string
& auth_token
) {
621 DCHECK(!fetch_pending_
) << "Tried to fetch two things at once!";
623 DVLOG(1) << "Starting OAuth login token exchange with auth_token";
624 request_body_
= MakeGetAuthCodeBody();
625 client_login_to_oauth2_gurl_
=
626 GURL(GaiaUrls::GetInstance()->client_login_to_oauth2_url());
628 fetcher_
.reset(CreateGaiaFetcher(getter_
,
630 MakeGetAuthCodeHeader(auth_token
),
631 client_login_to_oauth2_gurl_
,
632 kLoadFlagsIgnoreCookies
,
634 fetch_pending_
= true;
638 void GaiaAuthFetcher::StartCookieForOAuthLoginTokenExchange(
639 const std::string
& session_index
) {
640 DCHECK(!fetch_pending_
) << "Tried to fetch two things at once!";
642 DVLOG(1) << "Starting OAuth login token fetch with cookie jar";
643 request_body_
= MakeGetAuthCodeBody();
645 std::string url
= GaiaUrls::GetInstance()->client_login_to_oauth2_url();
646 if (!session_index
.empty())
647 url
+= "?authuser=" + session_index
;
649 client_login_to_oauth2_gurl_
= GURL(url
);
651 fetcher_
.reset(CreateGaiaFetcher(getter_
,
654 client_login_to_oauth2_gurl_
,
657 fetch_pending_
= true;
661 void GaiaAuthFetcher::StartGetUserInfo(const std::string
& lsid
) {
662 DCHECK(!fetch_pending_
) << "Tried to fetch two things at once!";
664 DVLOG(1) << "Starting GetUserInfo for lsid=" << lsid
;
665 request_body_
= MakeGetUserInfoBody(lsid
);
666 fetcher_
.reset(CreateGaiaFetcher(getter_
,
670 kLoadFlagsIgnoreCookies
,
672 fetch_pending_
= true;
676 void GaiaAuthFetcher::StartMergeSession(const std::string
& uber_token
) {
677 DCHECK(!fetch_pending_
) << "Tried to fetch two things at once!";
679 DVLOG(1) << "Starting MergeSession with uber_token=" << uber_token
;
681 // The continue URL is a required parameter of the MergeSession API, but in
682 // this case we don't actually need or want to navigate to it. Setting it to
683 // an arbitrary Google URL.
685 // In order for the new session to be merged correctly, the server needs to
686 // know what sessions already exist in the browser. The fetcher needs to be
687 // created such that it sends the cookies with the request, which is
688 // different from all other requests the fetcher can make.
689 std::string
continue_url("http://www.google.com");
690 request_body_
= MakeMergeSessionBody(uber_token
, continue_url
, source_
);
691 fetcher_
.reset(CreateGaiaFetcher(getter_
,
697 fetch_pending_
= true;
701 void GaiaAuthFetcher::StartTokenFetchForUberAuthExchange(
702 const std::string
& access_token
) {
703 DCHECK(!fetch_pending_
) << "Tried to fetch two things at once!";
705 DVLOG(1) << "Starting StartTokenFetchForUberAuthExchange with access_token="
707 std::string authentication_header
=
708 base::StringPrintf(kOAuthHeaderFormat
, access_token
.c_str());
709 fetcher_
.reset(CreateGaiaFetcher(getter_
,
711 authentication_header
,
712 uberauth_token_gurl_
,
713 kLoadFlagsIgnoreCookies
,
715 fetch_pending_
= true;
719 void GaiaAuthFetcher::StartClientOAuth(const std::string
& username
,
720 const std::string
& password
,
721 const std::vector
<std::string
>& scopes
,
722 const std::string
& persistent_id
,
723 const std::string
& locale
) {
724 DCHECK(!fetch_pending_
) << "Tried to fetch two things at once!";
726 request_body_
= MakeClientOAuthBody(username
, password
, scopes
, persistent_id
,
728 fetcher_
.reset(CreateGaiaFetcher(getter_
,
732 kLoadFlagsIgnoreCookies
,
734 fetch_pending_
= true;
738 void GaiaAuthFetcher::StartClientOAuthChallengeResponse(
739 GoogleServiceAuthError::State type
,
740 const std::string
& token
,
741 const std::string
& solution
) {
742 DCHECK(!fetch_pending_
) << "Tried to fetch two things at once!";
746 case GoogleServiceAuthError::CAPTCHA_REQUIRED
:
749 case GoogleServiceAuthError::TWO_FACTOR
:
756 request_body_
= MakeClientOAuthChallengeResponseBody(name
, token
, solution
);
757 fetcher_
.reset(CreateGaiaFetcher(getter_
,
761 kLoadFlagsIgnoreCookies
,
763 fetch_pending_
= true;
767 void GaiaAuthFetcher::StartOAuthLogin(const std::string
& access_token
,
768 const std::string
& service
) {
769 DCHECK(!fetch_pending_
) << "Tried to fetch two things at once!";
771 request_body_
= MakeOAuthLoginBody(service
, source_
);
772 std::string authentication_header
=
773 base::StringPrintf("Authorization: Bearer %s", access_token
.c_str());
774 fetcher_
.reset(CreateGaiaFetcher(getter_
,
776 authentication_header
,
778 kLoadFlagsIgnoreCookies
,
780 fetch_pending_
= true;
785 GoogleServiceAuthError
GaiaAuthFetcher::GenerateAuthError(
786 const std::string
& data
,
787 const net::URLRequestStatus
& status
) {
788 if (!status
.is_success()) {
789 if (status
.status() == net::URLRequestStatus::CANCELED
) {
790 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED
);
792 DLOG(WARNING
) << "Could not reach Google Accounts servers: errno "
794 return GoogleServiceAuthError::FromConnectionError(status
.error());
797 if (IsSecondFactorSuccess(data
)) {
798 return GoogleServiceAuthError(GoogleServiceAuthError::TWO_FACTOR
);
803 std::string captcha_url
;
804 std::string captcha_token
;
805 ParseClientLoginFailure(data
, &error
, &url
, &captcha_url
, &captcha_token
);
806 DLOG(WARNING
) << "ClientLogin failed with " << error
;
808 if (error
== kCaptchaError
) {
810 GaiaUrls::GetInstance()->captcha_url_prefix() + captcha_url
);
811 GURL
unlock_url(url
);
812 return GoogleServiceAuthError::FromClientLoginCaptchaChallenge(
813 captcha_token
, image_url
, unlock_url
);
815 if (error
== kAccountDeletedError
)
816 return GoogleServiceAuthError(GoogleServiceAuthError::ACCOUNT_DELETED
);
817 if (error
== kAccountDisabledError
)
818 return GoogleServiceAuthError(GoogleServiceAuthError::ACCOUNT_DISABLED
);
819 if (error
== kBadAuthenticationError
) {
820 return GoogleServiceAuthError(
821 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
);
823 if (error
== kServiceUnavailableError
) {
824 return GoogleServiceAuthError(
825 GoogleServiceAuthError::SERVICE_UNAVAILABLE
);
828 DLOG(WARNING
) << "Incomprehensible response from Google Accounts servers.";
829 return GoogleServiceAuthError(
830 GoogleServiceAuthError::SERVICE_UNAVAILABLE
);
834 return GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE
);
838 GoogleServiceAuthError
GaiaAuthFetcher::GenerateOAuthLoginError(
839 const std::string
& data
,
840 const net::URLRequestStatus
& status
) {
841 if (!status
.is_success()) {
842 if (status
.status() == net::URLRequestStatus::CANCELED
) {
843 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED
);
845 DLOG(WARNING
) << "Could not reach Google Accounts servers: errno "
847 return GoogleServiceAuthError::FromConnectionError(status
.error());
850 if (IsSecondFactorSuccess(data
)) {
851 return GoogleServiceAuthError(GoogleServiceAuthError::TWO_FACTOR
);
856 std::string captcha_url
;
857 std::string captcha_token
;
858 ParseClientLoginFailure(data
, &error
, &url
, &captcha_url
, &captcha_token
);
859 LOG(WARNING
) << "OAuthLogin failed with " << error
;
861 if (error
== kCaptchaErrorCode
) {
863 GaiaUrls::GetInstance()->captcha_url_prefix() + captcha_url
);
864 GURL
unlock_url(url
);
865 return GoogleServiceAuthError::FromClientLoginCaptchaChallenge(
866 captcha_token
, image_url
, unlock_url
);
868 if (error
== kAccountDeletedErrorCode
)
869 return GoogleServiceAuthError(GoogleServiceAuthError::ACCOUNT_DELETED
);
870 if (error
== kAccountDisabledErrorCode
)
871 return GoogleServiceAuthError(GoogleServiceAuthError::ACCOUNT_DISABLED
);
872 if (error
== kBadAuthenticationErrorCode
) {
873 return GoogleServiceAuthError(
874 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
);
876 if (error
== kServiceUnavailableErrorCode
) {
877 return GoogleServiceAuthError(
878 GoogleServiceAuthError::SERVICE_UNAVAILABLE
);
881 DLOG(WARNING
) << "Incomprehensible response from Google Accounts servers.";
882 return GoogleServiceAuthError(
883 GoogleServiceAuthError::SERVICE_UNAVAILABLE
);
887 return GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE
);
890 void GaiaAuthFetcher::OnClientLoginFetched(const std::string
& data
,
891 const net::URLRequestStatus
& status
,
893 if (status
.is_success() && response_code
== net::HTTP_OK
) {
894 DVLOG(1) << "ClientLogin successful!";
898 ParseClientLoginResponse(data
, &sid
, &lsid
, &token
);
899 consumer_
->OnClientLoginSuccess(
900 GaiaAuthConsumer::ClientLoginResult(sid
, lsid
, token
, data
));
902 consumer_
->OnClientLoginFailure(GenerateAuthError(data
, status
));
906 void GaiaAuthFetcher::OnIssueAuthTokenFetched(
907 const std::string
& data
,
908 const net::URLRequestStatus
& status
,
910 if (status
.is_success() && response_code
== net::HTTP_OK
) {
911 // Only the bare token is returned in the body of this Gaia call
912 // without any padding.
913 consumer_
->OnIssueAuthTokenSuccess(requested_service_
, data
);
915 consumer_
->OnIssueAuthTokenFailure(requested_service_
,
916 GenerateAuthError(data
, status
));
920 void GaiaAuthFetcher::OnClientLoginToOAuth2Fetched(
921 const std::string
& data
,
922 const net::ResponseCookies
& cookies
,
923 const net::URLRequestStatus
& status
,
925 if (status
.is_success() && response_code
== net::HTTP_OK
) {
926 std::string auth_code
;
927 ParseClientLoginToOAuth2Response(cookies
, &auth_code
);
928 StartOAuth2TokenPairFetch(auth_code
);
930 consumer_
->OnClientOAuthFailure(GenerateAuthError(data
, status
));
934 void GaiaAuthFetcher::StartOAuth2TokenPairFetch(const std::string
& auth_code
) {
935 DCHECK(!fetch_pending_
) << "Tried to fetch two things at once!";
937 DVLOG(1) << "Starting OAuth token pair fetch";
938 request_body_
= MakeGetTokenPairBody(auth_code
);
939 fetcher_
.reset(CreateGaiaFetcher(getter_
,
943 kLoadFlagsIgnoreCookies
,
945 fetch_pending_
= true;
949 void GaiaAuthFetcher::OnOAuth2TokenPairFetched(
950 const std::string
& data
,
951 const net::URLRequestStatus
& status
,
953 std::string refresh_token
;
954 std::string access_token
;
955 int expires_in_secs
= 0;
957 bool success
= false;
958 if (status
.is_success() && response_code
== net::HTTP_OK
) {
959 scoped_ptr
<base::Value
> value(base::JSONReader::Read(data
));
960 if (value
.get() && value
->GetType() == base::Value::TYPE_DICTIONARY
) {
961 DictionaryValue
* dict
= static_cast<DictionaryValue
*>(value
.get());
962 success
= ExtractOAuth2TokenPairResponse(dict
, &refresh_token
,
963 &access_token
, &expires_in_secs
);
968 consumer_
->OnClientOAuthSuccess(
969 GaiaAuthConsumer::ClientOAuthResult(refresh_token
, access_token
,
972 consumer_
->OnClientOAuthFailure(GenerateAuthError(data
, status
));
976 void GaiaAuthFetcher::OnGetUserInfoFetched(
977 const std::string
& data
,
978 const net::URLRequestStatus
& status
,
980 if (status
.is_success() && response_code
== net::HTTP_OK
) {
981 std::vector
<std::pair
<std::string
, std::string
> > tokens
;
983 base::SplitStringIntoKeyValuePairs(data
, '=', '\n', &tokens
);
984 std::vector
<std::pair
<std::string
, std::string
> >::iterator i
;
985 for (i
= tokens
.begin(); i
!= tokens
.end(); ++i
) {
986 matches
[i
->first
] = i
->second
;
988 consumer_
->OnGetUserInfoSuccess(matches
);
990 consumer_
->OnGetUserInfoFailure(GenerateAuthError(data
, status
));
994 void GaiaAuthFetcher::OnMergeSessionFetched(const std::string
& data
,
995 const net::URLRequestStatus
& status
,
997 if (status
.is_success() && response_code
== net::HTTP_OK
) {
998 consumer_
->OnMergeSessionSuccess(data
);
1000 consumer_
->OnMergeSessionFailure(GenerateAuthError(data
, status
));
1004 void GaiaAuthFetcher::OnUberAuthTokenFetch(const std::string
& data
,
1005 const net::URLRequestStatus
& status
,
1006 int response_code
) {
1007 if (status
.is_success() && response_code
== net::HTTP_OK
) {
1008 consumer_
->OnUberAuthTokenSuccess(data
);
1010 consumer_
->OnUberAuthTokenFailure(GenerateAuthError(data
, status
));
1014 void GaiaAuthFetcher::OnClientOAuthFetched(const std::string
& data
,
1015 const net::URLRequestStatus
& status
,
1016 int response_code
) {
1017 std::string refresh_token
;
1018 std::string access_token
;
1019 int expires_in_secs
= 0;
1021 bool success
= false;
1022 if (status
.is_success() && response_code
== net::HTTP_OK
) {
1023 scoped_ptr
<base::Value
> value(base::JSONReader::Read(data
));
1024 if (value
.get() && value
->GetType() == base::Value::TYPE_DICTIONARY
) {
1025 DictionaryValue
* dict
= static_cast<DictionaryValue
*>(value
.get());
1026 DictionaryValue
* dict_oauth2
;
1027 if (dict
->GetDictionaryWithoutPathExpansion("oauth2", &dict_oauth2
)) {
1028 success
= ExtractOAuth2TokenPairResponse(dict_oauth2
, &refresh_token
,
1035 // TODO(rogerta): for now this reuses the OnOAuthLoginTokenXXX callbacks
1036 // since the data is exactly the same. This ignores the optional
1037 // persistent_id data in the response, which we may need to handle.
1038 // If we do, we'll need to modify ExtractOAuth2TokenPairResponse() to parse
1039 // the optional data and declare new consumer callbacks to take it.
1041 consumer_
->OnClientOAuthSuccess(
1042 GaiaAuthConsumer::ClientOAuthResult(refresh_token
, access_token
,
1045 consumer_
->OnClientOAuthFailure(GenerateClientOAuthError(data
, status
));
1049 void GaiaAuthFetcher::OnOAuthLoginFetched(const std::string
& data
,
1050 const net::URLRequestStatus
& status
,
1051 int response_code
) {
1052 if (status
.is_success() && response_code
== net::HTTP_OK
) {
1053 DVLOG(1) << "ClientLogin successful!";
1057 ParseClientLoginResponse(data
, &sid
, &lsid
, &token
);
1058 consumer_
->OnClientLoginSuccess(
1059 GaiaAuthConsumer::ClientLoginResult(sid
, lsid
, token
, data
));
1061 consumer_
->OnClientLoginFailure(GenerateAuthError(data
, status
));
1065 void GaiaAuthFetcher::OnURLFetchComplete(const net::URLFetcher
* source
) {
1066 fetch_pending_
= false;
1067 // Some of the GAIA requests perform redirects, which results in the final
1068 // URL of the fetcher not being the original URL requested. Therefore use
1069 // the original URL when determining which OnXXX function to call.
1070 const GURL
& url
= source
->GetOriginalURL();
1071 const net::URLRequestStatus
& status
= source
->GetStatus();
1072 int response_code
= source
->GetResponseCode();
1074 source
->GetResponseAsString(&data
);
1075 DVLOG(2) << "Gaia fetcher response code: " << response_code
;
1076 DVLOG(2) << "Gaia fetcher response data: " << data
;
1077 if (url
== client_login_gurl_
) {
1078 OnClientLoginFetched(data
, status
, response_code
);
1079 } else if (url
== issue_auth_token_gurl_
) {
1080 OnIssueAuthTokenFetched(data
, status
, response_code
);
1081 } else if (url
== client_login_to_oauth2_gurl_
) {
1082 OnClientLoginToOAuth2Fetched(
1083 data
, source
->GetCookies(), status
, response_code
);
1084 } else if (url
== oauth2_token_gurl_
) {
1085 OnOAuth2TokenPairFetched(data
, status
, response_code
);
1086 } else if (url
== get_user_info_gurl_
) {
1087 OnGetUserInfoFetched(data
, status
, response_code
);
1088 } else if (url
== merge_session_gurl_
) {
1089 OnMergeSessionFetched(data
, status
, response_code
);
1090 } else if (url
== uberauth_token_gurl_
) {
1091 OnUberAuthTokenFetch(data
, status
, response_code
);
1092 } else if (url
== client_oauth_gurl_
) {
1093 OnClientOAuthFetched(data
, status
, response_code
);
1094 } else if (url
== oauth_login_gurl_
) {
1095 OnOAuthLoginFetched(data
, status
, response_code
);
1102 bool GaiaAuthFetcher::IsSecondFactorSuccess(
1103 const std::string
& alleged_error
) {
1104 return alleged_error
.find(kSecondFactor
) !=