Chromium Code Reviews| Index: components/proximity_auth/cryptauth/cryptauth_client.cc |
| diff --git a/components/proximity_auth/cryptauth/cryptauth_client.cc b/components/proximity_auth/cryptauth/cryptauth_client.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cdba4a1cc8f9891062bd170f34da5f8d48ba9dbc |
| --- /dev/null |
| +++ b/components/proximity_auth/cryptauth/cryptauth_client.cc |
| @@ -0,0 +1,207 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/bind.h" |
| +#include "base/command_line.h" |
| +#include "components/proximity_auth/cryptauth/cryptauth_access_token_fetcher.h" |
| +#include "components/proximity_auth/cryptauth/cryptauth_api_call_flow.h" |
| +#include "components/proximity_auth/cryptauth/cryptauth_client.h" |
| +#include "components/proximity_auth/switches.h" |
| +#include "net/url_request/url_request_context_getter.h" |
| + |
| +namespace proximity_auth { |
| + |
| +namespace { |
| + |
| +// 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.
|
| +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.
|
| + |
| +// URL subpath hosting the CryptAuth service. |
| +const char kCryptAuthPath[] = "cryptauth/v1/"; |
| + |
| +// URL subpaths for each CryptAuth API. |
| +const char kGetMyDevicesPath[] = "deviceSync/getmydevices"; |
| +const char kFindEligibleUnlockDevicesPath[] = |
| + "deviceSync/findeligibleunlockdevices"; |
| +const char kSendDeviceSyncTicklePath[] = "deviceSync/senddevicesynctickle"; |
| +const char kToggleEasyUnlockPath[] = "deviceSync/toggleeasyunlock"; |
| +const char kSetupEnrollmentPath[] = "enrollment/setupenrollment"; |
| +const char kFinishEnrollmentPath[] = "enrollment/finishenrollment"; |
| + |
| +// Query string of the API URL indicating that the response should be in a |
| +// serialized protobuf format. |
| +const char kQueryProtobuf[] = "?alt=proto"; |
| + |
| +// Creates the full CryptAuth URL for endpoint to the API with |request_path|. |
| +GURL CreateRequestUrl(const std::string& request_path) { |
| + CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| + GURL google_apis_url = GURL( |
| + command_line->HasSwitch(switches::kCryptAuthGoogleApisUrl) |
| + ? command_line->GetSwitchValueASCII(switches::kCryptAuthGoogleApisUrl) |
| + : kDefaultGoogleApisUrl); |
| + 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.
|
| + kQueryProtobuf); |
| +} |
| + |
| +} // namespace |
| + |
| +CryptAuthClient::CryptAuthClient( |
| + net::URLRequestContextGetter* url_request_context, |
| + CryptAuthAccessTokenFetcher* access_token_fetcher) |
| + : url_request_context_(url_request_context), |
| + access_token_fetcher_(access_token_fetcher), |
| + weak_ptr_factory_(this) { |
| +} |
| + |
| +CryptAuthClient::~CryptAuthClient() { |
| +} |
| + |
| +template <class RequestProto, class ResponseProto> |
| +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.
|
| + const std::string& request_path, |
| + const RequestProto& request_proto, |
| + base::Callback<void(const ResponseProto&)> response_callback, |
| + ErrorCallback error_callback) { |
| + COMPILE_ASSERT( |
| + (std::is_base_of<google::protobuf::MessageLite, RequestProto>::value), |
| + request_is_not_proto); |
| + COMPILE_ASSERT( |
| + (std::is_base_of<google::protobuf::MessageLite, RequestProto>::value), |
| + 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.
|
| + |
| + request_path_ = request_path; |
| + error_callback_ = error_callback; |
| + if (flow_) { |
| + 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
|
| + return; |
| + } |
| + |
| + std::string serialized_request; |
| + if (!request_proto.SerializeToString(&serialized_request)) { |
| + OnApiCallFailed(std::string("Failed to serialize ") + |
| + request_proto.GetTypeName() + " proto."); |
| + return; |
| + } |
| + |
| + access_token_fetcher_->FetchAccessToken(base::Bind( |
| + &CryptAuthClient::OnAccessTokenFetched<ResponseProto>, |
| + weak_ptr_factory_.GetWeakPtr(), serialized_request, response_callback)); |
| +} |
| + |
| +template <class ResponseProto> |
| +void CryptAuthClient::OnAccessTokenFetched( |
| + std::string serialized_request, |
| + base::Callback<void(const ResponseProto&)> response_callback, |
| + const std::string& access_token) { |
| + if (access_token.empty()) { |
| + OnApiCallFailed("Failed to get a valid access token"); |
| + return; |
| + } |
| + |
| + flow_.reset(CreateFlow(CreateRequestUrl(request_path_))); |
| + flow_->Start(url_request_context_.get(), access_token, serialized_request, |
| + base::Bind(&CryptAuthClient::OnFlowSuccess<ResponseProto>, |
| + weak_ptr_factory_.GetWeakPtr(), response_callback), |
| + base::Bind(&CryptAuthClient::OnApiCallFailed, |
| + weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +template <class ResponseProto> |
| +void CryptAuthClient::OnFlowSuccess( |
| + base::Callback<void(const ResponseProto&)> result_callback, |
| + const std::string& serialized_response) { |
| + flow_.reset(); |
| + ResponseProto response; |
| + if (!response.ParseFromString(serialized_response)) { |
| + OnApiCallFailed("parse error"); |
| + return; |
| + } |
| + result_callback.Run(response); |
| +}; |
| + |
| +void CryptAuthClient::OnApiCallFailed(const std::string& error_message) { |
| + flow_.reset(); |
| + error_callback_.Run(error_message); |
| +} |
| + |
| +CryptAuthApiCallFlow* CryptAuthClient::CreateFlow(GURL request_url) { |
| + 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
|
| +} |
| + |
| +void CryptAuthClient::GetMyDevices(bool allow_stale_read, |
| + GetMyDevicesCallback callback, |
| + ErrorCallback error_callback) { |
| + cryptauth::GetMyDevicesRequest request; |
| + request.set_approved_for_unlock_required(false); |
| + request.set_allow_stale_read(allow_stale_read); |
| + MakeApiCall<cryptauth::GetMyDevicesRequest, cryptauth::GetMyDevicesResponse>( |
| + kGetMyDevicesPath, request, callback, error_callback); |
| +} |
| + |
| +void CryptAuthClient::FindEligibleUnlockDevices( |
| + const std::string& bluetooth_address, |
| + FindEligibleUnlockDevicesCallback callback, |
| + ErrorCallback error_callback) { |
| + cryptauth::FindEligibleUnlockDevicesRequest request; |
| + request.set_callback_bluetooth_address(bluetooth_address); |
| + MakeApiCall<cryptauth::FindEligibleUnlockDevicesRequest, |
| + 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.
|
| + kFindEligibleUnlockDevicesPath, request, callback, error_callback); |
| +} |
| + |
| +void CryptAuthClient::SendDeviceSyncTickle( |
| + SendDeviceSyncTickleCallback callback, |
| + ErrorCallback error_callback) { |
| + cryptauth::SendDeviceSyncTickleRequest request; |
| + MakeApiCall<cryptauth::SendDeviceSyncTickleRequest, |
| + cryptauth::SendDeviceSyncTickleResponse>( |
| + kSendDeviceSyncTicklePath, request, callback, error_callback); |
| +} |
| + |
| +void CryptAuthClient::ToggleEasyUnlock(bool turn_on, |
| + bool apply_to_all, |
| + const std::string public_key, |
| + ToggleEasyUnlockCallback callback, |
| + ErrorCallback error_callback) { |
| + cryptauth::ToggleEasyUnlockRequest request; |
| + request.set_public_key(public_key); |
| + request.set_enable(turn_on); |
| + request.set_apply_to_all(apply_to_all); |
| + |
| + MakeApiCall<cryptauth::ToggleEasyUnlockRequest, |
| + cryptauth::ToggleEasyUnlockResponse>( |
| + kToggleEasyUnlockPath, request, callback, error_callback); |
| +} |
| + |
| +void CryptAuthClient::SetupEnrollment( |
| + const std::string application_id, |
| + const std::vector<std::string>& supported_protocols, |
| + SetupEnrollmentCallback callback, |
| + ErrorCallback error_callback) { |
| + cryptauth::SetupEnrollmentRequest request; |
| + request.set_application_id(application_id); |
| + for (auto type : supported_protocols) |
| + request.add_types(type); |
| + |
| + MakeApiCall<cryptauth::SetupEnrollmentRequest, |
| + cryptauth::SetupEnrollmentResponse>(kSetupEnrollmentPath, request, |
| + callback, error_callback); |
| +} |
| + |
| +void CryptAuthClient::FinishEnrollment(const std::string& enrollment_session_id, |
| + const std::string& enrollment_message, |
| + const std::string& device_ephermeral_key, |
| + FinishEnrollmentCallback callback, |
| + ErrorCallback error_callback) { |
| + cryptauth::FinishEnrollmentRequest request; |
| + request.set_enrollment_session_id(enrollment_session_id); |
| + request.set_enrollment_message(enrollment_message); |
| + request.set_device_ephemeral_key(device_ephermeral_key); |
| + |
| + MakeApiCall<cryptauth::FinishEnrollmentRequest, |
| + cryptauth::FinishEnrollmentResponse>( |
| + kFinishEnrollmentPath, request, callback, error_callback); |
| +} |
| + |
| +} // proximity_auth |