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/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" |
11 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
13 #include "base/prefs/pref_registry_simple.h" | 14 #include "base/prefs/pref_registry_simple.h" |
14 #include "base/prefs/pref_service.h" | 15 #include "base/prefs/pref_service.h" |
15 #include "base/values.h" | 16 #include "base/values.h" |
16 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
17 #include "chrome/browser/chromeos/settings/cros_settings.h" | |
18 #include "chrome/browser/chromeos/settings/token_encryptor.h" | 18 #include "chrome/browser/chromeos/settings/token_encryptor.h" |
19 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
20 #include "chromeos/cryptohome/system_salt_getter.h" | 20 #include "chromeos/cryptohome/system_salt_getter.h" |
| 21 #include "chromeos/settings/cros_settings_names.h" |
21 #include "google_apis/gaia/gaia_constants.h" | 22 #include "google_apis/gaia/gaia_constants.h" |
22 #include "google_apis/gaia/gaia_urls.h" | 23 #include "google_apis/gaia/gaia_urls.h" |
23 #include "google_apis/gaia/google_service_auth_error.h" | 24 #include "google_apis/gaia/google_service_auth_error.h" |
24 #include "google_apis/gaia/oauth2_access_token_fetcher_impl.h" | 25 #include "google_apis/gaia/oauth2_access_token_fetcher_impl.h" |
25 #include "policy/proto/device_management_backend.pb.h" | 26 #include "policy/proto/device_management_backend.pb.h" |
26 | 27 |
27 namespace chromeos { | 28 namespace chromeos { |
28 | 29 |
29 struct DeviceOAuth2TokenService::PendingRequest { | 30 struct DeviceOAuth2TokenService::PendingRequest { |
30 PendingRequest(const base::WeakPtr<RequestImpl>& request, | 31 PendingRequest(const base::WeakPtr<RequestImpl>& request, |
31 const std::string& client_id, | 32 const std::string& client_id, |
32 const std::string& client_secret, | 33 const std::string& client_secret, |
33 const ScopeSet& scopes) | 34 const ScopeSet& scopes) |
34 : request(request), | 35 : request(request), |
35 client_id(client_id), | 36 client_id(client_id), |
36 client_secret(client_secret), | 37 client_secret(client_secret), |
37 scopes(scopes) {} | 38 scopes(scopes) {} |
38 | 39 |
39 const base::WeakPtr<RequestImpl> request; | 40 const base::WeakPtr<RequestImpl> request; |
40 const std::string client_id; | 41 const std::string client_id; |
41 const std::string client_secret; | 42 const std::string client_secret; |
42 const ScopeSet scopes; | 43 const ScopeSet scopes; |
43 }; | 44 }; |
44 | 45 |
| 46 void DeviceOAuth2TokenService::OnServiceAccountIdentityChanged() { |
| 47 if (!GetRobotAccountId().empty() && !refresh_token_.empty()) |
| 48 FireRefreshTokenAvailable(GetRobotAccountId()); |
| 49 } |
| 50 |
45 DeviceOAuth2TokenService::DeviceOAuth2TokenService( | 51 DeviceOAuth2TokenService::DeviceOAuth2TokenService( |
46 net::URLRequestContextGetter* getter, | 52 net::URLRequestContextGetter* getter, |
47 PrefService* local_state) | 53 PrefService* local_state) |
48 : url_request_context_getter_(getter), | 54 : url_request_context_getter_(getter), |
49 local_state_(local_state), | 55 local_state_(local_state), |
50 state_(STATE_LOADING), | 56 state_(STATE_LOADING), |
51 max_refresh_token_validation_retries_(3), | 57 max_refresh_token_validation_retries_(3), |
| 58 service_account_identity_subscription_( |
| 59 CrosSettings::Get()->AddSettingsObserver( |
| 60 kServiceAccountIdentity, |
| 61 base::Bind( |
| 62 &DeviceOAuth2TokenService::OnServiceAccountIdentityChanged, |
| 63 base::Unretained(this))).Pass()), |
52 weak_ptr_factory_(this) { | 64 weak_ptr_factory_(this) { |
53 // Pull in the system salt. | 65 // Pull in the system salt. |
54 SystemSaltGetter::Get()->GetSystemSalt( | 66 SystemSaltGetter::Get()->GetSystemSalt( |
55 base::Bind(&DeviceOAuth2TokenService::DidGetSystemSalt, | 67 base::Bind(&DeviceOAuth2TokenService::DidGetSystemSalt, |
56 weak_ptr_factory_.GetWeakPtr())); | 68 weak_ptr_factory_.GetWeakPtr())); |
57 } | 69 } |
58 | 70 |
59 DeviceOAuth2TokenService::~DeviceOAuth2TokenService() { | 71 DeviceOAuth2TokenService::~DeviceOAuth2TokenService() { |
60 FlushPendingRequests(false, GoogleServiceAuthError::REQUEST_CANCELED); | 72 FlushPendingRequests(false, GoogleServiceAuthError::REQUEST_CANCELED); |
61 FlushTokenSaveCallbacks(false); | 73 FlushTokenSaveCallbacks(false); |
62 } | 74 } |
63 | 75 |
64 // static | 76 // static |
65 void DeviceOAuth2TokenService::RegisterPrefs(PrefRegistrySimple* registry) { | 77 void DeviceOAuth2TokenService::RegisterPrefs(PrefRegistrySimple* registry) { |
66 registry->RegisterStringPref(prefs::kDeviceRobotAnyApiRefreshToken, | 78 registry->RegisterStringPref(prefs::kDeviceRobotAnyApiRefreshToken, |
67 std::string()); | 79 std::string()); |
68 } | 80 } |
69 | 81 |
70 void DeviceOAuth2TokenService::SetAndSaveRefreshToken( | 82 void DeviceOAuth2TokenService::SetAndSaveRefreshToken( |
71 const std::string& refresh_token, | 83 const std::string& refresh_token, |
72 const StatusCallback& result_callback) { | 84 const StatusCallback& result_callback) { |
73 FlushPendingRequests(false, GoogleServiceAuthError::REQUEST_CANCELED); | 85 FlushPendingRequests(false, GoogleServiceAuthError::REQUEST_CANCELED); |
74 | 86 |
75 bool waiting_for_salt = state_ == STATE_LOADING; | 87 bool waiting_for_salt = state_ == STATE_LOADING; |
76 refresh_token_ = refresh_token; | 88 refresh_token_ = refresh_token; |
77 state_ = STATE_VALIDATION_PENDING; | 89 state_ = STATE_VALIDATION_PENDING; |
78 FireRefreshTokenAvailable(GetRobotAccountId()); | 90 |
| 91 // If the robot account ID is not available yet, do not announce the token. It |
| 92 // will be done from OnServiceAccountIdentityChanged() once the robot account |
| 93 // ID becomes available as well. |
| 94 if (!GetRobotAccountId().empty()) |
| 95 FireRefreshTokenAvailable(GetRobotAccountId()); |
79 | 96 |
80 token_save_callbacks_.push_back(result_callback); | 97 token_save_callbacks_.push_back(result_callback); |
81 if (!waiting_for_salt) { | 98 if (!waiting_for_salt) { |
82 if (system_salt_.empty()) | 99 if (system_salt_.empty()) |
83 FlushTokenSaveCallbacks(false); | 100 FlushTokenSaveCallbacks(false); |
84 else | 101 else |
85 EncryptAndSaveToken(); | 102 EncryptAndSaveToken(); |
86 } | 103 } |
87 } | 104 } |
88 | 105 |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 | 271 |
255 // Announce the token. | 272 // Announce the token. |
256 FireRefreshTokenAvailable(GetRobotAccountId()); | 273 FireRefreshTokenAvailable(GetRobotAccountId()); |
257 FireRefreshTokensLoaded(); | 274 FireRefreshTokensLoaded(); |
258 } | 275 } |
259 | 276 |
260 void DeviceOAuth2TokenService::CheckRobotAccountId( | 277 void DeviceOAuth2TokenService::CheckRobotAccountId( |
261 const std::string& gaia_robot_id) { | 278 const std::string& gaia_robot_id) { |
262 // Make sure the value returned by GetRobotAccountId has been validated | 279 // Make sure the value returned by GetRobotAccountId has been validated |
263 // against current device settings. | 280 // against current device settings. |
264 switch (CrosSettings::Get()->PrepareTrustedValues( | 281 switch (CrosSettings::Get()->PrepareTrustedValues(base::Bind( |
265 base::Bind(&DeviceOAuth2TokenService::CheckRobotAccountId, | 282 &DeviceOAuth2TokenService::CheckRobotAccountId, |
266 weak_ptr_factory_.GetWeakPtr(), | 283 weak_ptr_factory_.GetWeakPtr(), |
267 gaia_robot_id))) { | 284 gaia_robot_id))) { |
268 case CrosSettingsProvider::TRUSTED: | 285 case CrosSettingsProvider::TRUSTED: |
269 // All good, compare account ids below. | 286 // All good, compare account ids below. |
270 break; | 287 break; |
271 case CrosSettingsProvider::TEMPORARILY_UNTRUSTED: | 288 case CrosSettingsProvider::TEMPORARILY_UNTRUSTED: |
272 // The callback passed to PrepareTrustedValues above will trigger a | 289 // The callback passed to PrepareTrustedValues above will trigger a |
273 // re-check eventually. | 290 // re-check eventually. |
274 return; | 291 return; |
275 case CrosSettingsProvider::PERMANENTLY_UNTRUSTED: | 292 case CrosSettingsProvider::PERMANENTLY_UNTRUSTED: |
276 // There's no trusted account id, which is equivalent to no token present. | 293 // There's no trusted account id, which is equivalent to no token present. |
277 LOG(WARNING) << "Device settings permanently untrusted."; | 294 LOG(WARNING) << "Device settings permanently untrusted."; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 GoogleServiceAuthError auth_error(error); | 397 GoogleServiceAuthError auth_error(error); |
381 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 398 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
382 &RequestImpl::InformConsumer, | 399 &RequestImpl::InformConsumer, |
383 request->AsWeakPtr(), | 400 request->AsWeakPtr(), |
384 auth_error, | 401 auth_error, |
385 std::string(), | 402 std::string(), |
386 base::Time())); | 403 base::Time())); |
387 } | 404 } |
388 | 405 |
389 } // namespace chromeos | 406 } // namespace chromeos |
OLD | NEW |