SecureTokenResponse.swift 1.8 KB

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