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