| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_H | 5 #ifndef COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_H |
| 6 #define COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_H | 6 #define COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_H |
| 7 | 7 |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/memory/weak_ptr.h" | |
| 12 #include "components/proximity_auth/cryptauth/proto/cryptauth_api.pb.h" | 11 #include "components/proximity_auth/cryptauth/proto/cryptauth_api.pb.h" |
| 13 #include "net/url_request/url_request_context_getter.h" | 12 |
| 14 #include "url/gurl.h" | 13 namespace net { |
| 14 class URLRequestContextGetter; |
| 15 } |
| 15 | 16 |
| 16 namespace proximity_auth { | 17 namespace proximity_auth { |
| 17 | 18 |
| 18 class CryptAuthAccessTokenFetcher; | 19 class CryptAuthAccessTokenFetcher; |
| 19 class CryptAuthApiCallFlow; | 20 class CryptAuthApiCallFlow; |
| 20 | 21 |
| 21 // Use CryptAuthClient to make API requests to the CryptAuth service, which | 22 // Interface for making API requests to the CryptAuth service, which |
| 22 // manages cryptographic credentials (ie. public keys) for a user's devices. | 23 // manages cryptographic credentials (ie. public keys) for a user's devices. |
| 23 // CryptAuthClient only processes one request, so create a new instance for each | 24 // Implmentations shall only processes a single request, so create a new |
| 24 // request you make. DO NOT REUSE. | 25 // instance for each request you make. DO NOT REUSE. |
| 25 // For documentation on each API call, see | 26 // For documentation on each API call, see |
| 26 // components/proximity_auth/cryptauth/proto/cryptauth_api.proto | 27 // components/proximity_auth/cryptauth/proto/cryptauth_api.proto |
| 27 // Note: There is no need to set the |device_classifier| field in request | |
| 28 // messages. CryptAuthClient will fill this field for all requests. | |
| 29 class CryptAuthClient { | 28 class CryptAuthClient { |
| 30 public: | 29 public: |
| 31 typedef base::Callback<void(const std::string&)> ErrorCallback; | 30 typedef base::Callback<void(const std::string&)> ErrorCallback; |
| 32 | 31 |
| 33 // Creates the client using |url_request_context| to make the HTTP request. | 32 virtual ~CryptAuthClient() {} |
| 34 // CryptAuthClient takes ownership of |access_token_fetcher|, which provides | |
| 35 // the access token authorizing CryptAuth requests. | |
| 36 // The |device_classifier| argument contains basic device information of the | |
| 37 // caller (e.g. version and device type). | |
| 38 CryptAuthClient( | |
| 39 scoped_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher, | |
| 40 scoped_refptr<net::URLRequestContextGetter> url_request_context, | |
| 41 const cryptauth::DeviceClassifier& device_classifier); | |
| 42 virtual ~CryptAuthClient(); | |
| 43 | 33 |
| 44 // GetMyDevices | 34 // GetMyDevices |
| 45 typedef base::Callback<void(const cryptauth::GetMyDevicesResponse&)> | 35 typedef base::Callback<void(const cryptauth::GetMyDevicesResponse&)> |
| 46 GetMyDevicesCallback; | 36 GetMyDevicesCallback; |
| 47 void GetMyDevices(const cryptauth::GetMyDevicesRequest& request, | 37 virtual void GetMyDevices(const cryptauth::GetMyDevicesRequest& request, |
| 48 const GetMyDevicesCallback& callback, | 38 const GetMyDevicesCallback& callback, |
| 49 const ErrorCallback& error_callback); | 39 const ErrorCallback& error_callback) = 0; |
| 50 | 40 |
| 51 // FindEligibleUnlockDevices | 41 // FindEligibleUnlockDevices |
| 52 typedef base::Callback<void( | 42 typedef base::Callback<void( |
| 53 const cryptauth::FindEligibleUnlockDevicesResponse&)> | 43 const cryptauth::FindEligibleUnlockDevicesResponse&)> |
| 54 FindEligibleUnlockDevicesCallback; | 44 FindEligibleUnlockDevicesCallback; |
| 55 void FindEligibleUnlockDevices( | 45 virtual void FindEligibleUnlockDevices( |
| 56 const cryptauth::FindEligibleUnlockDevicesRequest& request, | 46 const cryptauth::FindEligibleUnlockDevicesRequest& request, |
| 57 const FindEligibleUnlockDevicesCallback& callback, | 47 const FindEligibleUnlockDevicesCallback& callback, |
| 58 const ErrorCallback& error_callback); | 48 const ErrorCallback& error_callback) = 0; |
| 59 | 49 |
| 60 // SendDeviceSyncTickle | 50 // SendDeviceSyncTickle |
| 61 typedef base::Callback<void(const cryptauth::SendDeviceSyncTickleResponse&)> | 51 typedef base::Callback<void(const cryptauth::SendDeviceSyncTickleResponse&)> |
| 62 SendDeviceSyncTickleCallback; | 52 SendDeviceSyncTickleCallback; |
| 63 void SendDeviceSyncTickle( | 53 virtual void SendDeviceSyncTickle( |
| 64 const cryptauth::SendDeviceSyncTickleRequest& request, | 54 const cryptauth::SendDeviceSyncTickleRequest& request, |
| 65 const SendDeviceSyncTickleCallback& callback, | 55 const SendDeviceSyncTickleCallback& callback, |
| 66 const ErrorCallback& error_callback); | 56 const ErrorCallback& error_callback) = 0; |
| 67 | 57 |
| 68 // ToggleEasyUnlock | 58 // ToggleEasyUnlock |
| 69 typedef base::Callback<void(const cryptauth::ToggleEasyUnlockResponse&)> | 59 typedef base::Callback<void(const cryptauth::ToggleEasyUnlockResponse&)> |
| 70 ToggleEasyUnlockCallback; | 60 ToggleEasyUnlockCallback; |
| 71 void ToggleEasyUnlock(const cryptauth::ToggleEasyUnlockRequest& request, | 61 virtual void ToggleEasyUnlock( |
| 72 const ToggleEasyUnlockCallback& callback, | 62 const cryptauth::ToggleEasyUnlockRequest& request, |
| 73 const ErrorCallback& error_callback); | 63 const ToggleEasyUnlockCallback& callback, |
| 64 const ErrorCallback& error_callback) = 0; |
| 74 | 65 |
| 75 // SetupEnrollment | 66 // SetupEnrollment |
| 76 typedef base::Callback<void(const cryptauth::SetupEnrollmentResponse&)> | 67 typedef base::Callback<void(const cryptauth::SetupEnrollmentResponse&)> |
| 77 SetupEnrollmentCallback; | 68 SetupEnrollmentCallback; |
| 78 void SetupEnrollment(const cryptauth::SetupEnrollmentRequest& request, | 69 virtual void SetupEnrollment(const cryptauth::SetupEnrollmentRequest& request, |
| 79 const SetupEnrollmentCallback& callback, | 70 const SetupEnrollmentCallback& callback, |
| 80 const ErrorCallback& error_callback); | 71 const ErrorCallback& error_callback) = 0; |
| 81 | 72 |
| 82 // FinishEnrollment | 73 // FinishEnrollment |
| 83 typedef base::Callback<void(const cryptauth::FinishEnrollmentResponse&)> | 74 typedef base::Callback<void(const cryptauth::FinishEnrollmentResponse&)> |
| 84 FinishEnrollmentCallback; | 75 FinishEnrollmentCallback; |
| 85 void FinishEnrollment(const cryptauth::FinishEnrollmentRequest& request, | 76 virtual void FinishEnrollment( |
| 86 const FinishEnrollmentCallback& callback, | 77 const cryptauth::FinishEnrollmentRequest& request, |
| 87 const ErrorCallback& error_callback); | 78 const FinishEnrollmentCallback& callback, |
| 88 | 79 const ErrorCallback& error_callback) = 0; |
| 89 protected: | |
| 90 // Creates a CryptAuthApiCallFlow object. Exposed for testing. | |
| 91 virtual scoped_ptr<CryptAuthApiCallFlow> CreateFlow(const GURL& request_url); | |
| 92 | |
| 93 private: | |
| 94 // Starts a call to the API given by |request_path|, with the templated | |
| 95 // request and response types. The client first fetches the access token and | |
| 96 // then makes the HTTP request. | |
| 97 template <class RequestProto, class ResponseProto> | |
| 98 void MakeApiCall( | |
| 99 const std::string& request_path, | |
| 100 const RequestProto& request_proto, | |
| 101 const base::Callback<void(const ResponseProto&)>& response_callback, | |
| 102 const ErrorCallback& error_callback); | |
| 103 | |
| 104 // Called when the access token is obtained so the API request can be made. | |
| 105 template <class ResponseProto> | |
| 106 void OnAccessTokenFetched( | |
| 107 const std::string& serialized_request, | |
| 108 const base::Callback<void(const ResponseProto&)>& response_callback, | |
| 109 const std::string& access_token); | |
| 110 | |
| 111 // Called with CryptAuthApiCallFlow completes successfully to deserialize and | |
| 112 // return the result. | |
| 113 template <class ResponseProto> | |
| 114 void OnFlowSuccess( | |
| 115 const base::Callback<void(const ResponseProto&)>& result_callback, | |
| 116 const std::string& serialized_response); | |
| 117 | |
| 118 // Called when the current API call fails at any step. | |
| 119 void OnApiCallFailed(const std::string& error_message); | |
| 120 | |
| 121 // The context for network requests. | |
| 122 scoped_refptr<net::URLRequestContextGetter> url_request_context_; | |
| 123 | |
| 124 // Fetches the access token authorizing the API calls. | |
| 125 scoped_ptr<CryptAuthAccessTokenFetcher> access_token_fetcher_; | |
| 126 | |
| 127 // Contains basic device info of the client making the request that is sent to | |
| 128 // CryptAuth with each API call. | |
| 129 const cryptauth::DeviceClassifier device_classifier_; | |
| 130 | |
| 131 // Handles the current API call. | |
| 132 scoped_ptr<CryptAuthApiCallFlow> flow_; | |
| 133 | |
| 134 // URL path of the current request. | |
| 135 std::string request_path_; | |
| 136 | |
| 137 // Called when the current request fails. | |
| 138 ErrorCallback error_callback_; | |
| 139 | |
| 140 base::WeakPtrFactory<CryptAuthClient> weak_ptr_factory_; | |
| 141 | |
| 142 DISALLOW_COPY_AND_ASSIGN(CryptAuthClient); | |
| 143 }; | 80 }; |
| 144 | 81 |
| 145 } // namespace proximity_auth | 82 } // namespace proximity_auth |
| 146 | 83 |
| 147 #endif // COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_H | 84 #endif // COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_H |
| OLD | NEW |