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

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

Issue 11991002: Merge 176800 (Closed) Base URL: svn://svn.chromium.org/chrome/branches/1364/src/
Patch Set: Created 7 years, 11 months 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 12 matching lines...) Expand all
23 #include "base/stringprintf.h" 23 #include "base/stringprintf.h"
24 #include "base/synchronization/lock.h" 24 #include "base/synchronization/lock.h"
25 #include "base/task_runner_util.h" 25 #include "base/task_runner_util.h"
26 #include "base/threading/worker_pool.h" 26 #include "base/threading/worker_pool.h"
27 #include "base/time.h" 27 #include "base/time.h"
28 #include "base/utf_string_conversions.h" 28 #include "base/utf_string_conversions.h"
29 #include "cc/switches.h" 29 #include "cc/switches.h"
30 #include "chrome/browser/browser_process.h" 30 #include "chrome/browser/browser_process.h"
31 #include "chrome/browser/browser_shutdown.h" 31 #include "chrome/browser/browser_shutdown.h"
32 #include "chrome/browser/chromeos/boot_times_loader.h" 32 #include "chrome/browser/chromeos/boot_times_loader.h"
33 #include "chrome/browser/chromeos/cros/cert_library.h"
34 #include "chrome/browser/chromeos/cros/cros_library.h" 33 #include "chrome/browser/chromeos/cros/cros_library.h"
35 #include "chrome/browser/chromeos/cros/cryptohome_library.h" 34 #include "chrome/browser/chromeos/cros/cryptohome_library.h"
36 #include "chrome/browser/chromeos/cros/network_library.h" 35 #include "chrome/browser/chromeos/cros/network_library.h"
37 #include "chrome/browser/chromeos/input_method/input_method_configuration.h" 36 #include "chrome/browser/chromeos/input_method/input_method_configuration.h"
38 #include "chrome/browser/chromeos/input_method/input_method_manager.h" 37 #include "chrome/browser/chromeos/input_method/input_method_manager.h"
39 #include "chrome/browser/chromeos/input_method/input_method_util.h" 38 #include "chrome/browser/chromeos/input_method/input_method_util.h"
40 #include "chrome/browser/chromeos/login/language_switch_menu.h" 39 #include "chrome/browser/chromeos/login/language_switch_menu.h"
41 #include "chrome/browser/chromeos/login/login_display_host.h" 40 #include "chrome/browser/chromeos/login/login_display_host.h"
42 #include "chrome/browser/chromeos/login/oauth1_token_fetcher.h" 41 #include "chrome/browser/chromeos/login/oauth_login_manager.h"
43 #include "chrome/browser/chromeos/login/oauth_login_verifier.h"
44 #include "chrome/browser/chromeos/login/parallel_authenticator.h" 42 #include "chrome/browser/chromeos/login/parallel_authenticator.h"
45 #include "chrome/browser/chromeos/login/policy_oauth_fetcher.h"
46 #include "chrome/browser/chromeos/login/profile_auth_data.h" 43 #include "chrome/browser/chromeos/login/profile_auth_data.h"
47 #include "chrome/browser/chromeos/login/screen_locker.h" 44 #include "chrome/browser/chromeos/login/screen_locker.h"
48 #include "chrome/browser/chromeos/login/user_manager.h" 45 #include "chrome/browser/chromeos/login/user_manager.h"
49 #include "chrome/browser/chromeos/settings/cros_settings.h" 46 #include "chrome/browser/chromeos/settings/cros_settings.h"
50 #include "chrome/browser/chromeos/settings/cros_settings_names.h" 47 #include "chrome/browser/chromeos/settings/cros_settings_names.h"
51 #include "chrome/browser/extensions/extension_service.h" 48 #include "chrome/browser/extensions/extension_service.h"
52 #include "chrome/browser/first_run/first_run.h" 49 #include "chrome/browser/first_run/first_run.h"
53 #include "chrome/browser/google/google_util_chromeos.h" 50 #include "chrome/browser/google/google_util_chromeos.h"
54 #include "chrome/browser/net/chrome_url_request_context.h" 51 #include "chrome/browser/net/chrome_url_request_context.h"
55 #include "chrome/browser/net/preconnect.h" 52 #include "chrome/browser/net/preconnect.h"
(...skipping 19 matching lines...) Expand all
75 #include "chrome/common/pref_names.h" 72 #include "chrome/common/pref_names.h"
76 #include "chrome/common/url_constants.h" 73 #include "chrome/common/url_constants.h"
77 #include "chromeos/chromeos_switches.h" 74 #include "chromeos/chromeos_switches.h"
78 #include "chromeos/dbus/dbus_thread_manager.h" 75 #include "chromeos/dbus/dbus_thread_manager.h"
79 #include "chromeos/dbus/session_manager_client.h" 76 #include "chromeos/dbus/session_manager_client.h"
80 #include "content/public/browser/browser_thread.h" 77 #include "content/public/browser/browser_thread.h"
81 #include "content/public/browser/notification_observer.h" 78 #include "content/public/browser/notification_observer.h"
82 #include "content/public/browser/notification_service.h" 79 #include "content/public/browser/notification_service.h"
83 #include "content/public/common/content_switches.h" 80 #include "content/public/common/content_switches.h"
84 #include "google_apis/gaia/gaia_auth_consumer.h" 81 #include "google_apis/gaia/gaia_auth_consumer.h"
82 #include "google_apis/gaia/gaia_constants.h"
85 #include "google_apis/gaia/gaia_urls.h" 83 #include "google_apis/gaia/gaia_urls.h"
86 #include "googleurl/src/gurl.h" 84 #include "googleurl/src/gurl.h"
87 #include "media/base/media_switches.h" 85 #include "media/base/media_switches.h"
88 #include "net/base/network_change_notifier.h" 86 #include "net/base/network_change_notifier.h"
89 #include "net/url_request/url_request_context.h" 87 #include "net/url_request/url_request_context.h"
90 #include "net/url_request/url_request_context_getter.h" 88 #include "net/url_request/url_request_context_getter.h"
91 #include "ui/base/ui_base_switches.h" 89 #include "ui/base/ui_base_switches.h"
92 #include "ui/compositor/compositor_switches.h" 90 #include "ui/compositor/compositor_switches.h"
93 #include "ui/gfx/switches.h" 91 #include "ui/gfx/switches.h"
94 #include "ui/gl/gl_switches.h" 92 #include "ui/gl/gl_switches.h"
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 } 170 }
173 171
174 int pid_; 172 int pid_;
175 std::string command_line_; 173 std::string command_line_;
176 PrefService* local_state_; 174 PrefService* local_state_;
177 base::OneShotTimer<JobRestartRequest> timer_; 175 base::OneShotTimer<JobRestartRequest> timer_;
178 }; 176 };
179 177
180 class LoginUtilsImpl 178 class LoginUtilsImpl
181 : public LoginUtils, 179 : public LoginUtils,
182 public OAuth1TokenFetcher::Delegate, 180 public OAuthLoginManager::Delegate,
183 public OAuthLoginVerifier::Delegate,
184 public net::NetworkChangeNotifier::ConnectionTypeObserver, 181 public net::NetworkChangeNotifier::ConnectionTypeObserver,
185 public content::NotificationObserver, 182 public content::NotificationObserver,
186 public base::SupportsWeakPtr<LoginUtilsImpl> { 183 public base::SupportsWeakPtr<LoginUtilsImpl> {
187 public: 184 public:
188 LoginUtilsImpl() 185 LoginUtilsImpl()
189 : pending_requests_(false), 186 : using_oauth_(false),
190 using_oauth_(false),
191 has_web_auth_cookies_(false), 187 has_web_auth_cookies_(false),
188 login_manager_(OAuthLoginManager::Create(this)),
192 delegate_(NULL), 189 delegate_(NULL),
193 job_restart_request_(NULL), 190 job_restart_request_(NULL),
194 should_restore_auth_session_(false), 191 should_restore_auth_session_(false),
195 url_request_context_getter_(NULL) { 192 url_request_context_getter_(NULL) {
196 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); 193 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
197 // During tests, the browser_process may not be initialized yet causing 194 // During tests, the browser_process may not be initialized yet causing
198 // this to fail. 195 // this to fail.
199 if (g_browser_process) { 196 if (g_browser_process) {
200 registrar_.Add( 197 registrar_.Add(
201 this, 198 this,
202 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED, 199 chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED,
203 content::Source<Profile>(ProfileManager::GetDefaultProfile())); 200 content::Source<Profile>(ProfileManager::GetDefaultProfile()));
204 } 201 }
205 } 202 }
206 203
207 virtual ~LoginUtilsImpl() { 204 virtual ~LoginUtilsImpl() {
208 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); 205 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
209 } 206 }
210 207
211 // LoginUtils implementation: 208 // LoginUtils implementation:
212 virtual void DoBrowserLaunch(Profile* profile, 209 virtual void DoBrowserLaunch(Profile* profile,
213 LoginDisplayHost* login_host) OVERRIDE; 210 LoginDisplayHost* login_host) OVERRIDE;
214 virtual void PrepareProfile( 211 virtual void PrepareProfile(
215 const std::string& username, 212 const std::string& username,
216 const std::string& display_email, 213 const std::string& display_email,
217 const std::string& password, 214 const std::string& password,
218 bool pending_requests,
219 bool using_oauth, 215 bool using_oauth,
220 bool has_cookies, 216 bool has_cookies,
221 LoginUtils::Delegate* delegate) OVERRIDE; 217 LoginUtils::Delegate* delegate) OVERRIDE;
222 virtual void DelegateDeleted(LoginUtils::Delegate* delegate) OVERRIDE; 218 virtual void DelegateDeleted(LoginUtils::Delegate* delegate) OVERRIDE;
223 virtual void CompleteOffTheRecordLogin(const GURL& start_url) OVERRIDE; 219 virtual void CompleteOffTheRecordLogin(const GURL& start_url) OVERRIDE;
224 virtual void SetFirstLoginPrefs(PrefService* prefs) OVERRIDE; 220 virtual void SetFirstLoginPrefs(PrefService* prefs) OVERRIDE;
225 virtual scoped_refptr<Authenticator> CreateAuthenticator( 221 virtual scoped_refptr<Authenticator> CreateAuthenticator(
226 LoginStatusConsumer* consumer) OVERRIDE; 222 LoginStatusConsumer* consumer) OVERRIDE;
227 virtual void PrewarmAuthentication() OVERRIDE; 223 virtual void PrewarmAuthentication() OVERRIDE;
228 virtual void RestoreAuthenticationSession(Profile* profile) OVERRIDE; 224 virtual void RestoreAuthenticationSession(Profile* profile) OVERRIDE;
229 virtual void StartTokenServices(Profile* user_profile) OVERRIDE;
230 virtual void StartSignedInServices(
231 Profile* profile,
232 const GaiaAuthConsumer::ClientLoginResult& credentials) OVERRIDE;
233 virtual void StopBackgroundFetchers() OVERRIDE; 225 virtual void StopBackgroundFetchers() OVERRIDE;
234 virtual void InitRlzDelayed(Profile* user_profile) OVERRIDE; 226 virtual void InitRlzDelayed(Profile* user_profile) OVERRIDE;
235 virtual void CompleteProfileCreate(Profile* user_profile) OVERRIDE; 227 virtual void CompleteProfileCreate(Profile* user_profile) OVERRIDE;
236 228
237 // OAuth1TokenFetcher::Delegate overrides. 229 // OAuthLoginManager::Delegate overrides.
238 void OnOAuth1AccessTokenAvailable(const std::string& token, 230 virtual void OnCompletedAuthentication(Profile* user_profile) OVERRIDE;
239 const std::string& secret) OVERRIDE; 231 virtual void OnFoundStoredTokens() OVERRIDE;
240 void OnOAuth1AccessTokenFetchFailed() OVERRIDE;
241
242 // OAuthLoginVerifier::Delegate overrides.
243 virtual void OnOAuthVerificationSucceeded(const std::string& user_name,
244 const std::string& sid,
245 const std::string& lsid,
246 const std::string& auth) OVERRIDE;
247 virtual void OnOAuthVerificationFailed(const std::string& user_name) OVERRIDE;
248 232
249 // net::NetworkChangeNotifier::ConnectionTypeObserver overrides. 233 // net::NetworkChangeNotifier::ConnectionTypeObserver overrides.
250 virtual void OnConnectionTypeChanged( 234 virtual void OnConnectionTypeChanged(
251 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE; 235 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
252 236
253 // content::NotificationObserver overrides. 237 // content::NotificationObserver overrides.
254 virtual void Observe(int type, 238 virtual void Observe(int type,
255 const content::NotificationSource& source, 239 const content::NotificationSource& source,
256 const content::NotificationDetails& details) OVERRIDE; 240 const content::NotificationDetails& details) OVERRIDE;
257 241
258 protected: 242 protected:
259 virtual std::string GetOffTheRecordCommandLine( 243 virtual std::string GetOffTheRecordCommandLine(
260 const GURL& start_url, 244 const GURL& start_url,
261 const CommandLine& base_command_line, 245 const CommandLine& base_command_line,
262 CommandLine *command_line); 246 CommandLine *command_line);
263 247
264 private: 248 private:
265 // Restarts OAuth session authentication check. 249 // Restarts OAuth session authentication check.
266 void KickStartAuthentication(Profile* profile); 250 void KickStartAuthentication(Profile* profile);
267 251
268 // Reads OAuth1 token from user profile's prefs.
269 bool ReadOAuth1AccessToken(Profile* user_profile,
270 std::string* token,
271 std::string* secret);
272
273 // Stores OAuth1 token + secret in profile's prefs.
274 void StoreOAuth1AccessToken(Profile* user_profile,
275 const std::string& token,
276 const std::string& secret);
277
278 // Verifies OAuth1 token by doing OAuthLogin and fetching credentials.
279 void VerifyOAuth1AccessToken(Profile* user_profile,
280 const std::string& token,
281 const std::string& secret);
282
283 // Fetch all secondary (OAuth2) tokens given OAuth1 access |token| and
284 // |secret|.
285 void FetchSecondaryTokens(Profile* offrecord_profile,
286 const std::string& token,
287 const std::string& secret);
288
289 // Fetch user credentials (sid/lsid) given OAuth1 access |token| and |secret|.
290 void FetchCredentials(Profile* user_profile,
291 const std::string& token,
292 const std::string& secret);
293
294 // Fetch enterprise policy OAuth2 given OAuth1 access |token| and |secret|.
295 void FetchPolicyToken(Profile* offrecord_profile,
296 const std::string& token,
297 const std::string& secret);
298
299 // Check user's profile for kApplicationLocale setting. 252 // Check user's profile for kApplicationLocale setting.
300 void RespectLocalePreference(Profile* pref); 253 void RespectLocalePreference(Profile* pref);
301 254
302 // Initializes basic preferences for newly created profile. 255 // Initializes basic preferences for newly created profile.
303 void InitProfilePreferences(Profile* user_profile); 256 void InitProfilePreferences(Profile* user_profile);
304 257
305 // Callback for asynchronous profile creation. 258 // Callback for asynchronous profile creation.
306 void OnProfileCreated(Profile* profile, 259 void OnProfileCreated(Profile* profile,
307 Profile::CreateStatus status); 260 Profile::CreateStatus status);
308 261
309 // Finalized profile preparation. 262 // Finalized profile preparation.
310 void FinalizePrepareProfile(Profile* user_profile); 263 void FinalizePrepareProfile(Profile* user_profile);
311 264
312 // Restores GAIA auth cookies for the created profile. 265 // Restores GAIA auth cookies for the created user profile from OAuth2 token.
313 void RestoreAuthCookies(Profile* user_profile); 266 void RestoreAuthSession(Profile* user_profile,
267 bool restore_from_auth_cookies);
314 268
315 // Initializes RLZ. If |disabled| is true, financial pings are turned off. 269 // Initializes RLZ. If |disabled| is true, financial pings are turned off.
316 void InitRlz(Profile* user_profile, bool disabled); 270 void InitRlz(Profile* user_profile, bool disabled);
317 271
272 // Starts signing related services. Initiates TokenService token retrieval.
273 void StartSignedInServices(Profile* profile);
274
318 std::string password_; 275 std::string password_;
319 bool pending_requests_;
320 bool using_oauth_; 276 bool using_oauth_;
321 // True if the authenrication profile's cookie jar should contain 277 // True if the authenrication profile's cookie jar should contain
322 // authentication cookies from the authentication extension log in flow. 278 // authentication cookies from the authentication extension log in flow.
323 bool has_web_auth_cookies_; 279 bool has_web_auth_cookies_;
324 // Has to be scoped_refptr, see comment for CreateAuthenticator(...). 280 // Has to be scoped_refptr, see comment for CreateAuthenticator(...).
325 scoped_refptr<Authenticator> authenticator_; 281 scoped_refptr<Authenticator> authenticator_;
326 scoped_ptr<PolicyOAuthFetcher> policy_oauth_fetcher_; 282 scoped_ptr<OAuthLoginManager> login_manager_;
327 scoped_ptr<OAuth1TokenFetcher> oauth1_token_fetcher_;
328 scoped_ptr<OAuthLoginVerifier> oauth_login_verifier_;
329 283
330 // Delegate to be fired when the profile will be prepared. 284 // Delegate to be fired when the profile will be prepared.
331 LoginUtils::Delegate* delegate_; 285 LoginUtils::Delegate* delegate_;
332 286
333 // Used to restart Chrome to switch to the guest mode. 287 // Used to restart Chrome to switch to the guest mode.
334 JobRestartRequest* job_restart_request_; 288 JobRestartRequest* job_restart_request_;
335 289
336 // True if should restore authentication session when notified about 290 // True if should restore authentication session when notified about
337 // online state change. 291 // online state change.
338 bool should_restore_auth_session_; 292 bool should_restore_auth_session_;
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 // browser before it is dereferenced by the login host. 357 // browser before it is dereferenced by the login host.
404 if (login_host) 358 if (login_host)
405 login_host->OnSessionStart(); 359 login_host->OnSessionStart();
406 UserManager::Get()->SessionStarted(); 360 UserManager::Get()->SessionStarted();
407 } 361 }
408 362
409 void LoginUtilsImpl::PrepareProfile( 363 void LoginUtilsImpl::PrepareProfile(
410 const std::string& username, 364 const std::string& username,
411 const std::string& display_email, 365 const std::string& display_email,
412 const std::string& password, 366 const std::string& password,
413 bool pending_requests,
414 bool using_oauth, 367 bool using_oauth,
415 bool has_cookies, 368 bool has_cookies,
416 LoginUtils::Delegate* delegate) { 369 LoginUtils::Delegate* delegate) {
417 BootTimesLoader* btl = BootTimesLoader::Get(); 370 BootTimesLoader* btl = BootTimesLoader::Get();
418 371
419 VLOG(1) << "Completing login for " << username; 372 VLOG(1) << "Completing login for " << username;
420 373
421 btl->AddLoginTimeMarker("StartSession-Start", false); 374 btl->AddLoginTimeMarker("StartSession-Start", false);
422 DBusThreadManager::Get()->GetSessionManagerClient()->StartSession( 375 DBusThreadManager::Get()->GetSessionManagerClient()->StartSession(
423 username); 376 username);
424 btl->AddLoginTimeMarker("StartSession-End", false); 377 btl->AddLoginTimeMarker("StartSession-End", false);
425 378
426 btl->AddLoginTimeMarker("UserLoggedIn-Start", false); 379 btl->AddLoginTimeMarker("UserLoggedIn-Start", false);
427 UserManager* user_manager = UserManager::Get(); 380 UserManager* user_manager = UserManager::Get();
428 user_manager->UserLoggedIn(username, false); 381 user_manager->UserLoggedIn(username, false);
429 btl->AddLoginTimeMarker("UserLoggedIn-End", false); 382 btl->AddLoginTimeMarker("UserLoggedIn-End", false);
430 383
431 // Switch log file as soon as possible. 384 // Switch log file as soon as possible.
432 if (base::chromeos::IsRunningOnChromeOS()) 385 if (base::chromeos::IsRunningOnChromeOS())
433 logging::RedirectChromeLogging(*(CommandLine::ForCurrentProcess())); 386 logging::RedirectChromeLogging(*(CommandLine::ForCurrentProcess()));
434 387
435 // Update user's displayed email. 388 // Update user's displayed email.
436 if (!display_email.empty()) 389 if (!display_email.empty())
437 user_manager->SaveUserDisplayEmail(username, display_email); 390 user_manager->SaveUserDisplayEmail(username, display_email);
438 391
439 password_ = password; 392 password_ = password;
440 393
441 pending_requests_ = pending_requests;
442 using_oauth_ = using_oauth; 394 using_oauth_ = using_oauth;
443 has_web_auth_cookies_ = has_cookies; 395 has_web_auth_cookies_ = has_cookies;
444 delegate_ = delegate; 396 delegate_ = delegate;
445 397
446 policy::BrowserPolicyConnector* connector = 398 policy::BrowserPolicyConnector* connector =
447 g_browser_process->browser_policy_connector(); 399 g_browser_process->browser_policy_connector();
448 400
449 // If this is an enterprise device and the user belongs to the enterprise 401 // If this is an enterprise device and the user belongs to the enterprise
450 // domain, then wait for a policy fetch before logging the user in. This 402 // domain, then wait for a policy fetch before logging the user in. This
451 // will delay Profile creation until the policy is fetched, so that features 403 // will delay Profile creation until the policy is fetched, so that features
(...skipping 22 matching lines...) Expand all
474 // requires the DeviceManagement token. Try to fetch it now. 426 // requires the DeviceManagement token. Try to fetch it now.
475 // TODO(atwilson): This is somewhat racy, as we are trying to fetch a 427 // TODO(atwilson): This is somewhat racy, as we are trying to fetch a
476 // DMToken in parallel with loading the cached policy blob (there could 428 // DMToken in parallel with loading the cached policy blob (there could
477 // already be a DMToken in the cached policy). Once the legacy policy 429 // already be a DMToken in the cached policy). Once the legacy policy
478 // framework is removed, this code can register a 430 // framework is removed, this code can register a
479 // CloudPolicyService::Observer to check whether the CloudPolicyClient was 431 // CloudPolicyService::Observer to check whether the CloudPolicyClient was
480 // able to register itself using the cached policy data, and then only 432 // able to register itself using the cached policy data, and then only
481 // create a PolicyOAuthFetcher if the client is still unregistered 433 // create a PolicyOAuthFetcher if the client is still unregistered
482 // (http://crbug.com/143187). 434 // (http://crbug.com/143187).
483 VLOG(1) << "Profile creation requires policy token, fetching now"; 435 VLOG(1) << "Profile creation requires policy token, fetching now";
484 policy_oauth_fetcher_.reset( 436 login_manager_->RestorePolicyTokens(
485 new PolicyOAuthFetcher(authenticator_->authentication_profile())); 437 authenticator_->authentication_profile()->GetRequestContext());
486 policy_oauth_fetcher_->Start();
487 } 438 }
488 } 439 }
489 440
490 void LoginUtilsImpl::DelegateDeleted(LoginUtils::Delegate* delegate) { 441 void LoginUtilsImpl::DelegateDeleted(LoginUtils::Delegate* delegate) {
491 if (delegate_ == delegate) 442 if (delegate_ == delegate)
492 delegate_ = NULL; 443 delegate_ = NULL;
493 } 444 }
494 445
495 void LoginUtilsImpl::InitProfilePreferences(Profile* user_profile) { 446 void LoginUtilsImpl::InitProfilePreferences(Profile* user_profile) {
496 if (UserManager::Get()->IsCurrentUserNew()) 447 if (UserManager::Get()->IsCurrentUserNew())
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
535 case Profile::CREATE_STATUS_FAIL: 486 case Profile::CREATE_STATUS_FAIL:
536 default: 487 default:
537 NOTREACHED(); 488 NOTREACHED();
538 return; 489 return;
539 } 490 }
540 491
541 BootTimesLoader* btl = BootTimesLoader::Get(); 492 BootTimesLoader* btl = BootTimesLoader::Get();
542 btl->AddLoginTimeMarker("UserProfileGotten", false); 493 btl->AddLoginTimeMarker("UserProfileGotten", false);
543 494
544 if (using_oauth_) { 495 if (using_oauth_) {
545 // Reuse the access token fetched by the PolicyOAuthFetcher, if it was
546 // used to fetch policies before Profile creation.
547 if (policy_oauth_fetcher_.get() &&
548 !policy_oauth_fetcher_->oauth1_token().empty()) {
549 VLOG(1) << "Resuming profile creation after fetching policy token";
550 StoreOAuth1AccessToken(user_profile,
551 policy_oauth_fetcher_->oauth1_token(),
552 policy_oauth_fetcher_->oauth1_secret());
553 }
554
555 // Transfer proxy authentication cache, cookies (optionally) and server 496 // Transfer proxy authentication cache, cookies (optionally) and server
556 // bound certs from the profile that was used for authentication. This 497 // bound certs from the profile that was used for authentication. This
557 // profile contains cookies that auth extension should have already put in 498 // profile contains cookies that auth extension should have already put in
558 // place that will ensure that the newly created session is authenticated 499 // place that will ensure that the newly created session is authenticated
559 // for the websites that work with the used authentication schema. 500 // for the websites that work with the used authentication schema.
560 ProfileAuthData::Transfer(authenticator_->authentication_profile(), 501 ProfileAuthData::Transfer(authenticator_->authentication_profile(),
561 user_profile, 502 user_profile,
562 has_web_auth_cookies_, // transfer_cookies 503 has_web_auth_cookies_, // transfer_cookies
563 base::Bind( 504 base::Bind(
564 &LoginUtilsImpl::CompleteProfileCreate, 505 &LoginUtilsImpl::CompleteProfileCreate,
565 AsWeakPtr(), 506 AsWeakPtr(),
566 user_profile)); 507 user_profile));
567 return; 508 return;
568 } 509 }
569 510
570 FinalizePrepareProfile(user_profile); 511 FinalizePrepareProfile(user_profile);
571 } 512 }
572 513
573 void LoginUtilsImpl::RestoreAuthCookies(Profile* user_profile) { 514 void LoginUtilsImpl::CompleteProfileCreate(Profile* user_profile) {
574 std::string oauth1_token; 515 RestoreAuthSession(user_profile, has_web_auth_cookies_);
575 std::string oauth1_secret; 516 FinalizePrepareProfile(user_profile);
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 } 517 }
598 518
599 void LoginUtilsImpl::CompleteProfileCreate(Profile* user_profile) { 519 void LoginUtilsImpl::RestoreAuthSession(Profile* user_profile,
600 RestoreAuthCookies(user_profile); 520 bool restore_from_auth_cookies) {
601 FinalizePrepareProfile(user_profile); 521 DCHECK(authenticator_ || !restore_from_auth_cookies);
522 // Remove legacy OAuth1 token if we have one. If it's valid, we should already
523 // have OAuth2 refresh token in TokenService that could be used to retrieve
524 // all other tokens and credentials.
525 login_manager_->RestoreSession(
526 user_profile,
527 authenticator_ ?
528 authenticator_->authentication_profile()->GetRequestContext() :
529 NULL,
530 restore_from_auth_cookies);
602 } 531 }
603 532
604 void LoginUtilsImpl::FinalizePrepareProfile(Profile* user_profile) { 533 void LoginUtilsImpl::FinalizePrepareProfile(Profile* user_profile) {
605 BootTimesLoader* btl = BootTimesLoader::Get(); 534 BootTimesLoader* btl = BootTimesLoader::Get();
606 // Own TPM device if, for any reason, it has not been done in EULA 535 // Own TPM device if, for any reason, it has not been done in EULA
607 // wizard screen. 536 // wizard screen.
608 CryptohomeLibrary* cryptohome = CrosLibrary::Get()->GetCryptohomeLibrary(); 537 CryptohomeLibrary* cryptohome = CrosLibrary::Get()->GetCryptohomeLibrary();
609 btl->AddLoginTimeMarker("TPMOwn-Start", false); 538 btl->AddLoginTimeMarker("TPMOwn-Start", false);
610 if (cryptohome->TpmIsEnabled() && !cryptohome->TpmIsBeingOwned()) { 539 if (cryptohome->TpmIsEnabled() && !cryptohome->TpmIsBeingOwned()) {
611 if (cryptohome->TpmIsOwned()) { 540 if (cryptohome->TpmIsOwned()) {
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
668 // Init the RLZ library. 597 // Init the RLZ library.
669 int ping_delay = user_profile->GetPrefs()->GetInteger( 598 int ping_delay = user_profile->GetPrefs()->GetInteger(
670 first_run::GetPingDelayPrefName().c_str()); 599 first_run::GetPingDelayPrefName().c_str());
671 RLZTracker::InitRlzFromProfileDelayed( 600 RLZTracker::InitRlzFromProfileDelayed(
672 user_profile, UserManager::Get()->IsCurrentUserNew(), ping_delay); 601 user_profile, UserManager::Get()->IsCurrentUserNew(), ping_delay);
673 if (delegate_) 602 if (delegate_)
674 delegate_->OnRlzInitialized(user_profile); 603 delegate_->OnRlzInitialized(user_profile);
675 #endif 604 #endif
676 } 605 }
677 606
678 void LoginUtilsImpl::StartTokenServices(Profile* user_profile) { 607 void LoginUtilsImpl::StartSignedInServices(Profile* user_profile) {
679 std::string oauth1_token;
680 std::string oauth1_secret;
681 if (!ReadOAuth1AccessToken(user_profile, &oauth1_token, &oauth1_secret))
682 return;
683
684 FetchSecondaryTokens(user_profile->GetOffTheRecordProfile(),
685 oauth1_token, oauth1_secret);
686 }
687
688 void LoginUtilsImpl::StartSignedInServices(
689 Profile* user_profile,
690 const GaiaAuthConsumer::ClientLoginResult& credentials) {
691 // Fetch/Create the SigninManager - this will cause the TokenService to load 608 // Fetch/Create the SigninManager - this will cause the TokenService to load
692 // tokens for the currently signed-in user if the SigninManager hasn't already 609 // tokens for the currently signed-in user if the SigninManager hasn't already
693 // been initialized. 610 // been initialized.
694 SigninManager* signin = SigninManagerFactory::GetForProfile(user_profile); 611 SigninManager* signin = SigninManagerFactory::GetForProfile(user_profile);
695 DCHECK(signin); 612 DCHECK(signin);
696 // Make sure SigninManager is connected to our current user (this should 613 // Make sure SigninManager is connected to our current user (this should
697 // happen automatically because we set kGoogleServicesUsername in 614 // happen automatically because we set kGoogleServicesUsername in
698 // OnProfileCreated()). 615 // OnProfileCreated()).
699 DCHECK_EQ(UserManager::Get()->GetLoggedInUser()->display_email(), 616 DCHECK_EQ(UserManager::Get()->GetLoggedInUser()->display_email(),
700 signin->GetAuthenticatedUsername()); 617 signin->GetAuthenticatedUsername());
(...skipping 11 matching lines...) Expand all
712 GoogleServiceSigninSuccessDetails details( 629 GoogleServiceSigninSuccessDetails details(
713 signin->GetAuthenticatedUsername(), 630 signin->GetAuthenticatedUsername(),
714 password_); 631 password_);
715 content::NotificationService::current()->Notify( 632 content::NotificationService::current()->Notify(
716 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, 633 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL,
717 content::Source<Profile>(user_profile), 634 content::Source<Profile>(user_profile),
718 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); 635 content::Details<const GoogleServiceSigninSuccessDetails>(&details));
719 } 636 }
720 } 637 }
721 password_.clear(); 638 password_.clear();
722 TokenService* token_service =
723 TokenServiceFactory::GetForProfile(user_profile);
724 token_service->UpdateCredentials(credentials);
725 if (token_service->AreCredentialsValid())
726 token_service->StartFetchingTokens();
727 } 639 }
728 640
729 void LoginUtilsImpl::RespectLocalePreference(Profile* profile) { 641 void LoginUtilsImpl::RespectLocalePreference(Profile* profile) {
730 DCHECK(profile != NULL); 642 DCHECK(profile != NULL);
731 PrefService* prefs = profile->GetPrefs(); 643 PrefService* prefs = profile->GetPrefs();
732 DCHECK(prefs != NULL); 644 DCHECK(prefs != NULL);
733 if (g_browser_process == NULL) 645 if (g_browser_process == NULL)
734 return; 646 return;
735 647
736 std::string pref_locale = prefs->GetString(prefs::kApplicationLocale); 648 std::string pref_locale = prefs->GetString(prefs::kApplicationLocale);
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
1017 void LoginUtilsImpl::RestoreAuthenticationSession(Profile* user_profile) { 929 void LoginUtilsImpl::RestoreAuthenticationSession(Profile* user_profile) {
1018 // We don't need to restore session for demo/guest users. 930 // We don't need to restore session for demo/guest users.
1019 if (!UserManager::Get()->IsUserLoggedIn() || 931 if (!UserManager::Get()->IsUserLoggedIn() ||
1020 UserManager::Get()->IsLoggedInAsGuest() || 932 UserManager::Get()->IsLoggedInAsGuest() ||
1021 UserManager::Get()->IsLoggedInAsDemoUser()) { 933 UserManager::Get()->IsLoggedInAsDemoUser()) {
1022 return; 934 return;
1023 } 935 }
1024 936
1025 if (!net::NetworkChangeNotifier::IsOffline()) { 937 if (!net::NetworkChangeNotifier::IsOffline()) {
1026 should_restore_auth_session_ = false; 938 should_restore_auth_session_ = false;
1027 KickStartAuthentication(user_profile); 939 RestoreAuthSession(user_profile, false);
1028 } else { 940 } else {
1029 // Even if we're online we should wait till initial 941 // Even if we're online we should wait till initial
1030 // OnConnectionTypeChanged() call. Otherwise starting fetchers too early may 942 // OnConnectionTypeChanged() call. Otherwise starting fetchers too early may
1031 // end up cancelling all request when initial network connection type is 943 // end up cancelling all request when initial network connection type is
1032 // processed. See http://crbug.com/121643. 944 // processed. See http://crbug.com/121643.
1033 should_restore_auth_session_ = true; 945 should_restore_auth_session_ = true;
1034 } 946 }
1035 } 947 }
1036 948
1037 void LoginUtilsImpl::KickStartAuthentication(Profile* user_profile) { 949 void LoginUtilsImpl::StopBackgroundFetchers() {
1038 std::string oauth1_token; 950 login_manager_.reset();
1039 std::string oauth1_secret;
1040 if (ReadOAuth1AccessToken(user_profile, &oauth1_token, &oauth1_secret))
1041 VerifyOAuth1AccessToken(user_profile, oauth1_token, oauth1_secret);
1042 } 951 }
1043 952
1044 void LoginUtilsImpl::StopBackgroundFetchers() { 953 void LoginUtilsImpl::OnCompletedAuthentication(Profile* user_profile) {
1045 policy_oauth_fetcher_.reset(); 954 StartSignedInServices(user_profile);
1046 oauth1_token_fetcher_.reset();
1047 oauth_login_verifier_.reset();
1048 } 955 }
1049 956
1050 void LoginUtilsImpl::FetchSecondaryTokens(Profile* offrecord_profile, 957 void LoginUtilsImpl::OnFoundStoredTokens() {
1051 const std::string& token, 958 // We don't need authenticator instance any more since its cookie jar
1052 const std::string& secret) { 959 // is not going to needed to mint OAuth tokens. Reset it so that
1053 FetchPolicyToken(offrecord_profile, token, secret);
1054 // TODO(rickcam, zelidrag): Wire TokenService there when it becomes
1055 // capable of handling OAuth1 tokens directly.
1056 }
1057
1058 bool LoginUtilsImpl::ReadOAuth1AccessToken(Profile* user_profile,
1059 std::string* token,
1060 std::string* secret) {
1061 // Skip reading oauth token if user does not have a valid status.
1062 if (UserManager::Get()->IsUserLoggedIn() &&
1063 UserManager::Get()->GetLoggedInUser()->oauth_token_status() !=
1064 User::OAUTH_TOKEN_STATUS_VALID) {
1065 return false;
1066 }
1067
1068 PrefService* pref_service = user_profile->GetPrefs();
1069 std::string encoded_token = pref_service->GetString(prefs::kOAuth1Token);
1070 std::string encoded_secret = pref_service->GetString(prefs::kOAuth1Secret);
1071 if (!encoded_token.length() || !encoded_secret.length())
1072 return false;
1073
1074 std::string decoded_token =
1075 CrosLibrary::Get()->GetCertLibrary()->DecryptToken(encoded_token);
1076 std::string decoded_secret =
1077 CrosLibrary::Get()->GetCertLibrary()->DecryptToken(encoded_secret);
1078 if (!decoded_token.length() || !decoded_secret.length())
1079 return false;
1080
1081 *token = decoded_token;
1082 *secret = decoded_secret;
1083 return true;
1084 }
1085
1086 void LoginUtilsImpl::StoreOAuth1AccessToken(Profile* user_profile,
1087 const std::string& token,
1088 const std::string& secret) {
1089 // First store OAuth1 token + service for the current user profile...
1090 std::string encrypted_token =
1091 CrosLibrary::Get()->GetCertLibrary()->EncryptToken(token);
1092 std::string encrypted_secret =
1093 CrosLibrary::Get()->GetCertLibrary()->EncryptToken(secret);
1094 PrefService* pref_service = user_profile->GetPrefs();
1095 User* user = UserManager::Get()->GetLoggedInUser();
1096 if (!encrypted_token.empty() && !encrypted_secret.empty()) {
1097 pref_service->SetString(prefs::kOAuth1Token, encrypted_token);
1098 pref_service->SetString(prefs::kOAuth1Secret, encrypted_secret);
1099
1100 // ...then record the presence of valid OAuth token for this account in
1101 // local state as well.
1102 UserManager::Get()->SaveUserOAuthStatus(
1103 user->email(), User::OAUTH_TOKEN_STATUS_VALID);
1104 } else {
1105 LOG(WARNING) << "Failed to get OAuth1 token/secret encrypted.";
1106 // Set the OAuth status invalid so that the user will go through full
1107 // GAIA login next time.
1108 UserManager::Get()->SaveUserOAuthStatus(
1109 user->email(), User::OAUTH_TOKEN_STATUS_INVALID);
1110 }
1111 }
1112
1113 void LoginUtilsImpl::VerifyOAuth1AccessToken(Profile* user_profile,
1114 const std::string& token,
1115 const std::string& secret) {
1116 // Kick off verification of OAuth1 access token (via OAuthLogin), this should
1117 // let us fetch credentials that will be used to initialize sync engine.
1118 FetchCredentials(user_profile, token, secret);
1119
1120 FetchSecondaryTokens(user_profile->GetOffTheRecordProfile(), token, secret);
1121 }
1122
1123 void LoginUtilsImpl::FetchCredentials(Profile* user_profile,
1124 const std::string& token,
1125 const std::string& secret) {
1126 oauth_login_verifier_.reset(new OAuthLoginVerifier(
1127 this, user_profile, token, secret,
1128 UserManager::Get()->GetLoggedInUser()->email()));
1129 oauth_login_verifier_->StartOAuthVerification();
1130 }
1131
1132
1133 void LoginUtilsImpl::FetchPolicyToken(Profile* offrecord_profile,
1134 const std::string& token,
1135 const std::string& secret) {
1136 // Fetch dm service token now, if it hasn't been fetched yet.
1137 if (!policy_oauth_fetcher_.get() || policy_oauth_fetcher_->failed()) {
1138 // Get the default system profile to use with the policy fetching. If there
1139 // is no |authenticator_| profile, manually load default system profile.
1140 // Otherwise, just use |authenticator_|'s profile.
1141 Profile* profile = NULL;
1142 if (authenticator_)
1143 profile = authenticator_->authentication_profile();
1144
1145 if (!profile) {
1146 FilePath user_data_dir;
1147 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
1148 ProfileManager* profile_manager = g_browser_process->profile_manager();
1149 // Temporarily allow until fix: http://crosbug.com/30391.
1150 base::ThreadRestrictions::ScopedAllowIO allow_io;
1151 profile = profile_manager->GetProfile(user_data_dir)->
1152 GetOffTheRecordProfile();
1153 }
1154
1155 // Trigger oauth token fetch for user policy.
1156 policy_oauth_fetcher_.reset(new PolicyOAuthFetcher(profile, token, secret));
1157 policy_oauth_fetcher_->Start();
1158 }
1159
1160 // TODO(zelidrag): We should add initialization of other services somewhere
1161 // here as well. This could be handled with TokenService class once it is
1162 // ready to handle OAuth tokens.
1163
1164 // We don't need authenticator instance any more, reset it so that
1165 // ScreenLocker would create a separate instance. 960 // ScreenLocker would create a separate instance.
1166 // TODO(nkostylev): There's a potential race if SL would be created before
1167 // OAuth tokens are fetched. It would use incorrect Authenticator instance.
1168 authenticator_ = NULL; 961 authenticator_ = NULL;
1169 } 962 }
1170 963
1171 void LoginUtilsImpl::OnOAuthVerificationFailed(const std::string& user_name) {
1172 UserManager::Get()->SaveUserOAuthStatus(user_name,
1173 User::OAUTH_TOKEN_STATUS_INVALID);
1174 }
1175
1176 void LoginUtilsImpl::OnOAuth1AccessTokenAvailable(const std::string& token,
1177 const std::string& secret) {
1178 Profile* user_profile = ProfileManager::GetDefaultProfile();
1179 StoreOAuth1AccessToken(user_profile, token, secret);
1180
1181 // Verify OAuth1 token by doing OAuthLogin and fetching credentials. If we
1182 // have just transfered auth cookies out of authenticated cookie jar, there
1183 // is no need to try to mint them from OAuth token again.
1184 VerifyOAuth1AccessToken(user_profile, token, secret);
1185 }
1186
1187 void LoginUtilsImpl::OnOAuth1AccessTokenFetchFailed() {
1188 // TODO(kochi): Show failure notification UI here?
1189 LOG(ERROR) << "Failed to fetch OAuth1 access token.";
1190 g_browser_process->browser_policy_connector()->RegisterForUserPolicy(
1191 EmptyString());
1192 }
1193
1194 void LoginUtilsImpl::OnOAuthVerificationSucceeded(
1195 const std::string& user_name, const std::string& sid,
1196 const std::string& lsid, const std::string& auth) {
1197 // Kick off sync engine.
1198 GaiaAuthConsumer::ClientLoginResult credentials(sid, lsid, auth,
1199 std::string());
1200 StartSignedInServices(ProfileManager::GetDefaultProfile(), credentials);
1201 }
1202
1203
1204 void LoginUtilsImpl::OnConnectionTypeChanged( 964 void LoginUtilsImpl::OnConnectionTypeChanged(
1205 net::NetworkChangeNotifier::ConnectionType type) { 965 net::NetworkChangeNotifier::ConnectionType type) {
966 if (!login_manager_.get())
967 return;
968
1206 if (type != net::NetworkChangeNotifier::CONNECTION_NONE && 969 if (type != net::NetworkChangeNotifier::CONNECTION_NONE &&
1207 UserManager::Get()->IsUserLoggedIn()) { 970 UserManager::Get()->IsUserLoggedIn()) {
1208 if (oauth_login_verifier_.get() && 971 if (login_manager_->state() ==
1209 !oauth_login_verifier_->is_done()) { 972 OAuthLoginManager::SESSION_RESTORE_IN_PROGRESS) {
1210 // If we come online for the first time after successful offline login, 973 // If we come online for the first time after successful offline login,
1211 // we need to kick off OAuth token verification process again. 974 // we need to kick off OAuth token verification process again.
1212 oauth_login_verifier_->ContinueVerification(); 975 login_manager_->ContinueSessionRestore();
1213 } else if (should_restore_auth_session_) { 976 } else if (should_restore_auth_session_) {
1214 should_restore_auth_session_ = false; 977 should_restore_auth_session_ = false;
1215 Profile* user_profile = ProfileManager::GetDefaultProfile(); 978 Profile* user_profile = ProfileManager::GetDefaultProfile();
1216 KickStartAuthentication(user_profile); 979 RestoreAuthSession(user_profile, has_web_auth_cookies_);
1217 } 980 }
1218 } 981 }
1219 } 982 }
1220 983
1221 void LoginUtilsImpl::Observe(int type, 984 void LoginUtilsImpl::Observe(int type,
1222 const content::NotificationSource& source, 985 const content::NotificationSource& source,
1223 const content::NotificationDetails& details) { 986 const content::NotificationDetails& details) {
1224 switch (type) { 987 switch (type) {
1225 case chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED: { 988 case chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED: {
1226 Profile* profile = content::Source<Profile>(source).ptr(); 989 Profile* profile = content::Source<Profile>(source).ptr();
(...skipping 23 matching lines...) Expand all
1250 bool LoginUtils::IsWhitelisted(const std::string& username) { 1013 bool LoginUtils::IsWhitelisted(const std::string& username) {
1251 CrosSettings* cros_settings = CrosSettings::Get(); 1014 CrosSettings* cros_settings = CrosSettings::Get();
1252 bool allow_new_user = false; 1015 bool allow_new_user = false;
1253 cros_settings->GetBoolean(kAccountsPrefAllowNewUser, &allow_new_user); 1016 cros_settings->GetBoolean(kAccountsPrefAllowNewUser, &allow_new_user);
1254 if (allow_new_user) 1017 if (allow_new_user)
1255 return true; 1018 return true;
1256 return cros_settings->FindEmailInList(kAccountsPrefUsers, username); 1019 return cros_settings->FindEmailInList(kAccountsPrefUsers, username);
1257 } 1020 }
1258 1021
1259 } // namespace chromeos 1022 } // 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