GetAccountInfoResponse.swift 5.3 KB

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