UserInfoImpl.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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(
  80. of: [NSString.self],
  81. forKey: UserInfoImpl.kProviderIDCodingKey
  82. ) as? String,
  83. let userID = coder.decodeObject(
  84. of: [NSString.self],
  85. forKey: UserInfoImpl.kUserIDCodingKey
  86. ) as? String
  87. else {
  88. return nil
  89. }
  90. let displayName = coder.decodeObject(
  91. of: [NSString.self],
  92. forKey: UserInfoImpl.kDisplayNameCodingKey
  93. ) as? String
  94. let photoURL = coder.decodeObject(
  95. of: [NSURL.self],
  96. forKey: UserInfoImpl.kPhotoURLCodingKey
  97. ) as? URL
  98. let email = coder.decodeObject(
  99. of: [NSString.self],
  100. forKey: UserInfoImpl.kEmailCodingKey
  101. ) as? String
  102. let phoneNumber = coder.decodeObject(
  103. of: [NSString.self],
  104. forKey: UserInfoImpl.kPhoneNumberCodingKey
  105. ) as? String
  106. self.init(withProviderID: providerID,
  107. userID: userID,
  108. displayName: displayName,
  109. photoURL: photoURL,
  110. email: email,
  111. phoneNumber: phoneNumber)
  112. }
  113. }