OLD | NEW |
---|---|
(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/cryptauth/proto/cryptauth_api.pb.h" | |
11 #include "components/proximity_auth/switches.h" | |
12 #include "net/url_request/url_request_context_getter.h" | |
13 | |
14 namespace proximity_auth { | |
15 | |
16 namespace { | |
17 | |
18 // Default URL of Google APIs endpoint hosting CryptAuth. | |
19 const char kDefaultCryptAuthHTTPHost[] = "https://www.googleapis.com"; | |
20 | |
21 // URL subpath hosting the CryptAuth service. | |
22 const char kCryptAuthPath[] = "cryptauth/v1/"; | |
23 | |
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"; | |
32 | |
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"; | |
36 | |
37 // Creates the full CryptAuth URL for endpoint to the API with |request_path|. | |
38 GURL CreateRequestUrl(const std::string& request_path) { | |
39 CommandLine* command_line = 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 + | |
45 kQueryProtobuf); | |
46 } | |
47 | |
48 } // namespace | |
49 | |
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) { | |
56 } | |
57 | |
58 CryptAuthClient::~CryptAuthClient() { | |
59 } | |
60 | |
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); | |
66 } | |
67 | |
68 void CryptAuthClient::FindEligibleUnlockDevices( | |
69 const cryptauth::FindEligibleUnlockDevicesRequest& request, | |
70 const FindEligibleUnlockDevicesCallback& callback, | |
71 const ErrorCallback& error_callback) { | |
72 MakeApiCall(kFindEligibleUnlockDevicesPath, request, callback, | |
73 error_callback); | |
74 } | |
75 | |
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); | |
81 } | |
82 | |
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); | |
88 } | |
89 | |
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); | |
95 } | |
96 | |
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); | |
102 } | |
103 | |
104 CryptAuthApiCallFlow* CryptAuthClient::CreateFlow(const GURL& request_url) { | |
105 return new CryptAuthApiCallFlow(request_url); | |
106 } | |
107 | |
108 template <class RequestProto, class ResponseProto> | |
109 void CryptAuthClient::MakeApiCall( | |
110 const std::string& request_path, | |
111 const RequestProto& request_proto, | |
112 const base::Callback<void(const ResponseProto&)>& response_callback, | |
113 const ErrorCallback& error_callback) { | |
114 if (flow_) { | |
115 error_callback.Run( | |
116 "Client has been used for another request. Do not reuse."); | |
117 return; | |
118 } | |
119 | |
120 std::string serialized_request; | |
121 if (!request_proto.SerializeToString(&serialized_request)) { | |
122 error_callback.Run(std::string("Failed to serialize ") + | |
123 request_proto.GetTypeName() + " proto."); | |
124 return; | |
125 } | |
126 | |
127 request_path_ = request_path; | |
128 error_callback_ = error_callback; | |
129 access_token_fetcher_->FetchAccessToken(base::Bind( | |
130 &CryptAuthClient::OnAccessTokenFetched<ResponseProto>, | |
131 weak_ptr_factory_.GetWeakPtr(), serialized_request, response_callback)); | |
132 } | |
133 | |
134 template <class ResponseProto> | |
135 void CryptAuthClient::OnAccessTokenFetched( | |
136 const std::string& serialized_request, | |
137 const base::Callback<void(const ResponseProto&)>& response_callback, | |
138 const std::string& access_token) { | |
139 if (access_token.empty()) { | |
140 OnApiCallFailed("Failed to get a valid access token"); | |
141 return; | |
142 } | |
143 | |
144 flow_.reset(CreateFlow(CreateRequestUrl(request_path_))); | |
145 flow_->Start(url_request_context_.get(), access_token, serialized_request, | |
146 base::Bind(&CryptAuthClient::OnFlowSuccess<ResponseProto>, | |
147 weak_ptr_factory_.GetWeakPtr(), response_callback), | |
148 base::Bind(&CryptAuthClient::OnApiCallFailed, | |
149 weak_ptr_factory_.GetWeakPtr())); | |
150 } | |
151 | |
152 template <class ResponseProto> | |
153 void CryptAuthClient::OnFlowSuccess( | |
154 const base::Callback<void(const ResponseProto&)>& result_callback, | |
155 const std::string& serialized_response) { | |
156 ResponseProto response; | |
157 if (!response.ParseFromString(serialized_response)) { | |
158 OnApiCallFailed("parse error"); | |
Ilya Sherman
2014/12/03 03:16:37
Please add test coverage for this code path.
Tim Song
2014/12/05 00:00:36
Done.
| |
159 return; | |
160 } | |
161 result_callback.Run(response); | |
162 }; | |
163 | |
164 void CryptAuthClient::OnApiCallFailed(const std::string& error_message) { | |
165 error_callback_.Run(error_message); | |
166 } | |
167 | |
168 } // proximity_auth | |
OLD | NEW |