Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Side by Side Diff: components/proximity_auth/cryptauth/cryptauth_client.cc

Issue 738593002: Introduce CryptAuthClient, a class capable of performing all CryptAuth APIs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium 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 #include "base/bind.h"
6 #include "base/command_line.h"
7 #include "components/proximity_auth/cryptauth/cryptauth_access_token_fetcher.h"
8 #include "components/proximity_auth/cryptauth/cryptauth_api_call_flow.h"
9 #include "components/proximity_auth/cryptauth/cryptauth_client.h"
10 #include "components/proximity_auth/switches.h"
11 #include "net/url_request/url_request_context_getter.h"
12
13 namespace proximity_auth {
14
15 namespace {
16
17 // Default rRL of Google APIs endpoint hosting CryptAuth.
Ilya Sherman 2014/11/18 22:30:44 nit: "rRL" -> "URL"
Tim Song 2014/12/03 01:18:23 Done.
18 const char kDefaultGoogleApisUrl[] = "https://www.googleapis.com";
Ilya Sherman 2014/11/18 22:30:44 nit: Perhaps "Url" -> "UrlPrefix"?
Tim Song 2014/12/03 01:18:24 Renamed to kDefaultCryptAuthHTTPHost.
19
20 // URL subpath hosting the CryptAuth service.
21 const char kCryptAuthPath[] = "cryptauth/v1/";
22
23 // URL subpaths for each CryptAuth API.
24 const char kGetMyDevicesPath[] = "deviceSync/getmydevices";
25 const char kFindEligibleUnlockDevicesPath[] =
26 "deviceSync/findeligibleunlockdevices";
27 const char kSendDeviceSyncTicklePath[] = "deviceSync/senddevicesynctickle";
28 const char kToggleEasyUnlockPath[] = "deviceSync/toggleeasyunlock";
29 const char kSetupEnrollmentPath[] = "enrollment/setupenrollment";
30 const char kFinishEnrollmentPath[] = "enrollment/finishenrollment";
31
32 // Query string of the API URL indicating that the response should be in a
33 // serialized protobuf format.
34 const char kQueryProtobuf[] = "?alt=proto";
35
36 // Creates the full CryptAuth URL for endpoint to the API with |request_path|.
37 GURL CreateRequestUrl(const std::string& request_path) {
38 CommandLine* command_line = CommandLine::ForCurrentProcess();
39 GURL google_apis_url = GURL(
40 command_line->HasSwitch(switches::kCryptAuthGoogleApisUrl)
41 ? command_line->GetSwitchValueASCII(switches::kCryptAuthGoogleApisUrl)
42 : kDefaultGoogleApisUrl);
43 return google_apis_url.Resolve(std::string(kCryptAuthPath) + request_path +
Ilya Sherman 2014/11/18 22:30:44 Optional nit: I don't think the explicit promotion
Tim Song 2014/12/03 01:18:24 Done.
44 kQueryProtobuf);
45 }
46
47 } // namespace
48
49 CryptAuthClient::CryptAuthClient(
50 net::URLRequestContextGetter* url_request_context,
51 CryptAuthAccessTokenFetcher* access_token_fetcher)
52 : url_request_context_(url_request_context),
53 access_token_fetcher_(access_token_fetcher),
54 weak_ptr_factory_(this) {
55 }
56
57 CryptAuthClient::~CryptAuthClient() {
58 }
59
60 template <class RequestProto, class ResponseProto>
61 void CryptAuthClient::MakeApiCall(
Ilya Sherman 2014/11/18 22:30:44 nit: Please move the methods so that they're defin
Tim Song 2014/12/03 01:18:24 Done.
62 const std::string& request_path,
63 const RequestProto& request_proto,
64 base::Callback<void(const ResponseProto&)> response_callback,
65 ErrorCallback error_callback) {
66 COMPILE_ASSERT(
67 (std::is_base_of<google::protobuf::MessageLite, RequestProto>::value),
68 request_is_not_proto);
69 COMPILE_ASSERT(
70 (std::is_base_of<google::protobuf::MessageLite, RequestProto>::value),
71 response_is_not_proto);
Ilya Sherman 2014/11/18 22:30:44 nit: Why not just let the compiler assert that the
Tim Song 2014/12/03 01:18:24 Done.
72
73 request_path_ = request_path;
74 error_callback_ = error_callback;
75 if (flow_) {
76 OnApiCallFailed("Another request in progress");
Ilya Sherman 2014/11/18 22:30:44 It looks like this wipes out the error_callback_,
Tim Song 2014/12/03 01:18:24 Nice catch. I made the interface such that CryptAu
77 return;
78 }
79
80 std::string serialized_request;
81 if (!request_proto.SerializeToString(&serialized_request)) {
82 OnApiCallFailed(std::string("Failed to serialize ") +
83 request_proto.GetTypeName() + " proto.");
84 return;
85 }
86
87 access_token_fetcher_->FetchAccessToken(base::Bind(
88 &CryptAuthClient::OnAccessTokenFetched<ResponseProto>,
89 weak_ptr_factory_.GetWeakPtr(), serialized_request, response_callback));
90 }
91
92 template <class ResponseProto>
93 void CryptAuthClient::OnAccessTokenFetched(
94 std::string serialized_request,
95 base::Callback<void(const ResponseProto&)> response_callback,
96 const std::string& access_token) {
97 if (access_token.empty()) {
98 OnApiCallFailed("Failed to get a valid access token");
99 return;
100 }
101
102 flow_.reset(CreateFlow(CreateRequestUrl(request_path_)));
103 flow_->Start(url_request_context_.get(), access_token, serialized_request,
104 base::Bind(&CryptAuthClient::OnFlowSuccess<ResponseProto>,
105 weak_ptr_factory_.GetWeakPtr(), response_callback),
106 base::Bind(&CryptAuthClient::OnApiCallFailed,
107 weak_ptr_factory_.GetWeakPtr()));
108 }
109
110 template <class ResponseProto>
111 void CryptAuthClient::OnFlowSuccess(
112 base::Callback<void(const ResponseProto&)> result_callback,
113 const std::string& serialized_response) {
114 flow_.reset();
115 ResponseProto response;
116 if (!response.ParseFromString(serialized_response)) {
117 OnApiCallFailed("parse error");
118 return;
119 }
120 result_callback.Run(response);
121 };
122
123 void CryptAuthClient::OnApiCallFailed(const std::string& error_message) {
124 flow_.reset();
125 error_callback_.Run(error_message);
126 }
127
128 CryptAuthApiCallFlow* CryptAuthClient::CreateFlow(GURL request_url) {
129 return new CryptAuthApiCallFlow(request_url);
Ilya Sherman 2014/11/18 22:30:43 nit: Seems like this could just be inlined.
Tim Song 2014/12/03 01:18:24 I'm overriding this function in the test to inject
130 }
131
132 void CryptAuthClient::GetMyDevices(bool allow_stale_read,
133 GetMyDevicesCallback callback,
134 ErrorCallback error_callback) {
135 cryptauth::GetMyDevicesRequest request;
136 request.set_approved_for_unlock_required(false);
137 request.set_allow_stale_read(allow_stale_read);
138 MakeApiCall<cryptauth::GetMyDevicesRequest, cryptauth::GetMyDevicesResponse>(
139 kGetMyDevicesPath, request, callback, error_callback);
140 }
141
142 void CryptAuthClient::FindEligibleUnlockDevices(
143 const std::string& bluetooth_address,
144 FindEligibleUnlockDevicesCallback callback,
145 ErrorCallback error_callback) {
146 cryptauth::FindEligibleUnlockDevicesRequest request;
147 request.set_callback_bluetooth_address(bluetooth_address);
148 MakeApiCall<cryptauth::FindEligibleUnlockDevicesRequest,
149 cryptauth::FindEligibleUnlockDevicesResponse>(
Ilya Sherman 2014/11/18 22:30:43 nit: I don't think that you have to specify the ty
Tim Song 2014/12/03 01:18:24 Done.
150 kFindEligibleUnlockDevicesPath, request, callback, error_callback);
151 }
152
153 void CryptAuthClient::SendDeviceSyncTickle(
154 SendDeviceSyncTickleCallback callback,
155 ErrorCallback error_callback) {
156 cryptauth::SendDeviceSyncTickleRequest request;
157 MakeApiCall<cryptauth::SendDeviceSyncTickleRequest,
158 cryptauth::SendDeviceSyncTickleResponse>(
159 kSendDeviceSyncTicklePath, request, callback, error_callback);
160 }
161
162 void CryptAuthClient::ToggleEasyUnlock(bool turn_on,
163 bool apply_to_all,
164 const std::string public_key,
165 ToggleEasyUnlockCallback callback,
166 ErrorCallback error_callback) {
167 cryptauth::ToggleEasyUnlockRequest request;
168 request.set_public_key(public_key);
169 request.set_enable(turn_on);
170 request.set_apply_to_all(apply_to_all);
171
172 MakeApiCall<cryptauth::ToggleEasyUnlockRequest,
173 cryptauth::ToggleEasyUnlockResponse>(
174 kToggleEasyUnlockPath, request, callback, error_callback);
175 }
176
177 void CryptAuthClient::SetupEnrollment(
178 const std::string application_id,
179 const std::vector<std::string>& supported_protocols,
180 SetupEnrollmentCallback callback,
181 ErrorCallback error_callback) {
182 cryptauth::SetupEnrollmentRequest request;
183 request.set_application_id(application_id);
184 for (auto type : supported_protocols)
185 request.add_types(type);
186
187 MakeApiCall<cryptauth::SetupEnrollmentRequest,
188 cryptauth::SetupEnrollmentResponse>(kSetupEnrollmentPath, request,
189 callback, error_callback);
190 }
191
192 void CryptAuthClient::FinishEnrollment(const std::string& enrollment_session_id,
193 const std::string& enrollment_message,
194 const std::string& device_ephermeral_key,
195 FinishEnrollmentCallback callback,
196 ErrorCallback error_callback) {
197 cryptauth::FinishEnrollmentRequest request;
198 request.set_enrollment_session_id(enrollment_session_id);
199 request.set_enrollment_message(enrollment_message);
200 request.set_device_ephemeral_key(device_ephermeral_key);
201
202 MakeApiCall<cryptauth::FinishEnrollmentRequest,
203 cryptauth::FinishEnrollmentResponse>(
204 kFinishEnrollmentPath, request, callback, error_callback);
205 }
206
207 } // proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698