SetAccountInfoResponse.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 provider user info part of the response from the setAccountInfo endpoint.
  16. /// See https: // developers.google.com/identity/toolkit/web/reference/relyingparty/setAccountInfo
  17. class SetAccountInfoResponseProviderUserInfo {
  18. /// The ID of the identity provider.
  19. var providerID: String?
  20. /// The user's display name at the identity provider.
  21. var displayName: String?
  22. /// The user's photo URL at the identity provider.
  23. var photoURL: URL?
  24. /// Designated initializer.
  25. /// - Parameter dictionary: The provider user info data from endpoint.
  26. init(dictionary: [String: Any]) {
  27. providerID = dictionary["providerId"] as? String
  28. displayName = dictionary["displayName"] as? String
  29. if let photoURL = dictionary["photoUrl"] as? String {
  30. self.photoURL = URL(string: photoURL)
  31. }
  32. }
  33. }
  34. /// Represents the response from the setAccountInfo endpoint.
  35. /// See https: // developers.google.com/identity/toolkit/web/reference/relyingparty/setAccountInfo
  36. class SetAccountInfoResponse: AuthRPCResponse {
  37. required init() {}
  38. /// The email or the user.
  39. var email: String?
  40. /// The display name of the user.
  41. var displayName: String?
  42. /// The user's profiles at the associated identity providers.
  43. var providerUserInfo: [SetAccountInfoResponseProviderUserInfo]?
  44. /// Either an authorization code suitable for performing an STS token exchange, or the
  45. /// access token from Secure Token Service, depending on whether `returnSecureToken` is set
  46. /// on the request.
  47. var idToken: String?
  48. /// The approximate expiration date of the access token.
  49. var approximateExpirationDate: Date?
  50. /// The refresh token from Secure Token Service.
  51. var refreshToken: String?
  52. func setFields(dictionary: [String: AnyHashable]) throws {
  53. email = dictionary["email"] as? String
  54. displayName = dictionary["displayName"] as? String
  55. idToken = dictionary["idToken"] as? String
  56. if let expiresIn = dictionary["expiresIn"] as? String {
  57. approximateExpirationDate = Date(timeIntervalSinceNow: (expiresIn as NSString)
  58. .doubleValue)
  59. }
  60. refreshToken = dictionary["refreshToken"] as? String
  61. if let providerUserInfoData = dictionary["providerUserInfo"] as? [[String: Any]] {
  62. providerUserInfo = providerUserInfoData.map { .init(dictionary: $0) }
  63. }
  64. }
  65. }