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 | #ifndef BUFFET_DEVICE_INFO_H_ |
| 6 | #define BUFFET_DEVICE_INFO_H_ |
| 7 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <map> |
| 10 | #include <memory> |
| 11 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 12 | #include <base/basictypes.h> |
| 13 | #include <base/time/time.h> |
| 14 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 15 | #include "buffet/data_encoding.h" |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 16 | #include "buffet/http_transport.h" |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame^] | 17 | #include "buffet/storage_interface.h" |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 18 | |
| 19 | namespace base { |
| 20 | class Value; |
| 21 | } // namespace base |
| 22 | |
| 23 | namespace buffet { |
| 24 | |
| 25 | // The DeviceRegistrationInfo class represents device registration information. |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 26 | class DeviceRegistrationInfo { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 27 | public: |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 28 | // This is a helper class for unit testing. |
| 29 | class TestHelper; |
| 30 | // Default-constructed uses CURL HTTP transport. |
| 31 | DeviceRegistrationInfo(); |
| 32 | // This constructor allows to pass in a custom HTTP transport |
| 33 | // (mainly for testing). |
| 34 | DeviceRegistrationInfo(std::shared_ptr<chromeos::http::Transport> transport, |
| 35 | std::shared_ptr<StorageInterface> storage); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 36 | |
| 37 | // Returns the authorization HTTP header that can be used to talk |
| 38 | // to GCD server for authenticated device communication. |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 39 | // Make sure ValidateAndRefreshAccessToken() is called before this call. |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 40 | std::pair<std::string, std::string> GetAuthorizationHeader() const; |
| 41 | |
| 42 | // Returns the GCD service request URL. If |subpath| is specified, it is |
| 43 | // appended to the base URL which is normally |
| 44 | // https://www.googleapis.com/clouddevices/v1/". |
| 45 | // If |params| are specified, each key-value pair is formatted using |
| 46 | // chromeos::data_encoding::WebParamsEncode() and appended to URL as a query |
| 47 | // string. |
| 48 | // So, calling: |
| 49 | // GetServiceURL("ticket", {{"key","apiKey"}}) |
| 50 | // will return something like: |
| 51 | // https://www.googleapis.com/clouddevices/v1/ticket?key=apiKey |
| 52 | std::string GetServiceURL( |
| 53 | const std::string& subpath = {}, |
| 54 | const chromeos::data_encoding::WebParamList& params = {}) const; |
| 55 | |
| 56 | // Returns a service URL to access the registered device on GCD server. |
| 57 | // The base URL used to construct the full URL looks like this: |
| 58 | // https://www.googleapis.com/clouddevices/v1/devices/<device_id>/ |
| 59 | std::string GetDeviceURL( |
| 60 | const std::string& subpath = {}, |
| 61 | const chromeos::data_encoding::WebParamList& params = {}) const; |
| 62 | |
| 63 | // Similar to GetServiceURL, GetOAuthURL() returns a URL of OAuth 2.0 server. |
| 64 | // The base URL used is https://accounts.google.com/o/oauth2/. |
| 65 | std::string GetOAuthURL( |
| 66 | const std::string& subpath = {}, |
| 67 | const chromeos::data_encoding::WebParamList& params = {}) const; |
| 68 | |
| 69 | // Returns the registered device ID (GUID) or empty string if failed |
| 70 | std::string GetDeviceId(); |
| 71 | |
| 72 | // Loads the device registration information from cache. |
| 73 | bool Load(); |
| 74 | |
| 75 | // Checks for the valid device registration as well as refreshes |
| 76 | // the device access token, if available. |
| 77 | bool CheckRegistration(); |
| 78 | |
| 79 | // Gets the full device description JSON object, or nullptr if |
| 80 | // the device is not registered or communication failure. |
| 81 | std::unique_ptr<base::Value> GetDeviceInfo(); |
| 82 | |
| 83 | // Starts device registration procedure. |params| are a list of |
| 84 | // key-value pairs of device information, such as client_id, client_secret, |
| 85 | // and so on. If a particular key-value pair is omitted, a default value |
| 86 | // is used when possible. Returns a device claim ID on success. |
| 87 | std::string StartRegistration( |
| 88 | const std::map<std::string, std::shared_ptr<base::Value>>& params, |
| 89 | std::string* error_msg); |
| 90 | |
| 91 | // Finalizes the device registration. If |user_auth_code| is provided, then |
| 92 | // the device record is populated with user email on user's behalf. Otherwise |
| 93 | // the user is responsible to issue a PATCH request to provide a valid |
| 94 | // email address before calling FinishRegistration. |
| 95 | bool FinishRegistration(const std::string& user_auth_code); |
| 96 | |
| 97 | private: |
| 98 | // Saves the device registration to cache. |
| 99 | bool Save() const; |
| 100 | |
| 101 | // Makes sure the access token is available and up-to-date. |
| 102 | bool ValidateAndRefreshAccessToken(); |
| 103 | |
| 104 | // Persistent data. Some of default values for testing purposes are used. |
| 105 | // TODO(avakulenko): remove these default values in the future. |
| 106 | // http://crbug.com/364692 |
| 107 | std::string client_id_ = |
| 108 | "583509257718-lnmeofvjef3b1tm33sbjmckfnumfvn8j.apps.googleusercontent.com"; |
| 109 | std::string client_secret_ = "6fzZwQhgnsHhvYYvvFdpv5SD"; |
| 110 | std::string api_key_ = "AIzaSyAp7KVig5m9g4LWWKr79mTS8sXWfUU6w9g"; |
| 111 | std::string refresh_token_; |
| 112 | std::string device_id_; |
| 113 | std::string device_robot_account_; |
| 114 | std::string oauth_url_ = "https://accounts.google.com/o/oauth2/"; |
| 115 | std::string service_url_ = |
| 116 | "https://www-googleapis-staging.sandbox.google.com/" |
| 117 | "clouddevices/v1/"; |
| 118 | |
| 119 | // Transient data |
| 120 | std::string access_token_; |
| 121 | base::Time access_token_expiration_; |
| 122 | std::string ticket_id_; |
| 123 | std::string device_kind_ = "vendor"; |
| 124 | std::string system_name_ = "coffee_pot"; |
| 125 | std::string display_name_ = "Coffee Pot"; |
| 126 | |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 127 | // HTTP transport used for communications. |
| 128 | std::shared_ptr<chromeos::http::Transport> transport_; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 129 | // Serialization interface to save and load device registration info. |
| 130 | std::shared_ptr<StorageInterface> storage_; |
Alex Vakulenko | a3062c5 | 2014-04-21 17:05:51 -0700 | [diff] [blame] | 131 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 132 | friend class TestHelper; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 133 | DISALLOW_COPY_AND_ASSIGN(DeviceRegistrationInfo); |
| 134 | }; |
| 135 | |
| 136 | } // namespace buffet |
| 137 | |
| 138 | #endif // BUFFET_DEVICE_INFO_H_ |