OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/settings/device_oauth2_token_service.h" | 5 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/prefs/pref_registry_simple.h" | 10 #include "base/prefs/pref_registry_simple.h" |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 // static | 230 // static |
231 void DeviceOAuth2TokenService::RegisterPrefs(PrefRegistrySimple* registry) { | 231 void DeviceOAuth2TokenService::RegisterPrefs(PrefRegistrySimple* registry) { |
232 registry->RegisterStringPref(prefs::kDeviceRobotAnyApiRefreshToken, | 232 registry->RegisterStringPref(prefs::kDeviceRobotAnyApiRefreshToken, |
233 std::string()); | 233 std::string()); |
234 } | 234 } |
235 | 235 |
236 void DeviceOAuth2TokenService::SetAndSaveRefreshToken( | 236 void DeviceOAuth2TokenService::SetAndSaveRefreshToken( |
237 const std::string& refresh_token) { | 237 const std::string& refresh_token) { |
238 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 238 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
239 | 239 |
240 // TODO(xiyuan): Use async GetSystemSalt after merging to M31. | |
241 const std::string system_salt = SystemSaltGetter::Get()->GetSystemSaltSync(); | |
242 if (system_salt.empty()) { | |
243 const int64 kRequestSystemSaltDelayMs = 500; | |
244 content::BrowserThread::PostDelayedTask( | |
245 content::BrowserThread::UI, | |
246 FROM_HERE, | |
247 base::Bind(&DeviceOAuth2TokenService::SetAndSaveRefreshToken, | |
248 weak_ptr_factory_.GetWeakPtr(), | |
249 refresh_token), | |
250 base::TimeDelta::FromMilliseconds(kRequestSystemSaltDelayMs)); | |
251 return; | |
252 } | |
253 | |
254 std::string encrypted_refresh_token = | 240 std::string encrypted_refresh_token = |
255 token_encryptor_->EncryptWithSystemSalt(refresh_token); | 241 token_encryptor_->EncryptWithSystemSalt(refresh_token); |
256 | 242 |
257 local_state_->SetString(prefs::kDeviceRobotAnyApiRefreshToken, | 243 local_state_->SetString(prefs::kDeviceRobotAnyApiRefreshToken, |
258 encrypted_refresh_token); | 244 encrypted_refresh_token); |
259 } | 245 } |
260 | 246 |
261 std::string DeviceOAuth2TokenService::GetRefreshToken( | 247 std::string DeviceOAuth2TokenService::GetRefreshToken( |
262 const std::string& account_id) { | 248 const std::string& account_id) { |
263 DCHECK_EQ(account_id, GetRobotAccountId()); | 249 DCHECK_EQ(account_id, GetRobotAccountId()); |
264 if (refresh_token_.empty()) { | 250 if (refresh_token_.empty()) { |
265 std::string encrypted_refresh_token = | 251 std::string encrypted_refresh_token = |
266 local_state_->GetString(prefs::kDeviceRobotAnyApiRefreshToken); | 252 local_state_->GetString(prefs::kDeviceRobotAnyApiRefreshToken); |
267 | 253 |
268 // TODO(xiyuan): This needs a proper fix after M31. | |
269 LOG_IF(ERROR, SystemSaltGetter::Get()->GetSystemSaltSync().empty()) | |
270 << "System salt is not available for decryption"; | |
271 | |
272 refresh_token_ = token_encryptor_->DecryptWithSystemSalt( | 254 refresh_token_ = token_encryptor_->DecryptWithSystemSalt( |
273 encrypted_refresh_token); | 255 encrypted_refresh_token); |
274 } | 256 } |
275 return refresh_token_; | 257 return refresh_token_; |
276 } | 258 } |
277 | 259 |
278 std::string DeviceOAuth2TokenService::GetRobotAccountId() { | 260 std::string DeviceOAuth2TokenService::GetRobotAccountId() { |
279 policy::BrowserPolicyConnector* connector = | 261 policy::BrowserPolicyConnector* connector = |
280 g_browser_process->browser_policy_connector(); | 262 g_browser_process->browser_policy_connector(); |
281 if (connector) | 263 if (connector) |
(...skipping 12 matching lines...) Expand all Loading... |
294 return OAuth2TokenService::CreateRequest(consumer); | 276 return OAuth2TokenService::CreateRequest(consumer); |
295 | 277 |
296 // Substitute our own consumer to wait for refresh token validation. | 278 // Substitute our own consumer to wait for refresh token validation. |
297 scoped_ptr<ValidatingConsumer> validating_consumer( | 279 scoped_ptr<ValidatingConsumer> validating_consumer( |
298 new ValidatingConsumer(this, consumer)); | 280 new ValidatingConsumer(this, consumer)); |
299 validating_consumer->StartValidation(); | 281 validating_consumer->StartValidation(); |
300 return validating_consumer.PassAs<RequestImpl>(); | 282 return validating_consumer.PassAs<RequestImpl>(); |
301 } | 283 } |
302 | 284 |
303 } // namespace chromeos | 285 } // namespace chromeos |
OLD | NEW |