UserInfoImpl.swift 4.6 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. 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,
  24. let uid = providerUserInfo.federatedID else {
  25. // This was a crash in ObjC implementation. Should providerID be not nullable?
  26. fatalError("Missing providerID or uid from GetAccountInfoResponseProviderUserInfo")
  27. }
  28. return UserInfoImpl(withProviderID: providerID,
  29. userID: uid,
  30. displayName: providerUserInfo.displayName,
  31. photoURL: providerUserInfo.photoURL,
  32. email: providerUserInfo.email,
  33. phoneNumber: providerUserInfo.phoneNumber)
  34. }
  35. /// Designated initializer.
  36. /// - Parameter providerID: The provider identifier.
  37. /// - Parameter userID: The unique user ID for the user (the value of the uid field in the token.)
  38. /// - Parameter displayName: The name of the user.
  39. /// - Parameter photoURL: The URL of the user's profile photo.
  40. /// - Parameter email: The user's email address.
  41. /// - Parameter phoneNumber: The user's phone number.
  42. private init(withProviderID providerID: String,
  43. userID: String,
  44. displayName: String?,
  45. photoURL: URL?,
  46. email: String?,
  47. phoneNumber: String?) {
  48. self.providerID = providerID
  49. uid = userID
  50. self.displayName = displayName
  51. self.photoURL = photoURL
  52. self.email = email
  53. self.phoneNumber = phoneNumber
  54. }
  55. var providerID: String
  56. var uid: String
  57. var displayName: String?
  58. var photoURL: URL?
  59. var email: String?
  60. var phoneNumber: String?
  61. // MARK: Secure Coding
  62. private static let kProviderIDCodingKey = "providerID"
  63. private static let kUserIDCodingKey = "userID"
  64. private static let kDisplayNameCodingKey = "displayName"
  65. private static let kPhotoURLCodingKey = "photoURL"
  66. private static let kEmailCodingKey = "email"
  67. private static let kPhoneNumberCodingKey = "phoneNumber"
  68. static var supportsSecureCoding: Bool {
  69. return true
  70. }
  71. func encode(with coder: NSCoder) {
  72. coder.encode(providerID, forKey: UserInfoImpl.kProviderIDCodingKey)
  73. coder.encode(uid, forKey: UserInfoImpl.kUserIDCodingKey)
  74. coder.encode(displayName, forKey: UserInfoImpl.kDisplayNameCodingKey)
  75. coder.encode(photoURL, forKey: UserInfoImpl.kPhotoURLCodingKey)
  76. coder.encode(email, forKey: UserInfoImpl.kEmailCodingKey)
  77. coder.encode(phoneNumber, forKey: UserInfoImpl.kPhoneNumberCodingKey)
  78. }
  79. required convenience init?(coder: NSCoder) {
  80. guard let providerID = coder.decodeObject(
  81. of: [NSString.self],
  82. forKey: UserInfoImpl.kProviderIDCodingKey
  83. ) as? String,
  84. let userID = coder.decodeObject(
  85. of: [NSString.self],
  86. forKey: UserInfoImpl.kUserIDCodingKey
  87. ) as? String
  88. else {
  89. return nil
  90. }
  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. }