1 // Copyright 2019 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 #ifndef DEVICE_FIDO_RESET_REQUEST_HANDLER_H_
6 #define DEVICE_FIDO_RESET_REQUEST_HANDLER_H_
7 
8 #include <memory>
9 
10 #include "base/callback.h"
11 #include "base/component_export.h"
12 #include "base/containers/flat_set.h"
13 #include "base/macros.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/sequence_checker.h"
16 #include "device/fido/fido_discovery_factory.h"
17 #include "device/fido/fido_request_handler_base.h"
18 #include "device/fido/fido_transport_protocol.h"
19 
20 namespace device {
21 
22 class FidoAuthenticator;
23 
24 namespace pin {
25 struct EmptyResponse;
26 }
27 
28 // ResetRequestHandler is a simple state machine that gets a touch from an
29 // authenticator and then sends a CTAP2 reset request. This is expected to be
30 // driven by Settings UI for users to manually reset authenticators.
COMPONENT_EXPORT(DEVICE_FIDO)31 class COMPONENT_EXPORT(DEVICE_FIDO) ResetRequestHandler
32     : public FidoRequestHandlerBase {
33  public:
34   // ResetSentCallback will be run once an authenticator has been touched and a
35   // reset command has been sent to it. This will always occur before
36   // |FinishedCallback|.
37   using ResetSentCallback = base::OnceCallback<void()>;
38   // FinishedCallback will be called once this process has completed. If the
39   // status is |kCtap1ErrInvalidCommand| then the user may have selected a non-
40   // CTAP2 authenticator, in which case no reset command was ever sent.
41   // Otherwise the status is the result of the reset command.
42   using FinishedCallback = base::OnceCallback<void(CtapDeviceResponseCode)>;
43 
44   ResetRequestHandler(
45       const base::flat_set<FidoTransportProtocol>& supported_transports,
46       ResetSentCallback reset_sent_callback,
47       FinishedCallback finished_callback,
48       std::unique_ptr<FidoDiscoveryFactory> fido_discovery_factory =
49           std::make_unique<FidoDiscoveryFactory>());
50   ~ResetRequestHandler() override;
51 
52  private:
53   // FidoRequestHandlerBase:
54   void DispatchRequest(FidoAuthenticator* authenticator) override;
55 
56   void OnTouch(FidoAuthenticator* authenticator);
57   void OnResetComplete(CtapDeviceResponseCode status,
58                        base::Optional<pin::EmptyResponse> response);
59 
60   ResetSentCallback reset_sent_callback_;
61   FinishedCallback finished_callback_;
62   bool processed_touch_ = false;
63   std::unique_ptr<FidoDiscoveryFactory> fido_discovery_factory_;
64   SEQUENCE_CHECKER(my_sequence_checker_);
65   base::WeakPtrFactory<ResetRequestHandler> weak_factory_{this};
66 
67   DISALLOW_COPY_AND_ASSIGN(ResetRequestHandler);
68 };
69 
70 }  // namespace device
71 
72 #endif  // DEVICE_FIDO_RESET_REQUEST_HANDLER_H_
73