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" |
11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h" | 14 #include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h" |
15 #include "chrome/browser/policy/browser_policy_connector.h" | 15 #include "chrome/browser/policy/browser_policy_connector.h" |
16 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" | 16 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" |
17 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
18 #include "chromeos/cryptohome/cryptohome_library.h" | 18 #include "chromeos/cryptohome/cryptohome_library.h" |
19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
20 #include "google_apis/gaia/gaia_urls.h" | 20 #include "google_apis/gaia/gaia_urls.h" |
21 #include "google_apis/gaia/google_service_auth_error.h" | 21 #include "google_apis/gaia/google_service_auth_error.h" |
22 | 22 |
23 namespace { | 23 namespace { |
24 const char kServiceScopeGetUserInfo[] = | 24 const char kServiceScopeGetUserInfo[] = |
25 "https://www.googleapis.com/auth/userinfo.email"; | 25 "https://www.googleapis.com/auth/userinfo.email"; |
26 } // namespace | 26 } |
27 | 27 |
28 namespace chromeos { | 28 namespace chromeos { |
29 | 29 |
30 // A wrapper for the consumer passed to StartRequest, which doesn't call | 30 // A wrapper for the consumer passed to StartRequest, which doesn't call |
31 // through to the target Consumer unless the refresh token validation is | 31 // through to the target Consumer unless the refresh token validation is |
32 // complete. Additionally implements the Request interface, so that it | 32 // complete. Additionally implements the Request interface, so that it |
33 // can be passed back to the caller and directly deleted when cancelling | 33 // can be passed back to the caller and directly deleted when cancelling |
34 // the request. | 34 // the request. |
35 class DeviceOAuth2TokenService::ValidatingConsumer | 35 class DeviceOAuth2TokenService::ValidatingConsumer |
36 : public OAuth2TokenService::Consumer, | 36 : public OAuth2TokenService::Consumer, |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 gaia_oauth_client_.reset(new gaia::GaiaOAuthClient( | 108 gaia_oauth_client_.reset(new gaia::GaiaOAuthClient( |
109 g_browser_process->system_request_context())); | 109 g_browser_process->system_request_context())); |
110 | 110 |
111 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); | 111 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); |
112 gaia::OAuthClientInfo client_info; | 112 gaia::OAuthClientInfo client_info; |
113 client_info.client_id = gaia_urls->oauth2_chrome_client_id(); | 113 client_info.client_id = gaia_urls->oauth2_chrome_client_id(); |
114 client_info.client_secret = gaia_urls->oauth2_chrome_client_secret(); | 114 client_info.client_secret = gaia_urls->oauth2_chrome_client_secret(); |
115 | 115 |
116 gaia_oauth_client_->RefreshToken( | 116 gaia_oauth_client_->RefreshToken( |
117 client_info, | 117 client_info, |
118 token_service_->GetRefreshToken(), | 118 token_service_->GetRefreshToken(token_service_->GetRobotAccountId()), |
119 std::vector<std::string>(1, kServiceScopeGetUserInfo), | 119 std::vector<std::string>(1, kServiceScopeGetUserInfo), |
120 token_service_->max_refresh_token_validation_retries_, | 120 token_service_->max_refresh_token_validation_retries_, |
121 this); | 121 this); |
122 } | 122 } |
123 | 123 |
124 void DeviceOAuth2TokenService::ValidatingConsumer::OnRefreshTokenResponse( | 124 void DeviceOAuth2TokenService::ValidatingConsumer::OnRefreshTokenResponse( |
125 const std::string& access_token, | 125 const std::string& access_token, |
126 int expires_in_seconds) { | 126 int expires_in_seconds) { |
127 gaia_oauth_client_->GetTokenInfo( | 127 gaia_oauth_client_->GetTokenInfo( |
128 access_token, | 128 access_token, |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 : refresh_token_is_valid_(false), | 214 : refresh_token_is_valid_(false), |
215 max_refresh_token_validation_retries_(3), | 215 max_refresh_token_validation_retries_(3), |
216 url_request_context_getter_(getter), | 216 url_request_context_getter_(getter), |
217 local_state_(local_state) { | 217 local_state_(local_state) { |
218 } | 218 } |
219 | 219 |
220 DeviceOAuth2TokenService::~DeviceOAuth2TokenService() { | 220 DeviceOAuth2TokenService::~DeviceOAuth2TokenService() { |
221 } | 221 } |
222 | 222 |
223 scoped_ptr<OAuth2TokenService::Request> DeviceOAuth2TokenService::StartRequest( | 223 scoped_ptr<OAuth2TokenService::Request> DeviceOAuth2TokenService::StartRequest( |
| 224 const std::string& account_id, |
224 const OAuth2TokenService::ScopeSet& scopes, | 225 const OAuth2TokenService::ScopeSet& scopes, |
225 OAuth2TokenService::Consumer* consumer) { | 226 OAuth2TokenService::Consumer* consumer) { |
226 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 227 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 228 DCHECK_EQ(account_id, GetRobotAccountId()); |
227 | 229 |
228 if (refresh_token_is_valid_) { | 230 if (refresh_token_is_valid_) { |
229 return OAuth2TokenService::StartRequest(scopes, consumer).Pass(); | 231 return OAuth2TokenService::StartRequest( |
| 232 account_id, scopes, consumer).Pass(); |
230 } else { | 233 } else { |
231 scoped_ptr<ValidatingConsumer> validating_consumer( | 234 scoped_ptr<ValidatingConsumer> validating_consumer( |
232 new ValidatingConsumer(this, consumer)); | 235 new ValidatingConsumer(this, consumer)); |
233 | 236 |
234 scoped_ptr<Request> request = OAuth2TokenService::StartRequest( | 237 scoped_ptr<Request> request = OAuth2TokenService::StartRequest( |
235 scopes, validating_consumer.get()); | 238 account_id, scopes, validating_consumer.get()); |
236 validating_consumer->StartValidation(request.Pass()); | 239 validating_consumer->StartValidation(request.Pass()); |
237 return validating_consumer.PassAs<Request>(); | 240 return validating_consumer.PassAs<Request>(); |
238 } | 241 } |
239 } | 242 } |
240 | 243 |
241 void DeviceOAuth2TokenService::OnValidationComplete( | 244 void DeviceOAuth2TokenService::OnValidationComplete( |
242 bool refresh_token_is_valid) { | 245 bool refresh_token_is_valid) { |
243 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 246 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
244 refresh_token_is_valid_ = refresh_token_is_valid; | 247 refresh_token_is_valid_ = refresh_token_is_valid; |
245 } | 248 } |
246 | 249 |
247 // static | 250 // static |
248 void DeviceOAuth2TokenService::RegisterPrefs(PrefRegistrySimple* registry) { | 251 void DeviceOAuth2TokenService::RegisterPrefs(PrefRegistrySimple* registry) { |
249 registry->RegisterStringPref(prefs::kDeviceRobotAnyApiRefreshToken, | 252 registry->RegisterStringPref(prefs::kDeviceRobotAnyApiRefreshToken, |
250 std::string()); | 253 std::string()); |
251 } | 254 } |
252 | 255 |
253 void DeviceOAuth2TokenService::SetAndSaveRefreshToken( | 256 void DeviceOAuth2TokenService::SetAndSaveRefreshToken( |
254 const std::string& refresh_token) { | 257 const std::string& refresh_token) { |
255 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 258 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
256 std::string encrypted_refresh_token = | 259 std::string encrypted_refresh_token = |
257 CryptohomeLibrary::Get()->EncryptWithSystemSalt(refresh_token); | 260 CryptohomeLibrary::Get()->EncryptWithSystemSalt(refresh_token); |
258 | 261 |
259 local_state_->SetString(prefs::kDeviceRobotAnyApiRefreshToken, | 262 local_state_->SetString(prefs::kDeviceRobotAnyApiRefreshToken, |
260 encrypted_refresh_token); | 263 encrypted_refresh_token); |
261 } | 264 } |
262 | 265 |
263 std::string DeviceOAuth2TokenService::GetRefreshToken() { | 266 std::string DeviceOAuth2TokenService::GetRefreshToken( |
| 267 const std::string& account_id) { |
| 268 DCHECK_EQ(account_id, GetRobotAccountId()); |
264 if (refresh_token_.empty()) { | 269 if (refresh_token_.empty()) { |
265 std::string encrypted_refresh_token = | 270 std::string encrypted_refresh_token = |
266 local_state_->GetString(prefs::kDeviceRobotAnyApiRefreshToken); | 271 local_state_->GetString(prefs::kDeviceRobotAnyApiRefreshToken); |
267 | 272 |
268 refresh_token_ = CryptohomeLibrary::Get()->DecryptWithSystemSalt( | 273 refresh_token_ = CryptohomeLibrary::Get()->DecryptWithSystemSalt( |
269 encrypted_refresh_token); | 274 encrypted_refresh_token); |
270 } | 275 } |
271 return refresh_token_; | 276 return refresh_token_; |
272 } | 277 } |
273 | 278 |
274 std::string DeviceOAuth2TokenService::GetRobotAccountId() { | 279 std::string DeviceOAuth2TokenService::GetRobotAccountId() { |
275 policy::BrowserPolicyConnector* connector = | 280 policy::BrowserPolicyConnector* connector = |
276 g_browser_process->browser_policy_connector(); | 281 g_browser_process->browser_policy_connector(); |
277 if (connector) | 282 if (connector) |
278 return connector->GetDeviceCloudPolicyManager()->GetRobotAccountId(); | 283 return connector->GetDeviceCloudPolicyManager()->GetRobotAccountId(); |
279 return std::string(); | 284 return std::string(); |
280 } | 285 } |
281 | 286 |
282 net::URLRequestContextGetter* DeviceOAuth2TokenService::GetRequestContext() { | 287 net::URLRequestContextGetter* DeviceOAuth2TokenService::GetRequestContext() { |
283 return url_request_context_getter_.get(); | 288 return url_request_context_getter_.get(); |
284 } | 289 } |
285 | 290 |
286 } // namespace chromeos | 291 } // namespace chromeos |
OLD | NEW |