EmailLinkSignInResponse.swift 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 emailLinkSignin endpoint.
  16. struct EmailLinkSignInResponse: AuthRPCResponse, AuthMFAResponse {
  17. /// The ID token in the email link sign-in response.
  18. private(set) var idToken: String?
  19. /// The email returned by the IdP.
  20. var email: String?
  21. /// The refreshToken returned by the server.
  22. var refreshToken: String?
  23. /// The approximate expiration date of the access token.
  24. var approximateExpirationDate: Date?
  25. /// Flag indicating that the user signing in is a new user and not a returning user.
  26. var isNewUser: Bool = false
  27. // MARK: - AuthMFAResponse
  28. /// An opaque string that functions as proof that the user has successfully passed the first
  29. /// factor check.
  30. private(set) var mfaPendingCredential: String?
  31. /// Info on which multi-factor authentication providers are enabled.
  32. private(set) var mfaInfo: [AuthProtoMFAEnrollment]?
  33. init(dictionary: [String: AnyHashable]) throws {
  34. email = dictionary["email"] as? String
  35. idToken = dictionary["idToken"] as? String
  36. isNewUser = dictionary["isNewUser"] as? Bool ?? false
  37. refreshToken = dictionary["refreshToken"] as? String
  38. approximateExpirationDate = (dictionary["expiresIn"] as? String)
  39. .flatMap { Date(timeIntervalSinceNow: ($0 as NSString).doubleValue)
  40. }
  41. if let mfaInfoArray = dictionary["mfaInfo"] as? [[String: AnyHashable]] {
  42. var mfaInfo: [AuthProtoMFAEnrollment] = []
  43. for entry in mfaInfoArray {
  44. let enrollment = AuthProtoMFAEnrollment(dictionary: entry)
  45. mfaInfo.append(enrollment)
  46. }
  47. self.mfaInfo = mfaInfo
  48. }
  49. mfaPendingCredential = dictionary["mfaPendingCredential"] as? String
  50. }
  51. }