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

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

Issue 8761016: Shaving parallel authenticator yak to remove unnecessary dependency on this class from OAuth spec... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years 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/chromeos/login/login_utils.h" 5 #include "chrome/browser/chromeos/login/login_utils.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/compiler_specific.h" 10 #include "base/compiler_specific.h"
11 #include "base/file_path.h" 11 #include "base/file_path.h"
12 #include "base/file_util.h" 12 #include "base/file_util.h"
13 #include "base/location.h" 13 #include "base/location.h"
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/singleton.h" 16 #include "base/memory/singleton.h"
17 #include "base/path_service.h" 17 #include "base/path_service.h"
18 #include "base/string_util.h" 18 #include "base/string_util.h"
19 #include "base/stringprintf.h" 19 #include "base/stringprintf.h"
20 #include "base/synchronization/lock.h" 20 #include "base/synchronization/lock.h"
21 #include "base/threading/thread_restrictions.h" 21 #include "base/threading/thread_restrictions.h"
22 #include "base/time.h" 22 #include "base/time.h"
23 #include "base/utf_string_conversions.h" 23 #include "base/utf_string_conversions.h"
24 #include "chrome/browser/browser_process.h" 24 #include "chrome/browser/browser_process.h"
25 #include "chrome/browser/browser_shutdown.h" 25 #include "chrome/browser/browser_shutdown.h"
26 #include "chrome/browser/chromeos/boot_times_loader.h" 26 #include "chrome/browser/chromeos/boot_times_loader.h"
27 #include "chrome/browser/chromeos/cros/network_library.h" 27 #include "chrome/browser/chromeos/cros/network_library.h"
28 #include "chrome/browser/chromeos/cros/cert_library.h"
Nikita (slow) 2011/12/01 14:34:13 nit: Move one line up.
zel 2011/12/02 02:35:23 Done.
28 #include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" 29 #include "chrome/browser/chromeos/dbus/dbus_thread_manager.h"
29 #include "chrome/browser/chromeos/dbus/session_manager_client.h" 30 #include "chrome/browser/chromeos/dbus/session_manager_client.h"
30 #include "chrome/browser/chromeos/input_method/input_method_manager.h" 31 #include "chrome/browser/chromeos/input_method/input_method_manager.h"
31 #include "chrome/browser/chromeos/input_method/input_method_util.h" 32 #include "chrome/browser/chromeos/input_method/input_method_util.h"
32 #include "chrome/browser/chromeos/login/background_view.h" 33 #include "chrome/browser/chromeos/login/background_view.h"
33 #include "chrome/browser/chromeos/login/cookie_fetcher.h" 34 #include "chrome/browser/chromeos/login/cookie_fetcher.h"
34 #include "chrome/browser/chromeos/login/language_switch_menu.h" 35 #include "chrome/browser/chromeos/login/language_switch_menu.h"
35 #include "chrome/browser/chromeos/login/login_display_host.h" 36 #include "chrome/browser/chromeos/login/login_display_host.h"
36 #include "chrome/browser/chromeos/login/ownership_service.h" 37 #include "chrome/browser/chromeos/login/ownership_service.h"
37 #include "chrome/browser/chromeos/login/parallel_authenticator.h" 38 #include "chrome/browser/chromeos/login/parallel_authenticator.h"
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 98
98 // The service scope of the OAuth v2 token that ChromeOS login will be 99 // The service scope of the OAuth v2 token that ChromeOS login will be
99 // requesting. 100 // requesting.
100 // TODO(zelidrag): Figure out if we need to add more services here. 101 // TODO(zelidrag): Figure out if we need to add more services here.
101 const char kServiceScopeChromeOS[] = 102 const char kServiceScopeChromeOS[] =
102 "https://www.googleapis.com/auth/chromesync"; 103 "https://www.googleapis.com/auth/chromesync";
103 104
104 const char kServiceScopeChromeOSDeviceManagement[] = 105 const char kServiceScopeChromeOSDeviceManagement[] =
105 "https://www.googleapis.com/auth/chromeosdevicemanagement"; 106 "https://www.googleapis.com/auth/chromeosdevicemanagement";
106 107
108 const char kServiceScopeChromeOSDocuments[] =
109 "https://docs.google.com/feeds/ "
110 "https://spreadsheets.google.com/feeds/ "
111 "https://docs.googleusercontent.com/";
112
107 class InitializeCookieMonsterHelper { 113 class InitializeCookieMonsterHelper {
108 public: 114 public:
109 explicit InitializeCookieMonsterHelper( 115 explicit InitializeCookieMonsterHelper(
110 net::URLRequestContextGetter* new_context) 116 net::URLRequestContextGetter* new_context)
111 : ALLOW_THIS_IN_INITIALIZER_LIST(callback_(base::Bind( 117 : ALLOW_THIS_IN_INITIALIZER_LIST(callback_(base::Bind(
112 &InitializeCookieMonsterHelper::InitializeCookieMonster, 118 &InitializeCookieMonsterHelper::InitializeCookieMonster,
113 base::Unretained(this)))), 119 base::Unretained(this)))),
114 new_context_(new_context) { 120 new_context_(new_context) {
115 } 121 }
116 122
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 class Delegate { 176 class Delegate {
171 public: 177 public:
172 virtual ~Delegate() {} 178 virtual ~Delegate() {}
173 virtual void OnOAuthVerificationSucceeded(const std::string& user_name, 179 virtual void OnOAuthVerificationSucceeded(const std::string& user_name,
174 const std::string& sid, 180 const std::string& sid,
175 const std::string& lsid, 181 const std::string& lsid,
176 const std::string& auth) {} 182 const std::string& auth) {}
177 virtual void OnOAuthVerificationFailed(const std::string& user_name) {} 183 virtual void OnOAuthVerificationFailed(const std::string& user_name) {}
178 virtual void OnUserCookiesFetchSucceeded(const std::string& user_name) {} 184 virtual void OnUserCookiesFetchSucceeded(const std::string& user_name) {}
179 virtual void OnUserCookiesFetchFailed(const std::string& user_name) {} 185 virtual void OnUserCookiesFetchFailed(const std::string& user_name) {}
186 virtual void OnDocumentsTokenFetchSucceeded(const std::string& username,
187 const std::string& oauth2_token) {}
188 virtual void OnDocumentsTokenFetchFailed(const std::string& username) {}
180 }; 189 };
181 190
182 OAuthLoginVerifier(OAuthLoginVerifier::Delegate* delegate, 191 OAuthLoginVerifier(OAuthLoginVerifier::Delegate* delegate,
183 Profile* user_profile, 192 Profile* user_profile,
184 const std::string& oauth1_token, 193 const std::string& oauth1_token,
185 const std::string& oauth1_secret, 194 const std::string& oauth1_secret,
186 const std::string& username) 195 const std::string& username)
187 : delegate_(delegate), 196 : delegate_(delegate),
188 oauth_fetcher_(this, 197 oauth_fetcher_(this,
189 user_profile->GetOffTheRecordProfile()->GetRequestContext(), 198 user_profile->GetOffTheRecordProfile()->GetRequestContext(),
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 VERIFICATION_STEP_FAILED, 274 VERIFICATION_STEP_FAILED,
266 } VerificationStep; 275 } VerificationStep;
267 276
268 // Kicks off GAIA session cookie retreival process. 277 // Kicks off GAIA session cookie retreival process.
269 void StartCookiesRetreival() { 278 void StartCookiesRetreival() {
270 DCHECK(!sid_.empty()); 279 DCHECK(!sid_.empty());
271 DCHECK(!lsid_.empty()); 280 DCHECK(!lsid_.empty());
272 gaia_fetcher_.StartIssueAuthToken(sid_, lsid_, GaiaConstants::kGaiaService); 281 gaia_fetcher_.StartIssueAuthToken(sid_, lsid_, GaiaConstants::kGaiaService);
273 } 282 }
274 283
284 void StartDocsTokenRetreival() {
285 DCHECK(!oauth1_token_.empty());
286 DCHECK(!oauth1_secret_.empty());
287 oauth_fetcher_.SetAutoFetchLimit(
288 GaiaOAuthFetcher::OAUTH2_SERVICE_ACCESS_TOKEN);
289 oauth_fetcher_.StartOAuthWrapBridge(
290 oauth1_token_, oauth1_secret_, GaiaConstants::kGaiaOAuthDuration,
291 std::string(kServiceScopeChromeOSDocuments));
292 }
293
275 // Decides how to proceed on GAIA response and other errors. It can schedule 294 // Decides how to proceed on GAIA response and other errors. It can schedule
276 // to rerun the verification process if detects transient network or service 295 // to rerun the verification process if detects transient network or service
277 // errors. 296 // errors.
278 bool RetryOnError(const GoogleServiceAuthError& error) { 297 bool RetryOnError(const GoogleServiceAuthError& error) {
279 // If we can't connect to GAIA due to network or service related reasons, 298 // If we can't connect to GAIA due to network or service related reasons,
280 // we should attempt OAuth token verification again. 299 // we should attempt OAuth token verification again.
281 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED || 300 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED ||
282 error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE) { 301 error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE) {
283 if (verification_count_ < kMaxOAuthTokenVerificationAttemptCount) { 302 if (verification_count_ < kMaxOAuthTokenVerificationAttemptCount) {
284 BrowserThread::PostDelayedTask(BrowserThread::UI, FROM_HERE, 303 BrowserThread::PostDelayedTask(BrowserThread::UI, FROM_HERE,
285 base::Bind(&OAuthLoginVerifier::ContinueVerification, AsWeakPtr()), 304 base::Bind(&OAuthLoginVerifier::ContinueVerification, AsWeakPtr()),
286 kOAuthVerificationRestartDelay); 305 kOAuthVerificationRestartDelay);
287 return true; 306 return true;
288 } 307 }
289 } 308 }
290 step_ = VERIFICATION_STEP_FAILED; 309 step_ = VERIFICATION_STEP_FAILED;
291 return false; 310 return false;
292 } 311 }
293 312
294 // GaiaOAuthConsumer implementation: 313 // GaiaOAuthConsumer implementation:
295 virtual void OnOAuthLoginSuccess(const std::string& sid, 314 virtual void OnOAuthLoginSuccess(const std::string& sid,
296 const std::string& lsid, 315 const std::string& lsid,
297 const std::string& auth) OVERRIDE { 316 const std::string& auth) OVERRIDE {
298 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 317 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
299 step_ = VERIFICATION_STEP_OAUTH_VERIFIED; 318 step_ = VERIFICATION_STEP_OAUTH_VERIFIED;
300 verification_count_ = 0; 319 verification_count_ = 0;
301 sid_ = sid; 320 sid_ = sid;
302 lsid_ = lsid; 321 lsid_ = lsid;
303 delegate_->OnOAuthVerificationSucceeded(username_, sid, lsid, auth); 322 delegate_->OnOAuthVerificationSucceeded(username_, sid, lsid, auth);
323 StartDocsTokenRetreival();
Nikita (slow) 2011/12/01 14:34:13 Could this be initiated from FetchSecondaryTokens(
zel 2011/12/02 02:35:23 All docs token retrieval related changes in this c
304 StartCookiesRetreival(); 324 StartCookiesRetreival();
305 } 325 }
306 326
307 virtual void OnOAuthLoginFailure( 327 virtual void OnOAuthLoginFailure(
308 const GoogleServiceAuthError& error) OVERRIDE { 328 const GoogleServiceAuthError& error) OVERRIDE {
309 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 329 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
310 LOG(WARNING) << "Failed to verify OAuth1 access tokens," 330 LOG(WARNING) << "Failed to verify OAuth1 access tokens,"
311 << " error.state=" << error.state(); 331 << " error.state=" << error.state();
312 if (!RetryOnError(error)) 332 if (!RetryOnError(error))
313 delegate_->OnOAuthVerificationFailed(username_); 333 delegate_->OnOAuthVerificationFailed(username_);
(...skipping 25 matching lines...) Expand all
339 delegate_->OnUserCookiesFetchSucceeded(username_); 359 delegate_->OnUserCookiesFetchSucceeded(username_);
340 } 360 }
341 361
342 virtual void OnMergeSessionFailure( 362 virtual void OnMergeSessionFailure(
343 const GoogleServiceAuthError& error) OVERRIDE { 363 const GoogleServiceAuthError& error) OVERRIDE {
344 DVLOG(1) << "Failed MergeSession request," 364 DVLOG(1) << "Failed MergeSession request,"
345 << " error.state=" << error.state(); 365 << " error.state=" << error.state();
346 OnCookueFetchFailed(error); 366 OnCookueFetchFailed(error);
347 } 367 }
348 368
369 virtual void OnOAuthWrapBridgeSuccess(
370 const std::string& service_name,
371 const std::string& token,
372 const std::string& expires_in) OVERRIDE {
373 VLOG(1) << "Got OAuth access token for " << service_name;
374 delegate_->OnDocumentsTokenFetchSucceeded(username_, token);
375 }
376
377 virtual void OnOAuthWrapBridgeFailure(
378 const std::string& service_name,
379 const GoogleServiceAuthError& error) OVERRIDE {
380 LOG(WARNING) << "Failed to get OAuth access token for " << service_name
381 << ", error: " << error.state();
382 if (!RetryOnError(error))
383 delegate_->OnDocumentsTokenFetchFailed(username_);
384 }
385
349 OAuthLoginVerifier::Delegate* delegate_; 386 OAuthLoginVerifier::Delegate* delegate_;
350 GaiaOAuthFetcher oauth_fetcher_; 387 GaiaOAuthFetcher oauth_fetcher_;
351 GaiaAuthFetcher gaia_fetcher_; 388 GaiaAuthFetcher gaia_fetcher_;
352 std::string oauth1_token_; 389 std::string oauth1_token_;
353 std::string oauth1_secret_; 390 std::string oauth1_secret_;
354 std::string sid_; 391 std::string sid_;
355 std::string lsid_; 392 std::string lsid_;
356 std::string username_; 393 std::string username_;
357 Profile* user_profile_; 394 Profile* user_profile_;
358 int verification_count_; 395 int verification_count_;
(...skipping 745 matching lines...) Expand 10 before | Expand all | Expand 10 after
1104 } 1141 }
1105 } 1142 }
1106 1143
1107 void LoginUtilsImpl::RestoreAuthenticationSession(const std::string& username, 1144 void LoginUtilsImpl::RestoreAuthenticationSession(const std::string& username,
1108 Profile* user_profile) { 1145 Profile* user_profile) {
1109 username_ = username; 1146 username_ = username;
1110 KickStartAuthentication(user_profile); 1147 KickStartAuthentication(user_profile);
1111 } 1148 }
1112 1149
1113 void LoginUtilsImpl::KickStartAuthentication(Profile* user_profile) { 1150 void LoginUtilsImpl::KickStartAuthentication(Profile* user_profile) {
1114 if (!authenticator_.get())
1115 CreateAuthenticator(NULL);
1116 std::string oauth1_token; 1151 std::string oauth1_token;
1117 std::string oauth1_secret; 1152 std::string oauth1_secret;
1118 if (ReadOAuth1AccessToken(user_profile, &oauth1_token, &oauth1_secret)) 1153 if (ReadOAuth1AccessToken(user_profile, &oauth1_token, &oauth1_secret))
1119 VerifyOAuth1AccessToken(user_profile, oauth1_token, oauth1_secret); 1154 VerifyOAuth1AccessToken(user_profile, oauth1_token, oauth1_secret);
1120
1121 authenticator_ = NULL;
1122 } 1155 }
1123 1156
1124 void LoginUtilsImpl::SetBackgroundView(BackgroundView* background_view) { 1157 void LoginUtilsImpl::SetBackgroundView(BackgroundView* background_view) {
1125 background_view_ = background_view; 1158 background_view_ = background_view;
1126 } 1159 }
1127 1160
1128 BackgroundView* LoginUtilsImpl::GetBackgroundView() { 1161 BackgroundView* LoginUtilsImpl::GetBackgroundView() {
1129 return background_view_; 1162 return background_view_;
1130 } 1163 }
1131 1164
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
1191 User::OAUTH_TOKEN_STATUS_VALID) { 1224 User::OAUTH_TOKEN_STATUS_VALID) {
1192 return false; 1225 return false;
1193 } 1226 }
1194 1227
1195 PrefService* pref_service = user_profile->GetPrefs(); 1228 PrefService* pref_service = user_profile->GetPrefs();
1196 std::string encoded_token = pref_service->GetString(prefs::kOAuth1Token); 1229 std::string encoded_token = pref_service->GetString(prefs::kOAuth1Token);
1197 std::string encoded_secret = pref_service->GetString(prefs::kOAuth1Secret); 1230 std::string encoded_secret = pref_service->GetString(prefs::kOAuth1Secret);
1198 if (!encoded_token.length() || !encoded_secret.length()) 1231 if (!encoded_token.length() || !encoded_secret.length())
1199 return false; 1232 return false;
1200 1233
1201 DCHECK(authenticator_.get()); 1234 std::string decoded_token =
1202 std::string decoded_token = authenticator_->DecryptToken(encoded_token); 1235 CrosLibrary::Get()->GetCertLibrary()->DecryptToken(encoded_token);
1203 std::string decoded_secret = authenticator_->DecryptToken(encoded_secret); 1236 std::string decoded_secret =
1237 CrosLibrary::Get()->GetCertLibrary()->DecryptToken(encoded_secret);
1204 if (!decoded_token.length() || !decoded_secret.length()) 1238 if (!decoded_token.length() || !decoded_secret.length())
1205 return false; 1239 return false;
1206 1240
1207 *token = decoded_token; 1241 *token = decoded_token;
1208 *secret = decoded_secret; 1242 *secret = decoded_secret;
1209 return true; 1243 return true;
1210 } 1244 }
1211 1245
1212 void LoginUtilsImpl::StoreOAuth1AccessToken(Profile* user_profile, 1246 void LoginUtilsImpl::StoreOAuth1AccessToken(Profile* user_profile,
1213 const std::string& token, 1247 const std::string& token,
1214 const std::string& secret) { 1248 const std::string& secret) {
1215 // First store OAuth1 token + service for the current user profile... 1249 // First store OAuth1 token + service for the current user profile...
1216 PrefService* pref_service = user_profile->GetPrefs(); 1250 PrefService* pref_service = user_profile->GetPrefs();
1217 pref_service->SetString(prefs::kOAuth1Token, 1251 pref_service->SetString(prefs::kOAuth1Token,
1218 authenticator_->EncryptToken(token)); 1252 CrosLibrary::Get()->GetCertLibrary()->EncryptToken(token));
1219 pref_service->SetString(prefs::kOAuth1Secret, 1253 pref_service->SetString(prefs::kOAuth1Secret,
1220 authenticator_->EncryptToken(secret)); 1254 CrosLibrary::Get()->GetCertLibrary()->EncryptToken(secret));
1221 1255
1222 // ...then record the presence of valid OAuth token for this account in local 1256 // ...then record the presence of valid OAuth token for this account in local
1223 // state as well. 1257 // state as well.
1224 UserManager::Get()->SaveUserOAuthStatus(username_, 1258 UserManager::Get()->SaveUserOAuthStatus(username_,
1225 User::OAUTH_TOKEN_STATUS_VALID); 1259 User::OAUTH_TOKEN_STATUS_VALID);
1226 } 1260 }
1227 1261
1228 void LoginUtilsImpl::VerifyOAuth1AccessToken(Profile* user_profile, 1262 void LoginUtilsImpl::VerifyOAuth1AccessToken(Profile* user_profile,
1229 const std::string& token, 1263 const std::string& token,
1230 const std::string& secret) { 1264 const std::string& secret) {
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
1327 // Mark login host for deletion after browser starts. This 1361 // Mark login host for deletion after browser starts. This
1328 // guarantees that the message loop will be referenced by the 1362 // guarantees that the message loop will be referenced by the
1329 // browser before it is dereferenced by the login host. 1363 // browser before it is dereferenced by the login host.
1330 if (login_host) { 1364 if (login_host) {
1331 login_host->OnSessionStart(); 1365 login_host->OnSessionStart();
1332 login_host = NULL; 1366 login_host = NULL;
1333 } 1367 }
1334 } 1368 }
1335 1369
1336 } // namespace chromeos 1370 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698