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

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: 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
« no previous file with comments | « chrome/browser/sync/signin_manager.h ('k') | chrome/browser/sync/sync_setup_wizard.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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()->SetString(
171 prefs::kGoogleServicesUsername, GetUsername());
Roger Tawa OOO till Jul 10th 2011/08/10 18:10:43 won't GetUsername() always return an empty string?
Rick Campbell 2011/08/10 18:55:16 Done. Changing this and the next one to ClearPref
172 profile_->GetPrefs()->SetBoolean(prefs::kSyncUsingOAuth, false);
149 profile_->GetPrefs()->ScheduleSavePersistentPrefs(); 173 profile_->GetPrefs()->ScheduleSavePersistentPrefs();
150 profile_->GetTokenService()->ResetCredentialsInMemory(); 174 profile_->GetTokenService()->ResetCredentialsInMemory();
151 profile_->GetTokenService()->EraseTokensFromDB(); 175 profile_->GetTokenService()->EraseTokensFromDB();
152 } 176 }
153 177
154 void SigninManager::OnClientLoginSuccess(const ClientLoginResult& result) { 178 void SigninManager::OnClientLoginSuccess(const ClientLoginResult& result) {
155 last_result_ = result; 179 last_result_ = result;
156 // Make a request for the canonical email address. 180 // Make a request for the canonical email address.
157 client_login_->StartGetUserInfo(result.lsid, kGetInfoEmailKey); 181 client_login_->StartGetUserInfo(result.lsid, kGetInfoEmailKey);
158 } 182 }
159 183
184 // NOTE: GetUserInfo is a ClientLogin request similar to OAuth's userinfo
160 void SigninManager::OnGetUserInfoSuccess(const std::string& key, 185 void SigninManager::OnGetUserInfoSuccess(const std::string& key,
161 const std::string& value) { 186 const std::string& value) {
162 DCHECK(key == kGetInfoEmailKey); 187 DCHECK(key == kGetInfoEmailKey);
163 188
164 username_ = value; 189 username_ = value;
165 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, username_); 190 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, username_);
191 profile_->GetPrefs()->SetBoolean(prefs::kSyncUsingOAuth, false);
166 profile_->GetPrefs()->ScheduleSavePersistentPrefs(); 192 profile_->GetPrefs()->ScheduleSavePersistentPrefs();
167 193
168 GoogleServiceSigninSuccessDetails details(username_, password_); 194 GoogleServiceSigninSuccessDetails details(username_, password_);
169 NotificationService::current()->Notify( 195 NotificationService::current()->Notify(
170 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, 196 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL,
171 Source<Profile>(profile_), 197 Source<Profile>(profile_),
172 Details<const GoogleServiceSigninSuccessDetails>(&details)); 198 Details<const GoogleServiceSigninSuccessDetails>(&details));
173 199
174 password_.clear(); // Don't need it anymore. 200 password_.clear(); // Don't need it anymore.
175 201
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 } 242 }
217 243
218 SignOut(); 244 SignOut();
219 } 245 }
220 246
221 void SigninManager::OnGetOAuthTokenSuccess(const std::string& oauth_token) { 247 void SigninManager::OnGetOAuthTokenSuccess(const std::string& oauth_token) {
222 VLOG(1) << "SigninManager::SigninManager::OnGetOAuthTokenSuccess"; 248 VLOG(1) << "SigninManager::SigninManager::OnGetOAuthTokenSuccess";
223 } 249 }
224 250
225 void SigninManager::OnGetOAuthTokenFailure() { 251 void SigninManager::OnGetOAuthTokenFailure() {
226 VLOG(1) << "SigninManager::OnGetOAuthTokenFailure"; 252 LOG(WARNING) << "SigninManager::OnGetOAuthTokenFailure";
227 } 253 }
228 254
229 void SigninManager::OnOAuthGetAccessTokenSuccess(const std::string& token, 255 void SigninManager::OnOAuthGetAccessTokenSuccess(const std::string& token,
230 const std::string& secret) { 256 const std::string& secret) {
231 VLOG(1) << "SigninManager::OnOAuthGetAccessTokenSuccess"; 257 VLOG(1) << "SigninManager::OnOAuthGetAccessTokenSuccess";
258 profile_->GetTokenService()->UpdateOAuthCredentials(token, secret);
232 } 259 }
233 260
234 void SigninManager::OnOAuthGetAccessTokenFailure( 261 void SigninManager::OnOAuthGetAccessTokenFailure(
235 const GoogleServiceAuthError& error) { 262 const GoogleServiceAuthError& error) {
236 VLOG(1) << "SigninManager::OnOAuthGetAccessTokenFailure"; 263 LOG(WARNING) << "SigninManager::OnOAuthGetAccessTokenFailure";
237 } 264 }
238 265
239 void SigninManager::OnOAuthWrapBridgeSuccess(const std::string& service_name, 266 void SigninManager::OnOAuthWrapBridgeSuccess(const std::string& service_name,
240 const std::string& token, 267 const std::string& token,
241 const std::string& expires_in) { 268 const std::string& expires_in) {
242 VLOG(1) << "SigninManager::OnOAuthWrapBridgeSuccess"; 269 VLOG(1) << "SigninManager::OnOAuthWrapBridgeSuccess";
243 } 270 }
244 271
245 void SigninManager::OnOAuthWrapBridgeFailure( 272 void SigninManager::OnOAuthWrapBridgeFailure(
246 const std::string& service_scope, 273 const std::string& service_scope,
247 const GoogleServiceAuthError& error) { 274 const GoogleServiceAuthError& error) {
248 VLOG(1) << "SigninManager::OnOAuthWrapBridgeFailure"; 275 LOG(WARNING) << "SigninManager::OnOAuthWrapBridgeFailure";
249 } 276 }
250 277
278 // NOTE: userinfo is an OAuth request similar to ClientLogin's GetUserInfo
251 void SigninManager::OnUserInfoSuccess(const std::string& email) { 279 void SigninManager::OnUserInfoSuccess(const std::string& email) {
252 VLOG(1) << "SigninManager::OnUserInfoSuccess(\"" << email << "\")"; 280 VLOG(1) << "SigninManager::OnUserInfoSuccess(\"" << email << "\")";
Roger Tawa OOO till Jul 10th 2011/08/10 18:10:43 should this VLOG also be changed?
Rick Campbell 2011/08/10 18:55:16 Done.
281 oauth_username_ = email;
282 profile_->GetPrefs()->SetString(
283 prefs::kGoogleServicesUsername, oauth_username_);
284 profile_->GetPrefs()->SetBoolean(prefs::kSyncUsingOAuth, true);
285 profile_->GetPrefs()->ScheduleSavePersistentPrefs();
286
287 DCHECK(password_.empty());
288 GoogleServiceSigninSuccessDetails details(oauth_username_, "");
289 NotificationService::current()->Notify(
290 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL,
291 Source<Profile>(profile_),
292 Details<const GoogleServiceSigninSuccessDetails>(&details));
293
294 DCHECK(profile_->GetTokenService()->AreOAuthCredentialsValid());
295 profile_->GetTokenService()->StartFetchingOAuthTokens();
253 } 296 }
254 297
255 void SigninManager::OnUserInfoFailure(const GoogleServiceAuthError& error) { 298 void SigninManager::OnUserInfoFailure(const GoogleServiceAuthError& error) {
256 VLOG(1) << "SigninManager::OnUserInfoFailure"; 299 LOG(WARNING) << "SigninManager::OnUserInfoFailure";
257 } 300 }
258 301
259 void SigninManager::Observe(int type, 302 void SigninManager::Observe(int type,
260 const NotificationSource& source, 303 const NotificationSource& source,
261 const NotificationDetails& details) { 304 const NotificationDetails& details) {
262 #if !defined(OS_CHROMEOS) 305 #if !defined(OS_CHROMEOS)
263 DCHECK(type == chrome::NOTIFICATION_TOKEN_AVAILABLE); 306 DCHECK(type == chrome::NOTIFICATION_TOKEN_AVAILABLE);
264 TokenService::TokenAvailableDetails* tok_details = 307 TokenService::TokenAvailableDetails* tok_details =
265 Details<TokenService::TokenAvailableDetails>(details).ptr(); 308 Details<TokenService::TokenAvailableDetails>(details).ptr();
266 309
267 // If a GAIA service token has become available, use it to pre-login the 310 // If a GAIA service token has become available, use it to pre-login the
268 // user to other services that depend on GAIA credentials. 311 // user to other services that depend on GAIA credentials.
269 if (tok_details->service() == GaiaConstants::kGaiaService) { 312 if (tok_details->service() == GaiaConstants::kGaiaService) {
270 if (client_login_.get() == NULL) { 313 if (client_login_.get() == NULL) {
271 client_login_.reset(new GaiaAuthFetcher(this, 314 client_login_.reset(new GaiaAuthFetcher(this,
272 GaiaConstants::kChromeSource, 315 GaiaConstants::kChromeSource,
273 profile_->GetRequestContext())); 316 profile_->GetRequestContext()));
274 } 317 }
275 318
276 client_login_->StartTokenAuth(tok_details->token()); 319 client_login_->StartTokenAuth(tok_details->token());
277 320
278 // We only want to do this once per sign-in. 321 // We only want to do this once per sign-in.
279 CleanupNotificationRegistration(); 322 CleanupNotificationRegistration();
280 } 323 }
281 #endif 324 #endif
282 } 325 }
OLDNEW
« no previous file with comments | « chrome/browser/sync/signin_manager.h ('k') | chrome/browser/sync/sync_setup_wizard.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698