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/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" |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 oauth1_secret_); | 185 oauth1_secret_); |
186 } | 186 } |
187 } | 187 } |
188 | 188 |
189 // GaiaOAuthConsumer implementation: | 189 // GaiaOAuthConsumer implementation: |
190 virtual void OnOAuthLoginSuccess(const std::string& sid, | 190 virtual void OnOAuthLoginSuccess(const std::string& sid, |
191 const std::string& lsid, | 191 const std::string& lsid, |
192 const std::string& auth) OVERRIDE { | 192 const std::string& auth) OVERRIDE { |
193 GaiaAuthConsumer::ClientLoginResult credentials(sid, | 193 GaiaAuthConsumer::ClientLoginResult credentials(sid, |
194 lsid, auth, std::string()); | 194 lsid, auth, std::string()); |
| 195 UserManager::Get()->set_offline_login(false); |
195 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 196 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
196 new StartSyncOnUIThreadTask(credentials)); | 197 new StartSyncOnUIThreadTask(credentials)); |
197 } | 198 } |
| 199 |
198 virtual void OnOAuthLoginFailure( | 200 virtual void OnOAuthLoginFailure( |
199 const GoogleServiceAuthError& error) OVERRIDE { | 201 const GoogleServiceAuthError& error) OVERRIDE { |
200 LOG(WARNING) << "Failed to verify OAuth1 access tokens," | 202 LOG(WARNING) << "Failed to verify OAuth1 access tokens," |
201 << " error.state=" << error.state(); | 203 << " error.state=" << error.state(); |
202 | 204 |
203 // Mark this account's OAuth token state as invalid if the failure is not | 205 // Mark this account's OAuth token state as invalid if the failure is not |
204 // caused by network error. | 206 // caused by network error. |
205 if (error.state() != GoogleServiceAuthError::CONNECTION_FAILED) { | 207 if (error.state() != GoogleServiceAuthError::CONNECTION_FAILED) { |
206 UserManager::Get()->SaveUserOAuthStatus(username_, | 208 UserManager::Get()->SaveUserOAuthStatus(username_, |
207 UserManager::OAUTH_TOKEN_STATUS_INVALID); | 209 UserManager::OAUTH_TOKEN_STATUS_INVALID); |
| 210 } else { |
| 211 UserManager::Get()->set_offline_login(true); |
208 } | 212 } |
209 } | 213 } |
210 | 214 |
211 private: | 215 private: |
212 GaiaOAuthFetcher oauth_fetcher_; | 216 GaiaOAuthFetcher oauth_fetcher_; |
213 std::string oauth1_token_; | 217 std::string oauth1_token_; |
214 std::string oauth1_secret_; | 218 std::string oauth1_secret_; |
215 std::string username_; | 219 std::string username_; |
216 | 220 |
217 DISALLOW_COPY_AND_ASSIGN(OAuthLoginVerifier); | 221 DISALLOW_COPY_AND_ASSIGN(OAuthLoginVerifier); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 GaiaOAuthFetcher oauth_fetcher_; | 264 GaiaOAuthFetcher oauth_fetcher_; |
261 std::string oauth1_token_; | 265 std::string oauth1_token_; |
262 std::string oauth1_secret_; | 266 std::string oauth1_secret_; |
263 | 267 |
264 DISALLOW_COPY_AND_ASSIGN(PolicyOAuthFetcher); | 268 DISALLOW_COPY_AND_ASSIGN(PolicyOAuthFetcher); |
265 }; | 269 }; |
266 | 270 |
267 | 271 |
268 class LoginUtilsImpl : public LoginUtils, | 272 class LoginUtilsImpl : public LoginUtils, |
269 public ProfileManagerObserver, | 273 public ProfileManagerObserver, |
270 public GaiaOAuthConsumer { | 274 public GaiaOAuthConsumer, |
| 275 public net::NetworkChangeNotifier::OnlineStateObserver { |
271 public: | 276 public: |
272 LoginUtilsImpl() | 277 LoginUtilsImpl() |
273 : background_view_(NULL), | 278 : background_view_(NULL), |
274 pending_requests_(false), | 279 pending_requests_(false), |
275 using_oauth_(false), | 280 using_oauth_(false), |
276 has_cookies_(false), | 281 has_cookies_(false), |
277 delegate_(NULL) { | 282 delegate_(NULL) { |
| 283 net::NetworkChangeNotifier::AddOnlineStateObserver(this); |
| 284 } |
| 285 |
| 286 virtual ~LoginUtilsImpl() { |
| 287 net::NetworkChangeNotifier::RemoveOnlineStateObserver(this); |
278 } | 288 } |
279 | 289 |
280 virtual void PrepareProfile( | 290 virtual void PrepareProfile( |
281 const std::string& username, | 291 const std::string& username, |
282 const std::string& password, | 292 const std::string& password, |
283 const GaiaAuthConsumer::ClientLoginResult& credentials, | 293 const GaiaAuthConsumer::ClientLoginResult& credentials, |
284 bool pending_requests, | 294 bool pending_requests, |
285 bool using_oauth, | 295 bool using_oauth, |
286 bool has_cookies, | 296 bool has_cookies, |
287 LoginUtils::Delegate* delegate) OVERRIDE; | 297 LoginUtils::Delegate* delegate) OVERRIDE; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 | 351 |
342 // GaiaOAuthConsumer overrides. | 352 // GaiaOAuthConsumer overrides. |
343 virtual void OnGetOAuthTokenSuccess(const std::string& oauth_token) OVERRIDE; | 353 virtual void OnGetOAuthTokenSuccess(const std::string& oauth_token) OVERRIDE; |
344 virtual void OnGetOAuthTokenFailure( | 354 virtual void OnGetOAuthTokenFailure( |
345 const GoogleServiceAuthError& error) OVERRIDE; | 355 const GoogleServiceAuthError& error) OVERRIDE; |
346 virtual void OnOAuthGetAccessTokenSuccess(const std::string& token, | 356 virtual void OnOAuthGetAccessTokenSuccess(const std::string& token, |
347 const std::string& secret) OVERRIDE; | 357 const std::string& secret) OVERRIDE; |
348 virtual void OnOAuthGetAccessTokenFailure( | 358 virtual void OnOAuthGetAccessTokenFailure( |
349 const GoogleServiceAuthError& error) OVERRIDE; | 359 const GoogleServiceAuthError& error) OVERRIDE; |
350 | 360 |
| 361 // net::NetworkChangeNotifier::OnlineStateObserver overrides. |
| 362 virtual void OnOnlineStateChanged(bool online) OVERRIDE; |
| 363 |
351 protected: | 364 protected: |
352 virtual std::string GetOffTheRecordCommandLine( | 365 virtual std::string GetOffTheRecordCommandLine( |
353 const GURL& start_url, | 366 const GURL& start_url, |
354 const CommandLine& base_command_line, | 367 const CommandLine& base_command_line, |
355 CommandLine *command_line); | 368 CommandLine *command_line); |
356 | 369 |
357 private: | 370 private: |
358 // Reads OAuth1 token from user profile's prefs. | 371 // Reads OAuth1 token from user profile's prefs. |
359 bool ReadOAuth1AccessToken(Profile* user_profile, | 372 bool ReadOAuth1AccessToken(Profile* user_profile, |
360 std::string* token, | 373 std::string* token, |
(...skipping 565 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
926 UserManager::OAUTH_TOKEN_STATUS_VALID) { | 939 UserManager::OAUTH_TOKEN_STATUS_VALID) { |
927 return false; | 940 return false; |
928 } | 941 } |
929 | 942 |
930 PrefService* pref_service = user_profile->GetPrefs(); | 943 PrefService* pref_service = user_profile->GetPrefs(); |
931 std::string encoded_token = pref_service->GetString(prefs::kOAuth1Token); | 944 std::string encoded_token = pref_service->GetString(prefs::kOAuth1Token); |
932 std::string encoded_secret = pref_service->GetString(prefs::kOAuth1Secret); | 945 std::string encoded_secret = pref_service->GetString(prefs::kOAuth1Secret); |
933 if (!encoded_token.length() || !encoded_secret.length()) | 946 if (!encoded_token.length() || !encoded_secret.length()) |
934 return false; | 947 return false; |
935 | 948 |
| 949 DCHECK(authenticator_.get()); |
936 std::string decoded_token = authenticator_->DecryptToken(encoded_token); | 950 std::string decoded_token = authenticator_->DecryptToken(encoded_token); |
937 std::string decoded_secret = authenticator_->DecryptToken(encoded_secret); | 951 std::string decoded_secret = authenticator_->DecryptToken(encoded_secret); |
938 if (!decoded_token.length() || !decoded_secret.length()) { | 952 if (!decoded_token.length() || !decoded_secret.length()) { |
939 // TODO(zelidrag): Remove legacy encryption support in R16. | 953 // TODO(zelidrag): Remove legacy encryption support in R16. |
940 // Check if tokens were encoded with the legacy encryption instead. | 954 // Check if tokens were encoded with the legacy encryption instead. |
941 decoded_token = authenticator_->DecryptLegacyToken(encoded_token); | 955 decoded_token = authenticator_->DecryptLegacyToken(encoded_token); |
942 decoded_secret = authenticator_->DecryptLegacyToken(encoded_secret); | 956 decoded_secret = authenticator_->DecryptLegacyToken(encoded_secret); |
943 if (!decoded_token.length() || !decoded_secret.length()) | 957 if (!decoded_token.length() || !decoded_secret.length()) |
944 return false; | 958 return false; |
945 | 959 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1010 // OAuth tokens are fetched. It would use incorrect Authenticator instance. | 1024 // OAuth tokens are fetched. It would use incorrect Authenticator instance. |
1011 authenticator_ = NULL; | 1025 authenticator_ = NULL; |
1012 } | 1026 } |
1013 | 1027 |
1014 void LoginUtilsImpl::OnOAuthGetAccessTokenFailure( | 1028 void LoginUtilsImpl::OnOAuthGetAccessTokenFailure( |
1015 const GoogleServiceAuthError& error) { | 1029 const GoogleServiceAuthError& error) { |
1016 // TODO(zelidrag): Pop up sync setup UI here? | 1030 // TODO(zelidrag): Pop up sync setup UI here? |
1017 LOG(WARNING) << "Failed fetching OAuth v1 token, error: " << error.state(); | 1031 LOG(WARNING) << "Failed fetching OAuth v1 token, error: " << error.state(); |
1018 } | 1032 } |
1019 | 1033 |
| 1034 void LoginUtilsImpl::OnOnlineStateChanged(bool online) { |
| 1035 // If we come online for the first time after successful offline login, |
| 1036 // we need to kick of OAuth token verification process again. |
| 1037 if (UserManager::Get()->user_is_logged_in() && |
| 1038 UserManager::Get()->offline_login() && online) { |
| 1039 if (!authenticator_.get()) |
| 1040 CreateAuthenticator(NULL); |
| 1041 std::string oauth1_token; |
| 1042 std::string oauth1_secret; |
| 1043 Profile* user_profile = ProfileManager::GetDefaultProfile(); |
| 1044 if (ReadOAuth1AccessToken(user_profile, &oauth1_token, &oauth1_secret)) |
| 1045 VerifyOAuth1AccessToken(user_profile, oauth1_token, oauth1_secret); |
| 1046 authenticator_ = NULL; |
| 1047 } |
| 1048 } |
| 1049 |
1020 LoginUtils* LoginUtils::Get() { | 1050 LoginUtils* LoginUtils::Get() { |
1021 return LoginUtilsWrapper::GetInstance()->get(); | 1051 return LoginUtilsWrapper::GetInstance()->get(); |
1022 } | 1052 } |
1023 | 1053 |
1024 void LoginUtils::Set(LoginUtils* mock) { | 1054 void LoginUtils::Set(LoginUtils* mock) { |
1025 LoginUtilsWrapper::GetInstance()->reset(mock); | 1055 LoginUtilsWrapper::GetInstance()->reset(mock); |
1026 } | 1056 } |
1027 | 1057 |
1028 void LoginUtils::DoBrowserLaunch(Profile* profile, | 1058 void LoginUtils::DoBrowserLaunch(Profile* profile, |
1029 LoginDisplayHost* login_host) { | 1059 LoginDisplayHost* login_host) { |
(...skipping 11 matching lines...) Expand all Loading... |
1041 // Mark login host for deletion after browser starts. This | 1071 // Mark login host for deletion after browser starts. This |
1042 // guarantees that the message loop will be referenced by the | 1072 // guarantees that the message loop will be referenced by the |
1043 // browser before it is dereferenced by the login host. | 1073 // browser before it is dereferenced by the login host. |
1044 if (login_host) { | 1074 if (login_host) { |
1045 login_host->OnSessionStart(); | 1075 login_host->OnSessionStart(); |
1046 login_host = NULL; | 1076 login_host = NULL; |
1047 } | 1077 } |
1048 } | 1078 } |
1049 | 1079 |
1050 } // namespace chromeos | 1080 } // namespace chromeos |
OLD | NEW |