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

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 23 matching lines...) Expand all
225 virtual scoped_refptr<Authenticator> CreateAuthenticator( 225 virtual scoped_refptr<Authenticator> CreateAuthenticator(
226 LoginStatusConsumer* consumer) OVERRIDE; 226 LoginStatusConsumer* consumer) OVERRIDE;
227 virtual void PrewarmAuthentication() OVERRIDE; 227 virtual void PrewarmAuthentication() OVERRIDE;
228 virtual void RestoreAuthenticationSession(Profile* profile) OVERRIDE; 228 virtual void RestoreAuthenticationSession(Profile* profile) OVERRIDE;
229 virtual void StartTokenServices(Profile* user_profile) OVERRIDE; 229 virtual void StartTokenServices(Profile* user_profile) OVERRIDE;
230 virtual void StartSignedInServices( 230 virtual void StartSignedInServices(
231 Profile* profile, 231 Profile* profile,
232 const GaiaAuthConsumer::ClientLoginResult& credentials) OVERRIDE; 232 const GaiaAuthConsumer::ClientLoginResult& credentials) OVERRIDE;
233 virtual void StopBackgroundFetchers() OVERRIDE; 233 virtual void StopBackgroundFetchers() OVERRIDE;
234 virtual void InitRlzDelayed(Profile* user_profile) OVERRIDE; 234 virtual void InitRlzDelayed(Profile* user_profile) OVERRIDE;
235 virtual void CompleteProfileCreate(Profile* user_profile) OVERRIDE;
235 236
236 // OAuth1TokenFetcher::Delegate overrides. 237 // OAuth1TokenFetcher::Delegate overrides.
237 void OnOAuth1AccessTokenAvailable(const std::string& token, 238 void OnOAuth1AccessTokenAvailable(const std::string& token,
238 const std::string& secret) OVERRIDE; 239 const std::string& secret) OVERRIDE;
239 void OnOAuth1AccessTokenFetchFailed() OVERRIDE; 240 void OnOAuth1AccessTokenFetchFailed() OVERRIDE;
240 241
241 // OAuthLoginVerifier::Delegate overrides. 242 // OAuthLoginVerifier::Delegate overrides.
242 virtual void OnOAuthVerificationSucceeded(const std::string& user_name, 243 virtual void OnOAuthVerificationSucceeded(const std::string& user_name,
243 const std::string& sid, 244 const std::string& sid,
244 const std::string& lsid, 245 const std::string& lsid,
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 const std::string& secret); 292 const std::string& secret);
292 293
293 // Fetch enterprise policy OAuth2 given OAuth1 access |token| and |secret|. 294 // Fetch enterprise policy OAuth2 given OAuth1 access |token| and |secret|.
294 void FetchPolicyToken(Profile* offrecord_profile, 295 void FetchPolicyToken(Profile* offrecord_profile,
295 const std::string& token, 296 const std::string& token,
296 const std::string& secret); 297 const std::string& secret);
297 298
298 // Check user's profile for kApplicationLocale setting. 299 // Check user's profile for kApplicationLocale setting.
299 void RespectLocalePreference(Profile* pref); 300 void RespectLocalePreference(Profile* pref);
300 301
302 // Initializes basic preferences for newly created profile.
303 void InitProfilePreferences(Profile* user_profile);
304
301 // Callback for asynchronous profile creation. 305 // Callback for asynchronous profile creation.
302 void OnProfileCreated(Profile* profile, 306 void OnProfileCreated(Profile* profile,
303 Profile::CreateStatus status); 307 Profile::CreateStatus status);
304 308
309 // Finalized profile preparation.
310 void FinalizePrepareProfile(Profile* user_profile);
311
312 // Restores GAIA auth cookies for the created profile.
313 void RestoreAuthCookies(Profile* user_profile);
314
305 // Initializes RLZ. If |disabled| is true, financial pings are turned off. 315 // Initializes RLZ. If |disabled| is true, financial pings are turned off.
306 void InitRlz(Profile* user_profile, bool disabled); 316 void InitRlz(Profile* user_profile, bool disabled);
307 317
308 std::string password_; 318 std::string password_;
309 bool pending_requests_; 319 bool pending_requests_;
310 bool using_oauth_; 320 bool using_oauth_;
311 bool has_cookies_; 321 // True if the authenrication profile's cookie jar should contain
322 // authentication cookies from the authentication extension log in flow.
323 bool has_web_auth_cookies_;
312 // Has to be scoped_refptr, see comment for CreateAuthenticator(...). 324 // Has to be scoped_refptr, see comment for CreateAuthenticator(...).
313 scoped_refptr<Authenticator> authenticator_; 325 scoped_refptr<Authenticator> authenticator_;
314 scoped_ptr<PolicyOAuthFetcher> policy_oauth_fetcher_; 326 scoped_ptr<PolicyOAuthFetcher> policy_oauth_fetcher_;
315 scoped_ptr<OAuth1TokenFetcher> oauth1_token_fetcher_; 327 scoped_ptr<OAuth1TokenFetcher> oauth1_token_fetcher_;
316 scoped_ptr<OAuthLoginVerifier> oauth_login_verifier_; 328 scoped_ptr<OAuthLoginVerifier> oauth_login_verifier_;
317 329
318 // Delegate to be fired when the profile will be prepared. 330 // Delegate to be fired when the profile will be prepared.
319 LoginUtils::Delegate* delegate_; 331 LoginUtils::Delegate* delegate_;
320 332
321 // Used to restart Chrome to switch to the guest mode. 333 // 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())); 433 logging::RedirectChromeLogging(*(CommandLine::ForCurrentProcess()));
422 434
423 // Update user's displayed email. 435 // Update user's displayed email.
424 if (!display_email.empty()) 436 if (!display_email.empty())
425 user_manager->SaveUserDisplayEmail(username, display_email); 437 user_manager->SaveUserDisplayEmail(username, display_email);
426 438
427 password_ = password; 439 password_ = password;
428 440
429 pending_requests_ = pending_requests; 441 pending_requests_ = pending_requests;
430 using_oauth_ = using_oauth; 442 using_oauth_ = using_oauth;
431 has_cookies_ = has_cookies; 443 has_web_auth_cookies_ = has_cookies;
432 delegate_ = delegate; 444 delegate_ = delegate;
433 445
434 policy::BrowserPolicyConnector* connector = 446 policy::BrowserPolicyConnector* connector =
435 g_browser_process->browser_policy_connector(); 447 g_browser_process->browser_policy_connector();
436 448
437 // If this is an enterprise device and the user belongs to the enterprise 449 // 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 450 // 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 451 // 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 452 // controlled by policy (e.g. Sync, Startup tabs) only start after the
441 // PrefService has the right values. 453 // PrefService has the right values.
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 new PolicyOAuthFetcher(authenticator_->authentication_profile())); 485 new PolicyOAuthFetcher(authenticator_->authentication_profile()));
474 policy_oauth_fetcher_->Start(); 486 policy_oauth_fetcher_->Start();
475 } 487 }
476 } 488 }
477 489
478 void LoginUtilsImpl::DelegateDeleted(LoginUtils::Delegate* delegate) { 490 void LoginUtilsImpl::DelegateDeleted(LoginUtils::Delegate* delegate) {
479 if (delegate_ == delegate) 491 if (delegate_ == delegate)
480 delegate_ = NULL; 492 delegate_ = NULL;
481 } 493 }
482 494
495 void LoginUtilsImpl::InitProfilePreferences(Profile* user_profile) {
496 if (UserManager::Get()->IsCurrentUserNew())
497 SetFirstLoginPrefs(user_profile->GetPrefs());
498 // Make sure that the google service username is properly set (we do this
499 // on every sign in, not just the first login, to deal with existing
500 // profiles that might not have it set yet).
501 StringPrefMember google_services_username;
502 google_services_username.Init(prefs::kGoogleServicesUsername,
503 user_profile->GetPrefs());
504 google_services_username.SetValue(
505 UserManager::Get()->GetLoggedInUser()->display_email());
506 // Make sure we flip every profile to not share proxies if the user hasn't
507 // specified so explicitly.
508 const PrefService::Preference* use_shared_proxies_pref =
509 user_profile->GetPrefs()->FindPreference(prefs::kUseSharedProxies);
510 if (use_shared_proxies_pref->IsDefaultValue())
511 user_profile->GetPrefs()->SetBoolean(prefs::kUseSharedProxies, false);
512 policy::NetworkConfigurationUpdater* network_configuration_updater =
513 g_browser_process->browser_policy_connector()->
514 GetNetworkConfigurationUpdater();
515 if (network_configuration_updater)
516 network_configuration_updater->OnUserPolicyInitialized();
517 RespectLocalePreference(user_profile);
518 }
519
483 void LoginUtilsImpl::OnProfileCreated( 520 void LoginUtilsImpl::OnProfileCreated(
484 Profile* user_profile, 521 Profile* user_profile,
485 Profile::CreateStatus status) { 522 Profile::CreateStatus status) {
486 CHECK(user_profile); 523 CHECK(user_profile);
524
525 if (delegate_)
526 delegate_->OnProfileCreated(user_profile);
527
487 switch (status) { 528 switch (status) {
488 case Profile::CREATE_STATUS_INITIALIZED: 529 case Profile::CREATE_STATUS_INITIALIZED:
489 break; 530 break;
490 case Profile::CREATE_STATUS_CREATED: { 531 case Profile::CREATE_STATUS_CREATED: {
491 if (UserManager::Get()->IsCurrentUserNew()) 532 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; 533 return;
514 } 534 }
515 case Profile::CREATE_STATUS_FAIL: 535 case Profile::CREATE_STATUS_FAIL:
516 default: 536 default:
517 NOTREACHED(); 537 NOTREACHED();
518 return; 538 return;
519 } 539 }
520 540
521 BootTimesLoader* btl = BootTimesLoader::Get(); 541 BootTimesLoader* btl = BootTimesLoader::Get();
522 btl->AddLoginTimeMarker("UserProfileGotten", false); 542 btl->AddLoginTimeMarker("UserProfileGotten", false);
523 543
524 if (using_oauth_) { 544 if (using_oauth_) {
525 // Reuse the access token fetched by the PolicyOAuthFetcher, if it was 545 // Reuse the access token fetched by the PolicyOAuthFetcher, if it was
526 // used to fetch policies before Profile creation. 546 // used to fetch policies before Profile creation.
527 if (policy_oauth_fetcher_.get() && 547 if (policy_oauth_fetcher_.get() &&
528 !policy_oauth_fetcher_->oauth1_token().empty()) { 548 !policy_oauth_fetcher_->oauth1_token().empty()) {
529 VLOG(1) << "Resuming profile creation after fetching policy token"; 549 VLOG(1) << "Resuming profile creation after fetching policy token";
530 StoreOAuth1AccessToken(user_profile, 550 StoreOAuth1AccessToken(user_profile,
531 policy_oauth_fetcher_->oauth1_token(), 551 policy_oauth_fetcher_->oauth1_token(),
532 policy_oauth_fetcher_->oauth1_secret()); 552 policy_oauth_fetcher_->oauth1_secret());
533 } 553 }
534 554
535 // Transfer proxy authentication cache and optionally cookies and server 555 // Transfer proxy authentication cache, cookies (optionally) and server
536 // bound certs from the profile that was used for authentication. This 556 // bound certs from the profile that was used for authentication. This
537 // profile contains cookies that auth extension should have already put in 557 // profile contains cookies that auth extension should have already put in
538 // place that will ensure that the newly created session is authenticated 558 // place that will ensure that the newly created session is authenticated
539 // for the websites that work with the used authentication schema. 559 // for the websites that work with the used authentication schema.
540 ProfileAuthData::Transfer(authenticator_->authentication_profile(), 560 ProfileAuthData::Transfer(authenticator_->authentication_profile(),
541 user_profile, 561 user_profile,
542 has_cookies_); // transfer_cookies 562 has_web_auth_cookies_, // transfer_cookies
543 563 base::Bind(
544 std::string oauth1_token; 564 &LoginUtilsImpl::CompleteProfileCreate,
545 std::string oauth1_secret; 565 AsWeakPtr(),
546 if (ReadOAuth1AccessToken(user_profile, &oauth1_token, &oauth1_secret) || 566 user_profile));
547 !has_cookies_) { 567 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 } 568 }
568 569
570 FinalizePrepareProfile(user_profile);
571 }
572
573 void LoginUtilsImpl::RestoreAuthCookies(Profile* user_profile) {
574 std::string oauth1_token;
575 std::string oauth1_secret;
576 if (ReadOAuth1AccessToken(user_profile, &oauth1_token, &oauth1_secret) ||
577 !has_web_auth_cookies_) {
578 // Verify OAuth access token when we find it in the profile and always if
579 // if we don't have cookies.
580 // TODO(xiyuan): Change back to use authenticator to verify token when
581 // we support Gaia in lock screen.
582 VerifyOAuth1AccessToken(user_profile, oauth1_token, oauth1_secret);
583 } else {
584 // If we don't have it, fetch OAuth1 access token.
585 // Once we get that, we will kick off individual requests for OAuth2
586 // tokens for all our services.
587 // Use off-the-record profile that was used for this step. It should
588 // already contain all needed cookies that will let us skip GAIA's user
589 // authentication UI.
590 //
591 // TODO(rickcam) We should use an isolated App here.
592 oauth1_token_fetcher_.reset(
593 new OAuth1TokenFetcher(this,
594 authenticator_->authentication_profile()));
595 oauth1_token_fetcher_->Start();
596 }
597 }
598
599 void LoginUtilsImpl::CompleteProfileCreate(Profile* user_profile) {
600 RestoreAuthCookies(user_profile);
601 FinalizePrepareProfile(user_profile);
602 }
603
604 void LoginUtilsImpl::FinalizePrepareProfile(Profile* user_profile) {
605 BootTimesLoader* btl = BootTimesLoader::Get();
569 // Own TPM device if, for any reason, it has not been done in EULA 606 // Own TPM device if, for any reason, it has not been done in EULA
570 // wizard screen. 607 // wizard screen.
571 CryptohomeLibrary* cryptohome = CrosLibrary::Get()->GetCryptohomeLibrary(); 608 CryptohomeLibrary* cryptohome = CrosLibrary::Get()->GetCryptohomeLibrary();
572 btl->AddLoginTimeMarker("TPMOwn-Start", false); 609 btl->AddLoginTimeMarker("TPMOwn-Start", false);
573 if (cryptohome->TpmIsEnabled() && !cryptohome->TpmIsBeingOwned()) { 610 if (cryptohome->TpmIsEnabled() && !cryptohome->TpmIsBeingOwned()) {
574 if (cryptohome->TpmIsOwned()) { 611 if (cryptohome->TpmIsOwned()) {
575 cryptohome->TpmClearStoredPassword(); 612 cryptohome->TpmClearStoredPassword();
576 } else { 613 } else {
577 cryptohome->TpmCanAttemptOwnership(); 614 cryptohome->TpmCanAttemptOwnership();
578 } 615 }
(...skipping 549 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 | « chrome/browser/chromeos/login/login_utils.h ('k') | chrome/browser/chromeos/login/login_utils_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698