MultiFactorResolver.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. open 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. open func resolveSignIn(with assertion: MultiFactorAssertion,
  45. completion: ((AuthDataResult?, Error?) -> Void)? = nil) {
  46. var finalizedMFARequestInfo: AuthProto?
  47. if let totpAssertion = assertion as? TOTPMultiFactorAssertion {
  48. switch totpAssertion.secretOrID {
  49. case .secret: fatalError("Missing enrollmentID in totpAssertion")
  50. case let .enrollmentID(enrollmentID):
  51. finalizedMFARequestInfo = AuthProtoFinalizeMFATOTPSignInRequestInfo(
  52. mfaEnrollmentID: enrollmentID,
  53. verificationCode: totpAssertion.oneTimePassword
  54. )
  55. }
  56. } else {
  57. let phoneAssertion = assertion as? PhoneMultiFactorAssertion
  58. guard let credential = phoneAssertion?.authCredential else {
  59. fatalError("Internal Error: Missing credential")
  60. }
  61. switch credential.credentialKind {
  62. case .phoneNumber: fatalError("Internal Error: Missing verificationCode")
  63. case let .verification(verificationID, code):
  64. finalizedMFARequestInfo =
  65. AuthProtoFinalizeMFAPhoneRequestInfo(
  66. sessionInfo: verificationID,
  67. verificationCode: code
  68. )
  69. }
  70. }
  71. let request = FinalizeMFASignInRequest(
  72. mfaPendingCredential: mfaPendingCredential,
  73. verificationInfo: finalizedMFARequestInfo,
  74. requestConfiguration: auth.requestConfiguration
  75. )
  76. Task {
  77. do {
  78. let response = try await AuthBackend.call(with: request)
  79. let user = try await self.auth.completeSignIn(withAccessToken: response.idToken,
  80. accessTokenExpirationDate: nil,
  81. refreshToken: response.refreshToken,
  82. anonymous: false)
  83. let result = AuthDataResult(withUser: user, additionalUserInfo: nil)
  84. let decoratedCallback = self.auth
  85. .signInFlowAuthDataResultCallback(byDecorating: completion)
  86. decoratedCallback(result, nil)
  87. } catch {
  88. if let completion {
  89. completion(nil, error)
  90. }
  91. }
  92. }
  93. }
  94. /** @fn resolveSignInWithAssertion:completion:
  95. @brief A helper function to help users complete sign in with a second factor using an
  96. FIRMultiFactorAssertion confirming the user successfully completed the second factor
  97. challenge.
  98. @param completion The block invoked when the request is complete, or fails.
  99. */
  100. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  101. open func resolveSignIn(with assertion: MultiFactorAssertion) async throws -> AuthDataResult {
  102. return try await withCheckedThrowingContinuation { continuation in
  103. self.resolveSignIn(with: assertion) { result, error in
  104. if let result {
  105. continuation.resume(returning: result)
  106. } else {
  107. continuation.resume(throwing: error!)
  108. }
  109. }
  110. }
  111. }
  112. let mfaPendingCredential: String?
  113. init(with mfaPendingCredential: String?, hints: [MultiFactorInfo], auth: Auth) {
  114. self.mfaPendingCredential = mfaPendingCredential
  115. self.hints = hints
  116. self.auth = auth
  117. session = MultiFactorSession(mfaCredential: mfaPendingCredential)
  118. }
  119. }
  120. #endif