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