Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "buffet/device_registration_info.h" |
| 6 | |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 7 | #include <memory> |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 8 | #include <utility> |
| 9 | #include <vector> |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 10 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 11 | #include <base/json/json_writer.h> |
| 12 | #include <base/values.h> |
Alex Vakulenko | e4879a2 | 2014-08-20 15:47:36 -0700 | [diff] [blame] | 13 | #include <chromeos/data_encoding.h> |
Alex Vakulenko | 3aeea1c | 2014-08-20 16:33:12 -0700 | [diff] [blame] | 14 | #include <chromeos/mime_utils.h> |
Alex Vakulenko | b8fc1df | 2014-08-20 15:38:07 -0700 | [diff] [blame] | 15 | #include <chromeos/string_utils.h> |
Alex Vakulenko | bd5b544 | 2014-08-20 16:16:34 -0700 | [diff] [blame] | 16 | #include <chromeos/url_utils.h> |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 17 | |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 18 | #include "buffet/commands/command_definition.h" |
| 19 | #include "buffet/commands/command_manager.h" |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 20 | #include "buffet/device_registration_storage_keys.h" |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 21 | #include "buffet/http_transport_curl.h" |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 22 | #include "buffet/http_utils.h" |
Alex Vakulenko | 5841c30 | 2014-07-23 10:49:49 -0700 | [diff] [blame] | 23 | #include "buffet/storage_impls.h" |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 24 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 25 | const char buffet::kErrorDomainOAuth2[] = "oauth2"; |
| 26 | const char buffet::kErrorDomainGCD[] = "gcd"; |
| 27 | const char buffet::kErrorDomainGCDServer[] = "gcd_server"; |
| 28 | const char buffet::kErrorDomainBuffet[] = "buffet"; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 29 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 30 | namespace buffet { |
| 31 | namespace storage_keys { |
| 32 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 33 | // Persistent keys |
| 34 | const char kClientId[] = "client_id"; |
| 35 | const char kClientSecret[] = "client_secret"; |
| 36 | const char kApiKey[] = "api_key"; |
| 37 | const char kRefreshToken[] = "refresh_token"; |
| 38 | const char kDeviceId[] = "device_id"; |
| 39 | const char kOAuthURL[] = "oauth_url"; |
| 40 | const char kServiceURL[] = "service_url"; |
| 41 | const char kRobotAccount[] = "robot_account"; |
| 42 | // Transient keys |
| 43 | const char kDeviceKind[] = "device_kind"; |
| 44 | const char kSystemName[] = "system_name"; |
| 45 | const char kDisplayName[] = "display_name"; |
| 46 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 47 | } // namespace storage_keys |
| 48 | } // namespace buffet |
| 49 | |
| 50 | namespace { |
| 51 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 52 | const base::FilePath::CharType kDeviceInfoFilePath[] = |
| 53 | FILE_PATH_LITERAL("/var/lib/buffet/device_reg_info"); |
| 54 | |
| 55 | bool GetParamValue( |
Alex Vakulenko | a904434 | 2014-08-23 19:31:27 -0700 | [diff] [blame] | 56 | const std::map<std::string, std::string>& params, |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 57 | const std::string& param_name, |
| 58 | std::string* param_value) { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 59 | auto p = params.find(param_name); |
| 60 | if (p == params.end()) |
| 61 | return false; |
| 62 | |
Alex Vakulenko | a904434 | 2014-08-23 19:31:27 -0700 | [diff] [blame] | 63 | *param_value = p->second; |
| 64 | return true; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | std::pair<std::string, std::string> BuildAuthHeader( |
| 68 | const std::string& access_token_type, |
| 69 | const std::string& access_token) { |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 70 | std::string authorization = |
Alex Vakulenko | b8fc1df | 2014-08-20 15:38:07 -0700 | [diff] [blame] | 71 | chromeos::string_utils::Join(' ', access_token_type, access_token); |
Alex Vakulenko | 96c84d3 | 2014-06-06 11:07:32 -0700 | [diff] [blame] | 72 | return {buffet::http::request_header::kAuthorization, authorization}; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | std::unique_ptr<base::DictionaryValue> ParseOAuthResponse( |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 76 | const buffet::http::Response* response, chromeos::ErrorPtr* error) { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 77 | int code = 0; |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 78 | auto resp = buffet::http::ParseJsonResponse(response, &code, error); |
| 79 | if (resp && code >= buffet::http::status_code::BadRequest) { |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 80 | if (error) { |
| 81 | std::string error_code, error_message; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 82 | if (resp->GetString("error", &error_code) && |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 83 | resp->GetString("error_description", &error_message)) { |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 84 | chromeos::Error::AddTo(error, buffet::kErrorDomainOAuth2, error_code, |
| 85 | error_message); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 86 | } else { |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 87 | chromeos::Error::AddTo(error, buffet::kErrorDomainOAuth2, |
| 88 | "unexpected_response", "Unexpected OAuth error"); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | return std::unique_ptr<base::DictionaryValue>(); |
| 92 | } |
| 93 | return resp; |
| 94 | } |
| 95 | |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 96 | inline void SetUnexpectedError(chromeos::ErrorPtr* error) { |
| 97 | chromeos::Error::AddTo(error, buffet::kErrorDomainGCD, "unexpected_response", |
| 98 | "Unexpected GCD error"); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 101 | void ParseGCDError(const base::DictionaryValue* json, |
| 102 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 103 | if (!error) |
| 104 | return; |
| 105 | |
| 106 | const base::Value* list_value = nullptr; |
| 107 | const base::ListValue* error_list = nullptr; |
| 108 | if (!json->Get("error.errors", &list_value) || |
| 109 | !list_value->GetAsList(&error_list)) { |
| 110 | SetUnexpectedError(error); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | for (size_t i = 0; i < error_list->GetSize(); i++) { |
| 115 | const base::Value* error_value = nullptr; |
| 116 | const base::DictionaryValue* error_object = nullptr; |
| 117 | if (!error_list->Get(i, &error_value) || |
| 118 | !error_value->GetAsDictionary(&error_object)) { |
| 119 | SetUnexpectedError(error); |
| 120 | continue; |
| 121 | } |
| 122 | std::string error_code, error_message; |
| 123 | if (error_object->GetString("reason", &error_code) && |
| 124 | error_object->GetString("message", &error_message)) { |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 125 | chromeos::Error::AddTo(error, buffet::kErrorDomainGCDServer, |
| 126 | error_code, error_message); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 127 | } else { |
| 128 | SetUnexpectedError(error); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
Alex Vakulenko | bda220a | 2014-04-18 15:25:44 -0700 | [diff] [blame] | 133 | std::string BuildURL(const std::string& url, |
| 134 | const std::vector<std::string>& subpaths, |
Alex Vakulenko | e4879a2 | 2014-08-20 15:47:36 -0700 | [diff] [blame] | 135 | const chromeos::data_encoding::WebParamList& params) { |
Alex Vakulenko | bd5b544 | 2014-08-20 16:16:34 -0700 | [diff] [blame] | 136 | std::string result = chromeos::url::CombineMultiple(url, subpaths); |
| 137 | return chromeos::url::AppendQueryParams(result, params); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 140 | } // anonymous namespace |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 141 | |
| 142 | namespace buffet { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 143 | |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame] | 144 | DeviceRegistrationInfo::DeviceRegistrationInfo( |
| 145 | const std::shared_ptr<CommandManager>& command_manager) |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 146 | : transport_(new http::curl::Transport()), |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 147 | // TODO(avakulenko): Figure out security implications of storing |
| 148 | // this data unencrypted. |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame] | 149 | storage_(new FileStorage(base::FilePath(kDeviceInfoFilePath))), |
| 150 | command_manager_(command_manager) { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | DeviceRegistrationInfo::DeviceRegistrationInfo( |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame] | 154 | const std::shared_ptr<CommandManager>& command_manager, |
| 155 | const std::shared_ptr<http::Transport>& transport, |
| 156 | const std::shared_ptr<StorageInterface>& storage) |
| 157 | : transport_(transport), |
| 158 | storage_(storage), |
| 159 | command_manager_(command_manager) { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 160 | } |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 161 | |
| 162 | std::pair<std::string, std::string> |
| 163 | DeviceRegistrationInfo::GetAuthorizationHeader() const { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 164 | return BuildAuthHeader("Bearer", access_token_); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | std::string DeviceRegistrationInfo::GetServiceURL( |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 168 | const std::string& subpath, |
Alex Vakulenko | e4879a2 | 2014-08-20 15:47:36 -0700 | [diff] [blame] | 169 | const chromeos::data_encoding::WebParamList& params) const { |
Alex Vakulenko | bda220a | 2014-04-18 15:25:44 -0700 | [diff] [blame] | 170 | return BuildURL(service_url_, {subpath}, params); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | std::string DeviceRegistrationInfo::GetDeviceURL( |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 174 | const std::string& subpath, |
Alex Vakulenko | e4879a2 | 2014-08-20 15:47:36 -0700 | [diff] [blame] | 175 | const chromeos::data_encoding::WebParamList& params) const { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 176 | CHECK(!device_id_.empty()) << "Must have a valid device ID"; |
Alex Vakulenko | bda220a | 2014-04-18 15:25:44 -0700 | [diff] [blame] | 177 | return BuildURL(service_url_, {"devices", device_id_, subpath}, params); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 180 | std::string DeviceRegistrationInfo::GetOAuthURL( |
| 181 | const std::string& subpath, |
Alex Vakulenko | e4879a2 | 2014-08-20 15:47:36 -0700 | [diff] [blame] | 182 | const chromeos::data_encoding::WebParamList& params) const { |
Alex Vakulenko | bda220a | 2014-04-18 15:25:44 -0700 | [diff] [blame] | 183 | return BuildURL(oauth_url_, {subpath}, params); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 186 | std::string DeviceRegistrationInfo::GetDeviceId(chromeos::ErrorPtr* error) { |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 187 | return CheckRegistration(error) ? device_id_ : std::string(); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | bool DeviceRegistrationInfo::Load() { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 191 | auto value = storage_->Load(); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 192 | const base::DictionaryValue* dict = nullptr; |
| 193 | if (!value || !value->GetAsDictionary(&dict)) |
| 194 | return false; |
| 195 | |
| 196 | // Get the values into temp variables first to make sure we can get |
| 197 | // all the data correctly before changing the state of this object. |
| 198 | std::string client_id; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 199 | if (!dict->GetString(storage_keys::kClientId, &client_id)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 200 | return false; |
| 201 | std::string client_secret; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 202 | if (!dict->GetString(storage_keys::kClientSecret, &client_secret)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 203 | return false; |
| 204 | std::string api_key; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 205 | if (!dict->GetString(storage_keys::kApiKey, &api_key)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 206 | return false; |
| 207 | std::string refresh_token; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 208 | if (!dict->GetString(storage_keys::kRefreshToken, &refresh_token)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 209 | return false; |
| 210 | std::string device_id; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 211 | if (!dict->GetString(storage_keys::kDeviceId, &device_id)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 212 | return false; |
| 213 | std::string oauth_url; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 214 | if (!dict->GetString(storage_keys::kOAuthURL, &oauth_url)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 215 | return false; |
| 216 | std::string service_url; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 217 | if (!dict->GetString(storage_keys::kServiceURL, &service_url)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 218 | return false; |
| 219 | std::string device_robot_account; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 220 | if (!dict->GetString(storage_keys::kRobotAccount, &device_robot_account)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 221 | return false; |
| 222 | |
| 223 | client_id_ = client_id; |
| 224 | client_secret_ = client_secret; |
| 225 | api_key_ = api_key; |
| 226 | refresh_token_ = refresh_token; |
| 227 | device_id_ = device_id; |
| 228 | oauth_url_ = oauth_url; |
| 229 | service_url_ = service_url; |
| 230 | device_robot_account_ = device_robot_account; |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | bool DeviceRegistrationInfo::Save() const { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 235 | base::DictionaryValue dict; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 236 | dict.SetString(storage_keys::kClientId, client_id_); |
| 237 | dict.SetString(storage_keys::kClientSecret, client_secret_); |
| 238 | dict.SetString(storage_keys::kApiKey, api_key_); |
| 239 | dict.SetString(storage_keys::kRefreshToken, refresh_token_); |
| 240 | dict.SetString(storage_keys::kDeviceId, device_id_); |
| 241 | dict.SetString(storage_keys::kOAuthURL, oauth_url_); |
| 242 | dict.SetString(storage_keys::kServiceURL, service_url_); |
| 243 | dict.SetString(storage_keys::kRobotAccount, device_robot_account_); |
| 244 | return storage_->Save(&dict); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 247 | bool DeviceRegistrationInfo::CheckRegistration(chromeos::ErrorPtr* error) { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 248 | LOG(INFO) << "Checking device registration record."; |
| 249 | if (refresh_token_.empty() || |
| 250 | device_id_.empty() || |
| 251 | device_robot_account_.empty()) { |
| 252 | LOG(INFO) << "No valid device registration record found."; |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 253 | chromeos::Error::AddTo(error, kErrorDomainGCD, "device_not_registered", |
| 254 | "No valid device registration record found"); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 255 | return false; |
| 256 | } |
| 257 | |
| 258 | LOG(INFO) << "Device registration record found."; |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 259 | return ValidateAndRefreshAccessToken(error); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 262 | bool DeviceRegistrationInfo::ValidateAndRefreshAccessToken( |
| 263 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 264 | LOG(INFO) << "Checking access token expiration."; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 265 | if (!access_token_.empty() && |
| 266 | !access_token_expiration_.is_null() && |
| 267 | access_token_expiration_ > base::Time::Now()) { |
| 268 | LOG(INFO) << "Access token is still valid."; |
| 269 | return true; |
| 270 | } |
| 271 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 272 | auto response = http::PostFormData(GetOAuthURL("token"), { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 273 | {"refresh_token", refresh_token_}, |
| 274 | {"client_id", client_id_}, |
| 275 | {"client_secret", client_secret_}, |
| 276 | {"grant_type", "refresh_token"}, |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 277 | }, transport_, error); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 278 | if (!response) |
| 279 | return false; |
| 280 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 281 | auto json = ParseOAuthResponse(response.get(), error); |
| 282 | if (!json) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 283 | return false; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 284 | |
| 285 | int expires_in = 0; |
| 286 | if (!json->GetString("access_token", &access_token_) || |
| 287 | !json->GetInteger("expires_in", &expires_in) || |
| 288 | access_token_.empty() || |
| 289 | expires_in <= 0) { |
| 290 | LOG(ERROR) << "Access token unavailable."; |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 291 | chromeos::Error::AddTo(error, kErrorDomainOAuth2, |
| 292 | "unexpected_server_response", |
| 293 | "Access token unavailable"); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 294 | return false; |
| 295 | } |
| 296 | |
| 297 | access_token_expiration_ = base::Time::Now() + |
| 298 | base::TimeDelta::FromSeconds(expires_in); |
| 299 | |
| 300 | LOG(INFO) << "Access token is refreshed for additional " << expires_in |
| 301 | << " seconds."; |
| 302 | return true; |
| 303 | } |
| 304 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 305 | std::unique_ptr<base::Value> DeviceRegistrationInfo::GetDeviceInfo( |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 306 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 307 | if (!CheckRegistration(error)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 308 | return std::unique_ptr<base::Value>(); |
| 309 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 310 | auto response = http::Get(GetDeviceURL(), |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 311 | {GetAuthorizationHeader()}, transport_, error); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 312 | int status_code = 0; |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 313 | std::unique_ptr<base::DictionaryValue> json = |
| 314 | http::ParseJsonResponse(response.get(), &status_code, error); |
| 315 | if (json) { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 316 | if (status_code >= http::status_code::BadRequest) { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 317 | LOG(WARNING) << "Failed to retrieve the device info. Response code = " |
| 318 | << status_code; |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 319 | ParseGCDError(json.get(), error); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 320 | return std::unique_ptr<base::Value>(); |
| 321 | } |
| 322 | } |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 323 | return std::unique_ptr<base::Value>(json.release()); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | bool CheckParam(const std::string& param_name, |
| 327 | const std::string& param_value, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 328 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 329 | if (!param_value.empty()) |
| 330 | return true; |
| 331 | |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 332 | chromeos::Error::AddToPrintf(error, kErrorDomainBuffet, "missing_parameter", |
| 333 | "Parameter %s not specified", |
| 334 | param_name.c_str()); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 335 | return false; |
| 336 | } |
| 337 | |
| 338 | std::string DeviceRegistrationInfo::StartRegistration( |
Alex Vakulenko | a904434 | 2014-08-23 19:31:27 -0700 | [diff] [blame] | 339 | const std::map<std::string, std::string>& params, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 340 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 341 | GetParamValue(params, storage_keys::kClientId, &client_id_); |
| 342 | GetParamValue(params, storage_keys::kClientSecret, &client_secret_); |
| 343 | GetParamValue(params, storage_keys::kApiKey, &api_key_); |
| 344 | GetParamValue(params, storage_keys::kDeviceId, &device_id_); |
| 345 | GetParamValue(params, storage_keys::kDeviceKind, &device_kind_); |
| 346 | GetParamValue(params, storage_keys::kSystemName, &system_name_); |
| 347 | GetParamValue(params, storage_keys::kDisplayName, &display_name_); |
| 348 | GetParamValue(params, storage_keys::kOAuthURL, &oauth_url_); |
| 349 | GetParamValue(params, storage_keys::kServiceURL, &service_url_); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 350 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 351 | if (!CheckParam(storage_keys::kClientId, client_id_, error)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 352 | return std::string(); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 353 | if (!CheckParam(storage_keys::kClientSecret, client_secret_, error)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 354 | return std::string(); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 355 | if (!CheckParam(storage_keys::kApiKey, api_key_, error)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 356 | return std::string(); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 357 | if (!CheckParam(storage_keys::kDeviceKind, device_kind_, error)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 358 | return std::string(); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 359 | if (!CheckParam(storage_keys::kSystemName, system_name_, error)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 360 | return std::string(); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 361 | if (!CheckParam(storage_keys::kOAuthURL, oauth_url_, error)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 362 | return std::string(); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 363 | if (!CheckParam(storage_keys::kServiceURL, service_url_, error)) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 364 | return std::string(); |
| 365 | |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 366 | std::unique_ptr<base::DictionaryValue> commands = |
| 367 | command_manager_->GetCommandDictionary().GetCommandsAsJson(true, error); |
| 368 | if (!commands) |
| 369 | return std::string(); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 370 | |
| 371 | base::DictionaryValue req_json; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 372 | req_json.SetString("oauthClientId", client_id_); |
| 373 | req_json.SetString("deviceDraft.deviceKind", device_kind_); |
| 374 | req_json.SetString("deviceDraft.systemName", system_name_); |
| 375 | req_json.SetString("deviceDraft.displayName", display_name_); |
| 376 | req_json.SetString("deviceDraft.channel.supportedType", "xmpp"); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 377 | req_json.Set("deviceDraft.commandDefs", commands.release()); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 378 | |
| 379 | std::string url = GetServiceURL("registrationTickets", {{"key", api_key_}}); |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 380 | auto resp_json = http::ParseJsonResponse( |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 381 | http::PostJson(url, &req_json, transport_, error).get(), nullptr, error); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 382 | if (!resp_json) |
| 383 | return std::string(); |
| 384 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 385 | if (!resp_json->GetString("id", &ticket_id_)) { |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 386 | chromeos::Error::AddTo(error, kErrorDomainGCD, "unexpected_response", |
| 387 | "Device ID missing"); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 388 | return std::string(); |
| 389 | } |
| 390 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 391 | std::string auth_url = GetOAuthURL("auth", { |
| 392 | {"scope", "https://www.googleapis.com/auth/clouddevices"}, |
| 393 | {"redirect_uri", "urn:ietf:wg:oauth:2.0:oob"}, |
| 394 | {"response_type", "code"}, |
| 395 | {"client_id", client_id_} |
| 396 | }); |
| 397 | |
| 398 | base::DictionaryValue json; |
| 399 | json.SetString("ticket_id", ticket_id_); |
| 400 | json.SetString("auth_url", auth_url); |
| 401 | |
| 402 | std::string ret; |
| 403 | base::JSONWriter::Write(&json, &ret); |
| 404 | return ret; |
| 405 | } |
| 406 | |
| 407 | bool DeviceRegistrationInfo::FinishRegistration( |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 408 | const std::string& user_auth_code, chromeos::ErrorPtr* error) { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 409 | if (ticket_id_.empty()) { |
| 410 | LOG(ERROR) << "Finish registration without ticket ID"; |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 411 | chromeos::Error::AddTo(error, kErrorDomainBuffet, |
| 412 | "registration_not_started", |
| 413 | "Device registration not started"); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 414 | return false; |
| 415 | } |
| 416 | |
| 417 | std::string url = GetServiceURL("registrationTickets/" + ticket_id_); |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 418 | std::unique_ptr<http::Response> response; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 419 | if (!user_auth_code.empty()) { |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 420 | response = http::PostFormData(GetOAuthURL("token"), { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 421 | {"code", user_auth_code}, |
| 422 | {"client_id", client_id_}, |
| 423 | {"client_secret", client_secret_}, |
| 424 | {"redirect_uri", "urn:ietf:wg:oauth:2.0:oob"}, |
| 425 | {"grant_type", "authorization_code"} |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 426 | }, transport_, error); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 427 | if (!response) |
| 428 | return false; |
| 429 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 430 | auto json_resp = ParseOAuthResponse(response.get(), error); |
| 431 | if (!json_resp) |
| 432 | return false; |
| 433 | |
| 434 | std::string user_access_token; |
| 435 | std::string token_type; |
| 436 | if (!json_resp->GetString("access_token", &user_access_token) || |
| 437 | !json_resp->GetString("token_type", &token_type)) { |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 438 | chromeos::Error::AddTo(error, kErrorDomainOAuth2, "unexpected_response", |
| 439 | "User access_token is missing in response"); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 440 | return false; |
| 441 | } |
| 442 | |
| 443 | base::DictionaryValue user_info; |
| 444 | user_info.SetString("userEmail", "me"); |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 445 | response = http::PatchJson( |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 446 | url, &user_info, {BuildAuthHeader(token_type, user_access_token)}, |
| 447 | transport_, error); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 448 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 449 | auto json = http::ParseJsonResponse(response.get(), nullptr, error); |
| 450 | if (!json) |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 451 | return false; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | std::string auth_code; |
| 455 | url += "/finalize?key=" + api_key_; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 456 | LOG(INFO) << "Sending request to: " << url; |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 457 | response = http::PostBinary(url, nullptr, 0, transport_, error); |
| 458 | if (!response) |
| 459 | return false; |
| 460 | auto json_resp = http::ParseJsonResponse(response.get(), nullptr, error); |
| 461 | if (!json_resp) |
| 462 | return false; |
| 463 | if (!response->IsSuccessful()) { |
| 464 | ParseGCDError(json_resp.get(), error); |
| 465 | return false; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 466 | } |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 467 | if (!json_resp->GetString("robotAccountEmail", &device_robot_account_) || |
| 468 | !json_resp->GetString("robotAccountAuthorizationCode", &auth_code) || |
| 469 | !json_resp->GetString("deviceDraft.id", &device_id_)) { |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 470 | chromeos::Error::AddTo(error, kErrorDomainGCD, "unexpected_response", |
| 471 | "Device account missing in response"); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 472 | return false; |
| 473 | } |
| 474 | |
| 475 | // Now get access_token and refresh_token |
| 476 | response = http::PostFormData(GetOAuthURL("token"), { |
| 477 | {"code", auth_code}, |
| 478 | {"client_id", client_id_}, |
| 479 | {"client_secret", client_secret_}, |
| 480 | {"redirect_uri", "oob"}, |
| 481 | {"scope", "https://www.googleapis.com/auth/clouddevices"}, |
| 482 | {"grant_type", "authorization_code"} |
| 483 | }, transport_, error); |
| 484 | if (!response) |
| 485 | return false; |
| 486 | |
| 487 | json_resp = ParseOAuthResponse(response.get(), error); |
| 488 | int expires_in = 0; |
| 489 | if (!json_resp || |
| 490 | !json_resp->GetString("access_token", &access_token_) || |
| 491 | !json_resp->GetString("refresh_token", &refresh_token_) || |
| 492 | !json_resp->GetInteger("expires_in", &expires_in) || |
| 493 | access_token_.empty() || |
| 494 | refresh_token_.empty() || |
| 495 | expires_in <= 0) { |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 496 | chromeos::Error::AddTo(error, kErrorDomainGCD, "unexpected_response", |
| 497 | "Device access_token missing in response"); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 498 | return false; |
| 499 | } |
| 500 | |
| 501 | access_token_expiration_ = base::Time::Now() + |
| 502 | base::TimeDelta::FromSeconds(expires_in); |
| 503 | |
| 504 | Save(); |
| 505 | return true; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | } // namespace buffet |