Index: chrome/browser/chromeos/policy/enrollment_handler_chromeos.cc |
diff --git a/chrome/browser/chromeos/policy/enrollment_handler_chromeos.cc b/chrome/browser/chromeos/policy/enrollment_handler_chromeos.cc |
index 7c5692d26925fbf182abb8094c7de006275cc0d4..2c5b618b2f8ffe64ff56191619739f9d5ead4530 100644 |
--- a/chrome/browser/chromeos/policy/enrollment_handler_chromeos.cc |
+++ b/chrome/browser/chromeos/policy/enrollment_handler_chromeos.cc |
@@ -7,10 +7,14 @@ |
#include "base/bind.h" |
#include "base/logging.h" |
#include "base/message_loop.h" |
+#include "chrome/browser/browser_process.h" |
#include "chrome/browser/chromeos/policy/device_cloud_policy_store_chromeos.h" |
#include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h" |
+#include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" |
+#include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h" |
#include "chrome/browser/policy/cloud/cloud_policy_constants.h" |
#include "chrome/browser/policy/cloud/proto/device_management_backend.pb.h" |
+#include "google_apis/gaia/gaia_urls.h" |
namespace em = enterprise_management; |
@@ -129,7 +133,9 @@ void EnrollmentHandlerChromeOS::OnRegistrationStateChanged( |
void EnrollmentHandlerChromeOS::OnClientError(CloudPolicyClient* client) { |
DCHECK_EQ(client_.get(), client); |
- if (enrollment_step_ < STEP_POLICY_FETCH) |
+ if (enrollment_step_ == STEP_ROBOT_AUTH_FETCH) |
+ ReportResult(EnrollmentStatus::ForRobotAuthError(client_->status())); |
+ else if (enrollment_step_ < STEP_POLICY_FETCH) |
ReportResult(EnrollmentStatus::ForRegistrationError(client_->status())); |
else |
ReportResult(EnrollmentStatus::ForFetchError(client_->status())); |
@@ -139,9 +145,24 @@ void EnrollmentHandlerChromeOS::OnStoreLoaded(CloudPolicyStore* store) { |
DCHECK_EQ(store_, store); |
if (enrollment_step_ == STEP_LOADING_STORE) { |
+ // If the |store_| wasn't initialized when StartEnrollment() was |
+ // called, then AttemptRegistration() bails silently. This gets |
+ // registration rolling again after the store finishes loading. |
AttemptRegistration(); |
} else if (enrollment_step_ == STEP_STORE_POLICY) { |
- ReportResult(EnrollmentStatus::ForStatus(EnrollmentStatus::STATUS_SUCCESS)); |
+ 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.
|
+ |
+ chromeos::DeviceOAuth2TokenService* token_service = |
+ chromeos::DeviceOAuth2TokenServiceFactory::Get(); |
+ if (token_service) { |
+ token_service->SetAndSaveRefreshToken(robot_refresh_token_); |
+ |
+ ReportResult(EnrollmentStatus::ForStatus( |
+ EnrollmentStatus::STATUS_SUCCESS)); |
+ } else { |
+ ReportResult(EnrollmentStatus::ForStatus( |
+ EnrollmentStatus::STATUS_ROBOT_REFRESH_FETCH_FAILED)); |
+ } |
} |
} |
@@ -165,14 +186,73 @@ void EnrollmentHandlerChromeOS::PolicyValidated( |
CHECK_EQ(STEP_VALIDATION, enrollment_step_); |
if (validator->success()) { |
policy_ = validator->policy().Pass(); |
- enrollment_step_ = STEP_LOCK_DEVICE; |
- WriteInstallAttributes(validator->policy_data()->username(), device_mode_, |
- validator->policy_data()->device_id()); |
+ username_ = validator->policy_data()->username(); |
+ device_id_ = validator->policy_data()->device_id(); |
+ |
+ enrollment_step_ = STEP_ROBOT_AUTH_FETCH; |
+ client_->FetchRobotAuthTokens(auth_token_); |
} else { |
ReportResult(EnrollmentStatus::ForValidationError(validator->status())); |
} |
} |
+void EnrollmentHandlerChromeOS::OnRobotAuthCodesFetched( |
+ CloudPolicyClient* client) { |
+ DCHECK_EQ(client_.get(), client); |
+ CHECK_EQ(STEP_ROBOT_AUTH_FETCH, enrollment_step_); |
+ |
+ enrollment_step_ = STEP_ROBOT_AUTH_REFRESH; |
+ |
+ gaia::OAuthClientInfo client_info; |
+ client_info.client_id = GaiaUrls::GetInstance()->oauth2_chrome_client_id(); |
+ client_info.client_secret = |
+ GaiaUrls::GetInstance()->oauth2_chrome_client_secret(); |
+ |
+ // Use the system request context to avoid sending user cookies. |
+ gaia_oauth_client_.reset(new gaia::GaiaOAuthClient( |
+ gaia::kGaiaOAuth2Url, |
+ g_browser_process->system_request_context())); |
+ gaia_oauth_client_->GetTokensFromAuthCode(client_info, |
+ client->robot_api_auth_code(), |
+ 0 /* max_retries */, |
+ this); |
+} |
+ |
+// GaiaOAuthClient::Delegate callback for OAuth2 refresh token fetched. |
+void EnrollmentHandlerChromeOS::OnGetTokensResponse( |
+ const std::string& refresh_token, |
+ const std::string& access_token, |
+ int expires_in_seconds) { |
+ CHECK_EQ(STEP_ROBOT_AUTH_REFRESH, enrollment_step_); |
+ |
+ robot_refresh_token_ = refresh_token; |
+ |
+ enrollment_step_ = STEP_LOCK_DEVICE, |
+ WriteInstallAttributes(username_, device_mode_, device_id_); |
+} |
+ |
+// GaiaOAuthClient::Delegate |
+void EnrollmentHandlerChromeOS::OnRefreshTokenResponse( |
+ const std::string& access_token, |
+ int expires_in_seconds) { |
+ // We never use the code that should trigger this callback. |
+ LOG(FATAL) << "Unexpected callback invoked"; |
+} |
+ |
+// GaiaOAuthClient::Delegate OAuth2 error when fetching refresh token request. |
+void EnrollmentHandlerChromeOS::OnOAuthError() { |
+ ReportResult(EnrollmentStatus::ForStatus( |
+ EnrollmentStatus::STATUS_ROBOT_REFRESH_FETCH_FAILED)); |
+} |
+ |
+// GaiaOAuthClient::Delegate network error when fetching refresh token. |
+void EnrollmentHandlerChromeOS::OnNetworkError(int response_code) { |
+ LOG(ERROR) << "Network error while fetching API refresh token: " |
+ << response_code; |
+ ReportResult(EnrollmentStatus::ForStatus( |
+ EnrollmentStatus::STATUS_ROBOT_REFRESH_FETCH_FAILED)); |
+} |
+ |
void EnrollmentHandlerChromeOS::WriteInstallAttributes( |
const std::string& user, |
DeviceMode device_mode, |