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

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

Issue 2502343003: Moved //components/proximity_auth/cryptauth to //components/cryptauth. (Closed)
Patch Set: Fixed proto #includes. Created 4 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 "components/proximity_auth/cryptauth/cryptauth_client_impl.h"
6
7 #include <utility>
8
9 #include "base/bind.h"
10 #include "base/command_line.h"
11 #include "base/memory/ptr_util.h"
12 #include "components/proximity_auth/cryptauth/cryptauth_access_token_fetcher_imp l.h"
13 #include "components/proximity_auth/cryptauth/switches.h"
14
15 namespace proximity_auth {
16
17 namespace {
18
19 // Default URL of Google APIs endpoint hosting CryptAuth.
20 const char kDefaultCryptAuthHTTPHost[] = "https://www.googleapis.com";
21
22 // URL subpath hosting the CryptAuth service.
23 const char kCryptAuthPath[] = "cryptauth/v1/";
24
25 // URL subpaths for each CryptAuth API.
26 const char kGetMyDevicesPath[] = "deviceSync/getmydevices";
27 const char kFindEligibleUnlockDevicesPath[] =
28 "deviceSync/findeligibleunlockdevices";
29 const char kSendDeviceSyncTicklePath[] = "deviceSync/senddevicesynctickle";
30 const char kToggleEasyUnlockPath[] = "deviceSync/toggleeasyunlock";
31 const char kSetupEnrollmentPath[] = "enrollment/setup";
32 const char kFinishEnrollmentPath[] = "enrollment/finish";
33
34 // Query string of the API URL indicating that the response should be in a
35 // serialized protobuf format.
36 const char kQueryProtobuf[] = "?alt=proto";
37
38 // Creates the full CryptAuth URL for endpoint to the API with |request_path|.
39 GURL CreateRequestUrl(const std::string& request_path) {
40 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
41 GURL google_apis_url =
42 GURL(command_line->HasSwitch(switches::kCryptAuthHTTPHost)
43 ? command_line->GetSwitchValueASCII(switches::kCryptAuthHTTPHost)
44 : kDefaultCryptAuthHTTPHost);
45 return google_apis_url.Resolve(kCryptAuthPath + request_path +
46 kQueryProtobuf);
47 }
48
49 } // namespace
50
51 CryptAuthClientImpl::CryptAuthClientImpl(
52 std::unique_ptr<CryptAuthApiCallFlow> api_call_flow,
53 std::unique_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher,
54 scoped_refptr<net::URLRequestContextGetter> url_request_context,
55 const cryptauth::DeviceClassifier& device_classifier)
56 : api_call_flow_(std::move(api_call_flow)),
57 access_token_fetcher_(std::move(access_token_fetcher)),
58 url_request_context_(url_request_context),
59 device_classifier_(device_classifier),
60 has_call_started_(false),
61 weak_ptr_factory_(this) {}
62
63 CryptAuthClientImpl::~CryptAuthClientImpl() {
64 }
65
66 void CryptAuthClientImpl::GetMyDevices(
67 const cryptauth::GetMyDevicesRequest& request,
68 const GetMyDevicesCallback& callback,
69 const ErrorCallback& error_callback) {
70 MakeApiCall(kGetMyDevicesPath, request, callback, error_callback);
71 }
72
73 void CryptAuthClientImpl::FindEligibleUnlockDevices(
74 const cryptauth::FindEligibleUnlockDevicesRequest& request,
75 const FindEligibleUnlockDevicesCallback& callback,
76 const ErrorCallback& error_callback) {
77 MakeApiCall(kFindEligibleUnlockDevicesPath, request, callback,
78 error_callback);
79 }
80
81 void CryptAuthClientImpl::SendDeviceSyncTickle(
82 const cryptauth::SendDeviceSyncTickleRequest& request,
83 const SendDeviceSyncTickleCallback& callback,
84 const ErrorCallback& error_callback) {
85 MakeApiCall(kSendDeviceSyncTicklePath, request, callback, error_callback);
86 }
87
88 void CryptAuthClientImpl::ToggleEasyUnlock(
89 const cryptauth::ToggleEasyUnlockRequest& request,
90 const ToggleEasyUnlockCallback& callback,
91 const ErrorCallback& error_callback) {
92 MakeApiCall(kToggleEasyUnlockPath, request, callback, error_callback);
93 }
94
95 void CryptAuthClientImpl::SetupEnrollment(
96 const cryptauth::SetupEnrollmentRequest& request,
97 const SetupEnrollmentCallback& callback,
98 const ErrorCallback& error_callback) {
99 MakeApiCall(kSetupEnrollmentPath, request, callback, error_callback);
100 }
101
102 void CryptAuthClientImpl::FinishEnrollment(
103 const cryptauth::FinishEnrollmentRequest& request,
104 const FinishEnrollmentCallback& callback,
105 const ErrorCallback& error_callback) {
106 MakeApiCall(kFinishEnrollmentPath, request, callback, error_callback);
107 }
108
109 std::string CryptAuthClientImpl::GetAccessTokenUsed() {
110 return access_token_used_;
111 }
112
113 template <class RequestProto, class ResponseProto>
114 void CryptAuthClientImpl::MakeApiCall(
115 const std::string& request_path,
116 const RequestProto& request_proto,
117 const base::Callback<void(const ResponseProto&)>& response_callback,
118 const ErrorCallback& error_callback) {
119 if (has_call_started_) {
120 error_callback.Run(
121 "Client has been used for another request. Do not reuse.");
122 return;
123 }
124 has_call_started_ = true;
125
126 // The |device_classifier| field must be present for all CryptAuth requests.
127 RequestProto request_copy(request_proto);
128 request_copy.mutable_device_classifier()->CopyFrom(device_classifier_);
129
130 std::string serialized_request;
131 if (!request_copy.SerializeToString(&serialized_request)) {
132 error_callback.Run(std::string("Failed to serialize ") +
133 request_proto.GetTypeName() + " proto.");
134 return;
135 }
136
137 request_path_ = request_path;
138 error_callback_ = error_callback;
139 access_token_fetcher_->FetchAccessToken(base::Bind(
140 &CryptAuthClientImpl::OnAccessTokenFetched<ResponseProto>,
141 weak_ptr_factory_.GetWeakPtr(), serialized_request, response_callback));
142 }
143
144 template <class ResponseProto>
145 void CryptAuthClientImpl::OnAccessTokenFetched(
146 const std::string& serialized_request,
147 const base::Callback<void(const ResponseProto&)>& response_callback,
148 const std::string& access_token) {
149 if (access_token.empty()) {
150 OnApiCallFailed("Failed to get a valid access token.");
151 return;
152 }
153 access_token_used_ = access_token;
154
155 api_call_flow_->Start(
156 CreateRequestUrl(request_path_), url_request_context_.get(), access_token,
157 serialized_request,
158 base::Bind(&CryptAuthClientImpl::OnFlowSuccess<ResponseProto>,
159 weak_ptr_factory_.GetWeakPtr(), response_callback),
160 base::Bind(&CryptAuthClientImpl::OnApiCallFailed,
161 weak_ptr_factory_.GetWeakPtr()));
162 }
163
164 template <class ResponseProto>
165 void CryptAuthClientImpl::OnFlowSuccess(
166 const base::Callback<void(const ResponseProto&)>& result_callback,
167 const std::string& serialized_response) {
168 ResponseProto response;
169 if (!response.ParseFromString(serialized_response)) {
170 OnApiCallFailed("Failed to parse response proto.");
171 return;
172 }
173 result_callback.Run(response);
174 };
175
176 void CryptAuthClientImpl::OnApiCallFailed(const std::string& error_message) {
177 error_callback_.Run(error_message);
178 }
179
180 // CryptAuthClientFactoryImpl
181 CryptAuthClientFactoryImpl::CryptAuthClientFactoryImpl(
182 OAuth2TokenService* token_service,
183 const std::string& account_id,
184 scoped_refptr<net::URLRequestContextGetter> url_request_context,
185 const cryptauth::DeviceClassifier& device_classifier)
186 : token_service_(token_service),
187 account_id_(account_id),
188 url_request_context_(url_request_context),
189 device_classifier_(device_classifier) {
190 }
191
192 CryptAuthClientFactoryImpl::~CryptAuthClientFactoryImpl() {
193 }
194
195 std::unique_ptr<CryptAuthClient> CryptAuthClientFactoryImpl::CreateInstance() {
196 return base::MakeUnique<CryptAuthClientImpl>(
197 base::WrapUnique(new CryptAuthApiCallFlow()),
198 base::WrapUnique(
199 new CryptAuthAccessTokenFetcherImpl(token_service_, account_id_)),
200 url_request_context_, device_classifier_);
201 }
202
203 } // proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698