aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorVitaly Buka <vitalybuka@google.com>2016-02-05 11:40:33 -0800
committerVitaly Buka <vitalybuka@google.com>2016-02-10 22:06:28 +0000
commit03ee8acf44ca0b46617183ff68da667bd6119993 (patch)
treed447d2e8b168acd1c1a668e194d64078af626b4f /src
parentf51743b22a64d38a5423edf488e6341ffc289823 (diff)
downloadlibweave-03ee8acf44ca0b46617183ff68da667bd6119993.tar.gz
Enable support of endpoints override.
Endpoints can be overridden only during device registration. New endpoints will be stored in device config only on successful registration. Device will keep endpoints as pending during registration process. BUG:23907593 BUG:26525138 Change-Id: I2a2ddcbad19746d631a78b33f7305da1c0bb07fb Reviewed-on: https://weave-review.googlesource.com/2203 Reviewed-by: Alex Vakulenko <avakulenko@google.com>
Diffstat (limited to 'src')
-rw-r--r--src/config.cc78
-rw-r--r--src/config.h3
-rw-r--r--src/device_manager.cc4
-rw-r--r--src/device_manager.h2
-rw-r--r--src/device_registration_info.cc119
-rw-r--r--src/device_registration_info.h16
-rw-r--r--src/device_registration_info_unittest.cc158
-rw-r--r--src/privet/cloud_delegate.cc15
-rw-r--r--src/privet/cloud_delegate.h4
-rw-r--r--src/privet/mock_delegates.h2
-rw-r--r--src/privet/privet_handler.cc3
-rw-r--r--src/privet/privet_handler_unittest.cc14
-rw-r--r--src/weave_unittest.cc6
13 files changed, 260 insertions, 164 deletions
diff --git a/src/config.cc b/src/config.cc
index 21a1c1f..5ae3fed 100644
--- a/src/config.cc
+++ b/src/config.cc
@@ -67,7 +67,7 @@ void MigrateFromV0(base::DictionaryValue* dict) {
dict->Set(config_keys::kCloudId, std::move(tmp));
}
-Config::Settings CreateDefaultSettings() {
+Config::Settings CreateDefaultSettings(provider::ConfigStore* config_store) {
Config::Settings result;
result.oauth_url = "https://accounts.google.com/o/oauth2/";
result.service_url = kWeaveUrl;
@@ -75,6 +75,36 @@ Config::Settings CreateDefaultSettings() {
result.local_anonymous_access_role = AuthScope::kViewer;
result.pairing_modes.insert(PairingType::kPinCode);
result.device_id = base::GenerateGUID();
+
+ if (!config_store)
+ return result;
+
+ // Crash on any mistakes in defaults.
+ CHECK(config_store->LoadDefaults(&result));
+
+ CHECK(!result.client_id.empty());
+ CHECK(!result.client_secret.empty());
+ CHECK(!result.api_key.empty());
+ CHECK(!result.oauth_url.empty());
+ CHECK(!result.service_url.empty());
+ CHECK(!result.xmpp_endpoint.empty());
+ CHECK(!result.oem_name.empty());
+ CHECK(!result.model_name.empty());
+ CHECK(!result.model_id.empty());
+ CHECK(!result.name.empty());
+ CHECK(!result.device_id.empty());
+ CHECK_EQ(result.embedded_code.empty(),
+ (result.pairing_modes.find(PairingType::kEmbeddedCode) ==
+ result.pairing_modes.end()));
+
+ // Values below will be generated at runtime.
+ CHECK(result.cloud_id.empty());
+ CHECK(result.refresh_token.empty());
+ CHECK(result.robot_account.empty());
+ CHECK(result.last_configured_ssid.empty());
+ CHECK(result.secret.empty());
+ CHECK(result.root_client_token_owner == RootClientTokenOwner::kNone);
+
return result;
}
@@ -91,8 +121,12 @@ LIBWEAVE_EXPORT EnumToStringMap<RootClientTokenOwner>::EnumToStringMap()
: EnumToStringMap(kRootClientTokenOwnerMap) {}
Config::Config(provider::ConfigStore* config_store)
- : settings_{CreateDefaultSettings()}, config_store_{config_store} {
- Load();
+ : defaults_{CreateDefaultSettings(config_store)},
+ settings_{defaults_},
+ config_store_{config_store} {
+ Transaction change{this};
+ change.save_ = false;
+ change.LoadState();
}
void Config::AddOnChangedCallback(const OnChangedCallback& callback) {
@@ -105,42 +139,8 @@ const Config::Settings& Config::GetSettings() const {
return settings_;
}
-void Config::Load() {
- Transaction change{this};
- change.save_ = false;
-
- settings_ = CreateDefaultSettings();
-
- if (!config_store_)
- return;
-
- // Crash on any mistakes in defaults.
- CHECK(config_store_->LoadDefaults(&settings_));
-
- CHECK(!settings_.client_id.empty());
- CHECK(!settings_.client_secret.empty());
- CHECK(!settings_.api_key.empty());
- CHECK(!settings_.oauth_url.empty());
- CHECK(!settings_.service_url.empty());
- CHECK(!settings_.xmpp_endpoint.empty());
- CHECK(!settings_.oem_name.empty());
- CHECK(!settings_.model_name.empty());
- CHECK(!settings_.model_id.empty());
- CHECK(!settings_.name.empty());
- CHECK(!settings_.device_id.empty());
- CHECK_EQ(settings_.embedded_code.empty(),
- (settings_.pairing_modes.find(PairingType::kEmbeddedCode) ==
- settings_.pairing_modes.end()));
-
- // Values below will be generated at runtime.
- CHECK(settings_.cloud_id.empty());
- CHECK(settings_.refresh_token.empty());
- CHECK(settings_.robot_account.empty());
- CHECK(settings_.last_configured_ssid.empty());
- CHECK(settings_.secret.empty());
- CHECK(settings_.root_client_token_owner == RootClientTokenOwner::kNone);
-
- change.LoadState();
+const Config::Settings& Config::GetDefaults() const {
+ return defaults_;
}
void Config::Transaction::LoadState() {
diff --git a/src/config.h b/src/config.h
index 8e0a8f3..692d9c5 100644
--- a/src/config.h
+++ b/src/config.h
@@ -45,6 +45,7 @@ class Config final {
void AddOnChangedCallback(const OnChangedCallback& callback);
const Config::Settings& GetSettings() const;
+ const Config::Settings& GetDefaults() const;
// Allows editing of config. Makes sure that callbacks were called and changes
// were saved.
@@ -121,9 +122,9 @@ class Config final {
};
private:
- void Load();
void Save();
+ const Settings defaults_;
Settings settings_;
provider::ConfigStore* config_store_{nullptr};
std::vector<OnChangedCallback> on_changed_;
diff --git a/src/device_manager.cc b/src/device_manager.cc
index deb5404..4c0d3ee 100644
--- a/src/device_manager.cc
+++ b/src/device_manager.cc
@@ -262,9 +262,9 @@ const base::DictionaryValue& DeviceManager::GetState() const {
return component_manager_->GetLegacyState();
}
-void DeviceManager::Register(const std::string& ticket_id,
+void DeviceManager::Register(const RegistrationData& registration_data,
const DoneCallback& callback) {
- device_info_->RegisterDevice(ticket_id, callback);
+ device_info_->RegisterDevice(registration_data, callback);
}
void DeviceManager::AddPairingChangedCallbacks(
diff --git a/src/device_manager.h b/src/device_manager.h
index d77bacc..f0ad464 100644
--- a/src/device_manager.h
+++ b/src/device_manager.h
@@ -69,7 +69,7 @@ class DeviceManager final : public Device {
ErrorPtr* error) override;
Command* FindCommand(const std::string& id) override;
void AddStateChangedCallback(const base::Closure& callback) override;
- void Register(const std::string& ticket_id,
+ void Register(const RegistrationData& registration_data,
const DoneCallback& callback) override;
GcdState GetGcdState() const override;
void AddGcdStateChangedCallback(
diff --git a/src/device_registration_info.cc b/src/device_registration_info.cc
index 0dc1f54..b692f06 100644
--- a/src/device_registration_info.cc
+++ b/src/device_registration_info.cc
@@ -28,6 +28,7 @@
#include "src/json_error_codes.h"
#include "src/notification/xmpp_channel.h"
#include "src/privet/auth_manager.h"
+#include "src/privet/constants.h"
#include "src/string_utils.h"
#include "src/utils.h"
@@ -281,8 +282,8 @@ std::string DeviceRegistrationInfo::GetDeviceURL(
const std::string& subpath,
const WebParamList& params) const {
CHECK(!GetSettings().cloud_id.empty()) << "Must have a valid device ID";
- return BuildURL(GetSettings().service_url,
- "devices/" + GetSettings().cloud_id + "/" + subpath, params);
+ return GetServiceURL("devices/" + GetSettings().cloud_id + "/" + subpath,
+ params);
}
std::string DeviceRegistrationInfo::GetOAuthURL(
@@ -515,8 +516,47 @@ void DeviceRegistrationInfo::RegisterDeviceError(const DoneCallback& callback,
base::Bind(callback, base::Passed(&error)), {});
}
-void DeviceRegistrationInfo::RegisterDevice(const std::string& ticket_id,
+void DeviceRegistrationInfo::RegisterDevice(RegistrationData registration_data,
const DoneCallback& callback) {
+ if (!GetSettings().allow_endpoints_override &&
+ registration_data != RegistrationData{registration_data.ticket_id}) {
+ ErrorPtr error;
+ Error::AddTo(&error, FROM_HERE, privet::errors::kInvalidParams,
+ "Endpoint change is not permitted");
+ return RegisterDeviceError(callback, std::move(error));
+ }
+
+ // Reset OAuth to defaults, if device was unregistered values can be
+ // customized. These muse be replaced all together.
+ if (registration_data.oauth_url.empty() ||
+ registration_data.client_id.empty() ||
+ registration_data.client_secret.empty() ||
+ registration_data.api_key.empty()) {
+ registration_data.oauth_url = GetDefaults().oauth_url;
+ registration_data.client_id = GetDefaults().client_id;
+ registration_data.client_secret = GetDefaults().client_secret;
+ registration_data.api_key = GetDefaults().api_key;
+ }
+
+ // Reset Server URL to default, if device was unregistered value can be
+ // customized.
+ if (registration_data.service_url.empty())
+ registration_data.service_url = GetDefaults().service_url;
+
+ // Reset XMPP to default, if device was unregistered value can be
+ // customized.
+ if (registration_data.xmpp_endpoint.empty())
+ registration_data.xmpp_endpoint = GetDefaults().xmpp_endpoint;
+
+ VLOG(1) << "RegisterDevice: "
+ << "ticket_id: " << registration_data.ticket_id
+ << ", oauth_url: " << registration_data.oauth_url
+ << ", client_id: " << registration_data.client_id
+ << ", client_secret: " << registration_data.client_secret
+ << ", api_key: " << registration_data.api_key
+ << ", service_url: " << registration_data.service_url
+ << ", xmpp_endpoint: " << registration_data.xmpp_endpoint;
+
if (HaveRegistrationCredentials()) {
ErrorPtr error;
Error::AddTo(&error, FROM_HERE, kErrorAlreayRegistered,
@@ -528,21 +568,23 @@ void DeviceRegistrationInfo::RegisterDevice(const std::string& ticket_id,
CHECK(device_draft);
base::DictionaryValue req_json;
- req_json.SetString("id", ticket_id);
- req_json.SetString("oauthClientId", GetSettings().client_id);
+ req_json.SetString("id", registration_data.ticket_id);
+ req_json.SetString("oauthClientId", registration_data.client_id);
req_json.Set("deviceDraft", device_draft.release());
- auto url = GetServiceURL("registrationTickets/" + ticket_id,
- {{"key", GetSettings().api_key}});
+ auto url = BuildURL(registration_data.service_url,
+ "registrationTickets/" + registration_data.ticket_id,
+ {{"key", registration_data.api_key}});
RequestSender sender{HttpClient::Method::kPatch, url, http_client_};
sender.SetJsonData(req_json);
sender.Send(base::Bind(&DeviceRegistrationInfo::RegisterDeviceOnTicketSent,
- weak_factory_.GetWeakPtr(), ticket_id, callback));
+ weak_factory_.GetWeakPtr(), registration_data,
+ callback));
}
void DeviceRegistrationInfo::RegisterDeviceOnTicketSent(
- const std::string& ticket_id,
+ const RegistrationData& registration_data,
const DoneCallback& callback,
std::unique_ptr<provider::HttpClient::Response> response,
ErrorPtr error) {
@@ -557,15 +599,17 @@ void DeviceRegistrationInfo::RegisterDeviceOnTicketSent(
return RegisterDeviceError(callback, std::move(error));
}
- std::string url =
- GetServiceURL("registrationTickets/" + ticket_id + "/finalize",
- {{"key", GetSettings().api_key}});
+ std::string url = BuildURL(
+ registration_data.service_url,
+ "registrationTickets/" + registration_data.ticket_id + "/finalize",
+ {{"key", registration_data.api_key}});
RequestSender{HttpClient::Method::kPost, url, http_client_}.Send(
base::Bind(&DeviceRegistrationInfo::RegisterDeviceOnTicketFinalized,
- weak_factory_.GetWeakPtr(), callback));
+ weak_factory_.GetWeakPtr(), registration_data, callback));
}
void DeviceRegistrationInfo::RegisterDeviceOnTicketFinalized(
+ const RegistrationData& registration_data,
const DoneCallback& callback,
std::unique_ptr<provider::HttpClient::Response> response,
ErrorPtr error) {
@@ -595,19 +639,21 @@ void DeviceRegistrationInfo::RegisterDeviceOnTicketFinalized(
UpdateDeviceInfoTimestamp(*device_draft_response);
// Now get access_token and refresh_token
- RequestSender sender2{HttpClient::Method::kPost, GetOAuthURL("token"),
+ RequestSender sender2{HttpClient::Method::kPost,
+ BuildURL(registration_data.oauth_url, "token", {}),
http_client_};
sender2.SetFormData({{"code", auth_code},
- {"client_id", GetSettings().client_id},
- {"client_secret", GetSettings().client_secret},
+ {"client_id", registration_data.client_id},
+ {"client_secret", registration_data.client_secret},
{"redirect_uri", "oob"},
{"grant_type", "authorization_code"}});
sender2.Send(base::Bind(&DeviceRegistrationInfo::RegisterDeviceOnAuthCodeSent,
- weak_factory_.GetWeakPtr(), cloud_id, robot_account,
- callback));
+ weak_factory_.GetWeakPtr(), registration_data,
+ cloud_id, robot_account, callback));
}
void DeviceRegistrationInfo::RegisterDeviceOnAuthCodeSent(
+ const RegistrationData& registration_data,
const std::string& cloud_id,
const std::string& robot_account,
const DoneCallback& callback,
@@ -631,9 +677,18 @@ void DeviceRegistrationInfo::RegisterDeviceOnAuthCodeSent(
base::Time::Now() + base::TimeDelta::FromSeconds(expires_in);
Config::Transaction change{config_};
+
change.set_cloud_id(cloud_id);
change.set_robot_account(robot_account);
change.set_refresh_token(refresh_token);
+
+ change.set_oauth_url(registration_data.oauth_url);
+ change.set_client_id(registration_data.client_id);
+ change.set_client_secret(registration_data.client_secret);
+ change.set_api_key(registration_data.api_key);
+ change.set_service_url(registration_data.service_url);
+ change.set_xmpp_endpoint(registration_data.xmpp_endpoint);
+
change.Commit();
task_runner_->PostDelayedTask(FROM_HERE, base::Bind(callback, nullptr), {});
@@ -828,34 +883,6 @@ void DeviceRegistrationInfo::UpdateBaseConfig(AuthScope anonymous_access_role,
change.set_local_pairing_enabled(local_pairing_enabled);
}
-bool DeviceRegistrationInfo::UpdateServiceConfig(
- const std::string& client_id,
- const std::string& client_secret,
- const std::string& api_key,
- const std::string& oauth_url,
- const std::string& service_url,
- const std::string& xmpp_endpoint,
- ErrorPtr* error) {
- if (HaveRegistrationCredentials()) {
- return Error::AddTo(error, FROM_HERE, kErrorAlreayRegistered,
- "Unable to change config for registered device");
- }
- Config::Transaction change{config_};
- if (!client_id.empty())
- change.set_client_id(client_id);
- if (!client_secret.empty())
- change.set_client_secret(client_secret);
- if (!api_key.empty())
- change.set_api_key(api_key);
- if (!oauth_url.empty())
- change.set_oauth_url(oauth_url);
- if (!service_url.empty())
- change.set_service_url(service_url);
- if (!xmpp_endpoint.empty())
- change.set_xmpp_endpoint(xmpp_endpoint);
- return true;
-}
-
void DeviceRegistrationInfo::UpdateCommand(
const std::string& command_id,
const base::DictionaryValue& command_patch,
diff --git a/src/device_registration_info.h b/src/device_registration_info.h
index a296258..ef79268 100644
--- a/src/device_registration_info.h
+++ b/src/device_registration_info.h
@@ -64,7 +64,8 @@ class DeviceRegistrationInfo : public NotificationDelegate,
void AddGcdStateChangedCallback(
const Device::GcdStateChangedCallback& callback);
- void RegisterDevice(const std::string& ticket_id,
+
+ void RegisterDevice(RegistrationData registration_data,
const DoneCallback& callback);
void UpdateDeviceInfo(const std::string& name,
@@ -73,13 +74,6 @@ class DeviceRegistrationInfo : public NotificationDelegate,
void UpdateBaseConfig(AuthScope anonymous_access_role,
bool local_discovery_enabled,
bool local_pairing_enabled);
- bool UpdateServiceConfig(const std::string& client_id,
- const std::string& client_secret,
- const std::string& api_key,
- const std::string& oauth_url,
- const std::string& service_url,
- const std::string& xmpp_endpoint,
- ErrorPtr* error);
void GetDeviceInfo(const CloudRequestDoneCallback& callback);
@@ -124,6 +118,8 @@ class DeviceRegistrationInfo : public NotificationDelegate,
private:
friend class DeviceRegistrationInfoTest;
+ const Config::Settings& GetDefaults() const { return config_->GetDefaults(); }
+
base::WeakPtr<DeviceRegistrationInfo> AsWeakPtr() {
return weak_factory_.GetWeakPtr();
}
@@ -276,15 +272,17 @@ class DeviceRegistrationInfo : public NotificationDelegate,
void RegisterDeviceError(const DoneCallback& callback, ErrorPtr error);
void RegisterDeviceOnTicketSent(
- const std::string& ticket_id,
+ const RegistrationData& registration_data,
const DoneCallback& callback,
std::unique_ptr<provider::HttpClient::Response> response,
ErrorPtr error);
void RegisterDeviceOnTicketFinalized(
+ const RegistrationData& registration_data,
const DoneCallback& callback,
std::unique_ptr<provider::HttpClient::Response> response,
ErrorPtr error);
void RegisterDeviceOnAuthCodeSent(
+ const RegistrationData& registration_data,
const std::string& cloud_id,
const std::string& robot_account,
const DoneCallback& callback,
diff --git a/src/device_registration_info_unittest.cc b/src/device_registration_info_unittest.cc
index bbc167e..d3d0c26 100644
--- a/src/device_registration_info_unittest.cc
+++ b/src/device_registration_info_unittest.cc
@@ -127,12 +127,12 @@ class DeviceRegistrationInfoTest : public ::testing::Test {
void SetUp() override {
EXPECT_CALL(clock_, Now())
.WillRepeatedly(Return(base::Time::FromTimeT(1450000000)));
- ReloadDefaults();
+ ReloadDefaults(true);
}
- void ReloadDefaults() {
+ void ReloadDefaults(bool allow_endpoints_override) {
EXPECT_CALL(config_store_, LoadDefaults(_))
- .WillOnce(Invoke([](Settings* settings) {
+ .WillOnce(Invoke([allow_endpoints_override](Settings* settings) {
settings->client_id = test_data::kClientId;
settings->client_secret = test_data::kClientSecret;
settings->api_key = test_data::kApiKey;
@@ -146,6 +146,7 @@ class DeviceRegistrationInfoTest : public ::testing::Test {
settings->oauth_url = test_data::kOAuthURL;
settings->service_url = test_data::kServiceURL;
settings->xmpp_endpoint = test_data::kXmppEndpoint;
+ settings->allow_endpoints_override = allow_endpoints_override;
return true;
}));
config_.reset(new Config{&config_store_});
@@ -155,7 +156,7 @@ class DeviceRegistrationInfoTest : public ::testing::Test {
dev_reg_->Start();
}
- void ReloadSettings(bool registered = true) {
+ void ReloadSettings(bool registered, bool allow_endpoints_override) {
base::DictionaryValue dict;
dict.SetInteger("version", 1);
if (registered) {
@@ -168,7 +169,7 @@ class DeviceRegistrationInfoTest : public ::testing::Test {
base::JSONWriter::WriteWithOptions(
dict, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json_string);
EXPECT_CALL(config_store_, LoadSettings()).WillOnce(Return(json_string));
- ReloadDefaults();
+ ReloadDefaults(allow_endpoints_override);
}
void PublishCommands(const base::ListValue& commands) {
@@ -196,6 +197,9 @@ class DeviceRegistrationInfoTest : public ::testing::Test {
return dev_reg_->HaveRegistrationCredentials();
}
+ void RegisterDevice(const RegistrationData registration_data,
+ const RegistrationData& expected_data);
+
provider::test::FakeTaskRunner task_runner_;
provider::test::MockConfigStore config_store_;
StrictMock<MockHttpClient> http_client_;
@@ -245,7 +249,7 @@ TEST_F(DeviceRegistrationInfoTest, GetOAuthURL) {
TEST_F(DeviceRegistrationInfoTest, HaveRegistrationCredentials) {
EXPECT_FALSE(HaveRegistrationCredentials());
- ReloadSettings();
+ ReloadSettings(true, false);
EXPECT_CALL(
http_client_,
@@ -287,7 +291,7 @@ TEST_F(DeviceRegistrationInfoTest, HaveRegistrationCredentials) {
}
TEST_F(DeviceRegistrationInfoTest, CheckAuthenticationFailure) {
- ReloadSettings();
+ ReloadSettings(true, false);
EXPECT_EQ(GcdState::kConnecting, GetGcdState());
EXPECT_CALL(
@@ -316,7 +320,7 @@ TEST_F(DeviceRegistrationInfoTest, CheckAuthenticationFailure) {
}
TEST_F(DeviceRegistrationInfoTest, CheckDeregistration) {
- ReloadSettings();
+ ReloadSettings(true, false);
EXPECT_EQ(GcdState::kConnecting, GetGcdState());
EXPECT_CALL(
@@ -346,7 +350,7 @@ TEST_F(DeviceRegistrationInfoTest, CheckDeregistration) {
}
TEST_F(DeviceRegistrationInfoTest, GetDeviceInfo) {
- ReloadSettings();
+ ReloadSettings(true, false);
SetAccessToken();
EXPECT_CALL(
@@ -377,9 +381,32 @@ TEST_F(DeviceRegistrationInfoTest, GetDeviceInfo) {
EXPECT_TRUE(succeeded);
}
-TEST_F(DeviceRegistrationInfoTest, RegisterDevice) {
- ReloadSettings(false);
+TEST_F(DeviceRegistrationInfoTest, ReRegisterDevice) {
+ ReloadSettings(true, false);
+
+ bool done = false;
+ dev_reg_->RegisterDevice(RegistrationData{test_data::kClaimTicketId},
+ base::Bind([this, &done](ErrorPtr error) {
+ EXPECT_TRUE(error->HasError("already_registered"));
+ done = true;
+ task_runner_.Break();
+ EXPECT_EQ(GcdState::kConnecting, GetGcdState());
+
+ // Validate the device info saved to storage...
+ EXPECT_EQ(test_data::kCloudId,
+ dev_reg_->GetSettings().cloud_id);
+ EXPECT_EQ(test_data::kRefreshToken,
+ dev_reg_->GetSettings().refresh_token);
+ EXPECT_EQ(test_data::kRobotAccountEmail,
+ dev_reg_->GetSettings().robot_account);
+ }));
+ task_runner_.Run();
+ EXPECT_TRUE(done);
+}
+void DeviceRegistrationInfoTest::RegisterDevice(
+ const RegistrationData registration_data,
+ const RegistrationData& expected_data) {
auto json_traits = CreateDictionaryValue(R"({
'base': {
'commands': {
@@ -408,25 +435,25 @@ TEST_F(DeviceRegistrationInfoTest, RegisterDevice) {
EXPECT_TRUE(component_manager_.SetStateProperty(
"comp", "base.firmwareVersion", ver, nullptr));
- std::string ticket_url = dev_reg_->GetServiceURL("registrationTickets/") +
- test_data::kClaimTicketId;
+ std::string ticket_url = expected_data.service_url + "registrationTickets/" +
+ expected_data.ticket_id;
EXPECT_CALL(http_client_,
SendRequest(HttpClient::Method::kPatch,
- ticket_url + "?key=" + test_data::kApiKey,
+ ticket_url + "?key=" + expected_data.api_key,
HttpClient::Headers{GetJsonHeader()}, _, _))
- .WillOnce(WithArgs<3, 4>(
- Invoke([](const std::string& data,
- const HttpClient::SendRequestCallback& callback) {
+ .WillOnce(WithArgs<3, 4>(Invoke(
+ [&expected_data](const std::string& data,
+ const HttpClient::SendRequestCallback& callback) {
auto json = test::CreateDictionaryValue(data);
EXPECT_NE(nullptr, json.get());
std::string value;
EXPECT_TRUE(json->GetString("id", &value));
- EXPECT_EQ(test_data::kClaimTicketId, value);
+ EXPECT_EQ(expected_data.ticket_id, value);
EXPECT_TRUE(
json->GetString("deviceDraft.channel.supportedType", &value));
EXPECT_EQ("pull", value);
EXPECT_TRUE(json->GetString("oauthClientId", &value));
- EXPECT_EQ(test_data::kClientId, value);
+ EXPECT_EQ(expected_data.client_id, value);
EXPECT_TRUE(json->GetString("deviceDraft.description", &value));
EXPECT_EQ("Easy to clean", value);
EXPECT_TRUE(json->GetString("deviceDraft.location", &value));
@@ -489,7 +516,7 @@ TEST_F(DeviceRegistrationInfoTest, RegisterDevice) {
EXPECT_CALL(http_client_,
SendRequest(HttpClient::Method::kPost,
- ticket_url + "/finalize?key=" + test_data::kApiKey,
+ ticket_url + "/finalize?key=" + expected_data.api_key,
HttpClient::Headers{}, _, _))
.WillOnce(WithArgs<4>(
Invoke([](const HttpClient::SendRequestCallback& callback) {
@@ -509,15 +536,15 @@ TEST_F(DeviceRegistrationInfoTest, RegisterDevice) {
EXPECT_CALL(
http_client_,
- SendRequest(HttpClient::Method::kPost, dev_reg_->GetOAuthURL("token"),
+ SendRequest(HttpClient::Method::kPost, expected_data.oauth_url + "token",
HttpClient::Headers{GetFormHeader()}, _, _))
- .WillOnce(WithArgs<3, 4>(Invoke([](
+ .WillOnce(WithArgs<3, 4>(Invoke([&expected_data](
const std::string& data,
const HttpClient::SendRequestCallback& callback) {
EXPECT_EQ("authorization_code", GetFormField(data, "grant_type"));
EXPECT_EQ(test_data::kRobotAccountAuthCode, GetFormField(data, "code"));
- EXPECT_EQ(test_data::kClientId, GetFormField(data, "client_id"));
- EXPECT_EQ(test_data::kClientSecret,
+ EXPECT_EQ(expected_data.client_id, GetFormField(data, "client_id"));
+ EXPECT_EQ(expected_data.client_secret,
GetFormField(data, "client_secret"));
EXPECT_EQ("oob", GetFormField(data, "redirect_uri"));
@@ -532,7 +559,9 @@ TEST_F(DeviceRegistrationInfoTest, RegisterDevice) {
EXPECT_CALL(
http_client_,
- SendRequest(HttpClient::Method::kPost, HasSubstr("upsertLocalAuthInfo"),
+ SendRequest(HttpClient::Method::kPost,
+ expected_data.service_url + "devices/" + test_data::kCloudId +
+ "/upsertLocalAuthInfo",
HttpClient::Headers{GetAuthHeader(), GetJsonHeader()}, _, _))
.WillOnce(WithArgs<3, 4>(
Invoke([](const std::string& data,
@@ -546,10 +575,12 @@ TEST_F(DeviceRegistrationInfoTest, RegisterDevice) {
bool done = false;
dev_reg_->RegisterDevice(
- test_data::kClaimTicketId, base::Bind([this, &done](ErrorPtr error) {
- EXPECT_FALSE(error);
+ registration_data,
+ base::Bind([this, &done, &expected_data](ErrorPtr error) {
done = true;
task_runner_.Break();
+
+ EXPECT_FALSE(error);
EXPECT_EQ(GcdState::kConnecting, GetGcdState());
// Validate the device info saved to storage...
@@ -558,29 +589,66 @@ TEST_F(DeviceRegistrationInfoTest, RegisterDevice) {
dev_reg_->GetSettings().refresh_token);
EXPECT_EQ(test_data::kRobotAccountEmail,
dev_reg_->GetSettings().robot_account);
+ EXPECT_EQ(expected_data.oauth_url, dev_reg_->GetSettings().oauth_url);
+ EXPECT_EQ(expected_data.client_id, dev_reg_->GetSettings().client_id);
+ EXPECT_EQ(expected_data.client_secret,
+ dev_reg_->GetSettings().client_secret);
+ EXPECT_EQ(expected_data.api_key, dev_reg_->GetSettings().api_key);
+ EXPECT_EQ(expected_data.service_url,
+ dev_reg_->GetSettings().service_url);
+ EXPECT_EQ(expected_data.xmpp_endpoint,
+ dev_reg_->GetSettings().xmpp_endpoint);
}));
task_runner_.Run();
EXPECT_TRUE(done);
}
-TEST_F(DeviceRegistrationInfoTest, ReRegisterDevice) {
- ReloadSettings();
+TEST_F(DeviceRegistrationInfoTest, RegisterDevice) {
+ ReloadSettings(false, true);
+
+ RegistrationData registration_data;
+ registration_data.ticket_id = "test_ticked_id";
+ registration_data.oauth_url = "https://test.oauth/";
+ registration_data.client_id = "test_client_id";
+ registration_data.client_secret = "test_client_secret";
+ registration_data.api_key = "test_api_key";
+ registration_data.service_url = "https://test.service/";
+ registration_data.xmpp_endpoint = "test.xmpp:1234";
+
+ RegisterDevice(registration_data, registration_data);
+}
- bool done = false;
- dev_reg_->RegisterDevice(
- test_data::kClaimTicketId, base::Bind([this, &done](ErrorPtr error) {
- EXPECT_TRUE(error->HasError("already_registered"));
- done = true;
- task_runner_.Break();
- EXPECT_EQ(GcdState::kConnecting, GetGcdState());
+TEST_F(DeviceRegistrationInfoTest, RegisterDeviceWithDefaultEndpoints) {
+ ReloadSettings(false, true);
- // Validate the device info saved to storage...
- EXPECT_EQ(test_data::kCloudId, dev_reg_->GetSettings().cloud_id);
- EXPECT_EQ(test_data::kRefreshToken,
- dev_reg_->GetSettings().refresh_token);
- EXPECT_EQ(test_data::kRobotAccountEmail,
- dev_reg_->GetSettings().robot_account);
- }));
+ RegistrationData registration_data;
+ registration_data.ticket_id = "test_ticked_id";
+
+ RegistrationData expected_data = registration_data;
+ expected_data.oauth_url = test_data::kOAuthURL;
+ expected_data.client_id = test_data::kClientId;
+ expected_data.client_secret = test_data::kClientSecret;
+ expected_data.api_key = test_data::kApiKey;
+ expected_data.service_url = test_data::kServiceURL;
+ expected_data.xmpp_endpoint = test_data::kXmppEndpoint;
+
+ RegisterDevice(registration_data, expected_data);
+}
+
+TEST_F(DeviceRegistrationInfoTest, RegisterDeviceEndpointsOverrideNotAllowed) {
+ ReloadSettings(false, false);
+
+ RegistrationData registration_data;
+ registration_data.ticket_id = "test_ticked_id";
+ registration_data.service_url = "https://test.service/";
+
+ bool done = false;
+ dev_reg_->RegisterDevice(registration_data,
+ base::Bind([this, &done](ErrorPtr error) {
+ done = true;
+ task_runner_.Break();
+ EXPECT_TRUE(error->HasError("invalidParams"));
+ }));
task_runner_.Run();
EXPECT_TRUE(done);
}
@@ -590,7 +658,7 @@ TEST_F(DeviceRegistrationInfoTest, OOBRegistrationStatus) {
// unregistered, depending on whether or not we've found credentials.
EXPECT_EQ(GcdState::kUnconfigured, GetGcdState());
// Put some credentials into our state, make sure we call that offline.
- ReloadSettings();
+ ReloadSettings(true, false);
EXPECT_EQ(GcdState::kConnecting, GetGcdState());
}
@@ -600,7 +668,7 @@ class DeviceRegistrationInfoUpdateCommandTest
void SetUp() override {
DeviceRegistrationInfoTest::SetUp();
- ReloadSettings();
+ ReloadSettings(true, false);
SetAccessToken();
auto json_traits = CreateDictionaryValue(R"({
diff --git a/src/privet/cloud_delegate.cc b/src/privet/cloud_delegate.cc
index 49fceaa..3266ef9 100644
--- a/src/privet/cloud_delegate.cc
+++ b/src/privet/cloud_delegate.cc
@@ -12,6 +12,7 @@
#include <base/memory/weak_ptr.h>
#include <base/values.h>
#include <weave/error.h>
+#include <weave/device.h>
#include <weave/provider/task_runner.h>
#include "src/backoff_entry.h"
@@ -109,15 +110,13 @@ class CloudDelegateImpl : public CloudDelegate {
const SetupState& GetSetupState() const override { return setup_state_; }
- bool Setup(const std::string& ticket_id,
- const std::string& user,
+ bool Setup(const RegistrationData& registration_data,
ErrorPtr* error) override {
- VLOG(1) << "GCD Setup started. ticket_id: " << ticket_id
- << ", user:" << user;
+ VLOG(1) << "GCD Setup started. ";
// Set (or reset) the retry counter, since we are starting a new
// registration process.
registation_retry_count_ = kMaxDeviceRegistrationRetries;
- ticket_id_ = ticket_id;
+ registration_data_ = registration_data;
if (setup_state_.IsStatusEqual(SetupState::kInProgress)) {
// Another registration is in progress. In case it fails, we will use
// the new ticket ID when retrying the request.
@@ -273,7 +272,7 @@ class CloudDelegateImpl : public CloudDelegate {
return;
}
- device_->RegisterDevice(ticket_id_,
+ device_->RegisterDevice(registration_data_,
base::Bind(&CloudDelegateImpl::RegisterDeviceDone,
setup_weak_factory_.GetWeakPtr()));
}
@@ -337,8 +336,8 @@ class CloudDelegateImpl : public CloudDelegate {
// State of the current or last setup.
SetupState setup_state_{SetupState::kNone};
- // Ticket ID for registering the device.
- std::string ticket_id_;
+ // Registration data for current registration process.
+ RegistrationData registration_data_;
// Number of remaining retries for device registration process.
int registation_retry_count_{0};
diff --git a/src/privet/cloud_delegate.h b/src/privet/cloud_delegate.h
index 9f053d8..01d2c30 100644
--- a/src/privet/cloud_delegate.h
+++ b/src/privet/cloud_delegate.h
@@ -12,6 +12,7 @@
#include <base/callback.h>
#include <base/memory/ref_counted.h>
#include <base/observer_list.h>
+#include <weave/device.h>
#include "src/privet/privet_types.h"
#include "src/privet/security_delegate.h"
@@ -88,8 +89,7 @@ class CloudDelegate {
virtual const SetupState& GetSetupState() const = 0;
// Starts GCD setup.
- virtual bool Setup(const std::string& ticket_id,
- const std::string& user,
+ virtual bool Setup(const RegistrationData& registration_data,
ErrorPtr* error) = 0;
// Returns cloud id if the registered device or empty string if unregistered.
diff --git a/src/privet/mock_delegates.h b/src/privet/mock_delegates.h
index c2e9a89..5b5bd2f 100644
--- a/src/privet/mock_delegates.h
+++ b/src/privet/mock_delegates.h
@@ -183,7 +183,7 @@ class MockCloudDelegate : public CloudDelegate {
MOCK_CONST_METHOD0(GetAnonymousMaxScope, AuthScope());
MOCK_CONST_METHOD0(GetConnectionState, const ConnectionState&());
MOCK_CONST_METHOD0(GetSetupState, const SetupState&());
- MOCK_METHOD3(Setup, bool(const std::string&, const std::string&, ErrorPtr*));
+ MOCK_METHOD2(Setup, bool(const RegistrationData&, ErrorPtr*));
MOCK_CONST_METHOD0(GetCloudId, std::string());
MOCK_CONST_METHOD0(GetLegacyState, const base::DictionaryValue&());
MOCK_CONST_METHOD0(GetLegacyCommandDef, const base::DictionaryValue&());
diff --git a/src/privet/privet_handler.cc b/src/privet/privet_handler.cc
index e8b1c77..517c60f 100644
--- a/src/privet/privet_handler.cc
+++ b/src/privet/privet_handler.cc
@@ -14,6 +14,7 @@
#include <base/location.h>
#include <base/strings/stringprintf.h>
#include <base/values.h>
+#include <weave/device.h>
#include <weave/enum_to_string.h>
#include <weave/provider/task_runner.h>
@@ -801,7 +802,7 @@ void PrivetHandler::HandleSetupStart(const base::DictionaryValue& input,
if (!ssid.empty() && !wifi_->ConfigureCredentials(ssid, passphrase, &error))
return ReturnError(*error, callback);
- if (!ticket.empty() && !cloud_->Setup(ticket, user, &error))
+ if (!ticket.empty() && !cloud_->Setup(RegistrationData{ticket}, &error))
return ReturnError(*error, callback);
ReplyWithSetupStatus(callback);
diff --git a/src/privet/privet_handler_unittest.cc b/src/privet/privet_handler_unittest.cc
index 20f5aa0..9e900df 100644
--- a/src/privet/privet_handler_unittest.cc
+++ b/src/privet/privet_handler_unittest.cc
@@ -15,6 +15,7 @@
#include <base/values.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
+#include <weave/device.h>
#include <weave/test/unittest_utils.h>
#include "src/privet/constants.h"
@@ -134,12 +135,11 @@ class PrivetHandlerTest : public testing::Test {
EXPECT_CALL(cloud_, GetCloudId()).WillRepeatedly(Return(""));
EXPECT_CALL(cloud_, GetConnectionState())
.WillRepeatedly(ReturnRef(gcd_disabled_state_));
- auto set_error = [](const std::string&, const std::string&,
- ErrorPtr* error) {
+ auto set_error = [](ErrorPtr* error) {
Error::AddTo(error, FROM_HERE, "setupUnavailable", "");
};
- EXPECT_CALL(cloud_, Setup(_, _, _))
- .WillRepeatedly(DoAll(Invoke(set_error), Return(false)));
+ EXPECT_CALL(cloud_, Setup(_, _))
+ .WillRepeatedly(DoAll(WithArgs<1>(Invoke(set_error)), Return(false)));
}
test::MockClock clock_;
@@ -638,10 +638,10 @@ TEST_F(PrivetHandlerSetupTest, GcdSetup) {
}
})";
- auto set_error = [](const std::string&, const std::string&, ErrorPtr* error) {
+ auto set_error = [](ErrorPtr* error) {
return Error::AddTo(error, FROM_HERE, "deviceBusy", "");
};
- EXPECT_CALL(cloud_, Setup(_, _, _)).WillOnce(Invoke(set_error));
+ EXPECT_CALL(cloud_, Setup(_, _)).WillOnce(WithArgs<1>(Invoke(set_error)));
EXPECT_PRED2(IsEqualError, CodeWithReason(503, "deviceBusy"),
HandleRequest("/privet/v3/setup/start", kInput));
@@ -651,7 +651,7 @@ TEST_F(PrivetHandlerSetupTest, GcdSetup) {
}
})";
cloud_.setup_state_ = SetupState{SetupState::kInProgress};
- EXPECT_CALL(cloud_, Setup("testTicket", "testUser", _))
+ EXPECT_CALL(cloud_, Setup(RegistrationData{"testTicket"}, _))
.WillOnce(Return(true));
EXPECT_JSON_EQ(kExpected, HandleRequest("/privet/v3/setup/start", kInput));
}
diff --git a/src/weave_unittest.cc b/src/weave_unittest.cc
index b300f57..452ac78 100644
--- a/src/weave_unittest.cc
+++ b/src/weave_unittest.cc
@@ -421,7 +421,8 @@ TEST_F(WeaveBasicTest, Register) {
InitDnsSdPublishing(true, "DB");
bool done = false;
- device_->Register("TICKET_ID", base::Bind([this, &done](ErrorPtr error) {
+ device_->Register(RegistrationData{"TICKET_ID"},
+ base::Bind([this, &done](ErrorPtr error) {
EXPECT_FALSE(error);
done = true;
task_runner_.Break();
@@ -431,7 +432,8 @@ TEST_F(WeaveBasicTest, Register) {
EXPECT_TRUE(done);
done = false;
- device_->Register("TICKET_ID2", base::Bind([this, &done](ErrorPtr error) {
+ device_->Register(RegistrationData{"TICKET_ID2"},
+ base::Bind([this, &done](ErrorPtr error) {
EXPECT_TRUE(error->HasError("already_registered"));
done = true;
task_runner_.Break();