MultiFactor+Combine.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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) || os(macOS)
  15. import Combine
  16. import FirebaseAuth
  17. @available(iOS 13.0, macCatalyst 13.0, macOS 10.15, *)
  18. @available(tvOS, unavailable)
  19. @available(watchOS, unavailable)
  20. public extension MultiFactor {
  21. /// Get a session for a second factor enrollment operation.
  22. ///
  23. /// The publisher will emit events on the **main** thread.
  24. ///
  25. /// - Returns: A publisher that emits a `MultiFactorSession` for a second factor
  26. /// enrollment operation. This is used to identify the current user trying to enroll a
  27. /// second factor. The publisher will emit on
  28. /// the *main* thread.
  29. @discardableResult
  30. func getSession() -> Future<MultiFactorSession, Error> {
  31. Future<MultiFactorSession, Error> { promise in
  32. self.getSessionWithCompletion { session, error in
  33. if let session {
  34. promise(.success(session))
  35. } else if let error {
  36. promise(.failure(error))
  37. }
  38. }
  39. }
  40. }
  41. /// Enrolls a second factor as identified by the `MultiFactorAssertion` parameter for the
  42. /// current user.
  43. ///
  44. /// The publisher will emit events on the **main** thread.
  45. ///
  46. /// - Parameters:
  47. /// - assertion: The base class for asserting ownership of a second factor.
  48. /// - displayName: An optional display name associated with the multi factor to enroll.
  49. ///
  50. /// - Returns: A publisher that emits whether the call was successful or not. The publisher
  51. /// will emit on the *main* thread.
  52. @discardableResult
  53. func enroll(with assertion: MultiFactorAssertion,
  54. displayName: String?) -> Future<Void, Error> {
  55. Future<Void, Error> { promise in
  56. self.enroll(with: assertion, displayName: displayName) { error in
  57. if let error {
  58. promise(.failure(error))
  59. } else {
  60. promise(.success(()))
  61. }
  62. }
  63. }
  64. }
  65. /// Unenroll the given multi factor.
  66. ///
  67. /// The publisher will emit events on the **main** thread.
  68. ///
  69. /// - Parameter factorInfo: The structure used to represent a second factor entity from a
  70. /// client perspective.
  71. /// - Returns: A publisher that emits when the request to send the unenrollment verification
  72. /// email is complete. The publisher
  73. /// will emit on the *main* thread.
  74. @discardableResult
  75. func unenroll(with factorInfo: MultiFactorInfo) -> Future<Void, Error> {
  76. Future<Void, Error> { promise in
  77. self.unenroll(with: factorInfo) { error in
  78. if let error {
  79. promise(.failure(error))
  80. } else {
  81. promise(.success(()))
  82. }
  83. }
  84. }
  85. }
  86. /// Unenroll the given multi factor.
  87. ///
  88. /// The publisher will emit events on the **main** thread.
  89. ///
  90. /// - Returns: A publisher that emits when the request to send the unenrollment verification
  91. /// email is complete.
  92. /// The publisher will emit on the *main* thread.
  93. @discardableResult
  94. func unenroll(withFactorUID factorUID: String) -> Future<Void, Error> {
  95. Future<Void, Error> { promise in
  96. self.unenroll(withFactorUID: factorUID) { error in
  97. if let error {
  98. promise(.failure(error))
  99. } else {
  100. promise(.success(()))
  101. }
  102. }
  103. }
  104. }
  105. }
  106. #endif // os(iOS) || targetEnvironment(macCatalyst) || os(macOS)