MultiFactorResolver.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2023 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. import Foundation
  15. #if os(iOS)
  16. /** @class FIRPhoneMultiFactorAssertion
  17. @brief The subclass of base class FIRMultiFactorAssertion, used to assert ownership of a phone
  18. second factor.
  19. This class is available on iOS only.
  20. */
  21. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  22. @objc(FIRMultiFactorResolver)
  23. public class MultiFactorResolver: NSObject {
  24. /**
  25. @brief The opaque session identifier for the current sign-in flow.
  26. */
  27. @objc public let session: MultiFactorSession
  28. /**
  29. @brief The list of hints for the second factors needed to complete the sign-in for the current
  30. session.
  31. */
  32. @objc public let hints: [MultiFactorInfo]
  33. /**
  34. @brief The Auth reference for the current FIRMultiResolver.
  35. */
  36. @objc public let auth: Auth
  37. /** @fn resolveSignInWithAssertion:completion:
  38. @brief A helper function to help users complete sign in with a second factor using an
  39. FIRMultiFactorAssertion confirming the user successfully completed the second factor
  40. challenge.
  41. @param completion The block invoked when the request is complete, or fails.
  42. */
  43. @objc(resolveSignInWithAssertion:completion:)
  44. public func resolveSignIn(with assertion: MultiFactorAssertion,
  45. completion: ((AuthDataResult?, Error?) -> Void)? = nil) {
  46. let phoneAssertion = assertion as? PhoneMultiFactorAssertion
  47. guard let credential = phoneAssertion?.authCredential else {
  48. fatalError("Internal Error: Missing credential")
  49. }
  50. switch credential.credentialKind {
  51. case .phoneNumber: fatalError("Internal Error: Missing verificationCode")
  52. case let .verification(verificationID, code):
  53. let finalizeMFAPhoneRequestInfo =
  54. AuthProtoFinalizeMFAPhoneRequestInfo(sessionInfo: verificationID, verificationCode: code)
  55. let request = FinalizeMFASignInRequest(
  56. mfaPendingCredential: mfaPendingCredential,
  57. verificationInfo: finalizeMFAPhoneRequestInfo,
  58. requestConfiguration: auth.requestConfiguration
  59. )
  60. AuthBackend.post(with: request) { response, error in
  61. if let error {
  62. if let completion {
  63. completion(nil, error)
  64. }
  65. } else if let response {
  66. self.auth.completeSignIn(withAccessToken: response.idToken,
  67. accessTokenExpirationDate: nil,
  68. refreshToken: response.refreshToken,
  69. anonymous: false) { user, error in
  70. guard let user else {
  71. fatalError("Internal Auth Error: completeSignIn didn't pass back a user")
  72. }
  73. let result = AuthDataResult(withUser: user, additionalUserInfo: nil)
  74. let decoratedCallback = self.auth
  75. .signInFlowAuthDataResultCallback(byDecorating: completion)
  76. decoratedCallback(result, nil)
  77. }
  78. }
  79. }
  80. }
  81. }
  82. /** @fn resolveSignInWithAssertion:completion:
  83. @brief A helper function to help users complete sign in with a second factor using an
  84. FIRMultiFactorAssertion confirming the user successfully completed the second factor
  85. challenge.
  86. @param completion The block invoked when the request is complete, or fails.
  87. */
  88. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  89. public func resolveSignIn(with assertion: MultiFactorAssertion) async throws -> AuthDataResult {
  90. return try await withCheckedThrowingContinuation { continuation in
  91. self.resolveSignIn(with: assertion) { result, error in
  92. if let result {
  93. continuation.resume(returning: result)
  94. } else {
  95. continuation.resume(throwing: error!)
  96. }
  97. }
  98. }
  99. }
  100. let mfaPendingCredential: String?
  101. init(with mfaPendingCredential: String?, hints: [MultiFactorInfo], auth: Auth) {
  102. self.mfaPendingCredential = mfaPendingCredential
  103. self.hints = hints
  104. self.auth = auth
  105. session = MultiFactorSession()
  106. session.mfaPendingCredential = mfaPendingCredential
  107. }
  108. }
  109. #endif