AdditionalUserInfo.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 AdditionalUserInfo: NSSecureCoding {}
  16. @objc(FIRAdditionalUserInfo) open class AdditionalUserInfo: NSObject {
  17. /** @property providerID
  18. @brief The provider identifier.
  19. */
  20. @objc public let providerID: String
  21. /** @property profile
  22. @brief Dictionary containing the additional IdP specific information.
  23. */
  24. @objc public let profile: [String: Any]?
  25. /** @property username
  26. @brief username The name of the user.
  27. */
  28. @objc public let username: String?
  29. /** @property isMewUser
  30. @brief Indicates whether or not the current user was signed in for the first time.
  31. */
  32. @objc public let isNewUser: Bool
  33. // Maintain newUser for Objective C API.
  34. @objc open func newUser() -> Bool {
  35. return isNewUser
  36. }
  37. init(providerID: String, profile: [String: Any]?, username: String?, isNewUser: Bool) {
  38. self.providerID = providerID
  39. self.profile = profile
  40. self.username = username
  41. self.isNewUser = isNewUser
  42. }
  43. // MARK: Secure Coding
  44. private static let providerIDCodingKey = "providerID"
  45. private static let profileCodingKey = "profile"
  46. private static let usernameCodingKey = "username"
  47. private static let newUserKey = "newUser"
  48. public static var supportsSecureCoding: Bool {
  49. return true
  50. }
  51. public required init?(coder aDecoder: NSCoder) {
  52. guard let providerID = aDecoder.decodeObject(
  53. of: NSString.self,
  54. forKey: AdditionalUserInfo.providerIDCodingKey
  55. ) as? String else {
  56. return nil
  57. }
  58. self.providerID = providerID
  59. profile = aDecoder.decodeObject(
  60. of: [NSDictionary.self, NSString.self],
  61. forKey: AdditionalUserInfo.profileCodingKey
  62. ) as? [String: Any]
  63. username = aDecoder.decodeObject(
  64. of: NSString.self,
  65. forKey: AdditionalUserInfo.usernameCodingKey
  66. ) as String?
  67. if let newUser = aDecoder.decodeObject(
  68. of: NSNumber.self,
  69. forKey: AdditionalUserInfo.newUserKey
  70. ) {
  71. isNewUser = newUser.intValue == 1
  72. } else {
  73. isNewUser = false
  74. }
  75. }
  76. public func encode(with aCoder: NSCoder) {
  77. aCoder.encode(providerID, forKey: AdditionalUserInfo.providerIDCodingKey)
  78. aCoder.encode(profile, forKey: AdditionalUserInfo.profileCodingKey)
  79. aCoder.encode(username, forKey: AdditionalUserInfo.usernameCodingKey)
  80. aCoder.encode(isNewUser ? NSNumber(1) : NSNumber(0), forKey: AdditionalUserInfo.newUserKey)
  81. }
  82. }