UserInfoImpl.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. extension UserInfoImpl: NSSecureCoding {}
  16. @objc(FIRUserInfoImpl) class UserInfoImpl: NSObject, UserInfo {
  17. /// A convenience factory method for constructing a `UserInfo` instance from data
  18. /// returned by the getAccountInfo endpoint.
  19. /// - Parameter providerUserInfo: Data returned by the getAccountInfo endpoint.
  20. /// - Returns: A new instance of `UserInfo` using data from the getAccountInfo endpoint.
  21. class func userInfo(withGetAccountInfoResponseProviderUserInfo providerUserInfo: GetAccountInfoResponseProviderUserInfo)
  22. -> UserInfoImpl {
  23. guard let providerID = providerUserInfo.providerID else {
  24. // This was a crash in ObjC implementation. Should providerID be not nullable?
  25. fatalError("Missing providerID from GetAccountInfoResponseProviderUserInfo")
  26. }
  27. return UserInfoImpl(withProviderID: providerID,
  28. userID: providerUserInfo.federatedID,
  29. displayName: providerUserInfo.displayName,
  30. photoURL: providerUserInfo.photoURL,
  31. email: providerUserInfo.email,
  32. phoneNumber: providerUserInfo.phoneNumber)
  33. }
  34. /// Designated initializer.
  35. /// - Parameter providerID: The provider identifier.
  36. /// - Parameter userID: The unique user ID for the user (the value of the uid field in the token.)
  37. /// - Parameter displayName: The name of the user.
  38. /// - Parameter photoURL: The URL of the user's profile photo.
  39. /// - Parameter email: The user's email address.
  40. /// - Parameter phoneNumber: The user's phone number.
  41. private init(withProviderID providerID: String,
  42. userID: String?,
  43. displayName: String?,
  44. photoURL: URL?,
  45. email: String?,
  46. phoneNumber: String?) {
  47. self.providerID = providerID
  48. uid = userID
  49. self.displayName = displayName
  50. self.photoURL = photoURL
  51. self.email = email
  52. self.phoneNumber = phoneNumber
  53. }
  54. var providerID: String
  55. var uid: String?
  56. var displayName: String?
  57. var photoURL: URL?
  58. var email: String?
  59. var phoneNumber: String?
  60. // MARK: Secure Coding
  61. private static let kProviderIDCodingKey = "providerID"
  62. private static let kUserIDCodingKey = "userID"
  63. private static let kDisplayNameCodingKey = "displayName"
  64. private static let kPhotoURLCodingKey = "photoURL"
  65. private static let kEmailCodingKey = "email"
  66. private static let kPhoneNumberCodingKey = "phoneNumber"
  67. static var supportsSecureCoding: Bool {
  68. return true
  69. }
  70. func encode(with coder: NSCoder) {
  71. coder.encode(providerID, forKey: UserInfoImpl.kProviderIDCodingKey)
  72. coder.encode(uid, forKey: UserInfoImpl.kUserIDCodingKey)
  73. coder.encode(displayName, forKey: UserInfoImpl.kDisplayNameCodingKey)
  74. coder.encode(photoURL, forKey: UserInfoImpl.kPhotoURLCodingKey)
  75. coder.encode(email, forKey: UserInfoImpl.kEmailCodingKey)
  76. coder.encode(phoneNumber, forKey: UserInfoImpl.kPhoneNumberCodingKey)
  77. }
  78. required convenience init?(coder: NSCoder) {
  79. guard let providerID = coder.decodeObject(of: [NSString.self],
  80. forKey: UserInfoImpl.kProviderIDCodingKey) as? String
  81. else {
  82. return nil
  83. }
  84. let uid = coder.decodeObject(
  85. of: [NSString.self],
  86. forKey: UserInfoImpl.kUserIDCodingKey
  87. ) as? String
  88. let displayName = coder.decodeObject(
  89. of: [NSString.self],
  90. forKey: UserInfoImpl.kDisplayNameCodingKey
  91. ) as? String
  92. let photoURL = coder.decodeObject(
  93. of: [NSURL.self],
  94. forKey: UserInfoImpl.kPhotoURLCodingKey
  95. ) as? URL
  96. let email = coder.decodeObject(
  97. of: [NSString.self],
  98. forKey: UserInfoImpl.kEmailCodingKey
  99. ) as? String
  100. let phoneNumber = coder.decodeObject(
  101. of: [NSString.self],
  102. forKey: UserInfoImpl.kPhoneNumberCodingKey
  103. ) as? String
  104. self.init(withProviderID: providerID,
  105. userID: uid,
  106. displayName: displayName,
  107. photoURL: photoURL,
  108. email: email,
  109. phoneNumber: phoneNumber)
  110. }
  111. }