MultiFactorInfo.swift 2.8 KB

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