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 ReportResult(EnrollmentStatus::ForRobotAuthError(client_->status())); | |
138 else if (enrollment_step_ < STEP_POLICY_FETCH) | |
133 ReportResult(EnrollmentStatus::ForRegistrationError(client_->status())); | 139 ReportResult(EnrollmentStatus::ForRegistrationError(client_->status())); |
134 else | 140 else |
135 ReportResult(EnrollmentStatus::ForFetchError(client_->status())); | 141 ReportResult(EnrollmentStatus::ForFetchError(client_->status())); |
136 } | 142 } |
137 | 143 |
138 void EnrollmentHandlerChromeOS::OnStoreLoaded(CloudPolicyStore* store) { | 144 void EnrollmentHandlerChromeOS::OnStoreLoaded(CloudPolicyStore* store) { |
139 DCHECK_EQ(store_, store); | 145 DCHECK_EQ(store_, store); |
140 | 146 |
141 if (enrollment_step_ == STEP_LOADING_STORE) { | 147 if (enrollment_step_ == STEP_LOADING_STORE) { |
148 // If the |store_| wasn't initialized when StartEnrollment() was | |
149 // called, then AttemptRegistration() bails silently. This gets | |
150 // registration rolling again after the store finishes loading. | |
142 AttemptRegistration(); | 151 AttemptRegistration(); |
143 } else if (enrollment_step_ == STEP_STORE_POLICY) { | 152 } else if (enrollment_step_ == STEP_STORE_POLICY) { |
144 ReportResult(EnrollmentStatus::ForStatus(EnrollmentStatus::STATUS_SUCCESS)); | 153 enrollment_step_ = STEP_STORE_ROBOT_AUTH; |
Mattias Nissler (ping if slow)
2013/04/22 10:59:00
Is this used anywhere? If not, just remove that st
David Roche
2013/04/23 01:36:08
Done.
| |
154 | |
155 chromeos::DeviceOAuth2TokenService* token_service = | |
156 chromeos::DeviceOAuth2TokenServiceFactory::Get(); | |
157 if (token_service) { | |
158 token_service->SetAndSaveRefreshToken(robot_refresh_token_); | |
159 | |
160 ReportResult(EnrollmentStatus::ForStatus( | |
161 EnrollmentStatus::STATUS_SUCCESS)); | |
162 } else { | |
163 ReportResult(EnrollmentStatus::ForStatus( | |
164 EnrollmentStatus::STATUS_ROBOT_REFRESH_FETCH_FAILED)); | |
165 } | |
145 } | 166 } |
146 } | 167 } |
147 | 168 |
148 void EnrollmentHandlerChromeOS::OnStoreError(CloudPolicyStore* store) { | 169 void EnrollmentHandlerChromeOS::OnStoreError(CloudPolicyStore* store) { |
149 DCHECK_EQ(store_, store); | 170 DCHECK_EQ(store_, store); |
150 ReportResult(EnrollmentStatus::ForStoreError(store_->status(), | 171 ReportResult(EnrollmentStatus::ForStoreError(store_->status(), |
151 store_->validation_status())); | 172 store_->validation_status())); |
152 } | 173 } |
153 | 174 |
154 void EnrollmentHandlerChromeOS::AttemptRegistration() { | 175 void EnrollmentHandlerChromeOS::AttemptRegistration() { |
155 CHECK_EQ(STEP_LOADING_STORE, enrollment_step_); | 176 CHECK_EQ(STEP_LOADING_STORE, enrollment_step_); |
156 if (store_->is_initialized()) { | 177 if (store_->is_initialized()) { |
157 enrollment_step_ = STEP_REGISTRATION; | 178 enrollment_step_ = STEP_REGISTRATION; |
158 client_->Register(em::DeviceRegisterRequest::DEVICE, | 179 client_->Register(em::DeviceRegisterRequest::DEVICE, |
159 auth_token_, client_id_, is_auto_enrollment_); | 180 auth_token_, client_id_, is_auto_enrollment_); |
160 } | 181 } |
161 } | 182 } |
162 | 183 |
163 void EnrollmentHandlerChromeOS::PolicyValidated( | 184 void EnrollmentHandlerChromeOS::PolicyValidated( |
164 DeviceCloudPolicyValidator* validator) { | 185 DeviceCloudPolicyValidator* validator) { |
165 CHECK_EQ(STEP_VALIDATION, enrollment_step_); | 186 CHECK_EQ(STEP_VALIDATION, enrollment_step_); |
166 if (validator->success()) { | 187 if (validator->success()) { |
167 policy_ = validator->policy().Pass(); | 188 policy_ = validator->policy().Pass(); |
168 enrollment_step_ = STEP_LOCK_DEVICE; | 189 username_ = validator->policy_data()->username(); |
169 WriteInstallAttributes(validator->policy_data()->username(), device_mode_, | 190 device_id_ = validator->policy_data()->device_id(); |
170 validator->policy_data()->device_id()); | 191 |
192 enrollment_step_ = STEP_ROBOT_AUTH_FETCH; | |
193 client_->FetchRobotAuthTokens(auth_token_); | |
171 } else { | 194 } else { |
172 ReportResult(EnrollmentStatus::ForValidationError(validator->status())); | 195 ReportResult(EnrollmentStatus::ForValidationError(validator->status())); |
173 } | 196 } |
174 } | 197 } |
175 | 198 |
199 void EnrollmentHandlerChromeOS::OnRobotAuthCodesFetched( | |
200 CloudPolicyClient* client) { | |
201 DCHECK_EQ(client_.get(), client); | |
202 CHECK_EQ(STEP_ROBOT_AUTH_FETCH, enrollment_step_); | |
203 | |
204 enrollment_step_ = STEP_ROBOT_AUTH_REFRESH; | |
205 | |
206 gaia::OAuthClientInfo client_info; | |
207 client_info.client_id = GaiaUrls::GetInstance()->oauth2_chrome_client_id(); | |
208 client_info.client_secret = | |
209 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(); | |
210 | |
211 // Use the system request context to avoid sending user cookies. | |
212 gaia_oauth_client_.reset(new gaia::GaiaOAuthClient( | |
213 gaia::kGaiaOAuth2Url, | |
214 g_browser_process->system_request_context())); | |
215 gaia_oauth_client_->GetTokensFromAuthCode(client_info, | |
216 client->robot_api_auth_code(), | |
217 0 /* max_retries */, | |
218 this); | |
219 } | |
220 | |
221 // GaiaOAuthClient::Delegate callback for OAuth2 refresh token fetched. | |
222 void EnrollmentHandlerChromeOS::OnGetTokensResponse( | |
223 const std::string& refresh_token, | |
224 const std::string& access_token, | |
225 int expires_in_seconds) { | |
226 CHECK_EQ(STEP_ROBOT_AUTH_REFRESH, enrollment_step_); | |
227 | |
228 robot_refresh_token_ = refresh_token; | |
229 | |
230 enrollment_step_ = STEP_LOCK_DEVICE, | |
231 WriteInstallAttributes(username_, device_mode_, device_id_); | |
232 } | |
233 | |
234 // GaiaOAuthClient::Delegate | |
235 void EnrollmentHandlerChromeOS::OnRefreshTokenResponse( | |
236 const std::string& access_token, | |
237 int expires_in_seconds) { | |
238 // We never use the code that should trigger this callback. | |
239 LOG(FATAL) << "Unexpected callback invoked"; | |
240 } | |
241 | |
242 // GaiaOAuthClient::Delegate OAuth2 error when fetching refresh token request. | |
243 void EnrollmentHandlerChromeOS::OnOAuthError() { | |
244 ReportResult(EnrollmentStatus::ForStatus( | |
245 EnrollmentStatus::STATUS_ROBOT_REFRESH_FETCH_FAILED)); | |
246 } | |
247 | |
248 // GaiaOAuthClient::Delegate network error when fetching refresh token. | |
249 void EnrollmentHandlerChromeOS::OnNetworkError(int response_code) { | |
250 LOG(ERROR) << "Network error while fetching API refresh token: " | |
251 << response_code; | |
252 ReportResult(EnrollmentStatus::ForStatus( | |
253 EnrollmentStatus::STATUS_ROBOT_REFRESH_FETCH_FAILED)); | |
254 } | |
255 | |
176 void EnrollmentHandlerChromeOS::WriteInstallAttributes( | 256 void EnrollmentHandlerChromeOS::WriteInstallAttributes( |
177 const std::string& user, | 257 const std::string& user, |
178 DeviceMode device_mode, | 258 DeviceMode device_mode, |
179 const std::string& device_id) { | 259 const std::string& device_id) { |
180 CHECK_EQ(STEP_LOCK_DEVICE, enrollment_step_); | 260 CHECK_EQ(STEP_LOCK_DEVICE, enrollment_step_); |
181 // Since this method is also called directly. | 261 // Since this method is also called directly. |
182 weak_factory_.InvalidateWeakPtrs(); | 262 weak_factory_.InvalidateWeakPtrs(); |
183 | 263 |
184 install_attributes_->LockDevice( | 264 install_attributes_->LockDevice( |
185 user, device_mode, device_id, | 265 user, device_mode, device_id, |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
253 << " " << status.client_status() | 333 << " " << status.client_status() |
254 << " " << status.validation_status() | 334 << " " << status.validation_status() |
255 << " " << status.store_status(); | 335 << " " << status.store_status(); |
256 } | 336 } |
257 | 337 |
258 if (!callback.is_null()) | 338 if (!callback.is_null()) |
259 callback.Run(status); | 339 callback.Run(status); |
260 } | 340 } |
261 | 341 |
262 } // namespace policy | 342 } // namespace policy |
OLD | NEW |