UserInfoImpl.swift 4.8 KB

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