SecureTokenResponse.swift 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. class SecureTokenResponse: AuthRPCResponse {
  30. required init() {}
  31. var approximateExpirationDate: Date?
  32. var refreshToken: String?
  33. var accessToken: String?
  34. var idToken: String?
  35. var expectedKind: String? { nil }
  36. func setFields(dictionary: [String: AnyHashable]) throws {
  37. refreshToken = dictionary[kRefreshTokenKey] as? String
  38. self.accessToken = dictionary[kAccessTokenKey] as? String
  39. idToken = dictionary[kIDTokenKey] as? String
  40. guard let accessToken = accessToken else {
  41. throw AuthErrorUtils.unexpectedResponse(deserializedResponse: dictionary)
  42. }
  43. guard !accessToken.isEmpty else {
  44. throw AuthErrorUtils.unexpectedResponse(deserializedResponse: dictionary)
  45. }
  46. if let expiresIn = dictionary[kExpiresInKey] as? String {
  47. approximateExpirationDate = Date(timeIntervalSinceNow: (expiresIn as NSString)
  48. .doubleValue)
  49. }
  50. }
  51. }