Commit b3eeabcc authored by Denis Kuznetsov's avatar Denis Kuznetsov Committed by Commit Bot

Make robot account request a one-time call

Bug: 854101
Change-Id: Ibd9375295ea26ff0533dadcc8153cabfe71ba786
Reviewed-on: https://chromium-review.googlesource.com/1251443
Commit-Queue: Denis Kuznetsov <antrim@chromium.org>
Reviewed-by: default avatarSergey Poromov <poromov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#595088}
parent 4e2538c0
...@@ -80,7 +80,6 @@ class AppInstallEventLogUploader : public CloudPolicyClient::Observer { ...@@ -80,7 +80,6 @@ class AppInstallEventLogUploader : public CloudPolicyClient::Observer {
// request when the client registers, by asking the delegate to serialize logs // request when the client registers, by asking the delegate to serialize logs
// and with the exponential backoff reset to its minimum. // and with the exponential backoff reset to its minimum.
void OnRegistrationStateChanged(CloudPolicyClient* client) override; void OnRegistrationStateChanged(CloudPolicyClient* client) override;
void OnRobotAuthCodesFetched(CloudPolicyClient* client) override {}
void OnClientError(CloudPolicyClient* client) override {} void OnClientError(CloudPolicyClient* client) override {}
private: private:
......
...@@ -551,7 +551,10 @@ void EnrollmentHandlerChromeOS::HandlePolicyValidationResult( ...@@ -551,7 +551,10 @@ void EnrollmentHandlerChromeOS::HandlePolicyValidationResult(
} else { } else {
domain_ = gaia::ExtractDomainName(gaia::CanonicalizeEmail(username)); domain_ = gaia::ExtractDomainName(gaia::CanonicalizeEmail(username));
SetStep(STEP_ROBOT_AUTH_FETCH); SetStep(STEP_ROBOT_AUTH_FETCH);
client_->FetchRobotAuthCodes(dm_auth_->Clone()); client_->FetchRobotAuthCodes(
dm_auth_->Clone(),
base::BindOnce(&EnrollmentHandlerChromeOS::OnRobotAuthCodesFetched,
weak_ptr_factory_.GetWeakPtr()));
} }
} else { } else {
ReportResult(EnrollmentStatus::ForValidationError(validator->status())); ReportResult(EnrollmentStatus::ForValidationError(validator->status()));
...@@ -559,11 +562,14 @@ void EnrollmentHandlerChromeOS::HandlePolicyValidationResult( ...@@ -559,11 +562,14 @@ void EnrollmentHandlerChromeOS::HandlePolicyValidationResult(
} }
void EnrollmentHandlerChromeOS::OnRobotAuthCodesFetched( void EnrollmentHandlerChromeOS::OnRobotAuthCodesFetched(
CloudPolicyClient* client) { DeviceManagementStatus status,
DCHECK_EQ(client_.get(), client); const std::string& auth_code) {
CHECK_EQ(STEP_ROBOT_AUTH_FETCH, enrollment_step_); CHECK_EQ(STEP_ROBOT_AUTH_FETCH, enrollment_step_);
if (status != DM_STATUS_SUCCESS) {
if (client->robot_api_auth_code().empty()) { OnClientError(client_.get());
return;
}
if (auth_code.empty()) {
// If the server doesn't provide an auth code, skip the robot auth setup. // If the server doesn't provide an auth code, skip the robot auth setup.
// This allows clients running against the test server to transparently skip // This allows clients running against the test server to transparently skip
// robot auth. // robot auth.
...@@ -583,8 +589,8 @@ void EnrollmentHandlerChromeOS::OnRobotAuthCodesFetched( ...@@ -583,8 +589,8 @@ void EnrollmentHandlerChromeOS::OnRobotAuthCodesFetched(
// Use the system request context to avoid sending user cookies. // Use the system request context to avoid sending user cookies.
gaia_oauth_client_.reset(new gaia::GaiaOAuthClient( gaia_oauth_client_.reset(new gaia::GaiaOAuthClient(
g_browser_process->shared_url_loader_factory())); g_browser_process->shared_url_loader_factory()));
gaia_oauth_client_->GetTokensFromAuthCode( gaia_oauth_client_->GetTokensFromAuthCode(client_info, auth_code,
client_info, client->robot_api_auth_code(), 0 /* max_retries */, this); 0 /* max_retries */, this);
} }
// GaiaOAuthClient::Delegate callback for OAuth2 refresh token fetched. // GaiaOAuthClient::Delegate callback for OAuth2 refresh token fetched.
......
...@@ -100,7 +100,6 @@ class EnrollmentHandlerChromeOS : public CloudPolicyClient::Observer, ...@@ -100,7 +100,6 @@ class EnrollmentHandlerChromeOS : public CloudPolicyClient::Observer,
// CloudPolicyClient::Observer: // CloudPolicyClient::Observer:
void OnPolicyFetched(CloudPolicyClient* client) override; void OnPolicyFetched(CloudPolicyClient* client) override;
void OnRegistrationStateChanged(CloudPolicyClient* client) override; void OnRegistrationStateChanged(CloudPolicyClient* client) override;
void OnRobotAuthCodesFetched(CloudPolicyClient* client) override;
void OnClientError(CloudPolicyClient* client) override; void OnClientError(CloudPolicyClient* client) override;
// CloudPolicyStore::Observer: // CloudPolicyStore::Observer:
...@@ -211,6 +210,10 @@ class EnrollmentHandlerChromeOS : public CloudPolicyClient::Observer, ...@@ -211,6 +210,10 @@ class EnrollmentHandlerChromeOS : public CloudPolicyClient::Observer,
// Handles the policy validation result for the offline demo mode. // Handles the policy validation result for the offline demo mode.
void OnOfflinePolicyValidated(DeviceCloudPolicyValidator* validator); void OnOfflinePolicyValidated(DeviceCloudPolicyValidator* validator);
// Handles the fetching auth codes for robot accounts during enrollment.
void OnRobotAuthCodesFetched(DeviceManagementStatus status,
const std::string& auth_code);
std::unique_ptr<DeviceCloudPolicyValidator> CreateValidator( std::unique_ptr<DeviceCloudPolicyValidator> CreateValidator(
std::unique_ptr<enterprise_management::PolicyFetchResponse> policy, std::unique_ptr<enterprise_management::PolicyFetchResponse> policy,
const std::string& domain); const std::string& domain);
......
...@@ -150,9 +150,6 @@ TranslatePolicyValidationResultSeverity( ...@@ -150,9 +150,6 @@ TranslatePolicyValidationResultSeverity(
CloudPolicyClient::Observer::~Observer() {} CloudPolicyClient::Observer::~Observer() {}
void CloudPolicyClient::Observer::OnRobotAuthCodesFetched(
CloudPolicyClient* client) {}
CloudPolicyClient::CloudPolicyClient( CloudPolicyClient::CloudPolicyClient(
const std::string& machine_id, const std::string& machine_id,
const std::string& machine_model, const std::string& machine_model,
...@@ -461,7 +458,8 @@ void CloudPolicyClient::UploadPolicyValidationReport( ...@@ -461,7 +458,8 @@ void CloudPolicyClient::UploadPolicyValidationReport(
request_jobs_.back()->Start(job_callback); request_jobs_.back()->Start(job_callback);
} }
void CloudPolicyClient::FetchRobotAuthCodes(std::unique_ptr<DMAuth> auth) { void CloudPolicyClient::FetchRobotAuthCodes(std::unique_ptr<DMAuth> auth,
RobotAuthCodeCallback callback) {
CHECK(is_registered()); CHECK(is_registered());
DCHECK(auth->has_dm_token()); DCHECK(auth->has_dm_token());
...@@ -479,9 +477,9 @@ void CloudPolicyClient::FetchRobotAuthCodes(std::unique_ptr<DMAuth> auth) { ...@@ -479,9 +477,9 @@ void CloudPolicyClient::FetchRobotAuthCodes(std::unique_ptr<DMAuth> auth) {
request->add_auth_scope(GaiaConstants::kAnyApiOAuth2Scope); request->add_auth_scope(GaiaConstants::kAnyApiOAuth2Scope);
request->set_device_type(em::DeviceServiceApiAccessRequest::CHROME_OS); request->set_device_type(em::DeviceServiceApiAccessRequest::CHROME_OS);
policy_fetch_request_job_->Start( policy_fetch_request_job_->Start(base::AdaptCallbackForRepeating(
base::Bind(&CloudPolicyClient::OnFetchRobotAuthCodesCompleted, base::BindOnce(&CloudPolicyClient::OnFetchRobotAuthCodesCompleted,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr(), std::move(callback))));
} }
void CloudPolicyClient::Unregister() { void CloudPolicyClient::Unregister() {
...@@ -870,6 +868,7 @@ void CloudPolicyClient::OnRegisterCompleted( ...@@ -870,6 +868,7 @@ void CloudPolicyClient::OnRegisterCompleted(
} }
void CloudPolicyClient::OnFetchRobotAuthCodesCompleted( void CloudPolicyClient::OnFetchRobotAuthCodesCompleted(
RobotAuthCodeCallback callback,
DeviceManagementStatus status, DeviceManagementStatus status,
int net_error, int net_error,
const em::DeviceManagementResponse& response) { const em::DeviceManagementResponse& response) {
...@@ -878,16 +877,14 @@ void CloudPolicyClient::OnFetchRobotAuthCodesCompleted( ...@@ -878,16 +877,14 @@ void CloudPolicyClient::OnFetchRobotAuthCodesCompleted(
LOG(WARNING) << "Invalid service api access response."; LOG(WARNING) << "Invalid service api access response.";
status = DM_STATUS_RESPONSE_DECODING_ERROR; status = DM_STATUS_RESPONSE_DECODING_ERROR;
} }
status_ = status; status_ = status;
if (status == DM_STATUS_SUCCESS) { if (status == DM_STATUS_SUCCESS) {
robot_api_auth_code_ = response.service_api_access_response().auth_code();
DVLOG(1) << "Device robot account auth code fetch complete - code = " DVLOG(1) << "Device robot account auth code fetch complete - code = "
<< robot_api_auth_code_; << response.service_api_access_response().auth_code();
std::move(callback).Run(status,
NotifyRobotAuthCodesFetched(); response.service_api_access_response().auth_code());
} else { } else {
NotifyClientError(); std::move(callback).Run(status, std::string());
} }
} }
...@@ -1142,11 +1139,6 @@ void CloudPolicyClient::NotifyRegistrationStateChanged() { ...@@ -1142,11 +1139,6 @@ void CloudPolicyClient::NotifyRegistrationStateChanged() {
observer.OnRegistrationStateChanged(this); observer.OnRegistrationStateChanged(this);
} }
void CloudPolicyClient::NotifyRobotAuthCodesFetched() {
for (auto& observer : observers_)
observer.OnRobotAuthCodesFetched(this);
}
void CloudPolicyClient::NotifyClientError() { void CloudPolicyClient::NotifyClientError() {
for (auto& observer : observers_) for (auto& observer : observers_)
observer.OnClientError(this); observer.OnClientError(this);
......
...@@ -71,6 +71,11 @@ class POLICY_EXPORT CloudPolicyClient { ...@@ -71,6 +71,11 @@ class POLICY_EXPORT CloudPolicyClient {
DeviceManagementStatus, DeviceManagementStatus,
const std::vector<enterprise_management::RemoteCommand>&)>; const std::vector<enterprise_management::RemoteCommand>&)>;
// A callback for fetching device robot OAuth2 authorization tokens.
// Only occurs during enrollment, after the device is registered.
using RobotAuthCodeCallback =
base::OnceCallback<void(DeviceManagementStatus, const std::string&)>;
// A callback which fetches device dm_token based on user affiliation. // A callback which fetches device dm_token based on user affiliation.
// Should be called once per registration. // Should be called once per registration.
using DeviceDMTokenCallback = base::RepeatingCallback<std::string( using DeviceDMTokenCallback = base::RepeatingCallback<std::string(
...@@ -89,11 +94,6 @@ class POLICY_EXPORT CloudPolicyClient { ...@@ -89,11 +94,6 @@ class POLICY_EXPORT CloudPolicyClient {
// successful completion of registration and unregistration requests. // successful completion of registration and unregistration requests.
virtual void OnRegistrationStateChanged(CloudPolicyClient* client) = 0; virtual void OnRegistrationStateChanged(CloudPolicyClient* client) = 0;
// Called when a request for device robot OAuth2 authorization tokens
// returns successfully. Only occurs during enrollment. Optional
// (default implementation is a noop).
virtual void OnRobotAuthCodesFetched(CloudPolicyClient* client);
// Indicates there's been an error in a previously-issued request. // Indicates there's been an error in a previously-issued request.
virtual void OnClientError(CloudPolicyClient* client) = 0; virtual void OnClientError(CloudPolicyClient* client) = 0;
}; };
...@@ -183,7 +183,9 @@ class POLICY_EXPORT CloudPolicyClient { ...@@ -183,7 +183,9 @@ class POLICY_EXPORT CloudPolicyClient {
// Requests OAuth2 auth codes for the device robot account. The client being // Requests OAuth2 auth codes for the device robot account. The client being
// registered is a prerequisite to this operation and this call will CHECK if // registered is a prerequisite to this operation and this call will CHECK if
// the client is not in registered state. // the client is not in registered state.
virtual void FetchRobotAuthCodes(std::unique_ptr<DMAuth> auth); // The |callback| will be called when the operation completes.
virtual void FetchRobotAuthCodes(std::unique_ptr<DMAuth> auth,
RobotAuthCodeCallback callback);
// Sends an unregistration request to the server. // Sends an unregistration request to the server.
virtual void Unregister(); virtual void Unregister();
...@@ -349,10 +351,6 @@ class POLICY_EXPORT CloudPolicyClient { ...@@ -349,10 +351,6 @@ class POLICY_EXPORT CloudPolicyClient {
return status_; return status_;
} }
const std::string& robot_api_auth_code() const {
return robot_api_auth_code_;
}
// Returns the invalidation version that was used for the last FetchPolicy. // Returns the invalidation version that was used for the last FetchPolicy.
// Observers can call this method from their OnPolicyFetched method to // Observers can call this method from their OnPolicyFetched method to
// determine which at which invalidation version the policy was fetched. // determine which at which invalidation version the policy was fetched.
...@@ -403,6 +401,7 @@ class POLICY_EXPORT CloudPolicyClient { ...@@ -403,6 +401,7 @@ class POLICY_EXPORT CloudPolicyClient {
// Callback for robot account api authorization requests. // Callback for robot account api authorization requests.
void OnFetchRobotAuthCodesCompleted( void OnFetchRobotAuthCodesCompleted(
RobotAuthCodeCallback callback,
DeviceManagementStatus status, DeviceManagementStatus status,
int net_error, int net_error,
const enterprise_management::DeviceManagementResponse& response); const enterprise_management::DeviceManagementResponse& response);
...@@ -475,7 +474,6 @@ class POLICY_EXPORT CloudPolicyClient { ...@@ -475,7 +474,6 @@ class POLICY_EXPORT CloudPolicyClient {
// Observer notification helpers. // Observer notification helpers.
void NotifyPolicyFetched(); void NotifyPolicyFetched();
void NotifyRegistrationStateChanged(); void NotifyRegistrationStateChanged();
void NotifyRobotAuthCodesFetched();
void NotifyClientError(); void NotifyClientError();
// Data necessary for constructing policy requests. // Data necessary for constructing policy requests.
...@@ -492,7 +490,6 @@ class POLICY_EXPORT CloudPolicyClient { ...@@ -492,7 +490,6 @@ class POLICY_EXPORT CloudPolicyClient {
base::Time last_policy_timestamp_; base::Time last_policy_timestamp_;
int public_key_version_ = -1; int public_key_version_ = -1;
bool public_key_version_valid_ = false; bool public_key_version_valid_ = false;
std::string robot_api_auth_code_;
// Device DMToken for affiliated user policy requests. // Device DMToken for affiliated user policy requests.
// Retrieved from |device_dm_token_callback_| on registration. // Retrieved from |device_dm_token_callback_| on registration.
std::string device_dm_token_; std::string device_dm_token_;
......
...@@ -105,7 +105,6 @@ class MockCloudPolicyClientObserver : public CloudPolicyClient::Observer { ...@@ -105,7 +105,6 @@ class MockCloudPolicyClientObserver : public CloudPolicyClient::Observer {
MOCK_METHOD1(OnPolicyFetched, void(CloudPolicyClient*)); MOCK_METHOD1(OnPolicyFetched, void(CloudPolicyClient*));
MOCK_METHOD1(OnRegistrationStateChanged, void(CloudPolicyClient*)); MOCK_METHOD1(OnRegistrationStateChanged, void(CloudPolicyClient*));
MOCK_METHOD1(OnRobotAuthCodesFetched, void(CloudPolicyClient*));
MOCK_METHOD1(OnClientError, void(CloudPolicyClient*)); MOCK_METHOD1(OnClientError, void(CloudPolicyClient*));
private: private:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment