MultiFactor+Combine.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #if os(iOS) || targetEnvironment(macCatalyst)
  15. import Combine
  16. import FirebaseAuth
  17. @available(iOS 13.0, macCatalyst 13.0, *)
  18. @available(macOS, unavailable)
  19. @available(tvOS, unavailable)
  20. @available(watchOS, unavailable)
  21. public extension MultiFactor {
  22. /// Get a session for a second factor enrollment operation.
  23. ///
  24. /// The publisher will emit events on the **main** thread.
  25. ///
  26. /// - Returns: A publisher that emits a `MultiFactorSession` for a second factor
  27. /// enrollment operation. This is used to identify the current user trying to enroll a
  28. /// second factor. The publisher will emit on
  29. /// the *main* thread.
  30. @discardableResult
  31. func getSession() -> Future<MultiFactorSession, Error> {
  32. Future<MultiFactorSession, Error> { promise in
  33. self.getSessionWithCompletion { session, error in
  34. if let session = session {
  35. promise(.success(session))
  36. } else if let error = error {
  37. promise(.failure(error))
  38. }
  39. }
  40. }
  41. }
  42. /// Enrolls a second factor as identified by the `MultiFactorAssertion` parameter for the
  43. /// current user.
  44. ///
  45. /// The publisher will emit events on the **main** thread.
  46. ///
  47. /// - Parameters:
  48. /// - assertion: The base class for asserting ownership of a second factor.
  49. /// - displayName: An optional display name associated with the multi factor to enroll.
  50. ///
  51. /// - Returns: A publisher that emits whether the call was successful or not. The publisher
  52. /// will emit on the *main* thread.
  53. @discardableResult
  54. func enroll(with assertion: MultiFactorAssertion,
  55. displayName: String?) -> Future<Void, Error> {
  56. Future<Void, Error> { promise in
  57. self.enroll(with: assertion, displayName: displayName) { error in
  58. if let error = error {
  59. promise(.failure(error))
  60. } else {
  61. promise(.success(()))
  62. }
  63. }
  64. }
  65. }
  66. /// Unenroll the given multi factor.
  67. ///
  68. /// The publisher will emit events on the **main** thread.
  69. ///
  70. /// - Parameter factorInfo: The structure used to represent a second factor entity from a
  71. /// client perspective.
  72. /// - Returns: A publisher that emits when the request to send the unenrollment verification
  73. /// email is complete. The publisher
  74. /// will emit on the *main* thread.
  75. @discardableResult
  76. func unenroll(with factorInfo: MultiFactorInfo) -> Future<Void, Error> {
  77. Future<Void, Error> { promise in
  78. self.unenroll(with: factorInfo) { error in
  79. if let error = error {
  80. promise(.failure(error))
  81. } else {
  82. promise(.success(()))
  83. }
  84. }
  85. }
  86. }
  87. /// Unenroll the given multi factor.
  88. ///
  89. /// The publisher will emit events on the **main** thread.
  90. ///
  91. /// - Returns: A publisher that emits when the request to send the unenrollment verification
  92. /// email is complete.
  93. /// The publisher will emit on the *main* thread.
  94. @discardableResult
  95. func unenroll(withFactorUID factorUID: String) -> Future<Void, Error> {
  96. Future<Void, Error> { promise in
  97. self.unenroll(withFactorUID: factorUID) { error in
  98. if let error = error {
  99. promise(.failure(error))
  100. } else {
  101. promise(.success(()))
  102. }
  103. }
  104. }
  105. }
  106. }
  107. #endif // os(iOS) || targetEnvironment(macCatalyst)