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.
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/cryptauth/proto/cryptauth_api.pb.h"
11 #include "components/proximity_auth/switches.h"
12 #include "net/url_request/url_request_context_getter.h"
14 namespace proximity_auth
{
18 // Default URL of Google APIs endpoint hosting CryptAuth.
19 const char kDefaultCryptAuthHTTPHost
[] = "https://www.googleapis.com";
21 // URL subpath hosting the CryptAuth service.
22 const char kCryptAuthPath
[] = "cryptauth/v1/";
24 // URL subpaths for each CryptAuth API.
25 const char kGetMyDevicesPath
[] = "deviceSync/getmydevices";
26 const char kFindEligibleUnlockDevicesPath
[] =
27 "deviceSync/findeligibleunlockdevices";
28 const char kSendDeviceSyncTicklePath
[] = "deviceSync/senddevicesynctickle";
29 const char kToggleEasyUnlockPath
[] = "deviceSync/toggleeasyunlock";
30 const char kSetupEnrollmentPath
[] = "enrollment/setupenrollment";
31 const char kFinishEnrollmentPath
[] = "enrollment/finishenrollment";
33 // Query string of the API URL indicating that the response should be in a
34 // serialized protobuf format.
35 const char kQueryProtobuf
[] = "?alt=proto";
37 // Creates the full CryptAuth URL for endpoint to the API with |request_path|.
38 GURL
CreateRequestUrl(const std::string
& request_path
) {
39 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
40 GURL google_apis_url
=
41 GURL(command_line
->HasSwitch(switches::kCryptAuthHTTPHost
)
42 ? command_line
->GetSwitchValueASCII(switches::kCryptAuthHTTPHost
)
43 : kDefaultCryptAuthHTTPHost
);
44 return google_apis_url
.Resolve(kCryptAuthPath
+ request_path
+
50 CryptAuthClient::CryptAuthClient(
51 scoped_ptr
<CryptAuthAccessTokenFetcher
> access_token_fetcher
,
52 scoped_refptr
<net::URLRequestContextGetter
> url_request_context
)
53 : url_request_context_(url_request_context
),
54 access_token_fetcher_(access_token_fetcher
.Pass()),
55 weak_ptr_factory_(this) {
58 CryptAuthClient::~CryptAuthClient() {
61 void CryptAuthClient::GetMyDevices(
62 const cryptauth::GetMyDevicesRequest
& request
,
63 const GetMyDevicesCallback
& callback
,
64 const ErrorCallback
& error_callback
) {
65 MakeApiCall(kGetMyDevicesPath
, request
, callback
, error_callback
);
68 void CryptAuthClient::FindEligibleUnlockDevices(
69 const cryptauth::FindEligibleUnlockDevicesRequest
& request
,
70 const FindEligibleUnlockDevicesCallback
& callback
,
71 const ErrorCallback
& error_callback
) {
72 MakeApiCall(kFindEligibleUnlockDevicesPath
, request
, callback
,
76 void CryptAuthClient::SendDeviceSyncTickle(
77 const cryptauth::SendDeviceSyncTickleRequest
& request
,
78 const SendDeviceSyncTickleCallback
& callback
,
79 const ErrorCallback
& error_callback
) {
80 MakeApiCall(kSendDeviceSyncTicklePath
, request
, callback
, error_callback
);
83 void CryptAuthClient::ToggleEasyUnlock(
84 const cryptauth::ToggleEasyUnlockRequest
& request
,
85 const ToggleEasyUnlockCallback
& callback
,
86 const ErrorCallback
& error_callback
) {
87 MakeApiCall(kToggleEasyUnlockPath
, request
, callback
, error_callback
);
90 void CryptAuthClient::SetupEnrollment(
91 const cryptauth::SetupEnrollmentRequest
& request
,
92 const SetupEnrollmentCallback
& callback
,
93 const ErrorCallback
& error_callback
) {
94 MakeApiCall(kSetupEnrollmentPath
, request
, callback
, error_callback
);
97 void CryptAuthClient::FinishEnrollment(
98 const cryptauth::FinishEnrollmentRequest
& request
,
99 const FinishEnrollmentCallback
& callback
,
100 const ErrorCallback
& error_callback
) {
101 MakeApiCall(kFinishEnrollmentPath
, request
, callback
, error_callback
);
104 scoped_ptr
<CryptAuthApiCallFlow
> CryptAuthClient::CreateFlow(
105 const GURL
& request_url
) {
106 return make_scoped_ptr(new CryptAuthApiCallFlow(request_url
));
109 template <class RequestProto
, class ResponseProto
>
110 void CryptAuthClient::MakeApiCall(
111 const std::string
& request_path
,
112 const RequestProto
& request_proto
,
113 const base::Callback
<void(const ResponseProto
&)>& response_callback
,
114 const ErrorCallback
& error_callback
) {
117 "Client has been used for another request. Do not reuse.");
121 std::string serialized_request
;
122 if (!request_proto
.SerializeToString(&serialized_request
)) {
123 error_callback
.Run(std::string("Failed to serialize ") +
124 request_proto
.GetTypeName() + " proto.");
128 request_path_
= request_path
;
129 error_callback_
= error_callback
;
130 access_token_fetcher_
->FetchAccessToken(base::Bind(
131 &CryptAuthClient::OnAccessTokenFetched
<ResponseProto
>,
132 weak_ptr_factory_
.GetWeakPtr(), serialized_request
, response_callback
));
135 template <class ResponseProto
>
136 void CryptAuthClient::OnAccessTokenFetched(
137 const std::string
& serialized_request
,
138 const base::Callback
<void(const ResponseProto
&)>& response_callback
,
139 const std::string
& access_token
) {
140 if (access_token
.empty()) {
141 OnApiCallFailed("Failed to get a valid access token.");
145 flow_
= CreateFlow(CreateRequestUrl(request_path_
));
146 flow_
->Start(url_request_context_
.get(), access_token
, serialized_request
,
147 base::Bind(&CryptAuthClient::OnFlowSuccess
<ResponseProto
>,
148 weak_ptr_factory_
.GetWeakPtr(), response_callback
),
149 base::Bind(&CryptAuthClient::OnApiCallFailed
,
150 weak_ptr_factory_
.GetWeakPtr()));
153 template <class ResponseProto
>
154 void CryptAuthClient::OnFlowSuccess(
155 const base::Callback
<void(const ResponseProto
&)>& result_callback
,
156 const std::string
& serialized_response
) {
157 ResponseProto response
;
158 if (!response
.ParseFromString(serialized_response
)) {
159 OnApiCallFailed("Failed to parse response proto.");
162 result_callback
.Run(response
);
165 void CryptAuthClient::OnApiCallFailed(const std::string
& error_message
) {
166 error_callback_
.Run(error_message
);