blob: da0d4097d9c519640eb674bcc474c59f7548fa9a [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 <string>
9#include <map>
10#include <memory>
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 Vakulenkob3aac252014-05-07 17:35:24 -070027extern const char kErrorDomainOAuth2[];
28extern const char kErrorDomainGCD[];
29extern const char kErrorDomainGCDServer[];
30extern const char kErrorDomainBuffet[];
31
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070032// The DeviceRegistrationInfo class represents device registration information.
Alex Vakulenko8e34d392014-04-29 11:02:56 -070033class DeviceRegistrationInfo {
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070034 public:
Alex Vakulenko8e34d392014-04-29 11:02:56 -070035 // This is a helper class for unit testing.
36 class TestHelper;
37 // Default-constructed uses CURL HTTP transport.
38 DeviceRegistrationInfo();
39 // This constructor allows to pass in a custom HTTP transport
40 // (mainly for testing).
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070041 DeviceRegistrationInfo(std::shared_ptr<http::Transport> transport,
Alex Vakulenko8e34d392014-04-29 11:02:56 -070042 std::shared_ptr<StorageInterface> storage);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070043
44 // Returns the authorization HTTP header that can be used to talk
45 // to GCD server for authenticated device communication.
Alex Vakulenko8e34d392014-04-29 11:02:56 -070046 // Make sure ValidateAndRefreshAccessToken() is called before this call.
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070047 std::pair<std::string, std::string> GetAuthorizationHeader() const;
48
49 // Returns the GCD service request URL. If |subpath| is specified, it is
50 // appended to the base URL which is normally
51 // https://www.googleapis.com/clouddevices/v1/".
52 // If |params| are specified, each key-value pair is formatted using
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070053 // data_encoding::WebParamsEncode() and appended to URL as a query
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070054 // string.
55 // So, calling:
56 // GetServiceURL("ticket", {{"key","apiKey"}})
57 // will return something like:
58 // https://www.googleapis.com/clouddevices/v1/ticket?key=apiKey
59 std::string GetServiceURL(
60 const std::string& subpath = {},
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070061 const data_encoding::WebParamList& params = {}) const;
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070062
63 // Returns a service URL to access the registered device on GCD server.
64 // The base URL used to construct the full URL looks like this:
65 // https://www.googleapis.com/clouddevices/v1/devices/<device_id>/
66 std::string GetDeviceURL(
67 const std::string& subpath = {},
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070068 const data_encoding::WebParamList& params = {}) const;
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070069
70 // Similar to GetServiceURL, GetOAuthURL() returns a URL of OAuth 2.0 server.
71 // The base URL used is https://accounts.google.com/o/oauth2/.
72 std::string GetOAuthURL(
73 const std::string& subpath = {},
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070074 const data_encoding::WebParamList& params = {}) const;
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070075
76 // Returns the registered device ID (GUID) or empty string if failed
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070077 std::string GetDeviceId(ErrorPtr* error);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070078
79 // Loads the device registration information from cache.
80 bool Load();
81
82 // Checks for the valid device registration as well as refreshes
83 // the device access token, if available.
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070084 bool CheckRegistration(ErrorPtr* error);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070085
86 // Gets the full device description JSON object, or nullptr if
87 // the device is not registered or communication failure.
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070088 std::unique_ptr<base::Value> GetDeviceInfo(ErrorPtr* error);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070089
90 // Starts device registration procedure. |params| are a list of
91 // key-value pairs of device information, such as client_id, client_secret,
92 // and so on. If a particular key-value pair is omitted, a default value
93 // is used when possible. Returns a device claim ID on success.
94 std::string StartRegistration(
95 const std::map<std::string, std::shared_ptr<base::Value>>& params,
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070096 ErrorPtr* error);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070097
98 // Finalizes the device registration. If |user_auth_code| is provided, then
99 // the device record is populated with user email on user's behalf. Otherwise
100 // the user is responsible to issue a PATCH request to provide a valid
101 // email address before calling FinishRegistration.
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700102 bool FinishRegistration(const std::string& user_auth_code,
Alex Vakulenkoaf23b322014-05-08 16:25:45 -0700103 ErrorPtr* error);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700104
105 private:
106 // Saves the device registration to cache.
107 bool Save() const;
108
109 // Makes sure the access token is available and up-to-date.
Alex Vakulenkoaf23b322014-05-08 16:25:45 -0700110 bool ValidateAndRefreshAccessToken(ErrorPtr* error);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700111
112 // Persistent data. Some of default values for testing purposes are used.
113 // TODO(avakulenko): remove these default values in the future.
114 // http://crbug.com/364692
115 std::string client_id_ =
116 "583509257718-lnmeofvjef3b1tm33sbjmckfnumfvn8j.apps.googleusercontent.com";
117 std::string client_secret_ = "6fzZwQhgnsHhvYYvvFdpv5SD";
118 std::string api_key_ = "AIzaSyAp7KVig5m9g4LWWKr79mTS8sXWfUU6w9g";
119 std::string refresh_token_;
120 std::string device_id_;
121 std::string device_robot_account_;
122 std::string oauth_url_ = "https://accounts.google.com/o/oauth2/";
123 std::string service_url_ =
124 "https://www-googleapis-staging.sandbox.google.com/"
125 "clouddevices/v1/";
126
127 // Transient data
128 std::string access_token_;
129 base::Time access_token_expiration_;
130 std::string ticket_id_;
131 std::string device_kind_ = "vendor";
132 std::string system_name_ = "coffee_pot";
133 std::string display_name_ = "Coffee Pot";
134
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700135 // HTTP transport used for communications.
Alex Vakulenkoaf23b322014-05-08 16:25:45 -0700136 std::shared_ptr<http::Transport> transport_;
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700137 // Serialization interface to save and load device registration info.
138 std::shared_ptr<StorageInterface> storage_;
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700139
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700140 friend class TestHelper;
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700141 DISALLOW_COPY_AND_ASSIGN(DeviceRegistrationInfo);
142};
143
144} // namespace buffet
145
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700146#endif // BUFFET_DEVICE_REGISTRATION_INFO_H_