MultiFactorInfo.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // TODO(Swift 6 Breaking): Make checked Sendable.
  16. #if os(iOS)
  17. extension MultiFactorInfo: NSSecureCoding {}
  18. /// Safe public structure used to represent a second factor entity from a client perspective.
  19. ///
  20. /// This class is available on iOS only.
  21. @objc(FIRMultiFactorInfo) open class MultiFactorInfo: NSObject, @unchecked Sendable {
  22. /// The multi-factor enrollment ID.
  23. @objc(UID) public let uid: String
  24. /// The user friendly name of the current second factor.
  25. @objc public let displayName: String?
  26. /// The second factor enrollment date.
  27. @objc public let enrollmentDate: Date
  28. /// The identifier of the second factor.
  29. @objc public let factorID: String
  30. init(proto: AuthProtoMFAEnrollment, factorID: String) {
  31. guard let uid = proto.mfaEnrollmentID else {
  32. fatalError("Auth Internal Error: Failed to initialize MFA: missing enrollment ID")
  33. }
  34. self.uid = uid
  35. self.factorID = factorID
  36. displayName = proto.displayName
  37. enrollmentDate = proto.enrolledAt ?? Date()
  38. }
  39. // MARK: NSSecureCoding
  40. private let kUIDCodingKey = "uid"
  41. private let kDisplayNameCodingKey = "displayName"
  42. private let kEnrollmentDateCodingKey = "enrollmentDate"
  43. private let kFactorIDCodingKey = "factorID"
  44. public class var supportsSecureCoding: Bool { return true }
  45. public func encode(with coder: NSCoder) {
  46. coder.encode(uid, forKey: kUIDCodingKey)
  47. coder.encode(displayName, forKey: kDisplayNameCodingKey)
  48. coder.encode(enrollmentDate, forKey: kEnrollmentDateCodingKey)
  49. coder.encode(factorID, forKey: kFactorIDCodingKey)
  50. }
  51. public required init?(coder: NSCoder) {
  52. guard let uid = coder.decodeObject(of: [NSString.self], forKey: kUIDCodingKey) as? String,
  53. let factorID = coder.decodeObject(of: [NSString.self],
  54. forKey: kFactorIDCodingKey) as? String,
  55. let enrollmentDate = coder.decodeObject(of: [NSDate.self],
  56. forKey: kEnrollmentDateCodingKey) as? Date
  57. else {
  58. return nil
  59. }
  60. self.uid = uid
  61. self.factorID = factorID
  62. self.enrollmentDate = enrollmentDate
  63. displayName = coder.decodeObject(
  64. of: [NSString.self],
  65. forKey: kDisplayNameCodingKey
  66. ) as? String
  67. }
  68. }
  69. #endif