MultiFactorSession.swift 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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) || os(macOS)
  16. /// Opaque object that identifies the current session to enroll a second factor or to
  17. /// complete sign in when previously enrolled.
  18. ///
  19. /// Identifies the current session to enroll a second factor
  20. /// or to complete sign in when previously enrolled. It contains additional context on the
  21. /// existing user, notably the confirmation that the user passed the first factor challenge.
  22. ///
  23. /// This class is available on iOS and macOS.
  24. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  25. @objc(FIRMultiFactorSession) open class MultiFactorSession: NSObject {
  26. /// The ID token for an enroll flow. This has to be retrieved after recent authentication.
  27. let idToken: String?
  28. /// The pending credential after an enrolled second factor user signs in successfully with the
  29. /// first factor.
  30. let mfaPendingCredential: String?
  31. /// Current user object.
  32. let currentUser: User?
  33. /// Multi factor info for the current user.
  34. var multiFactorInfo: MultiFactorInfo?
  35. class func session(for user: User?) -> MultiFactorSession {
  36. let currentUser = user ?? Auth.auth().currentUser
  37. guard let currentUser else {
  38. fatalError("Internal Auth Error: missing user for multifactor auth")
  39. }
  40. return .init(
  41. idToken: currentUser.tokenService.accessToken,
  42. mfaPendingCredential: nil,
  43. multiFactorInfo: nil,
  44. currentUser: currentUser
  45. )
  46. }
  47. convenience init(mfaCredential: String?) {
  48. self.init(
  49. idToken: nil,
  50. mfaPendingCredential: mfaCredential,
  51. multiFactorInfo: nil,
  52. currentUser: nil
  53. )
  54. }
  55. private init(idToken: String?, mfaPendingCredential: String?, multiFactorInfo: MultiFactorInfo?,
  56. currentUser: User?) {
  57. self.idToken = idToken
  58. self.mfaPendingCredential = mfaPendingCredential
  59. self.multiFactorInfo = multiFactorInfo
  60. self.currentUser = currentUser
  61. }
  62. }
  63. #endif