blob: a1502537a957b4bb41ec410537b1860be46ba235 [file] [log] [blame]
Alex Vakulenko3cb466c2014-04-15 11:36:32 -07001// 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 Vakulenkob3aac252014-05-07 17:35:24 -07005#ifndef BUFFET_DEVICE_REGISTRATION_INFO_H_
6#define BUFFET_DEVICE_REGISTRATION_INFO_H_
Alex Vakulenko3cb466c2014-04-15 11:36:32 -07007
Alex Vakulenko3cb466c2014-04-15 11:36:32 -07008#include <map>
9#include <memory>
Alex Vakulenko5841c302014-07-23 10:49:49 -070010#include <string>
Alex Vakulenkob3aac252014-05-07 17:35:24 -070011#include <utility>
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070012
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070013#include <base/basictypes.h>
14#include <base/time/time.h>
15
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070016#include "buffet/data_encoding.h"
Alex Vakulenkob3aac252014-05-07 17:35:24 -070017#include "buffet/error.h"
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070018#include "buffet/http_transport.h"
Christopher Wiley006e94e2014-05-02 13:44:48 -070019#include "buffet/storage_interface.h"
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070020
21namespace base {
22 class Value;
23} // namespace base
24
25namespace buffet {
26
Alex Vakulenko1f30a622014-07-23 11:13:15 -070027class CommandManager;
28
Alex Vakulenkob3aac252014-05-07 17:35:24 -070029extern const char kErrorDomainOAuth2[];
30extern const char kErrorDomainGCD[];
31extern const char kErrorDomainGCDServer[];
32extern const char kErrorDomainBuffet[];
33
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070034// The DeviceRegistrationInfo class represents device registration information.
Alex Vakulenko8e34d392014-04-29 11:02:56 -070035class DeviceRegistrationInfo {
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070036 public:
Alex Vakulenko8e34d392014-04-29 11:02:56 -070037 // This is a helper class for unit testing.
38 class TestHelper;
Alex Vakulenko1f30a622014-07-23 11:13:15 -070039 // This constructor uses CURL HTTP transport.
40 explicit DeviceRegistrationInfo(
41 const std::shared_ptr<CommandManager>& command_manager);
Alex Vakulenko8e34d392014-04-29 11:02:56 -070042 // This constructor allows to pass in a custom HTTP transport
43 // (mainly for testing).
Alex Vakulenko1f30a622014-07-23 11:13:15 -070044 DeviceRegistrationInfo(const std::shared_ptr<CommandManager>& command_manager,
45 const std::shared_ptr<http::Transport>& transport,
46 const std::shared_ptr<StorageInterface>& storage);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070047
48 // Returns the authorization HTTP header that can be used to talk
49 // to GCD server for authenticated device communication.
Alex Vakulenko8e34d392014-04-29 11:02:56 -070050 // Make sure ValidateAndRefreshAccessToken() is called before this call.
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070051 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 Vakulenkoaf23b322014-05-08 16:25:45 -070057 // data_encoding::WebParamsEncode() and appended to URL as a query
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070058 // 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 Vakulenkoaf23b322014-05-08 16:25:45 -070065 const data_encoding::WebParamList& params = {}) const;
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070066
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 Vakulenkoaf23b322014-05-08 16:25:45 -070072 const data_encoding::WebParamList& params = {}) const;
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070073
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 Vakulenkoaf23b322014-05-08 16:25:45 -070078 const data_encoding::WebParamList& params = {}) const;
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070079
80 // Returns the registered device ID (GUID) or empty string if failed
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070081 std::string GetDeviceId(ErrorPtr* error);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070082
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 Vakulenkoaf23b322014-05-08 16:25:45 -070088 bool CheckRegistration(ErrorPtr* error);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070089
90 // Gets the full device description JSON object, or nullptr if
91 // the device is not registered or communication failure.
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070092 std::unique_ptr<base::Value> GetDeviceInfo(ErrorPtr* error);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070093
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 Vakulenkoaf23b322014-05-08 16:25:45 -0700100 ErrorPtr* error);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700101
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 Vakulenkob3aac252014-05-07 17:35:24 -0700106 bool FinishRegistration(const std::string& user_auth_code,
Alex Vakulenkoaf23b322014-05-08 16:25:45 -0700107 ErrorPtr* error);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700108
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 Vakulenkoaf23b322014-05-08 16:25:45 -0700114 bool ValidateAndRefreshAccessToken(ErrorPtr* error);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700115
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 Vakulenkoa3062c52014-04-21 17:05:51 -0700139 // HTTP transport used for communications.
Alex Vakulenkoaf23b322014-05-08 16:25:45 -0700140 std::shared_ptr<http::Transport> transport_;
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700141 // Serialization interface to save and load device registration info.
142 std::shared_ptr<StorageInterface> storage_;
Alex Vakulenko1f30a622014-07-23 11:13:15 -0700143 // Global command manager.
144 std::shared_ptr<CommandManager> command_manager_;
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700145
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700146 friend class TestHelper;
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700147 DISALLOW_COPY_AND_ASSIGN(DeviceRegistrationInfo);
148};
149
150} // namespace buffet
151
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700152#endif // BUFFET_DEVICE_REGISTRATION_INFO_H_