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