UserInfoImpl.swift 4.8 KB

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