enroller.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2020 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. #ifndef DEVICE_FIDO_BIO_ENROLLER_H_
  5. #define DEVICE_FIDO_BIO_ENROLLER_H_
  6. #include <vector>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/sequence_checker.h"
  10. #include "device/fido/bio/enrollment.h"
  11. #include "device/fido/pin.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. namespace device {
  14. class FidoAuthenticator;
  15. // Handles enrolling fingerprints in an authenticator.
  16. class BioEnroller {
  17. public:
  18. class Delegate {
  19. public:
  20. // Called when the authenticator reports a sample has been collected. Does
  21. // not indicate success.
  22. virtual void OnSampleCollected(BioEnrollmentSampleStatus status,
  23. int samples_remaining) = 0;
  24. // Called when the enrollment has completed. |template_id| may be
  25. // absl::nullopt if the enrollment has been cancelled.
  26. virtual void OnEnrollmentDone(
  27. absl::optional<std::vector<uint8_t>> template_id) = 0;
  28. virtual void OnEnrollmentError(CtapDeviceResponseCode status) = 0;
  29. };
  30. BioEnroller(Delegate* delegate,
  31. FidoAuthenticator* authenticator,
  32. pin::TokenResponse token);
  33. ~BioEnroller();
  34. BioEnroller(const BioEnroller&) = delete;
  35. BioEnroller(BioEnroller&&) = delete;
  36. // Attempts to cancel an in progress enrollment. Does nothing if the request
  37. // is already completed (i.e. |OnEnrollmentDone| has been called already).
  38. // Otherwise, guaranteed to call |OnEnrollmentDone|.
  39. void Cancel();
  40. pin::TokenResponse token() { return token_; }
  41. FidoAuthenticator* authenticator() { return authenticator_; }
  42. private:
  43. enum State {
  44. kInProgress,
  45. kCancelled,
  46. kDone,
  47. };
  48. void FinishWithError(CtapDeviceResponseCode status);
  49. void FinishSuccessfully(absl::optional<std::vector<uint8_t>> template_id);
  50. void OnEnrollResponse(CtapDeviceResponseCode status,
  51. absl::optional<BioEnrollmentResponse> response);
  52. void OnEnrollCancelled(CtapDeviceResponseCode status,
  53. absl::optional<BioEnrollmentResponse> response);
  54. State state_ = State::kInProgress;
  55. raw_ptr<Delegate> delegate_;
  56. raw_ptr<FidoAuthenticator> authenticator_;
  57. pin::TokenResponse token_;
  58. absl::optional<std::vector<uint8_t>> template_id_;
  59. SEQUENCE_CHECKER(my_sequence_checker_);
  60. base::WeakPtrFactory<BioEnroller> weak_factory_{this};
  61. };
  62. } // namespace device
  63. #endif // DEVICE_FIDO_BIO_ENROLLER_H_