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

Side by Side Diff: chrome/browser/sync/signin_manager.cc

Issue 7497069: Support Sync following Gaia OAuth authentication (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Incorporating code review comments from Roger. Created 9 years, 4 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) 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 14 matching lines...) Expand all
25 25
26 // static 26 // static
27 void SigninManager::RegisterUserPrefs(PrefService* user_prefs) { 27 void SigninManager::RegisterUserPrefs(PrefService* user_prefs) {
28 user_prefs->RegisterStringPref(prefs::kGoogleServicesUsername, 28 user_prefs->RegisterStringPref(prefs::kGoogleServicesUsername,
29 "", 29 "",
30 PrefService::UNSYNCABLE_PREF); 30 PrefService::UNSYNCABLE_PREF);
31 } 31 }
32 32
33 void SigninManager::Initialize(Profile* profile) { 33 void SigninManager::Initialize(Profile* profile) {
34 profile_ = profile; 34 profile_ = profile;
35 username_ = profile_->GetPrefs()->GetString(prefs::kGoogleServicesUsername); 35 if (SyncSetupWizard::IsUsingOAuth())
36 oauth_username_ =
37 profile_->GetPrefs()->GetString(prefs::kGoogleServicesUsername);
38 else
39 username_ = profile_->GetPrefs()->GetString(prefs::kGoogleServicesUsername);
36 profile_->GetTokenService()->Initialize( 40 profile_->GetTokenService()->Initialize(
37 GaiaConstants::kChromeSource, profile_); 41 GaiaConstants::kChromeSource, profile_);
38 if (!username_.empty()) { 42 if (!username_.empty() || !oauth_username_.empty()) {
39 profile_->GetTokenService()->LoadTokensFromDB(); 43 profile_->GetTokenService()->LoadTokensFromDB();
40 } 44 }
41 } 45 }
42 46
43 bool SigninManager::IsInitialized() const { 47 bool SigninManager::IsInitialized() const {
44 return profile_ != NULL; 48 return profile_ != NULL;
45 } 49 }
46 50
47 void SigninManager::CleanupNotificationRegistration() { 51 void SigninManager::CleanupNotificationRegistration() {
48 #if !defined(OS_CHROMEOS) 52 #if !defined(OS_CHROMEOS)
49 Source<TokenService> token_service(profile_->GetTokenService()); 53 Source<TokenService> token_service(profile_->GetTokenService());
50 if (registrar_.IsRegistered(this, 54 if (registrar_.IsRegistered(this,
51 chrome::NOTIFICATION_TOKEN_AVAILABLE, 55 chrome::NOTIFICATION_TOKEN_AVAILABLE,
52 token_service)) { 56 token_service)) {
53 registrar_.Remove(this, 57 registrar_.Remove(this,
54 chrome::NOTIFICATION_TOKEN_AVAILABLE, 58 chrome::NOTIFICATION_TOKEN_AVAILABLE,
55 token_service); 59 token_service);
56 } 60 }
57 #endif 61 #endif
58 } 62 }
59 63
60 // If a username already exists, the user is logged in. 64 // If a username already exists, the user is logged in.
61 const std::string& SigninManager::GetUsername() { 65 const std::string& SigninManager::GetUsername() {
66 if (SyncSetupWizard::IsUsingOAuth())
67 return oauth_username_;
62 return username_; 68 return username_;
63 } 69 }
64 70
65 void SigninManager::SetUsername(const std::string& username) { 71 void SigninManager::SetUsername(const std::string& username) {
66 username_ = username; 72 if (SyncSetupWizard::IsUsingOAuth())
73 oauth_username_ = username;
74 else
75 username_ = username;
67 } 76 }
68 77
69 // static 78 // static
70 void SigninManager::PrepareForSignin() { 79 void SigninManager::PrepareForSignin() {
71 DCHECK(username_.empty()); 80 DCHECK(username_.empty());
72 #if !defined(OS_CHROMEOS) 81 #if !defined(OS_CHROMEOS)
73 // The Sign out should clear the token service credentials. 82 // The Sign out should clear the token service credentials.
74 // Note: In CHROMEOS we might have valid credentials but still need to 83 // Note: In CHROMEOS we might have valid credentials but still need to
75 // set up 2-factor authentication. 84 // set up 2-factor authentication.
76 DCHECK(!profile_->GetTokenService()->AreCredentialsValid()); 85 DCHECK(!profile_->GetTokenService()->AreCredentialsValid());
77 #endif 86 #endif
78 } 87 }
79 88
89 // static
90 void SigninManager::PrepareForOAuthSignin() {
91 DCHECK(oauth_username_.empty());
92 #if !defined(OS_CHROMEOS)
93 // The Sign out should clear the token service credentials.
94 // Note: In CHROMEOS we might have valid credentials but still need to
95 // set up 2-factor authentication.
96 DCHECK(!profile_->GetTokenService()->AreOAuthCredentialsValid());
97 #endif
98 }
99
80 // Users must always sign out before they sign in again. 100 // Users must always sign out before they sign in again.
81 void SigninManager::StartOAuthSignIn() { 101 void SigninManager::StartOAuthSignIn() {
82 PrepareForSignin(); 102 PrepareForOAuthSignin();
83 oauth_login_.reset(new GaiaOAuthFetcher(this, 103 oauth_login_.reset(new GaiaOAuthFetcher(this,
84 profile_->GetRequestContext(), 104 profile_->GetRequestContext(),
85 profile_, 105 profile_,
86 GaiaConstants::kSyncServiceOAuth)); 106 GaiaConstants::kSyncServiceOAuth));
87 oauth_login_->StartGetOAuthToken(); 107 oauth_login_->StartGetOAuthToken();
108 // TODO(rogerta?): Bug 92325: Expand Autologin to include OAuth signin
88 } 109 }
89 110
90 // Users must always sign out before they sign in again. 111 // Users must always sign out before they sign in again.
91 void SigninManager::StartSignIn(const std::string& username, 112 void SigninManager::StartSignIn(const std::string& username,
92 const std::string& password, 113 const std::string& password,
93 const std::string& login_token, 114 const std::string& login_token,
94 const std::string& login_captcha) { 115 const std::string& login_captcha) {
95 PrepareForSignin(); 116 PrepareForSignin();
96 username_.assign(username); 117 username_.assign(username);
97 password_.assign(password); 118 password_.assign(password);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 157
137 void SigninManager::SignOut() { 158 void SigninManager::SignOut() {
138 if (!profile_) 159 if (!profile_)
139 return; 160 return;
140 161
141 CleanupNotificationRegistration(); 162 CleanupNotificationRegistration();
142 163
143 client_login_.reset(); 164 client_login_.reset();
144 last_result_ = ClientLoginResult(); 165 last_result_ = ClientLoginResult();
145 username_.clear(); 166 username_.clear();
167 oauth_username_.clear();
146 password_.clear(); 168 password_.clear();
147 had_two_factor_error_ = false; 169 had_two_factor_error_ = false;
148 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, username_); 170 profile_->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername);
171 profile_->GetPrefs()->ClearPref(prefs::kSyncUsingOAuth, false);
149 profile_->GetPrefs()->ScheduleSavePersistentPrefs(); 172 profile_->GetPrefs()->ScheduleSavePersistentPrefs();
150 profile_->GetTokenService()->ResetCredentialsInMemory(); 173 profile_->GetTokenService()->ResetCredentialsInMemory();
151 profile_->GetTokenService()->EraseTokensFromDB(); 174 profile_->GetTokenService()->EraseTokensFromDB();
152 } 175 }
153 176
154 void SigninManager::OnClientLoginSuccess(const ClientLoginResult& result) { 177 void SigninManager::OnClientLoginSuccess(const ClientLoginResult& result) {
155 last_result_ = result; 178 last_result_ = result;
156 // Make a request for the canonical email address. 179 // Make a request for the canonical email address.
157 client_login_->StartGetUserInfo(result.lsid, kGetInfoEmailKey); 180 client_login_->StartGetUserInfo(result.lsid, kGetInfoEmailKey);
158 } 181 }
159 182
183 // NOTE: GetUserInfo is a ClientLogin request similar to OAuth's userinfo
160 void SigninManager::OnGetUserInfoSuccess(const std::string& key, 184 void SigninManager::OnGetUserInfoSuccess(const std::string& key,
161 const std::string& value) { 185 const std::string& value) {
162 DCHECK(key == kGetInfoEmailKey); 186 DCHECK(key == kGetInfoEmailKey);
163 187
164 username_ = value; 188 username_ = value;
165 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, username_); 189 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, username_);
190 profile_->GetPrefs()->SetBoolean(prefs::kSyncUsingOAuth, false);
166 profile_->GetPrefs()->ScheduleSavePersistentPrefs(); 191 profile_->GetPrefs()->ScheduleSavePersistentPrefs();
167 192
168 GoogleServiceSigninSuccessDetails details(username_, password_); 193 GoogleServiceSigninSuccessDetails details(username_, password_);
169 NotificationService::current()->Notify( 194 NotificationService::current()->Notify(
170 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, 195 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL,
171 Source<Profile>(profile_), 196 Source<Profile>(profile_),
172 Details<const GoogleServiceSigninSuccessDetails>(&details)); 197 Details<const GoogleServiceSigninSuccessDetails>(&details));
173 198
174 password_.clear(); // Don't need it anymore. 199 password_.clear(); // Don't need it anymore.
175 200
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 } 241 }
217 242
218 SignOut(); 243 SignOut();
219 } 244 }
220 245
221 void SigninManager::OnGetOAuthTokenSuccess(const std::string& oauth_token) { 246 void SigninManager::OnGetOAuthTokenSuccess(const std::string& oauth_token) {
222 VLOG(1) << "SigninManager::SigninManager::OnGetOAuthTokenSuccess"; 247 VLOG(1) << "SigninManager::SigninManager::OnGetOAuthTokenSuccess";
223 } 248 }
224 249
225 void SigninManager::OnGetOAuthTokenFailure() { 250 void SigninManager::OnGetOAuthTokenFailure() {
226 VLOG(1) << "SigninManager::OnGetOAuthTokenFailure"; 251 LOG(WARNING) << "SigninManager::OnGetOAuthTokenFailure";
227 } 252 }
228 253
229 void SigninManager::OnOAuthGetAccessTokenSuccess(const std::string& token, 254 void SigninManager::OnOAuthGetAccessTokenSuccess(const std::string& token,
230 const std::string& secret) { 255 const std::string& secret) {
231 VLOG(1) << "SigninManager::OnOAuthGetAccessTokenSuccess"; 256 VLOG(1) << "SigninManager::OnOAuthGetAccessTokenSuccess";
257 profile_->GetTokenService()->UpdateOAuthCredentials(token, secret);
232 } 258 }
233 259
234 void SigninManager::OnOAuthGetAccessTokenFailure( 260 void SigninManager::OnOAuthGetAccessTokenFailure(
235 const GoogleServiceAuthError& error) { 261 const GoogleServiceAuthError& error) {
236 VLOG(1) << "SigninManager::OnOAuthGetAccessTokenFailure"; 262 LOG(WARNING) << "SigninManager::OnOAuthGetAccessTokenFailure";
237 } 263 }
238 264
239 void SigninManager::OnOAuthWrapBridgeSuccess(const std::string& service_name, 265 void SigninManager::OnOAuthWrapBridgeSuccess(const std::string& service_name,
240 const std::string& token, 266 const std::string& token,
241 const std::string& expires_in) { 267 const std::string& expires_in) {
242 VLOG(1) << "SigninManager::OnOAuthWrapBridgeSuccess"; 268 VLOG(1) << "SigninManager::OnOAuthWrapBridgeSuccess";
243 } 269 }
244 270
245 void SigninManager::OnOAuthWrapBridgeFailure( 271 void SigninManager::OnOAuthWrapBridgeFailure(
246 const std::string& service_scope, 272 const std::string& service_scope,
247 const GoogleServiceAuthError& error) { 273 const GoogleServiceAuthError& error) {
248 VLOG(1) << "SigninManager::OnOAuthWrapBridgeFailure"; 274 LOG(WARNING) << "SigninManager::OnOAuthWrapBridgeFailure";
249 } 275 }
250 276
277 // NOTE: userinfo is an OAuth request similar to ClientLogin's GetUserInfo
251 void SigninManager::OnUserInfoSuccess(const std::string& email) { 278 void SigninManager::OnUserInfoSuccess(const std::string& email) {
252 VLOG(1) << "SigninManager::OnUserInfoSuccess(\"" << email << "\")"; 279 VLOG(1) << "Sync signin for " << email << " is complete.";
280 oauth_username_ = email;
281 profile_->GetPrefs()->SetString(
282 prefs::kGoogleServicesUsername, oauth_username_);
283 profile_->GetPrefs()->SetBoolean(prefs::kSyncUsingOAuth, true);
284 profile_->GetPrefs()->ScheduleSavePersistentPrefs();
285
286 DCHECK(password_.empty());
287 GoogleServiceSigninSuccessDetails details(oauth_username_, "");
288 NotificationService::current()->Notify(
289 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL,
290 Source<Profile>(profile_),
291 Details<const GoogleServiceSigninSuccessDetails>(&details));
292
293 DCHECK(profile_->GetTokenService()->AreOAuthCredentialsValid());
294 profile_->GetTokenService()->StartFetchingOAuthTokens();
253 } 295 }
254 296
255 void SigninManager::OnUserInfoFailure(const GoogleServiceAuthError& error) { 297 void SigninManager::OnUserInfoFailure(const GoogleServiceAuthError& error) {
256 VLOG(1) << "SigninManager::OnUserInfoFailure"; 298 LOG(WARNING) << "SigninManager::OnUserInfoFailure";
257 } 299 }
258 300
259 void SigninManager::Observe(int type, 301 void SigninManager::Observe(int type,
260 const NotificationSource& source, 302 const NotificationSource& source,
261 const NotificationDetails& details) { 303 const NotificationDetails& details) {
262 #if !defined(OS_CHROMEOS) 304 #if !defined(OS_CHROMEOS)
263 DCHECK(type == chrome::NOTIFICATION_TOKEN_AVAILABLE); 305 DCHECK(type == chrome::NOTIFICATION_TOKEN_AVAILABLE);
264 TokenService::TokenAvailableDetails* tok_details = 306 TokenService::TokenAvailableDetails* tok_details =
265 Details<TokenService::TokenAvailableDetails>(details).ptr(); 307 Details<TokenService::TokenAvailableDetails>(details).ptr();
266 308
267 // If a GAIA service token has become available, use it to pre-login the 309 // If a GAIA service token has become available, use it to pre-login the
268 // user to other services that depend on GAIA credentials. 310 // user to other services that depend on GAIA credentials.
269 if (tok_details->service() == GaiaConstants::kGaiaService) { 311 if (tok_details->service() == GaiaConstants::kGaiaService) {
270 if (client_login_.get() == NULL) { 312 if (client_login_.get() == NULL) {
271 client_login_.reset(new GaiaAuthFetcher(this, 313 client_login_.reset(new GaiaAuthFetcher(this,
272 GaiaConstants::kChromeSource, 314 GaiaConstants::kChromeSource,
273 profile_->GetRequestContext())); 315 profile_->GetRequestContext()));
274 } 316 }
275 317
276 client_login_->StartTokenAuth(tok_details->token()); 318 client_login_->StartTokenAuth(tok_details->token());
277 319
278 // We only want to do this once per sign-in. 320 // We only want to do this once per sign-in.
279 CleanupNotificationRegistration(); 321 CleanupNotificationRegistration();
280 } 322 }
281 #endif 323 #endif
282 } 324 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698