MultiFactorInfo.swift 2.9 KB

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