UserInfoImpl.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. @objc(FIRUserInfoImpl) public class UserInfoImpl: NSObject, UserInfo, NSSecureCoding {
  16. /** @fn userInfoWithGetAccountInfoResponseProviderUserInfo:
  17. @brief A convenience factory method for constructing a @c FIRUserInfo instance from data
  18. returned by the getAccountInfo endpoint.
  19. @param providerUserInfo Data returned by the getAccountInfo endpoint.
  20. @return A new instance of @c FIRUserInfo using data from the getAccountInfo endpoint.
  21. */
  22. @objc public class func userInfo(withGetAccountInfoResponseProviderUserInfo providerUserInfo: GetAccountInfoResponseProviderUserInfo)
  23. -> UserInfoImpl {
  24. guard let providerID = providerUserInfo.providerID,
  25. let uid = providerUserInfo.federatedID else {
  26. // This was a crash in ObjC implementation. Should providerID and federatedID be not nullable?
  27. fatalError("Missing providerID or federatedID from GetAccountInfoResponseProviderUserInfo")
  28. }
  29. return UserInfoImpl(withProviderID: providerID,
  30. userID: uid,
  31. displayName: providerUserInfo.displayName,
  32. photoURL: providerUserInfo.photoURL,
  33. email: providerUserInfo.email,
  34. phoneNumber: providerUserInfo.phoneNumber)
  35. }
  36. /** @fn initWithProviderID:userID:displayName:photoURL:email:
  37. @brief Designated initializer.
  38. @param providerID The provider identifier.
  39. @param userID The unique user ID for the user (the value of the @c uid field in the token.)
  40. @param displayName The name of the user.
  41. @param photoURL The URL of the user's profile photo.
  42. @param email The user's email address.
  43. @param phoneNumber The user's phone number.
  44. */
  45. @objc public 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. public var providerID: String
  59. public var uid: String
  60. public var displayName: String?
  61. public var photoURL: URL?
  62. public var email: String?
  63. public 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. public static var supportsSecureCoding: Bool {
  72. return true
  73. }
  74. public func encode(with coder: NSCoder) {
  75. coder.encode(providerID, forKey: UserInfoImpl.kProviderIDCodingKey)
  76. coder.encode(uid, forKey: UserInfoImpl.kUserIDCodingKey)
  77. coder.encode(displayName, forKey: UserInfoImpl.kDisplayNameCodingKey)
  78. coder.encode(photoURL, forKey: UserInfoImpl.kPhotoURLCodingKey)
  79. coder.encode(email, forKey: UserInfoImpl.kEmailCodingKey)
  80. coder.encode(phoneNumber, forKey: UserInfoImpl.kPhoneNumberCodingKey)
  81. }
  82. public required convenience init?(coder: NSCoder) {
  83. guard let providerID = coder.decodeObject(of: [NSString.self],
  84. forKey: UserInfoImpl.kProviderIDCodingKey) as? String,
  85. let uid = coder.decodeObject(
  86. of: [NSString.self],
  87. forKey: UserInfoImpl.kUserIDCodingKey
  88. ) as? String
  89. else {
  90. return nil
  91. }
  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: uid,
  110. displayName: displayName,
  111. photoURL: photoURL,
  112. email: email,
  113. phoneNumber: phoneNumber)
  114. }
  115. }