Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Side by Side Diff: chrome/browser/chromeos/login/login_utils.cc

Issue 11576065: Improved GAIA cookie retrieval logic in ChromeOS login (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/browser/chromeos/login/login_utils.h" 5 #include "chrome/browser/chromeos/login/login_utils.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector> 8 #include <vector>
9 9
10 #include "ash/ash_switches.h" 10 #include "ash/ash_switches.h"
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 : public LoginUtils, 181 : public LoginUtils,
182 public OAuth1TokenFetcher::Delegate, 182 public OAuth1TokenFetcher::Delegate,
183 public OAuthLoginVerifier::Delegate, 183 public OAuthLoginVerifier::Delegate,
184 public net::NetworkChangeNotifier::ConnectionTypeObserver, 184 public net::NetworkChangeNotifier::ConnectionTypeObserver,
185 public content::NotificationObserver, 185 public content::NotificationObserver,
186 public base::SupportsWeakPtr<LoginUtilsImpl> { 186 public base::SupportsWeakPtr<LoginUtilsImpl> {
187 public: 187 public:
188 LoginUtilsImpl() 188 LoginUtilsImpl()
189 : pending_requests_(false), 189 : pending_requests_(false),
190 using_oauth_(false), 190 using_oauth_(false),
191 has_cookies_(false), 191 has_web_auth_cookies_(false),
192 delegate_(NULL), 192 delegate_(NULL),
193 job_restart_request_(NULL), 193 job_restart_request_(NULL),
194 should_restore_auth_session_(false), 194 should_restore_auth_session_(false),
195 url_request_context_getter_(NULL) { 195 url_request_context_getter_(NULL) {
196 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); 196 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
197 // During tests, the browser_process may not be initialized yet causing 197 // During tests, the browser_process may not be initialized yet causing
198 // this to fail. 198 // this to fail.
199 if (g_browser_process) { 199 if (g_browser_process) {
200 registrar_.Add( 200 registrar_.Add(
201 this, 201 this,
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 const std::string& secret); 291 const std::string& secret);
292 292
293 // Fetch enterprise policy OAuth2 given OAuth1 access |token| and |secret|. 293 // Fetch enterprise policy OAuth2 given OAuth1 access |token| and |secret|.
294 void FetchPolicyToken(Profile* offrecord_profile, 294 void FetchPolicyToken(Profile* offrecord_profile,
295 const std::string& token, 295 const std::string& token,
296 const std::string& secret); 296 const std::string& secret);
297 297
298 // Check user's profile for kApplicationLocale setting. 298 // Check user's profile for kApplicationLocale setting.
299 void RespectLocalePreference(Profile* pref); 299 void RespectLocalePreference(Profile* pref);
300 300
301 // Initializes basic preferences for newly created profile.
302 void InitProfilePreferences(Profile* user_profile);
303
301 // Callback for asynchronous profile creation. 304 // Callback for asynchronous profile creation.
302 void OnProfileCreated(Profile* profile, 305 void OnProfileCreated(Profile* profile,
303 Profile::CreateStatus status); 306 Profile::CreateStatus status);
304 307
308 // Finalized profile preparation.
309 void FinalizePrepareProfile(Profile* user_profile);
310
311 // Restores GAIA auth cookies for the created profile.
312 void RestoreAuthCookies(Profile* user_profile);
313
314 // Kicks off OAuth verification and profile preparation after the initial
315 // cookie jar transfer.
316 void OnCookieJarTransferCompleted(Profile* user_profile);
317
305 // Initializes RLZ. If |disabled| is true, financial pings are turned off. 318 // Initializes RLZ. If |disabled| is true, financial pings are turned off.
306 void InitRlz(Profile* user_profile, bool disabled); 319 void InitRlz(Profile* user_profile, bool disabled);
307 320
308 std::string password_; 321 std::string password_;
309 bool pending_requests_; 322 bool pending_requests_;
310 bool using_oauth_; 323 bool using_oauth_;
311 bool has_cookies_; 324 // True if the authenrication profile's cookie jar should contain
325 // authentication cookies from the authentication extension log in flow.
326 bool has_web_auth_cookies_;
312 // Has to be scoped_refptr, see comment for CreateAuthenticator(...). 327 // Has to be scoped_refptr, see comment for CreateAuthenticator(...).
313 scoped_refptr<Authenticator> authenticator_; 328 scoped_refptr<Authenticator> authenticator_;
314 scoped_ptr<PolicyOAuthFetcher> policy_oauth_fetcher_; 329 scoped_ptr<PolicyOAuthFetcher> policy_oauth_fetcher_;
315 scoped_ptr<OAuth1TokenFetcher> oauth1_token_fetcher_; 330 scoped_ptr<OAuth1TokenFetcher> oauth1_token_fetcher_;
316 scoped_ptr<OAuthLoginVerifier> oauth_login_verifier_; 331 scoped_ptr<OAuthLoginVerifier> oauth_login_verifier_;
317 332
318 // Delegate to be fired when the profile will be prepared. 333 // Delegate to be fired when the profile will be prepared.
319 LoginUtils::Delegate* delegate_; 334 LoginUtils::Delegate* delegate_;
320 335
321 // Used to restart Chrome to switch to the guest mode. 336 // Used to restart Chrome to switch to the guest mode.
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 logging::RedirectChromeLogging(*(CommandLine::ForCurrentProcess())); 436 logging::RedirectChromeLogging(*(CommandLine::ForCurrentProcess()));
422 437
423 // Update user's displayed email. 438 // Update user's displayed email.
424 if (!display_email.empty()) 439 if (!display_email.empty())
425 user_manager->SaveUserDisplayEmail(username, display_email); 440 user_manager->SaveUserDisplayEmail(username, display_email);
426 441
427 password_ = password; 442 password_ = password;
428 443
429 pending_requests_ = pending_requests; 444 pending_requests_ = pending_requests;
430 using_oauth_ = using_oauth; 445 using_oauth_ = using_oauth;
431 has_cookies_ = has_cookies; 446 has_web_auth_cookies_ = has_cookies;
432 delegate_ = delegate; 447 delegate_ = delegate;
433 448
434 policy::BrowserPolicyConnector* connector = 449 policy::BrowserPolicyConnector* connector =
435 g_browser_process->browser_policy_connector(); 450 g_browser_process->browser_policy_connector();
436 451
437 // If this is an enterprise device and the user belongs to the enterprise 452 // If this is an enterprise device and the user belongs to the enterprise
438 // domain, then wait for a policy fetch before logging the user in. This 453 // domain, then wait for a policy fetch before logging the user in. This
439 // will delay Profile creation until the policy is fetched, so that features 454 // will delay Profile creation until the policy is fetched, so that features
440 // controlled by policy (e.g. Sync, Startup tabs) only start after the 455 // controlled by policy (e.g. Sync, Startup tabs) only start after the
441 // PrefService has the right values. 456 // PrefService has the right values.
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 new PolicyOAuthFetcher(authenticator_->authentication_profile())); 488 new PolicyOAuthFetcher(authenticator_->authentication_profile()));
474 policy_oauth_fetcher_->Start(); 489 policy_oauth_fetcher_->Start();
475 } 490 }
476 } 491 }
477 492
478 void LoginUtilsImpl::DelegateDeleted(LoginUtils::Delegate* delegate) { 493 void LoginUtilsImpl::DelegateDeleted(LoginUtils::Delegate* delegate) {
479 if (delegate_ == delegate) 494 if (delegate_ == delegate)
480 delegate_ = NULL; 495 delegate_ = NULL;
481 } 496 }
482 497
498 void LoginUtilsImpl::InitProfilePreferences(Profile* user_profile) {
499 if (UserManager::Get()->IsCurrentUserNew())
500 SetFirstLoginPrefs(user_profile->GetPrefs());
501 // Make sure that the google service username is properly set (we do this
502 // on every sign in, not just the first login, to deal with existing
503 // profiles that might not have it set yet).
504 StringPrefMember google_services_username;
505 google_services_username.Init(prefs::kGoogleServicesUsername,
506 user_profile->GetPrefs());
507 google_services_username.SetValue(
508 UserManager::Get()->GetLoggedInUser()->display_email());
509 // Make sure we flip every profile to not share proxies if the user hasn't
510 // specified so explicitly.
511 const PrefService::Preference* use_shared_proxies_pref =
512 user_profile->GetPrefs()->FindPreference(prefs::kUseSharedProxies);
513 if (use_shared_proxies_pref->IsDefaultValue())
514 user_profile->GetPrefs()->SetBoolean(prefs::kUseSharedProxies, false);
515 policy::NetworkConfigurationUpdater* network_configuration_updater =
516 g_browser_process->browser_policy_connector()->
517 GetNetworkConfigurationUpdater();
518 if (network_configuration_updater)
519 network_configuration_updater->OnUserPolicyInitialized();
520 RespectLocalePreference(user_profile);
521 }
522
483 void LoginUtilsImpl::OnProfileCreated( 523 void LoginUtilsImpl::OnProfileCreated(
484 Profile* user_profile, 524 Profile* user_profile,
485 Profile::CreateStatus status) { 525 Profile::CreateStatus status) {
486 CHECK(user_profile); 526 CHECK(user_profile);
487 switch (status) { 527 switch (status) {
488 case Profile::CREATE_STATUS_INITIALIZED: 528 case Profile::CREATE_STATUS_INITIALIZED:
489 break; 529 break;
490 case Profile::CREATE_STATUS_CREATED: { 530 case Profile::CREATE_STATUS_CREATED: {
491 if (UserManager::Get()->IsCurrentUserNew()) 531 InitProfilePreferences(user_profile);
492 SetFirstLoginPrefs(user_profile->GetPrefs());
493 // Make sure that the google service username is properly set (we do this
494 // on every sign in, not just the first login, to deal with existing
495 // profiles that might not have it set yet).
496 StringPrefMember google_services_username;
497 google_services_username.Init(prefs::kGoogleServicesUsername,
498 user_profile->GetPrefs());
499 google_services_username.SetValue(
500 UserManager::Get()->GetLoggedInUser()->display_email());
501 // Make sure we flip every profile to not share proxies if the user hasn't
502 // specified so explicitly.
503 const PrefService::Preference* use_shared_proxies_pref =
504 user_profile->GetPrefs()->FindPreference(prefs::kUseSharedProxies);
505 if (use_shared_proxies_pref->IsDefaultValue())
506 user_profile->GetPrefs()->SetBoolean(prefs::kUseSharedProxies, false);
507 policy::NetworkConfigurationUpdater* network_configuration_updater =
508 g_browser_process->browser_policy_connector()->
509 GetNetworkConfigurationUpdater();
510 if (network_configuration_updater)
511 network_configuration_updater->OnUserPolicyInitialized();
512 RespectLocalePreference(user_profile);
513 return; 532 return;
514 } 533 }
515 case Profile::CREATE_STATUS_FAIL: 534 case Profile::CREATE_STATUS_FAIL:
516 default: 535 default:
517 NOTREACHED(); 536 NOTREACHED();
518 return; 537 return;
519 } 538 }
520 539
521 BootTimesLoader* btl = BootTimesLoader::Get(); 540 BootTimesLoader* btl = BootTimesLoader::Get();
522 btl->AddLoginTimeMarker("UserProfileGotten", false); 541 btl->AddLoginTimeMarker("UserProfileGotten", false);
523 542
524 if (using_oauth_) { 543 if (using_oauth_) {
525 // Reuse the access token fetched by the PolicyOAuthFetcher, if it was 544 // Reuse the access token fetched by the PolicyOAuthFetcher, if it was
526 // used to fetch policies before Profile creation. 545 // used to fetch policies before Profile creation.
527 if (policy_oauth_fetcher_.get() && 546 if (policy_oauth_fetcher_.get() &&
528 !policy_oauth_fetcher_->oauth1_token().empty()) { 547 !policy_oauth_fetcher_->oauth1_token().empty()) {
529 VLOG(1) << "Resuming profile creation after fetching policy token"; 548 VLOG(1) << "Resuming profile creation after fetching policy token";
530 StoreOAuth1AccessToken(user_profile, 549 StoreOAuth1AccessToken(user_profile,
531 policy_oauth_fetcher_->oauth1_token(), 550 policy_oauth_fetcher_->oauth1_token(),
532 policy_oauth_fetcher_->oauth1_secret()); 551 policy_oauth_fetcher_->oauth1_secret());
533 } 552 }
534 553
535 // Transfer proxy authentication cache and optionally cookies and server 554 // Transfer proxy authentication cache, cookies (optionally) and server
536 // bound certs from the profile that was used for authentication. This 555 // bound certs from the profile that was used for authentication. This
537 // profile contains cookies that auth extension should have already put in 556 // profile contains cookies that auth extension should have already put in
538 // place that will ensure that the newly created session is authenticated 557 // place that will ensure that the newly created session is authenticated
539 // for the websites that work with the used authentication schema. 558 // for the websites that work with the used authentication schema.
540 ProfileAuthData::Transfer(authenticator_->authentication_profile(), 559 ProfileAuthData::Transfer(authenticator_->authentication_profile(),
541 user_profile, 560 user_profile,
542 has_cookies_); // transfer_cookies 561 has_web_auth_cookies_, // transfer_cookies
xiyuan 2012/12/20 01:57:45 nit: two-space after comma
zel 2012/12/20 16:56:19 Done.
543 562 base::Bind(
544 std::string oauth1_token; 563 &LoginUtilsImpl::OnCookieJarTransferCompleted,
545 std::string oauth1_secret; 564 AsWeakPtr(),
546 if (ReadOAuth1AccessToken(user_profile, &oauth1_token, &oauth1_secret) || 565 user_profile));
547 !has_cookies_) { 566 return;
548 // Verify OAuth access token when we find it in the profile and always if
549 // if we don't have cookies.
550 // TODO(xiyuan): Change back to use authenticator to verify token when
551 // we support Gaia in lock screen.
552 VerifyOAuth1AccessToken(user_profile, oauth1_token, oauth1_secret);
553 } else {
554 // If we don't have it, fetch OAuth1 access token.
555 // Once we get that, we will kick off individual requests for OAuth2
556 // tokens for all our services.
557 // Use off-the-record profile that was used for this step. It should
558 // already contain all needed cookies that will let us skip GAIA's user
559 // authentication UI.
560 //
561 // TODO(rickcam) We should use an isolated App here.
562 oauth1_token_fetcher_.reset(
563 new OAuth1TokenFetcher(this,
564 authenticator_->authentication_profile()));
565 oauth1_token_fetcher_->Start();
566 }
567 } 567 }
568 568
569 FinalizePrepareProfile(user_profile);
570 }
571
572 void LoginUtilsImpl::RestoreAuthCookies(Profile* user_profile) {
573 std::string oauth1_token;
574 std::string oauth1_secret;
575 if (ReadOAuth1AccessToken(user_profile, &oauth1_token, &oauth1_secret) ||
576 !has_web_auth_cookies_) {
577 // Verify OAuth access token when we find it in the profile and always if
578 // if we don't have cookies.
579 // TODO(xiyuan): Change back to use authenticator to verify token when
580 // we support Gaia in lock screen.
581 VerifyOAuth1AccessToken(user_profile, oauth1_token, oauth1_secret);
582 } else {
583 // If we don't have it, fetch OAuth1 access token.
584 // Once we get that, we will kick off individual requests for OAuth2
585 // tokens for all our services.
586 // Use off-the-record profile that was used for this step. It should
587 // already contain all needed cookies that will let us skip GAIA's user
588 // authentication UI.
589 //
590 // TODO(rickcam) We should use an isolated App here.
591 oauth1_token_fetcher_.reset(
592 new OAuth1TokenFetcher(this,
593 authenticator_->authentication_profile()));
594 oauth1_token_fetcher_->Start();
595 }
596 }
597
598 void LoginUtilsImpl::OnCookieJarTransferCompleted(Profile* user_profile) {
599 RestoreAuthCookies(user_profile);
600 FinalizePrepareProfile(user_profile);
601 }
602
603 void LoginUtilsImpl::FinalizePrepareProfile(Profile* user_profile) {
604 BootTimesLoader* btl = BootTimesLoader::Get();
569 // Own TPM device if, for any reason, it has not been done in EULA 605 // Own TPM device if, for any reason, it has not been done in EULA
570 // wizard screen. 606 // wizard screen.
571 CryptohomeLibrary* cryptohome = CrosLibrary::Get()->GetCryptohomeLibrary(); 607 CryptohomeLibrary* cryptohome = CrosLibrary::Get()->GetCryptohomeLibrary();
572 btl->AddLoginTimeMarker("TPMOwn-Start", false); 608 btl->AddLoginTimeMarker("TPMOwn-Start", false);
573 if (cryptohome->TpmIsEnabled() && !cryptohome->TpmIsBeingOwned()) { 609 if (cryptohome->TpmIsEnabled() && !cryptohome->TpmIsBeingOwned()) {
574 if (cryptohome->TpmIsOwned()) { 610 if (cryptohome->TpmIsOwned()) {
575 cryptohome->TpmClearStoredPassword(); 611 cryptohome->TpmClearStoredPassword();
576 } else { 612 } else {
577 cryptohome->TpmCanAttemptOwnership(); 613 cryptohome->TpmCanAttemptOwnership();
578 } 614 }
(...skipping 14 matching lines...) Expand all
593 // TODO(altimofeev): This pointer should probably never be NULL, but it looks 629 // TODO(altimofeev): This pointer should probably never be NULL, but it looks
594 // like LoginUtilsImpl::OnProfileCreated() may be getting called before 630 // like LoginUtilsImpl::OnProfileCreated() may be getting called before
595 // LoginUtilsImpl::PrepareProfile() has set |delegate_| when Chrome is killed 631 // LoginUtilsImpl::PrepareProfile() has set |delegate_| when Chrome is killed
596 // during shutdown in tests -- see http://crosbug.com/18269. Replace this 632 // during shutdown in tests -- see http://crosbug.com/18269. Replace this
597 // 'if' statement with a CHECK(delegate_) once the underlying issue is 633 // 'if' statement with a CHECK(delegate_) once the underlying issue is
598 // resolved. 634 // resolved.
599 if (delegate_) 635 if (delegate_)
600 delegate_->OnProfilePrepared(user_profile); 636 delegate_->OnProfilePrepared(user_profile);
601 } 637 }
602 638
639
xiyuan 2012/12/20 01:57:45 nit: nuke one of the empty lines
zel 2012/12/20 16:56:19 Done.
603 void LoginUtilsImpl::InitRlzDelayed(Profile* user_profile) { 640 void LoginUtilsImpl::InitRlzDelayed(Profile* user_profile) {
604 #if defined(ENABLE_RLZ) 641 #if defined(ENABLE_RLZ)
605 if (!g_browser_process->local_state()->HasPrefPath(prefs::kRLZBrand)) { 642 if (!g_browser_process->local_state()->HasPrefPath(prefs::kRLZBrand)) {
606 // Read brand code asynchronously from an OEM file and repost ourselves. 643 // Read brand code asynchronously from an OEM file and repost ourselves.
607 google_util::chromeos::SetBrandFromFile( 644 google_util::chromeos::SetBrandFromFile(
608 base::Bind(&LoginUtilsImpl::InitRlzDelayed, AsWeakPtr(), user_profile)); 645 base::Bind(&LoginUtilsImpl::InitRlzDelayed, AsWeakPtr(), user_profile));
609 return; 646 return;
610 } 647 }
611 base::PostTaskAndReplyWithResult( 648 base::PostTaskAndReplyWithResult(
612 base::WorkerPool::GetTaskRunner(false /* task_is_slow */), 649 base::WorkerPool::GetTaskRunner(false /* task_is_slow */),
(...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after
1128 void LoginUtilsImpl::OnOAuthVerificationFailed(const std::string& user_name) { 1165 void LoginUtilsImpl::OnOAuthVerificationFailed(const std::string& user_name) {
1129 UserManager::Get()->SaveUserOAuthStatus(user_name, 1166 UserManager::Get()->SaveUserOAuthStatus(user_name,
1130 User::OAUTH_TOKEN_STATUS_INVALID); 1167 User::OAUTH_TOKEN_STATUS_INVALID);
1131 } 1168 }
1132 1169
1133 void LoginUtilsImpl::OnOAuth1AccessTokenAvailable(const std::string& token, 1170 void LoginUtilsImpl::OnOAuth1AccessTokenAvailable(const std::string& token,
1134 const std::string& secret) { 1171 const std::string& secret) {
1135 Profile* user_profile = ProfileManager::GetDefaultProfile(); 1172 Profile* user_profile = ProfileManager::GetDefaultProfile();
1136 StoreOAuth1AccessToken(user_profile, token, secret); 1173 StoreOAuth1AccessToken(user_profile, token, secret);
1137 1174
1138 // Verify OAuth1 token by doing OAuthLogin and fetching credentials. 1175 // Verify OAuth1 token by doing OAuthLogin and fetching credentials. If we
1176 // have just transfered auth cookies out of authenticated cookie jar, there
1177 // is no need to try to mint them from OAuth token again.
1139 VerifyOAuth1AccessToken(user_profile, token, secret); 1178 VerifyOAuth1AccessToken(user_profile, token, secret);
1140 } 1179 }
1141 1180
1142 void LoginUtilsImpl::OnOAuth1AccessTokenFetchFailed() { 1181 void LoginUtilsImpl::OnOAuth1AccessTokenFetchFailed() {
1143 // TODO(kochi): Show failure notification UI here? 1182 // TODO(kochi): Show failure notification UI here?
1144 LOG(ERROR) << "Failed to fetch OAuth1 access token."; 1183 LOG(ERROR) << "Failed to fetch OAuth1 access token.";
1145 g_browser_process->browser_policy_connector()->RegisterForUserPolicy( 1184 g_browser_process->browser_policy_connector()->RegisterForUserPolicy(
1146 EmptyString()); 1185 EmptyString());
1147 } 1186 }
1148 1187
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
1205 bool LoginUtils::IsWhitelisted(const std::string& username) { 1244 bool LoginUtils::IsWhitelisted(const std::string& username) {
1206 CrosSettings* cros_settings = CrosSettings::Get(); 1245 CrosSettings* cros_settings = CrosSettings::Get();
1207 bool allow_new_user = false; 1246 bool allow_new_user = false;
1208 cros_settings->GetBoolean(kAccountsPrefAllowNewUser, &allow_new_user); 1247 cros_settings->GetBoolean(kAccountsPrefAllowNewUser, &allow_new_user);
1209 if (allow_new_user) 1248 if (allow_new_user)
1210 return true; 1249 return true;
1211 return cros_settings->FindEmailInList(kAccountsPrefUsers, username); 1250 return cros_settings->FindEmailInList(kAccountsPrefUsers, username);
1212 } 1251 }
1213 1252
1214 } // namespace chromeos 1253 } // namespace chromeos
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/chromeos/login/profile_auth_data.h » ('j') | chrome/browser/chromeos/login/profile_auth_data.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698