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