Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/sync/signin_manager.h" | 5 #include "chrome/browser/sync/signin_manager.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "chrome/browser/net/gaia/token_service.h" | 9 #include "chrome/browser/net/gaia/token_service.h" |
| 10 #include "chrome/browser/prefs/pref_service.h" | 10 #include "chrome/browser/prefs/pref_service.h" |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 user_prefs->RegisterStringPref(prefs::kGoogleServicesUsername, | 32 user_prefs->RegisterStringPref(prefs::kGoogleServicesUsername, |
| 33 "", | 33 "", |
| 34 PrefService::UNSYNCABLE_PREF); | 34 PrefService::UNSYNCABLE_PREF); |
| 35 user_prefs->RegisterBooleanPref(prefs::kAutologinEnabled, | 35 user_prefs->RegisterBooleanPref(prefs::kAutologinEnabled, |
| 36 true, | 36 true, |
| 37 PrefService::UNSYNCABLE_PREF); | 37 PrefService::UNSYNCABLE_PREF); |
| 38 } | 38 } |
| 39 | 39 |
| 40 void SigninManager::Initialize(Profile* profile) { | 40 void SigninManager::Initialize(Profile* profile) { |
| 41 profile_ = profile; | 41 profile_ = profile; |
| 42 SetUsername(profile_->GetPrefs()->GetString(prefs::kGoogleServicesUsername)); | 42 |
| 43 std::string user = profile_->GetPrefs()->GetString( | |
| 44 prefs::kGoogleServicesUsername); | |
| 45 if (!user.empty()) | |
| 46 SetAuthenticatedUsername(user); | |
| 43 profile_->GetTokenService()->Initialize( | 47 profile_->GetTokenService()->Initialize( |
| 44 GaiaConstants::kChromeSource, profile_); | 48 GaiaConstants::kChromeSource, profile_); |
| 45 if (!GetUsername().empty()) { | 49 if (!authenticated_username_.empty() && |
|
Andrew T Wilson (Slow)
2011/11/28 04:54:02
Would the token service ever actually have a token
tim (not reviewing)
2011/12/05 21:24:50
Some tests were catching this since I think they a
| |
| 50 !profile_->GetTokenService()->HasTokenForService( | |
|
lipalani1
2011/12/05 21:56:57
I heard other services also depend on the tokens o
tim (not reviewing)
2011/12/06 02:18:22
Those services shouldn't depend on sync calling Lo
| |
| 51 browser_sync::SyncServiceName())) { | |
| 46 profile_->GetTokenService()->LoadTokensFromDB(); | 52 profile_->GetTokenService()->LoadTokensFromDB(); |
| 47 } | 53 } |
| 48 } | 54 } |
| 49 | 55 |
| 50 bool SigninManager::IsInitialized() const { | 56 bool SigninManager::IsInitialized() const { |
| 51 return profile_ != NULL; | 57 return profile_ != NULL; |
| 52 } | 58 } |
| 53 | 59 |
| 54 void SigninManager::CleanupNotificationRegistration() { | 60 void SigninManager::CleanupNotificationRegistration() { |
| 55 #if !defined(OS_CHROMEOS) | 61 #if !defined(OS_CHROMEOS) |
| 56 content::Source<TokenService> token_service(profile_->GetTokenService()); | 62 content::Source<TokenService> token_service(profile_->GetTokenService()); |
| 57 if (registrar_.IsRegistered(this, | 63 if (registrar_.IsRegistered(this, |
| 58 chrome::NOTIFICATION_TOKEN_AVAILABLE, | 64 chrome::NOTIFICATION_TOKEN_AVAILABLE, |
| 59 token_service)) { | 65 token_service)) { |
| 60 registrar_.Remove(this, | 66 registrar_.Remove(this, |
| 61 chrome::NOTIFICATION_TOKEN_AVAILABLE, | 67 chrome::NOTIFICATION_TOKEN_AVAILABLE, |
| 62 token_service); | 68 token_service); |
| 63 } | 69 } |
| 64 #endif | 70 #endif |
| 65 } | 71 } |
| 66 | 72 |
| 67 // If a username already exists, the user is logged in. | 73 const std::string& SigninManager::GetAuthenticatedUsername() { |
| 68 const std::string& SigninManager::GetUsername() { | 74 return authenticated_username_; |
| 69 return browser_sync::IsUsingOAuth() ? oauth_username_ : username_; | |
| 70 } | 75 } |
| 71 | 76 |
| 72 void SigninManager::SetUsername(const std::string& username) { | 77 void SigninManager::SetAuthenticatedUsername(const std::string& username) { |
| 73 if (browser_sync::IsUsingOAuth()) | 78 DCHECK(authenticated_username_.empty()); |
| 74 oauth_username_ = username; | 79 authenticated_username_ = username; |
| 75 else | 80 // TODO(tim): We could go further in ensuring kGoogleServicesUsername and |
| 76 username_ = username; | 81 // authenticated_username_ are consistent once established (e.g. remove |
| 82 // authenticated_username_ altogether). | |
| 77 } | 83 } |
| 78 | 84 |
| 79 // static | |
| 80 void SigninManager::PrepareForSignin() { | 85 void SigninManager::PrepareForSignin() { |
| 81 DCHECK(!browser_sync::IsUsingOAuth()); | 86 DCHECK(!browser_sync::IsUsingOAuth()); |
| 82 DCHECK(username_.empty()); | 87 DCHECK(possibly_invalid_username_.empty()); |
| 88 // This attempt is either 1) the user trying to establish initial sync, or | |
| 89 // 2) trying to refresh credentials for an existing username. If it is 2, we | |
| 90 // need to try again, but take care to leave state around tracking that the | |
| 91 // user has successfully signed in once before with this username, so that on | |
| 92 // restart we don't think sync setup has never completed. | |
| 93 ClearTransientSigninData(); | |
| 83 } | 94 } |
| 84 | 95 |
| 85 // static | |
| 86 void SigninManager::PrepareForOAuthSignin() { | 96 void SigninManager::PrepareForOAuthSignin() { |
| 87 DCHECK(browser_sync::IsUsingOAuth()); | 97 DCHECK(browser_sync::IsUsingOAuth()); |
| 88 DCHECK(oauth_username_.empty()); | |
| 89 #if !defined(OS_CHROMEOS) | 98 #if !defined(OS_CHROMEOS) |
| 90 // The Sign out should clear the token service credentials. | 99 // The Sign out should clear the token service credentials. |
| 91 // Note: In CHROMEOS we might have valid credentials but still need to | 100 // Note: In CHROMEOS we might have valid credentials but still need to |
| 92 // set up 2-factor authentication. | 101 // set up 2-factor authentication. |
| 93 DCHECK(!profile_->GetTokenService()->HasOAuthCredentials()); | 102 DCHECK(!profile_->GetTokenService()->HasOAuthCredentials()); |
| 94 #endif | 103 #endif |
| 104 ClearTransientSigninData(); | |
| 95 } | 105 } |
| 96 | 106 |
| 97 // Users must always sign out before they sign in again. | 107 // Users must always sign out before they sign in again. |
| 98 void SigninManager::StartOAuthSignIn(const std::string& oauth1_request_token) { | 108 void SigninManager::StartOAuthSignIn(const std::string& oauth1_request_token) { |
| 99 DCHECK(browser_sync::IsUsingOAuth()); | 109 DCHECK(browser_sync::IsUsingOAuth()); |
| 100 PrepareForOAuthSignin(); | 110 PrepareForOAuthSignin(); |
| 101 oauth1_request_token_.assign(oauth1_request_token); | 111 oauth1_request_token_.assign(oauth1_request_token); |
| 102 oauth_login_.reset(new GaiaOAuthFetcher(this, | 112 oauth_login_.reset(new GaiaOAuthFetcher(this, |
| 103 profile_->GetRequestContext(), | 113 profile_->GetRequestContext(), |
| 104 profile_, | 114 profile_, |
| 105 GaiaConstants::kSyncServiceOAuth)); | 115 GaiaConstants::kSyncServiceOAuth)); |
| 106 oauth_login_->StartOAuthGetAccessToken(oauth1_request_token_); | 116 oauth_login_->StartOAuthGetAccessToken(oauth1_request_token_); |
| 107 // TODO(rogerta?): Bug 92325: Expand Autologin to include OAuth signin | 117 // TODO(rogerta?): Bug 92325: Expand Autologin to include OAuth signin |
| 108 } | 118 } |
| 109 | 119 |
| 110 // Users must always sign out before they sign in again. | 120 // Users must always sign out before they sign in again. |
| 111 void SigninManager::StartSignIn(const std::string& username, | 121 void SigninManager::StartSignIn(const std::string& username, |
| 112 const std::string& password, | 122 const std::string& password, |
| 113 const std::string& login_token, | 123 const std::string& login_token, |
| 114 const std::string& login_captcha) { | 124 const std::string& login_captcha) { |
| 115 DCHECK(!browser_sync::IsUsingOAuth()); | 125 DCHECK(!browser_sync::IsUsingOAuth()); |
| 126 DCHECK(authenticated_username_.empty() || | |
| 127 username == authenticated_username_); | |
| 116 PrepareForSignin(); | 128 PrepareForSignin(); |
| 117 username_.assign(username); | 129 possibly_invalid_username_.assign(username); |
| 118 password_.assign(password); | 130 password_.assign(password); |
| 119 | 131 |
| 120 client_login_.reset(new GaiaAuthFetcher(this, | 132 client_login_.reset(new GaiaAuthFetcher(this, |
| 121 GaiaConstants::kChromeSource, | 133 GaiaConstants::kChromeSource, |
| 122 profile_->GetRequestContext())); | 134 profile_->GetRequestContext())); |
| 123 client_login_->StartClientLogin(username, | 135 client_login_->StartClientLogin(username, |
| 124 password, | 136 password, |
| 125 "", | 137 "", |
| 126 login_token, | 138 login_token, |
| 127 login_captcha, | 139 login_captcha, |
| 128 GaiaAuthFetcher::HostedAccountsNotAllowed); | 140 GaiaAuthFetcher::HostedAccountsNotAllowed); |
| 129 | 141 |
| 130 // Register for token availability. The signin manager will pre-login the | 142 // Register for token availability. The signin manager will pre-login the |
| 131 // user when the GAIA service token is ready for use. Only do this if we | 143 // user when the GAIA service token is ready for use. Only do this if we |
| 132 // are not running in ChomiumOS, since it handles pre-login itself. | 144 // are not running in ChomiumOS, since it handles pre-login itself. |
| 133 #if !defined(OS_CHROMEOS) | 145 #if !defined(OS_CHROMEOS) |
| 134 registrar_.Add(this, | 146 registrar_.Add(this, |
| 135 chrome::NOTIFICATION_TOKEN_AVAILABLE, | 147 chrome::NOTIFICATION_TOKEN_AVAILABLE, |
| 136 content::Source<TokenService>(profile_->GetTokenService())); | 148 content::Source<TokenService>(profile_->GetTokenService())); |
| 137 #endif | 149 #endif |
| 138 } | 150 } |
| 139 | 151 |
| 140 void SigninManager::ProvideSecondFactorAccessCode( | 152 void SigninManager::ProvideSecondFactorAccessCode( |
| 141 const std::string& access_code) { | 153 const std::string& access_code) { |
| 142 DCHECK(!browser_sync::IsUsingOAuth()); | 154 DCHECK(!browser_sync::IsUsingOAuth()); |
| 143 DCHECK(!username_.empty() && !password_.empty() && | 155 DCHECK(!possibly_invalid_username_.empty() && !password_.empty() && |
| 144 last_result_.data.empty()); | 156 last_result_.data.empty()); |
| 145 | 157 |
| 146 client_login_.reset(new GaiaAuthFetcher(this, | 158 client_login_.reset(new GaiaAuthFetcher(this, |
| 147 GaiaConstants::kChromeSource, | 159 GaiaConstants::kChromeSource, |
| 148 profile_->GetRequestContext())); | 160 profile_->GetRequestContext())); |
| 149 client_login_->StartClientLogin(username_, | 161 client_login_->StartClientLogin(possibly_invalid_username_, |
| 150 access_code, | 162 access_code, |
| 151 "", | 163 "", |
| 152 std::string(), | 164 std::string(), |
| 153 std::string(), | 165 std::string(), |
| 154 GaiaAuthFetcher::HostedAccountsNotAllowed); | 166 GaiaAuthFetcher::HostedAccountsNotAllowed); |
| 155 } | 167 } |
| 156 | 168 |
| 157 void SigninManager::ClearInMemoryData() { | 169 void SigninManager::ClearTransientSigninData() { |
| 158 if (!profile_) | 170 if (!profile_) |
| 159 return; | 171 return; |
| 160 | 172 |
| 161 CleanupNotificationRegistration(); | 173 CleanupNotificationRegistration(); |
| 162 client_login_.reset(); | 174 client_login_.reset(); |
| 163 last_result_ = ClientLoginResult(); | 175 last_result_ = ClientLoginResult(); |
| 164 username_.clear(); | 176 possibly_invalid_username_.clear(); |
|
lipalani1
2011/12/05 21:56:57
Without clearing the AuthenticateduserName the PSS
| |
| 165 oauth_username_.clear(); | |
| 166 password_.clear(); | 177 password_.clear(); |
| 167 had_two_factor_error_ = false; | 178 had_two_factor_error_ = false; |
| 168 } | 179 } |
| 169 | 180 |
| 170 void SigninManager::SignOut() { | 181 void SigninManager::SignOut() { |
| 171 if (!profile_) | 182 if (!profile_) |
| 172 return; | 183 return; |
| 173 | 184 |
| 174 ClearInMemoryData(); | 185 ClearTransientSigninData(); |
| 186 authenticated_username_.clear(); | |
| 175 profile_->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername); | 187 profile_->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername); |
| 176 profile_->GetPrefs()->ClearPref(prefs::kSyncUsingOAuth); | 188 profile_->GetPrefs()->ClearPref(prefs::kSyncUsingOAuth); |
| 177 profile_->GetPrefs()->ScheduleSavePersistentPrefs(); | 189 profile_->GetPrefs()->ScheduleSavePersistentPrefs(); |
| 178 profile_->GetTokenService()->ResetCredentialsInMemory(); | 190 profile_->GetTokenService()->ResetCredentialsInMemory(); |
| 179 profile_->GetTokenService()->EraseTokensFromDB(); | 191 profile_->GetTokenService()->EraseTokensFromDB(); |
| 180 } | 192 } |
| 181 | 193 |
| 182 void SigninManager::OnClientLoginSuccess(const ClientLoginResult& result) { | 194 void SigninManager::OnClientLoginSuccess(const ClientLoginResult& result) { |
| 183 DCHECK(!browser_sync::IsUsingOAuth()); | 195 DCHECK(!browser_sync::IsUsingOAuth()); |
| 184 last_result_ = result; | 196 last_result_ = result; |
| 185 // Make a request for the canonical email address. | 197 // Make a request for the canonical email address. |
| 186 client_login_->StartGetUserInfo(result.lsid, kGetInfoEmailKey); | 198 client_login_->StartGetUserInfo(result.lsid, kGetInfoEmailKey); |
| 187 } | 199 } |
| 188 | 200 |
| 189 // NOTE: GetUserInfo is a ClientLogin request similar to OAuth's userinfo | 201 // NOTE: GetUserInfo is a ClientLogin request similar to OAuth's userinfo |
| 190 void SigninManager::OnGetUserInfoSuccess(const std::string& key, | 202 void SigninManager::OnGetUserInfoSuccess(const std::string& key, |
| 191 const std::string& value) { | 203 const std::string& value) { |
| 192 DCHECK(!browser_sync::IsUsingOAuth()); | 204 DCHECK(!browser_sync::IsUsingOAuth()); |
| 193 DCHECK(key == kGetInfoEmailKey); | 205 DCHECK(key == kGetInfoEmailKey); |
| 206 DCHECK(authenticated_username_.empty() || authenticated_username_ == value); | |
| 194 | 207 |
| 195 username_ = value; | 208 authenticated_username_ = value; |
| 196 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, username_); | 209 possibly_invalid_username_.clear(); |
| 210 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, | |
| 211 authenticated_username_); | |
| 197 profile_->GetPrefs()->SetBoolean(prefs::kSyncUsingOAuth, false); | 212 profile_->GetPrefs()->SetBoolean(prefs::kSyncUsingOAuth, false); |
| 198 profile_->GetPrefs()->ScheduleSavePersistentPrefs(); | 213 profile_->GetPrefs()->ScheduleSavePersistentPrefs(); |
| 199 | 214 |
| 200 GoogleServiceSigninSuccessDetails details(username_, password_); | 215 GoogleServiceSigninSuccessDetails details(authenticated_username_, |
| 216 password_); | |
| 201 content::NotificationService::current()->Notify( | 217 content::NotificationService::current()->Notify( |
| 202 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, | 218 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, |
| 203 content::Source<Profile>(profile_), | 219 content::Source<Profile>(profile_), |
| 204 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); | 220 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); |
| 205 | 221 |
| 206 password_.clear(); // Don't need it anymore. | 222 password_.clear(); // Don't need it anymore. |
| 207 | 223 |
| 208 profile_->GetTokenService()->UpdateCredentials(last_result_); | 224 profile_->GetTokenService()->UpdateCredentials(last_result_); |
| 209 DCHECK(profile_->GetTokenService()->AreCredentialsValid()); | 225 DCHECK(profile_->GetTokenService()->AreCredentialsValid()); |
| 210 profile_->GetTokenService()->StartFetchingTokens(); | 226 profile_->GetTokenService()->StartFetchingTokens(); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 244 // a second factor error, and we don't sign out if we're dealing with | 260 // a second factor error, and we don't sign out if we're dealing with |
| 245 // an invalid access code (again, because the password was valid). | 261 // an invalid access code (again, because the password was valid). |
| 246 bool invalid_gaia = error.state() == | 262 bool invalid_gaia = error.state() == |
| 247 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS; | 263 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS; |
| 248 if (error.state() == GoogleServiceAuthError::TWO_FACTOR || | 264 if (error.state() == GoogleServiceAuthError::TWO_FACTOR || |
| 249 (had_two_factor_error_ && invalid_gaia)) { | 265 (had_two_factor_error_ && invalid_gaia)) { |
| 250 had_two_factor_error_ = true; | 266 had_two_factor_error_ = true; |
| 251 return; | 267 return; |
| 252 } | 268 } |
| 253 | 269 |
| 254 ClearInMemoryData(); | 270 ClearTransientSigninData(); |
| 255 } | 271 } |
| 256 | 272 |
| 257 void SigninManager::OnOAuthGetAccessTokenSuccess(const std::string& token, | 273 void SigninManager::OnOAuthGetAccessTokenSuccess(const std::string& token, |
| 258 const std::string& secret) { | 274 const std::string& secret) { |
| 259 DCHECK(browser_sync::IsUsingOAuth()); | 275 DCHECK(browser_sync::IsUsingOAuth()); |
| 260 VLOG(1) << "SigninManager::OnOAuthGetAccessTokenSuccess"; | 276 VLOG(1) << "SigninManager::OnOAuthGetAccessTokenSuccess"; |
| 261 profile_->GetTokenService()->UpdateOAuthCredentials(token, secret); | 277 profile_->GetTokenService()->UpdateOAuthCredentials(token, secret); |
| 262 } | 278 } |
| 263 | 279 |
| 264 void SigninManager::OnOAuthGetAccessTokenFailure( | 280 void SigninManager::OnOAuthGetAccessTokenFailure( |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 287 | 303 |
| 288 TokenService* token_service = profile_->GetTokenService(); | 304 TokenService* token_service = profile_->GetTokenService(); |
| 289 CHECK(token_service); | 305 CHECK(token_service); |
| 290 | 306 |
| 291 // If |SignOut()| was called between the login start and |OnUserInfoSucess()|, | 307 // If |SignOut()| was called between the login start and |OnUserInfoSucess()|, |
| 292 // then the OAuth credentials would have been cleared. | 308 // then the OAuth credentials would have been cleared. |
| 293 if (!token_service->HasOAuthCredentials()) | 309 if (!token_service->HasOAuthCredentials()) |
| 294 return; | 310 return; |
| 295 | 311 |
| 296 VLOG(1) << "Sync signin for " << email << " is complete."; | 312 VLOG(1) << "Sync signin for " << email << " is complete."; |
| 297 oauth_username_ = email; | 313 authenticated_username_ = email; |
| 298 profile_->GetPrefs()->SetString( | 314 profile_->GetPrefs()->SetString( |
| 299 prefs::kGoogleServicesUsername, oauth_username_); | 315 prefs::kGoogleServicesUsername, authenticated_username_); |
| 300 profile_->GetPrefs()->SetBoolean(prefs::kSyncUsingOAuth, true); | 316 profile_->GetPrefs()->SetBoolean(prefs::kSyncUsingOAuth, true); |
| 301 profile_->GetPrefs()->ScheduleSavePersistentPrefs(); | 317 profile_->GetPrefs()->ScheduleSavePersistentPrefs(); |
| 302 | 318 |
| 303 DCHECK(password_.empty()); | 319 DCHECK(password_.empty()); |
| 304 GoogleServiceSigninSuccessDetails details(oauth_username_, ""); | 320 GoogleServiceSigninSuccessDetails details(authenticated_username_, ""); |
| 305 content::NotificationService::current()->Notify( | 321 content::NotificationService::current()->Notify( |
| 306 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, | 322 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, |
| 307 content::Source<Profile>(profile_), | 323 content::Source<Profile>(profile_), |
| 308 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); | 324 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); |
| 309 | 325 |
| 310 DCHECK(token_service->HasOAuthCredentials()); | 326 DCHECK(token_service->HasOAuthCredentials()); |
| 311 token_service->StartFetchingOAuthTokens(); | 327 token_service->StartFetchingOAuthTokens(); |
| 312 } | 328 } |
| 313 | 329 |
| 314 void SigninManager::OnUserInfoFailure(const GoogleServiceAuthError& error) { | 330 void SigninManager::OnUserInfoFailure(const GoogleServiceAuthError& error) { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 334 profile_->GetRequestContext())); | 350 profile_->GetRequestContext())); |
| 335 } | 351 } |
| 336 | 352 |
| 337 client_login_->StartMergeSession(tok_details->token()); | 353 client_login_->StartMergeSession(tok_details->token()); |
| 338 | 354 |
| 339 // We only want to do this once per sign-in. | 355 // We only want to do this once per sign-in. |
| 340 CleanupNotificationRegistration(); | 356 CleanupNotificationRegistration(); |
| 341 } | 357 } |
| 342 #endif | 358 #endif |
| 343 } | 359 } |
| OLD | NEW |