v2_registration.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_CABLE_V2_REGISTRATION_H_
  5. #define DEVICE_FIDO_CABLE_V2_REGISTRATION_H_
  6. #include <stdint.h>
  7. #include <array>
  8. #include <memory>
  9. #include <vector>
  10. #include "base/callback_forward.h"
  11. #include "base/containers/span.h"
  12. #include "device/fido/cable/v2_constants.h"
  13. #include "device/fido/fido_constants.h"
  14. #include "third_party/abseil-cpp/absl/types/optional.h"
  15. namespace instance_id {
  16. class InstanceIDDriver;
  17. }
  18. namespace device {
  19. namespace cablev2 {
  20. namespace authenticator {
  21. // Registration represents a subscription to events from the tunnel service.
  22. class Registration {
  23. public:
  24. // Type enumerates the types of registrations that are maintained.
  25. enum class Type : uint8_t {
  26. // LINKING is for link information shared with desktops after scanning a QR
  27. // code. If the user chooses to unlink devices then this registration can be
  28. // rotated by calling |RotateContactID|. That will cause the server to
  29. // inform clients that the registration is no longer valid and that they
  30. // should forget about it.
  31. LINKING = 0,
  32. // SYNC is for information shared via the Sync service. This is separate so
  33. // that unlinking devices doesn't break sync peers.
  34. SYNC = 1,
  35. };
  36. // An Event contains the information sent by the tunnel service when a peer is
  37. // trying to connect.
  38. struct Event {
  39. Event();
  40. ~Event();
  41. Event(const Event&) = delete;
  42. Event& operator=(const Event&) = delete;
  43. // Serialize returns a serialized form of the |Event|. This format is
  44. // not stable and is suitable only for transient storage.
  45. absl::optional<std::vector<uint8_t>> Serialize();
  46. // FromSerialized parses the bytes produced by |Serialize|. It assumes that
  47. // the input is well formed. It returns |nullptr| on error.
  48. static std::unique_ptr<Event> FromSerialized(base::span<const uint8_t> in);
  49. Type source;
  50. FidoRequestType request_type;
  51. std::array<uint8_t, kTunnelIdSize> tunnel_id;
  52. std::array<uint8_t, kRoutingIdSize> routing_id;
  53. std::array<uint8_t, kPairingIDSize> pairing_id;
  54. std::array<uint8_t, kClientNonceSize> client_nonce;
  55. absl::optional<std::vector<uint8_t>> contact_id;
  56. // protocol_revision can be optionally asserted while we transition from
  57. // revision zero to revision one. This might be removed in the future.
  58. unsigned protocol_revision = 0;
  59. };
  60. virtual ~Registration();
  61. // PrepareContactID indicates that |contact_id| will soon be called. In order
  62. // to save resources for the case when |contact_id| is never used,
  63. // registration will be deferred until this is called.
  64. virtual void PrepareContactID() = 0;
  65. // RotateContactID invalidates the current contact ID and prepares a fresh
  66. // one.
  67. virtual void RotateContactID() = 0;
  68. // contact_id returns an opaque token that may be placed in pairing data for
  69. // desktops to later connect to. |nullopt| will be returned if the value is
  70. // not yet ready.
  71. virtual absl::optional<std::vector<uint8_t>> contact_id() const = 0;
  72. };
  73. // Register subscribes to the tunnel service and returns a |Registration|. This
  74. // should only be called once in an address space. Subsequent calls may CHECK.
  75. // The |event_callback| is called, on the same thread, whenever a paired device
  76. // requests a tunnel.
  77. std::unique_ptr<Registration> Register(
  78. instance_id::InstanceIDDriver* instance_id_driver,
  79. Registration::Type type,
  80. base::OnceCallback<void()> on_ready,
  81. base::RepeatingCallback<void(std::unique_ptr<Registration::Event>)>
  82. event_callback);
  83. } // namespace authenticator
  84. } // namespace cablev2
  85. } // namespace device
  86. #endif // DEVICE_FIDO_CABLE_V2_REGISTRATION_H_