| 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 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 + | |
| 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 const cryptauth::DeviceClassifier& device_classifier) | |
| 54 : url_request_context_(url_request_context), | |
| 55 access_token_fetcher_(access_token_fetcher.Pass()), | |
| 56 device_classifier_(device_classifier), | |
| 57 weak_ptr_factory_(this) { | |
| 58 } | |
| 59 | |
| 60 CryptAuthClient::~CryptAuthClient() { | |
| 61 } | |
| 62 | |
| 63 void CryptAuthClient::GetMyDevices( | |
| 64 const cryptauth::GetMyDevicesRequest& request, | |
| 65 const GetMyDevicesCallback& callback, | |
| 66 const ErrorCallback& error_callback) { | |
| 67 MakeApiCall(kGetMyDevicesPath, request, callback, error_callback); | |
| 68 } | |
| 69 | |
| 70 void CryptAuthClient::FindEligibleUnlockDevices( | |
| 71 const cryptauth::FindEligibleUnlockDevicesRequest& request, | |
| 72 const FindEligibleUnlockDevicesCallback& callback, | |
| 73 const ErrorCallback& error_callback) { | |
| 74 MakeApiCall(kFindEligibleUnlockDevicesPath, request, callback, | |
| 75 error_callback); | |
| 76 } | |
| 77 | |
| 78 void CryptAuthClient::SendDeviceSyncTickle( | |
| 79 const cryptauth::SendDeviceSyncTickleRequest& request, | |
| 80 const SendDeviceSyncTickleCallback& callback, | |
| 81 const ErrorCallback& error_callback) { | |
| 82 MakeApiCall(kSendDeviceSyncTicklePath, request, callback, error_callback); | |
| 83 } | |
| 84 | |
| 85 void CryptAuthClient::ToggleEasyUnlock( | |
| 86 const cryptauth::ToggleEasyUnlockRequest& request, | |
| 87 const ToggleEasyUnlockCallback& callback, | |
| 88 const ErrorCallback& error_callback) { | |
| 89 MakeApiCall(kToggleEasyUnlockPath, request, callback, error_callback); | |
| 90 } | |
| 91 | |
| 92 void CryptAuthClient::SetupEnrollment( | |
| 93 const cryptauth::SetupEnrollmentRequest& request, | |
| 94 const SetupEnrollmentCallback& callback, | |
| 95 const ErrorCallback& error_callback) { | |
| 96 MakeApiCall(kSetupEnrollmentPath, request, callback, error_callback); | |
| 97 } | |
| 98 | |
| 99 void CryptAuthClient::FinishEnrollment( | |
| 100 const cryptauth::FinishEnrollmentRequest& request, | |
| 101 const FinishEnrollmentCallback& callback, | |
| 102 const ErrorCallback& error_callback) { | |
| 103 MakeApiCall(kFinishEnrollmentPath, request, callback, error_callback); | |
| 104 } | |
| 105 | |
| 106 scoped_ptr<CryptAuthApiCallFlow> CryptAuthClient::CreateFlow( | |
| 107 const GURL& request_url) { | |
| 108 return make_scoped_ptr(new CryptAuthApiCallFlow(request_url)); | |
| 109 } | |
| 110 | |
| 111 template <class RequestProto, class ResponseProto> | |
| 112 void CryptAuthClient::MakeApiCall( | |
| 113 const std::string& request_path, | |
| 114 const RequestProto& request_proto, | |
| 115 const base::Callback<void(const ResponseProto&)>& response_callback, | |
| 116 const ErrorCallback& error_callback) { | |
| 117 if (flow_) { | |
| 118 error_callback.Run( | |
| 119 "Client has been used for another request. Do not reuse."); | |
| 120 return; | |
| 121 } | |
| 122 | |
| 123 // The |device_classifier| field must be present for all CryptAuth requests. | |
| 124 RequestProto request_copy(request_proto); | |
| 125 request_copy.mutable_device_classifier()->CopyFrom(device_classifier_); | |
| 126 | |
| 127 std::string serialized_request; | |
| 128 if (!request_copy.SerializeToString(&serialized_request)) { | |
| 129 error_callback.Run(std::string("Failed to serialize ") + | |
| 130 request_proto.GetTypeName() + " proto."); | |
| 131 return; | |
| 132 } | |
| 133 | |
| 134 request_path_ = request_path; | |
| 135 error_callback_ = error_callback; | |
| 136 access_token_fetcher_->FetchAccessToken(base::Bind( | |
| 137 &CryptAuthClient::OnAccessTokenFetched<ResponseProto>, | |
| 138 weak_ptr_factory_.GetWeakPtr(), serialized_request, response_callback)); | |
| 139 } | |
| 140 | |
| 141 template <class ResponseProto> | |
| 142 void CryptAuthClient::OnAccessTokenFetched( | |
| 143 const std::string& serialized_request, | |
| 144 const base::Callback<void(const ResponseProto&)>& response_callback, | |
| 145 const std::string& access_token) { | |
| 146 if (access_token.empty()) { | |
| 147 OnApiCallFailed("Failed to get a valid access token."); | |
| 148 return; | |
| 149 } | |
| 150 | |
| 151 flow_ = CreateFlow(CreateRequestUrl(request_path_)); | |
| 152 flow_->Start(url_request_context_.get(), access_token, serialized_request, | |
| 153 base::Bind(&CryptAuthClient::OnFlowSuccess<ResponseProto>, | |
| 154 weak_ptr_factory_.GetWeakPtr(), response_callback), | |
| 155 base::Bind(&CryptAuthClient::OnApiCallFailed, | |
| 156 weak_ptr_factory_.GetWeakPtr())); | |
| 157 } | |
| 158 | |
| 159 template <class ResponseProto> | |
| 160 void CryptAuthClient::OnFlowSuccess( | |
| 161 const base::Callback<void(const ResponseProto&)>& result_callback, | |
| 162 const std::string& serialized_response) { | |
| 163 ResponseProto response; | |
| 164 if (!response.ParseFromString(serialized_response)) { | |
| 165 OnApiCallFailed("Failed to parse response proto."); | |
| 166 return; | |
| 167 } | |
| 168 result_callback.Run(response); | |
| 169 }; | |
| 170 | |
| 171 void CryptAuthClient::OnApiCallFailed(const std::string& error_message) { | |
| 172 error_callback_.Run(error_message); | |
| 173 } | |
| 174 | |
| 175 } // proximity_auth | |
| OLD | NEW |