AdditionalUserInfo.swift 3.2 KB

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