Persist kInvalidCredentials state
External code may want to do some additional processing when device
was unregistered from the cloud side, e.g. factory reset of the device.
Existing code switches from kInvalidCredentials to kUnregistered after
reboot. This makes processing kInvalidCredentials unreliable.
Persistence of kInvalidCredentials implemented as special case when
cloud_id is not empty but credentials are missing.
BUG:25342842
Change-Id: I80d4ce8157c70e132a55fd752a9006064cf70b57
Reviewed-on: https://weave-review.googlesource.com/1580
Reviewed-by: Vitaly Buka <vitalybuka@google.com>
diff --git a/src/device_registration_info.cc b/src/device_registration_info.cc
index 8b8e752..751e530 100644
--- a/src/device_registration_info.cc
+++ b/src/device_registration_info.cc
@@ -262,6 +262,11 @@
cloud_backoff_entry_.reset(new BackoffEntry{cloud_backoff_policy_.get()});
oauth2_backoff_entry_.reset(new BackoffEntry{cloud_backoff_policy_.get()});
+ bool revoked =
+ !GetSettings().cloud_id.empty() && !HaveRegistrationCredentials();
+ gcd_state_ =
+ revoked ? GcdState::kInvalidCredentials : GcdState::kUnconfigured;
+
command_manager_->AddCommandDefChanged(
base::Bind(&DeviceRegistrationInfo::OnCommandDefsChanged,
weak_factory_.GetWeakPtr()));
@@ -511,9 +516,8 @@
void DeviceRegistrationInfo::GetDeviceInfo(
const CloudRequestDoneCallback& callback) {
ErrorPtr error;
- if (!VerifyRegistrationCredentials(&error)) {
+ if (!VerifyRegistrationCredentials(&error))
return callback.Run({}, std::move(error));
- }
DoCloudRequest(HttpClient::Method::kGet, GetDeviceURL(), nullptr, callback);
}
@@ -675,9 +679,8 @@
// TODO(antonm): Add support for device removal.
ErrorPtr error;
- if (!VerifyRegistrationCredentials(&error)) {
+ if (!VerifyRegistrationCredentials(&error))
return data->callback.Run({}, std::move(error));
- }
if (cloud_backoff_entry_->ShouldRejectRequest()) {
VLOG(1) << "Cloud request delayed for "
@@ -762,13 +765,13 @@
void DeviceRegistrationInfo::CheckAccessTokenError(ErrorPtr error) {
if (error && error->HasError(kErrorDomainOAuth2, "invalid_grant"))
- MarkDeviceUnregistered();
+ RemoveCredentials();
}
void DeviceRegistrationInfo::ConnectToCloud(ErrorPtr error) {
if (error) {
if (error->HasError(kErrorDomainOAuth2, "invalid_grant"))
- MarkDeviceUnregistered();
+ RemoveCredentials();
return;
}
@@ -1270,10 +1273,10 @@
<< cloud_id << "'";
return;
}
- MarkDeviceUnregistered();
+ RemoveCredentials();
}
-void DeviceRegistrationInfo::MarkDeviceUnregistered() {
+void DeviceRegistrationInfo::RemoveCredentials() {
if (!HaveRegistrationCredentials())
return;
@@ -1281,7 +1284,7 @@
LOG(INFO) << "Device is unregistered from the cloud. Deleting credentials";
Config::Transaction change{config_.get()};
- change.set_cloud_id("");
+ // Keep cloud_id to switch to detect kInvalidCredentials after restart.
change.set_robot_account("");
change.set_refresh_token("");
change.Commit();