MultiFactorInfo.swift 3.0 KB

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