blob: c60cf69b3835295f5ed1949617a9e613bcdeb6b9 [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
5#ifndef BUFFET_DEVICE_INFO_H_
6#define BUFFET_DEVICE_INFO_H_
7
Alex Vakulenko3cb466c2014-04-15 11:36:32 -07008#include <string>
9#include <map>
10#include <memory>
11
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070012#include <base/basictypes.h>
13#include <base/time/time.h>
14
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070015#include "buffet/data_encoding.h"
Alex Vakulenkoa3062c52014-04-21 17:05:51 -070016#include "buffet/http_transport.h"
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070017
18namespace base {
19 class Value;
20} // namespace base
21
22namespace buffet {
23
24// The DeviceRegistrationInfo class represents device registration information.
Alex Vakulenko8e34d392014-04-29 11:02:56 -070025class DeviceRegistrationInfo {
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070026 public:
Alex Vakulenko8e34d392014-04-29 11:02:56 -070027 // The device registration configuration storage interface.
28 struct StorageInterface {
29 virtual ~StorageInterface() = default;
30 // Load the device registration configuration from storage.
31 // If it fails (e.g. the storage container [file?] doesn't exist), then
32 // it returns empty unique_ptr (aka nullptr).
33 virtual std::unique_ptr<base::Value> Load() = 0;
34 // Save the device registration configuration to storage.
35 // If saved successfully, returns true. Could fail when writing to
36 // physical storage like file system for various reasons (out of disk space,
37 // access permissions, etc).
38 virtual bool Save(const base::Value* config) = 0;
39 };
40 // This is a helper class for unit testing.
41 class TestHelper;
42 // Default-constructed uses CURL HTTP transport.
43 DeviceRegistrationInfo();
44 // This constructor allows to pass in a custom HTTP transport
45 // (mainly for testing).
46 DeviceRegistrationInfo(std::shared_ptr<chromeos::http::Transport> transport,
47 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
58 // chromeos::data_encoding::WebParamsEncode() and appended to URL as a query
59 // 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 = {},
66 const chromeos::data_encoding::WebParamList& params = {}) const;
67
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 = {},
73 const chromeos::data_encoding::WebParamList& params = {}) const;
74
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 = {},
79 const chromeos::data_encoding::WebParamList& params = {}) const;
80
81 // Returns the registered device ID (GUID) or empty string if failed
82 std::string GetDeviceId();
83
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.
89 bool CheckRegistration();
90
91 // Gets the full device description JSON object, or nullptr if
92 // the device is not registered or communication failure.
93 std::unique_ptr<base::Value> GetDeviceInfo();
94
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.
99 std::string StartRegistration(
100 const std::map<std::string, std::shared_ptr<base::Value>>& params,
101 std::string* error_msg);
102
103 // Finalizes the device registration. If |user_auth_code| is provided, then
104 // the device record is populated with user email on user's behalf. Otherwise
105 // the user is responsible to issue a PATCH request to provide a valid
106 // email address before calling FinishRegistration.
107 bool FinishRegistration(const std::string& user_auth_code);
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.
114 bool ValidateAndRefreshAccessToken();
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 Vakulenkoa3062c52014-04-21 17:05:51 -0700139 // HTTP transport used for communications.
140 std::shared_ptr<chromeos::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 Vakulenkoa3062c52014-04-21 17:05:51 -0700143
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700144 friend class TestHelper;
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700145 DISALLOW_COPY_AND_ASSIGN(DeviceRegistrationInfo);
146};
147
148} // namespace buffet
149
150#endif // BUFFET_DEVICE_INFO_H_