blob: f48bae9de49a75dbeb80ab922f7a60e465691f70 [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"
Christopher Wiley006e94e2014-05-02 13:44:48 -070017#include "buffet/storage_interface.h"
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070018
19namespace base {
20 class Value;
21} // namespace base
22
23namespace buffet {
24
25// The DeviceRegistrationInfo class represents device registration information.
Alex Vakulenko8e34d392014-04-29 11:02:56 -070026class DeviceRegistrationInfo {
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070027 public:
Alex Vakulenko8e34d392014-04-29 11:02:56 -070028 // This is a helper class for unit testing.
29 class TestHelper;
30 // Default-constructed uses CURL HTTP transport.
31 DeviceRegistrationInfo();
32 // This constructor allows to pass in a custom HTTP transport
33 // (mainly for testing).
34 DeviceRegistrationInfo(std::shared_ptr<chromeos::http::Transport> transport,
35 std::shared_ptr<StorageInterface> storage);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070036
37 // Returns the authorization HTTP header that can be used to talk
38 // to GCD server for authenticated device communication.
Alex Vakulenko8e34d392014-04-29 11:02:56 -070039 // Make sure ValidateAndRefreshAccessToken() is called before this call.
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070040 std::pair<std::string, std::string> GetAuthorizationHeader() const;
41
42 // Returns the GCD service request URL. If |subpath| is specified, it is
43 // appended to the base URL which is normally
44 // https://www.googleapis.com/clouddevices/v1/".
45 // If |params| are specified, each key-value pair is formatted using
46 // chromeos::data_encoding::WebParamsEncode() and appended to URL as a query
47 // string.
48 // So, calling:
49 // GetServiceURL("ticket", {{"key","apiKey"}})
50 // will return something like:
51 // https://www.googleapis.com/clouddevices/v1/ticket?key=apiKey
52 std::string GetServiceURL(
53 const std::string& subpath = {},
54 const chromeos::data_encoding::WebParamList& params = {}) const;
55
56 // Returns a service URL to access the registered device on GCD server.
57 // The base URL used to construct the full URL looks like this:
58 // https://www.googleapis.com/clouddevices/v1/devices/<device_id>/
59 std::string GetDeviceURL(
60 const std::string& subpath = {},
61 const chromeos::data_encoding::WebParamList& params = {}) const;
62
63 // Similar to GetServiceURL, GetOAuthURL() returns a URL of OAuth 2.0 server.
64 // The base URL used is https://accounts.google.com/o/oauth2/.
65 std::string GetOAuthURL(
66 const std::string& subpath = {},
67 const chromeos::data_encoding::WebParamList& params = {}) const;
68
69 // Returns the registered device ID (GUID) or empty string if failed
70 std::string GetDeviceId();
71
72 // Loads the device registration information from cache.
73 bool Load();
74
75 // Checks for the valid device registration as well as refreshes
76 // the device access token, if available.
77 bool CheckRegistration();
78
79 // Gets the full device description JSON object, or nullptr if
80 // the device is not registered or communication failure.
81 std::unique_ptr<base::Value> GetDeviceInfo();
82
83 // Starts device registration procedure. |params| are a list of
84 // key-value pairs of device information, such as client_id, client_secret,
85 // and so on. If a particular key-value pair is omitted, a default value
86 // is used when possible. Returns a device claim ID on success.
87 std::string StartRegistration(
88 const std::map<std::string, std::shared_ptr<base::Value>>& params,
89 std::string* error_msg);
90
91 // Finalizes the device registration. If |user_auth_code| is provided, then
92 // the device record is populated with user email on user's behalf. Otherwise
93 // the user is responsible to issue a PATCH request to provide a valid
94 // email address before calling FinishRegistration.
95 bool FinishRegistration(const std::string& user_auth_code);
96
97 private:
98 // Saves the device registration to cache.
99 bool Save() const;
100
101 // Makes sure the access token is available and up-to-date.
102 bool ValidateAndRefreshAccessToken();
103
104 // Persistent data. Some of default values for testing purposes are used.
105 // TODO(avakulenko): remove these default values in the future.
106 // http://crbug.com/364692
107 std::string client_id_ =
108 "583509257718-lnmeofvjef3b1tm33sbjmckfnumfvn8j.apps.googleusercontent.com";
109 std::string client_secret_ = "6fzZwQhgnsHhvYYvvFdpv5SD";
110 std::string api_key_ = "AIzaSyAp7KVig5m9g4LWWKr79mTS8sXWfUU6w9g";
111 std::string refresh_token_;
112 std::string device_id_;
113 std::string device_robot_account_;
114 std::string oauth_url_ = "https://accounts.google.com/o/oauth2/";
115 std::string service_url_ =
116 "https://www-googleapis-staging.sandbox.google.com/"
117 "clouddevices/v1/";
118
119 // Transient data
120 std::string access_token_;
121 base::Time access_token_expiration_;
122 std::string ticket_id_;
123 std::string device_kind_ = "vendor";
124 std::string system_name_ = "coffee_pot";
125 std::string display_name_ = "Coffee Pot";
126
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700127 // HTTP transport used for communications.
128 std::shared_ptr<chromeos::http::Transport> transport_;
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700129 // Serialization interface to save and load device registration info.
130 std::shared_ptr<StorageInterface> storage_;
Alex Vakulenkoa3062c52014-04-21 17:05:51 -0700131
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700132 friend class TestHelper;
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700133 DISALLOW_COPY_AND_ASSIGN(DeviceRegistrationInfo);
134};
135
136} // namespace buffet
137
138#endif // BUFFET_DEVICE_INFO_H_