GetAccountInfoResponse.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. /// The key for the "error" value in JSON responses from the server.
  16. private let kErrorKey = "error"
  17. /// Represents the provider user info part of the response from the getAccountInfo endpoint.
  18. /// See https://developers.google.com/identity/toolkit/web/reference/relyingparty/getAccountInfo
  19. class GetAccountInfoResponseProviderUserInfo {
  20. /// The ID of the identity provider.
  21. let providerID: String?
  22. /// The user's display name at the identity provider.
  23. let displayName: String?
  24. /// The user's photo URL at the identity provider.
  25. let photoURL: URL?
  26. /// The user's identifier at the identity provider.
  27. let federatedID: String?
  28. /// The user's email at the identity provider.
  29. let email: String?
  30. /// A phone number associated with the user.
  31. let phoneNumber: String?
  32. /// Designated initializer.
  33. /// - Parameter dictionary: The provider user info data from endpoint.
  34. init(dictionary: [String: Any]) {
  35. providerID = dictionary["providerId"] as? String
  36. displayName = dictionary["displayName"] as? String
  37. if let photoURL = dictionary["photoUrl"] as? String {
  38. self.photoURL = URL(string: photoURL)
  39. } else {
  40. photoURL = nil
  41. }
  42. federatedID =
  43. dictionary["federatedId"] as? String
  44. email = dictionary["email"] as? String
  45. phoneNumber = dictionary["phoneNumber"] as? String
  46. }
  47. }
  48. /// Represents the firebase user info part of the response from the getAccountInfo endpoint.
  49. /// See https://developers.google.com/identity/toolkit/web/reference/relyingparty/getAccountInfo
  50. class GetAccountInfoResponseUser {
  51. /// The ID of the user.
  52. let localID: String?
  53. /// The email or the user.
  54. let email: String?
  55. /// Whether the email has been verified.
  56. let emailVerified: Bool
  57. /// The display name of the user.
  58. let displayName: String?
  59. /// The user's photo URL.
  60. let photoURL: URL?
  61. /// The user's creation date.
  62. let creationDate: Date?
  63. /// The user's last login date.
  64. let lastLoginDate: Date?
  65. /// The user's profiles at the associated identity providers.
  66. let providerUserInfo: [GetAccountInfoResponseProviderUserInfo]?
  67. /// Information about user's password.
  68. /// This is not necessarily the hash of user's actual password.
  69. let passwordHash: String?
  70. /// A phone number associated with the user.
  71. let phoneNumber: String?
  72. let mfaEnrollments: [AuthProtoMFAEnrollment]?
  73. /// Designated initializer.
  74. /// - Parameter dictionary: The provider user info data from endpoint.
  75. init(dictionary: [String: Any]) {
  76. if let providerUserInfoData = dictionary["providerUserInfo"] as? [[String: Any]] {
  77. providerUserInfo = providerUserInfoData.map {
  78. GetAccountInfoResponseProviderUserInfo(dictionary: $0)
  79. }
  80. } else {
  81. providerUserInfo = nil
  82. }
  83. localID = dictionary["localId"] as? String
  84. displayName = dictionary["displayName"] as? String
  85. email = dictionary["email"] as? String
  86. if let photoURL = dictionary["photoUrl"] as? String {
  87. self.photoURL = URL(string: photoURL)
  88. } else {
  89. photoURL = nil
  90. }
  91. if let createdAt = dictionary["createdAt"] as? String,
  92. let timeInterval = Double(createdAt) {
  93. // Divide by 1000 in order to convert milliseconds to seconds.
  94. creationDate = Date(timeIntervalSince1970: timeInterval / 1000)
  95. } else {
  96. creationDate = nil
  97. }
  98. if let lastLoginAt = dictionary["lastLoginAt"] as? String,
  99. let timeInterval = Double(lastLoginAt) {
  100. // Divide by 1000 in order to convert milliseconds to seconds.
  101. lastLoginDate = Date(timeIntervalSince1970: timeInterval / 1000)
  102. } else {
  103. lastLoginDate = nil
  104. }
  105. emailVerified = dictionary["emailVerified"] as? Bool ?? false
  106. passwordHash = dictionary["passwordHash"] as? String
  107. phoneNumber = dictionary["phoneNumber"] as? String
  108. if let mfaEnrollmentData = dictionary["mfaInfo"] as? [[String: AnyHashable]] {
  109. mfaEnrollments = mfaEnrollmentData.map { AuthProtoMFAEnrollment(dictionary: $0)
  110. }
  111. } else {
  112. mfaEnrollments = nil
  113. }
  114. }
  115. }
  116. /// Represents the response from the setAccountInfo endpoint.
  117. /// See https://developers.google.com/identity/toolkit/web/reference/relyingparty/getAccountInfo
  118. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  119. class GetAccountInfoResponse: AuthRPCResponse {
  120. required init() {}
  121. /// The requested users' profiles.
  122. var users: [GetAccountInfoResponseUser]?
  123. func setFields(dictionary: [String: AnyHashable]) throws {
  124. guard let usersData = dictionary["users"] as? [[String: AnyHashable]] else {
  125. throw AuthErrorUtils.unexpectedResponse(deserializedResponse: dictionary)
  126. }
  127. guard usersData.count == 1 else {
  128. throw AuthErrorUtils.unexpectedResponse(deserializedResponse: dictionary)
  129. }
  130. users = [GetAccountInfoResponseUser(dictionary: usersData[0])]
  131. }
  132. }