OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/policy/enrollment_handler_chromeos.h" | 5 #include "chrome/browser/chromeos/policy/enrollment_handler_chromeos.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "chrome/browser/browser_process.h" | |
10 #include "chrome/browser/chromeos/policy/device_cloud_policy_store_chromeos.h" | 11 #include "chrome/browser/chromeos/policy/device_cloud_policy_store_chromeos.h" |
11 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h" | 12 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h" |
13 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" | |
14 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h " | |
12 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" | 15 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" |
13 #include "chrome/browser/policy/cloud/proto/device_management_backend.pb.h" | 16 #include "chrome/browser/policy/cloud/proto/device_management_backend.pb.h" |
17 #include "google_apis/gaia/gaia_urls.h" | |
14 | 18 |
15 namespace em = enterprise_management; | 19 namespace em = enterprise_management; |
16 | 20 |
17 namespace policy { | 21 namespace policy { |
18 | 22 |
19 namespace { | 23 namespace { |
20 | 24 |
21 // Retry for InstallAttrs initialization every 500ms. | 25 // Retry for InstallAttrs initialization every 500ms. |
22 const int kLockRetryIntervalMs = 500; | 26 const int kLockRetryIntervalMs = 500; |
23 // Maximum time to retry InstallAttrs initialization before we give up. | 27 // Maximum time to retry InstallAttrs initialization before we give up. |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
122 client_->FetchPolicy(); | 126 client_->FetchPolicy(); |
123 } else { | 127 } else { |
124 LOG(FATAL) << "Registration state changed to " << client_->is_registered() | 128 LOG(FATAL) << "Registration state changed to " << client_->is_registered() |
125 << " in step " << enrollment_step_; | 129 << " in step " << enrollment_step_; |
126 } | 130 } |
127 } | 131 } |
128 | 132 |
129 void EnrollmentHandlerChromeOS::OnClientError(CloudPolicyClient* client) { | 133 void EnrollmentHandlerChromeOS::OnClientError(CloudPolicyClient* client) { |
130 DCHECK_EQ(client_.get(), client); | 134 DCHECK_EQ(client_.get(), client); |
131 | 135 |
132 if (enrollment_step_ < STEP_POLICY_FETCH) | 136 if (enrollment_step_ == STEP_ROBOT_AUTH_FETCH) { |
137 LOG(WARNING) << "API authentication code fetch failed: " | |
138 << client_->status(); | |
139 // Robot auth tokens are currently optional. Skip fetching the refresh | |
140 // token and jump directly to the lock device step. | |
Mattias Nissler (ping if slow)
2013/04/24 13:43:13
same here.
David Roche
2013/04/24 15:34:21
I can't puzzle out what "same here" means in this
Mattias Nissler (ping if slow)
2013/04/24 17:58:52
I'm not sure either? Maybe this is an artifact of
| |
141 robot_refresh_token_.clear(); | |
142 WriteInstallAttributesAndLockDevice(); | |
143 } else if (enrollment_step_ < STEP_POLICY_FETCH) { | |
133 ReportResult(EnrollmentStatus::ForRegistrationError(client_->status())); | 144 ReportResult(EnrollmentStatus::ForRegistrationError(client_->status())); |
134 else | 145 } else { |
135 ReportResult(EnrollmentStatus::ForFetchError(client_->status())); | 146 ReportResult(EnrollmentStatus::ForFetchError(client_->status())); |
147 } | |
136 } | 148 } |
137 | 149 |
138 void EnrollmentHandlerChromeOS::OnStoreLoaded(CloudPolicyStore* store) { | 150 void EnrollmentHandlerChromeOS::OnStoreLoaded(CloudPolicyStore* store) { |
139 DCHECK_EQ(store_, store); | 151 DCHECK_EQ(store_, store); |
140 | 152 |
141 if (enrollment_step_ == STEP_LOADING_STORE) { | 153 if (enrollment_step_ == STEP_LOADING_STORE) { |
154 // If the |store_| wasn't initialized when StartEnrollment() was | |
155 // called, then AttemptRegistration() bails silently. This gets | |
156 // registration rolling again after the store finishes loading. | |
142 AttemptRegistration(); | 157 AttemptRegistration(); |
143 } else if (enrollment_step_ == STEP_STORE_POLICY) { | 158 } else if (enrollment_step_ == STEP_STORE_POLICY) { |
159 // Store the robot API auth refresh token. | |
160 // Currently optional, until DMServer support is fully in production. | |
161 chromeos::DeviceOAuth2TokenService* token_service = | |
162 chromeos::DeviceOAuth2TokenServiceFactory::Get(); | |
163 if (token_service && !robot_refresh_token_.empty()) { | |
164 token_service->SetAndSaveRefreshToken(robot_refresh_token_); | |
165 | |
166 } | |
144 ReportResult(EnrollmentStatus::ForStatus(EnrollmentStatus::STATUS_SUCCESS)); | 167 ReportResult(EnrollmentStatus::ForStatus(EnrollmentStatus::STATUS_SUCCESS)); |
145 } | 168 } |
146 } | 169 } |
147 | 170 |
148 void EnrollmentHandlerChromeOS::OnStoreError(CloudPolicyStore* store) { | 171 void EnrollmentHandlerChromeOS::OnStoreError(CloudPolicyStore* store) { |
149 DCHECK_EQ(store_, store); | 172 DCHECK_EQ(store_, store); |
150 ReportResult(EnrollmentStatus::ForStoreError(store_->status(), | 173 ReportResult(EnrollmentStatus::ForStoreError(store_->status(), |
151 store_->validation_status())); | 174 store_->validation_status())); |
152 } | 175 } |
153 | 176 |
154 void EnrollmentHandlerChromeOS::AttemptRegistration() { | 177 void EnrollmentHandlerChromeOS::AttemptRegistration() { |
155 CHECK_EQ(STEP_LOADING_STORE, enrollment_step_); | 178 CHECK_EQ(STEP_LOADING_STORE, enrollment_step_); |
156 if (store_->is_initialized()) { | 179 if (store_->is_initialized()) { |
157 enrollment_step_ = STEP_REGISTRATION; | 180 enrollment_step_ = STEP_REGISTRATION; |
158 client_->Register(em::DeviceRegisterRequest::DEVICE, | 181 client_->Register(em::DeviceRegisterRequest::DEVICE, |
159 auth_token_, client_id_, is_auto_enrollment_); | 182 auth_token_, client_id_, is_auto_enrollment_); |
160 } | 183 } |
161 } | 184 } |
162 | 185 |
163 void EnrollmentHandlerChromeOS::PolicyValidated( | 186 void EnrollmentHandlerChromeOS::PolicyValidated( |
164 DeviceCloudPolicyValidator* validator) { | 187 DeviceCloudPolicyValidator* validator) { |
165 CHECK_EQ(STEP_VALIDATION, enrollment_step_); | 188 CHECK_EQ(STEP_VALIDATION, enrollment_step_); |
166 if (validator->success()) { | 189 if (validator->success()) { |
167 policy_ = validator->policy().Pass(); | 190 policy_ = validator->policy().Pass(); |
168 enrollment_step_ = STEP_LOCK_DEVICE; | 191 username_ = validator->policy_data()->username(); |
169 WriteInstallAttributes(validator->policy_data()->username(), device_mode_, | 192 device_id_ = validator->policy_data()->device_id(); |
170 validator->policy_data()->device_id()); | 193 |
194 enrollment_step_ = STEP_ROBOT_AUTH_FETCH; | |
195 client_->FetchRobotAuthCodes(auth_token_); | |
171 } else { | 196 } else { |
172 ReportResult(EnrollmentStatus::ForValidationError(validator->status())); | 197 ReportResult(EnrollmentStatus::ForValidationError(validator->status())); |
173 } | 198 } |
174 } | 199 } |
175 | 200 |
201 void EnrollmentHandlerChromeOS::OnRobotAuthCodesFetched( | |
202 CloudPolicyClient* client) { | |
203 DCHECK_EQ(client_.get(), client); | |
204 CHECK_EQ(STEP_ROBOT_AUTH_FETCH, enrollment_step_); | |
205 | |
206 enrollment_step_ = STEP_ROBOT_AUTH_REFRESH; | |
207 | |
208 gaia::OAuthClientInfo client_info; | |
209 client_info.client_id = GaiaUrls::GetInstance()->oauth2_chrome_client_id(); | |
210 client_info.client_secret = | |
211 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(); | |
212 | |
213 // Use the system request context to avoid sending user cookies. | |
214 gaia_oauth_client_.reset(new gaia::GaiaOAuthClient( | |
215 gaia::kGaiaOAuth2Url, | |
216 g_browser_process->system_request_context())); | |
217 gaia_oauth_client_->GetTokensFromAuthCode(client_info, | |
218 client->robot_api_auth_code(), | |
219 0 /* max_retries */, | |
220 this); | |
221 } | |
222 | |
223 // GaiaOAuthClient::Delegate callback for OAuth2 refresh token fetched. | |
224 void EnrollmentHandlerChromeOS::OnGetTokensResponse( | |
225 const std::string& refresh_token, | |
226 const std::string& access_token, | |
227 int expires_in_seconds) { | |
228 CHECK_EQ(STEP_ROBOT_AUTH_REFRESH, enrollment_step_); | |
229 | |
230 robot_refresh_token_ = refresh_token; | |
231 | |
232 WriteInstallAttributesAndLockDevice(); | |
233 } | |
234 | |
235 void EnrollmentHandlerChromeOS::WriteInstallAttributesAndLockDevice() { | |
236 enrollment_step_ = STEP_LOCK_DEVICE, | |
237 WriteInstallAttributes(username_, device_mode_, device_id_); | |
238 } | |
239 | |
240 // GaiaOAuthClient::Delegate | |
241 void EnrollmentHandlerChromeOS::OnRefreshTokenResponse( | |
242 const std::string& access_token, | |
243 int expires_in_seconds) { | |
244 // We never use the code that should trigger this callback. | |
245 LOG(FATAL) << "Unexpected callback invoked"; | |
246 } | |
247 | |
248 // GaiaOAuthClient::Delegate OAuth2 error when fetching refresh token request. | |
249 void EnrollmentHandlerChromeOS::OnOAuthError() { | |
250 // TODO(davidroche): return STATUS_ROBOT_REFRESH_FETCH_FAILED when fetching | |
251 // tokens is required for enrollment. | |
252 CHECK_EQ(STEP_ROBOT_AUTH_REFRESH, enrollment_step_); | |
253 WriteInstallAttributesAndLockDevice(); | |
254 } | |
255 | |
256 // GaiaOAuthClient::Delegate network error when fetching refresh token. | |
257 void EnrollmentHandlerChromeOS::OnNetworkError(int response_code) { | |
258 LOG(ERROR) << "Network error while fetching API refresh token: " | |
259 << response_code; | |
260 // TODO(davidroche): return STATUS_ROBOT_REFRESH_FETCH_FAILED when fetching | |
261 // tokens is required for enrollment. | |
262 CHECK_EQ(STEP_ROBOT_AUTH_REFRESH, enrollment_step_); | |
263 WriteInstallAttributesAndLockDevice(); | |
264 } | |
265 | |
176 void EnrollmentHandlerChromeOS::WriteInstallAttributes( | 266 void EnrollmentHandlerChromeOS::WriteInstallAttributes( |
177 const std::string& user, | 267 const std::string& user, |
178 DeviceMode device_mode, | 268 DeviceMode device_mode, |
179 const std::string& device_id) { | 269 const std::string& device_id) { |
180 CHECK_EQ(STEP_LOCK_DEVICE, enrollment_step_); | 270 CHECK_EQ(STEP_LOCK_DEVICE, enrollment_step_); |
181 // Since this method is also called directly. | 271 // Since this method is also called directly. |
182 weak_factory_.InvalidateWeakPtrs(); | 272 weak_factory_.InvalidateWeakPtrs(); |
183 | 273 |
184 install_attributes_->LockDevice( | 274 install_attributes_->LockDevice( |
185 user, device_mode, device_id, | 275 user, device_mode, device_id, |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
253 << " " << status.client_status() | 343 << " " << status.client_status() |
254 << " " << status.validation_status() | 344 << " " << status.validation_status() |
255 << " " << status.store_status(); | 345 << " " << status.store_status(); |
256 } | 346 } |
257 | 347 |
258 if (!callback.is_null()) | 348 if (!callback.is_null()) |
259 callback.Run(status); | 349 callback.Run(status); |
260 } | 350 } |
261 | 351 |
262 } // namespace policy | 352 } // namespace policy |
OLD | NEW |