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.
5 #ifndef COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_H
6 #define COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_H
8 #include "base/callback.h"
9 #include "base/macros.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"
13 #include "net/url_request/url_request_context_getter.h"
16 class OAuth2TokenService
;
18 namespace proximity_auth
{
20 class CryptAuthAccessTokenFetcher
;
21 class CryptAuthApiCallFlow
;
23 // Use CryptAuthClient to make API requests to the CryptAuth service, which
24 // manages cryptographic credentials (ie. public keys) for a user's devices.
25 // CryptAuthClient only processes one request, so create a new instance for each
26 // request you make. DO NOT REUSE.
27 // For documentation on each API call, see
28 // components/proximity_auth/cryptauth/proto/cryptauth_api.proto
29 class CryptAuthClient
{
31 typedef base::Callback
<void(const std::string
&)> ErrorCallback
;
33 // Creates the client using |url_request_context| to make the HTTP request.
34 // CryptAuthClient takes ownership of |access_token_fetcher|, which provides
35 // the access token authorizing CryptAuth requests.
37 scoped_ptr
<CryptAuthAccessTokenFetcher
> access_token_fetcher
,
38 scoped_refptr
<net::URLRequestContextGetter
> url_request_context
);
39 virtual ~CryptAuthClient();
42 typedef base::Callback
<void(const cryptauth::GetMyDevicesResponse
&)>
44 void GetMyDevices(const cryptauth::GetMyDevicesRequest
& request
,
45 const GetMyDevicesCallback
& callback
,
46 const ErrorCallback
& error_callback
);
48 // FindEligibleUnlockDevices
49 typedef base::Callback
<void(
50 const cryptauth::FindEligibleUnlockDevicesResponse
&)>
51 FindEligibleUnlockDevicesCallback
;
52 void FindEligibleUnlockDevices(
53 const cryptauth::FindEligibleUnlockDevicesRequest
& request
,
54 const FindEligibleUnlockDevicesCallback
& callback
,
55 const ErrorCallback
& error_callback
);
57 // SendDeviceSyncTickle
58 typedef base::Callback
<void(const cryptauth::SendDeviceSyncTickleResponse
&)>
59 SendDeviceSyncTickleCallback
;
60 void SendDeviceSyncTickle(
61 const cryptauth::SendDeviceSyncTickleRequest
& request
,
62 const SendDeviceSyncTickleCallback
& callback
,
63 const ErrorCallback
& error_callback
);
66 typedef base::Callback
<void(const cryptauth::ToggleEasyUnlockResponse
&)>
67 ToggleEasyUnlockCallback
;
68 void ToggleEasyUnlock(const cryptauth::ToggleEasyUnlockRequest
& request
,
69 const ToggleEasyUnlockCallback
& callback
,
70 const ErrorCallback
& error_callback
);
73 typedef base::Callback
<void(const cryptauth::SetupEnrollmentResponse
&)>
74 SetupEnrollmentCallback
;
75 void SetupEnrollment(const cryptauth::SetupEnrollmentRequest
& request
,
76 const SetupEnrollmentCallback
& callback
,
77 const ErrorCallback
& error_callback
);
80 typedef base::Callback
<void(const cryptauth::FinishEnrollmentResponse
&)>
81 FinishEnrollmentCallback
;
82 void FinishEnrollment(const cryptauth::FinishEnrollmentRequest
& request
,
83 const FinishEnrollmentCallback
& callback
,
84 const ErrorCallback
& error_callback
);
87 // Creates a CryptAuthApiCallFlow object. Exposed for testing.
88 virtual scoped_ptr
<CryptAuthApiCallFlow
> CreateFlow(const GURL
& request_url
);
91 // Starts a call to the API given by |request_path|, with the templated
92 // request and response types. The client first fetches the access token and
93 // then makes the HTTP request.
94 template <class RequestProto
, class ResponseProto
>
96 const std::string
& request_path
,
97 const RequestProto
& request_proto
,
98 const base::Callback
<void(const ResponseProto
&)>& response_callback
,
99 const ErrorCallback
& error_callback
);
101 // Called when the access token is obtained so the API request can be made.
102 template <class ResponseProto
>
103 void OnAccessTokenFetched(
104 const std::string
& serialized_request
,
105 const base::Callback
<void(const ResponseProto
&)>& response_callback
,
106 const std::string
& access_token
);
108 // Called with CryptAuthApiCallFlow completes successfully to deserialize and
109 // return the result.
110 template <class ResponseProto
>
112 const base::Callback
<void(const ResponseProto
&)>& result_callback
,
113 const std::string
& serialized_response
);
115 // Called when the current API call fails at any step.
116 void OnApiCallFailed(const std::string
& error_message
);
118 // The context for network requests.
119 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_
;
121 // Fetches the access token authorizing the API calls.
122 scoped_ptr
<CryptAuthAccessTokenFetcher
> access_token_fetcher_
;
124 // Handles the current API call.
125 scoped_ptr
<CryptAuthApiCallFlow
> flow_
;
127 // URL path of the current request.
128 std::string request_path_
;
130 // Called when the current request fails.
131 ErrorCallback error_callback_
;
133 base::WeakPtrFactory
<CryptAuthClient
> weak_ptr_factory_
;
135 DISALLOW_COPY_AND_ASSIGN(CryptAuthClient
);
138 } // namespace proximity_auth
140 #endif // COMPONENTS_PROXIMITY_AUTH_CRYPT_AUTH_CLIENT_H