SecureTokenResponse.swift 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. private let kExpiresInKey = "expires_in"
  16. /** @var kRefreshTokenKey
  17. @brief The key for the refresh token.
  18. */
  19. private let kRefreshTokenKey = "refresh_token"
  20. /** @var kAccessTokenKey
  21. @brief The key for the access token.
  22. */
  23. private let kAccessTokenKey = "access_token"
  24. /** @var kIDTokenKey
  25. @brief The key for the "id_token" value in the response.
  26. */
  27. private let kIDTokenKey = "id_token"
  28. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  29. @objc(FIRSecureTokenResponse) public class SecureTokenResponse: NSObject, AuthRPCResponse {
  30. @objc public var approximateExpirationDate: Date?
  31. @objc public var refreshToken: String?
  32. @objc public var accessToken: String?
  33. @objc public var IDToken: String?
  34. var expectedKind: String? { nil }
  35. public func setFields(dictionary: [String: AnyHashable]) throws {
  36. refreshToken = dictionary[kRefreshTokenKey] as? String
  37. self.accessToken = dictionary[kAccessTokenKey] as? String
  38. IDToken = dictionary[kIDTokenKey] as? String
  39. guard let accessToken = accessToken else {
  40. throw AuthErrorUtils.unexpectedResponse(deserializedResponse: dictionary)
  41. }
  42. guard !accessToken.isEmpty else {
  43. throw AuthErrorUtils.unexpectedResponse(deserializedResponse: dictionary)
  44. }
  45. if let expiresIn = dictionary[kExpiresInKey] as? String {
  46. approximateExpirationDate = Date(timeIntervalSinceNow: (expiresIn as NSString)
  47. .doubleValue)
  48. }
  49. }
  50. }