blob: 3e6f4c61ff22f82b0057487a55f65820879d1deb [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>
Alex Vakulenkoe4879a22014-08-20 15:47:36 -070015#include <chromeos/data_encoding.h>
Alex Vakulenkoa8b95bc2014-08-27 11:00:57 -070016#include <chromeos/errors/error.h>
17#include <chromeos/http/http_transport.h>
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070018
Christopher Wiley006e94e2014-05-02 13:44:48 -070019#include "buffet/storage_interface.h"
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070020
21namespace base {
Alex Vakulenko5a9e7182014-08-11 15:59:58 -070022class Value;
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070023} // 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 Vakulenkocca20932014-08-20 17:35:12 -070044 DeviceRegistrationInfo(
45 const std::shared_ptr<CommandManager>& command_manager,
46 const std::shared_ptr<chromeos::http::Transport>& transport,
47 const std::shared_ptr<StorageInterface>& storage);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070048
49 // Returns the authorization HTTP header that can be used to talk
50 // to GCD server for authenticated device communication.
Alex Vakulenko8e34d392014-04-29 11:02:56 -070051 // Make sure ValidateAndRefreshAccessToken() is called before this call.
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070052 std::pair<std::string, std::string> GetAuthorizationHeader() const;
53
54 // Returns the GCD service request URL. If |subpath| is specified, it is
55 // appended to the base URL which is normally
56 // https://www.googleapis.com/clouddevices/v1/".
57 // If |params| are specified, each key-value pair is formatted using
Alex Vakulenkoe4879a22014-08-20 15:47:36 -070058 // chromeos::data_encoding::WebParamsEncode() and appended to URL as a query
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070059 // string.
60 // So, calling:
61 // GetServiceURL("ticket", {{"key","apiKey"}})
62 // will return something like:
63 // https://www.googleapis.com/clouddevices/v1/ticket?key=apiKey
64 std::string GetServiceURL(
65 const std::string& subpath = {},
Alex Vakulenkoe4879a22014-08-20 15:47:36 -070066 const chromeos::data_encoding::WebParamList& params = {}) const;
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070067
68 // Returns a service URL to access the registered device on GCD server.
69 // The base URL used to construct the full URL looks like this:
70 // https://www.googleapis.com/clouddevices/v1/devices/<device_id>/
71 std::string GetDeviceURL(
72 const std::string& subpath = {},
Alex Vakulenkoe4879a22014-08-20 15:47:36 -070073 const chromeos::data_encoding::WebParamList& params = {}) const;
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070074
75 // Similar to GetServiceURL, GetOAuthURL() returns a URL of OAuth 2.0 server.
76 // The base URL used is https://accounts.google.com/o/oauth2/.
77 std::string GetOAuthURL(
78 const std::string& subpath = {},
Alex Vakulenkoe4879a22014-08-20 15:47:36 -070079 const chromeos::data_encoding::WebParamList& params = {}) const;
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070080
81 // Returns the registered device ID (GUID) or empty string if failed
Alex Vakulenko5f472062014-08-14 17:54:04 -070082 std::string GetDeviceId(chromeos::ErrorPtr* error);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070083
84 // Loads the device registration information from cache.
85 bool Load();
86
87 // Checks for the valid device registration as well as refreshes
88 // the device access token, if available.
Alex Vakulenko5f472062014-08-14 17:54:04 -070089 bool CheckRegistration(chromeos::ErrorPtr* error);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070090
91 // Gets the full device description JSON object, or nullptr if
92 // the device is not registered or communication failure.
Alex Vakulenko5f472062014-08-14 17:54:04 -070093 std::unique_ptr<base::Value> GetDeviceInfo(chromeos::ErrorPtr* error);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070094
95 // Starts device registration procedure. |params| are a list of
96 // key-value pairs of device information, such as client_id, client_secret,
97 // and so on. If a particular key-value pair is omitted, a default value
98 // is used when possible. Returns a device claim ID on success.
Alex Vakulenkoa9044342014-08-23 19:31:27 -070099 // The values are all strings for now.
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700100 std::string StartRegistration(
Alex Vakulenkoa9044342014-08-23 19:31:27 -0700101 const std::map<std::string, std::string>& params,
Alex Vakulenko5f472062014-08-14 17:54:04 -0700102 chromeos::ErrorPtr* error);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700103
104 // Finalizes the device registration. If |user_auth_code| is provided, then
105 // the device record is populated with user email on user's behalf. Otherwise
106 // the user is responsible to issue a PATCH request to provide a valid
107 // email address before calling FinishRegistration.
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700108 bool FinishRegistration(const std::string& user_auth_code,
Alex Vakulenko5f472062014-08-14 17:54:04 -0700109 chromeos::ErrorPtr* error);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700110
111 private:
112 // Saves the device registration to cache.
113 bool Save() const;
114
115 // Makes sure the access token is available and up-to-date.
Alex Vakulenko5f472062014-08-14 17:54:04 -0700116 bool ValidateAndRefreshAccessToken(chromeos::ErrorPtr* error);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700117
118 // Persistent data. Some of default values for testing purposes are used.
119 // TODO(avakulenko): remove these default values in the future.
120 // http://crbug.com/364692
121 std::string client_id_ =
122 "583509257718-lnmeofvjef3b1tm33sbjmckfnumfvn8j.apps.googleusercontent.com";
123 std::string client_secret_ = "6fzZwQhgnsHhvYYvvFdpv5SD";
124 std::string api_key_ = "AIzaSyAp7KVig5m9g4LWWKr79mTS8sXWfUU6w9g";
125 std::string refresh_token_;
126 std::string device_id_;
127 std::string device_robot_account_;
128 std::string oauth_url_ = "https://accounts.google.com/o/oauth2/";
129 std::string service_url_ =
130 "https://www-googleapis-staging.sandbox.google.com/"
131 "clouddevices/v1/";
132
133 // Transient data
134 std::string access_token_;
135 base::Time access_token_expiration_;
136 std::string ticket_id_;
137 std::string device_kind_ = "vendor";
138 std::string system_name_ = "coffee_pot";
139 std::string display_name_ = "Coffee Pot";
140
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700141 // HTTP transport used for communications.
Alex Vakulenkocca20932014-08-20 17:35:12 -0700142 std::shared_ptr<chromeos::http::Transport> transport_;
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700143 // Serialization interface to save and load device registration info.
144 std::shared_ptr<StorageInterface> storage_;
Alex Vakulenko1f30a622014-07-23 11:13:15 -0700145 // Global command manager.
146 std::shared_ptr<CommandManager> command_manager_;
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700147
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700148 friend class TestHelper;
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700149 DISALLOW_COPY_AND_ASSIGN(DeviceRegistrationInfo);
150};
151
152} // namespace buffet
153
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700154#endif // BUFFET_DEVICE_REGISTRATION_INFO_H_