ExchangeServiceModels.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2022 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. // MARK: - Request Models
  16. /// Request for exchanging a Firebase Installations token for an Auth Exchange access token.
  17. struct ExchangeInstallationAuthTokenRequest: Encodable {
  18. /// A Firebase Installations auth token as a base64-encoded JWT.
  19. let installationAuthToken: String
  20. }
  21. /// Request for exchanging a custom auth token for an Auth Exchange access token.
  22. struct ExchangeCustomTokenRequest: Encodable {
  23. /// A custom base64-encoded JWT signed with the developer's credentials.
  24. let customToken: String
  25. }
  26. /// Request for exchanging an OpenID Connect (OIDC) token for an Auth Exchange access token.
  27. struct ExchangeOIDCTokenRequest: Encodable {
  28. /// The display name or identifier of the OIDC provider.
  29. let providerID: String
  30. struct ImplicitCredentials: Encodable {
  31. let idToken: String
  32. }
  33. struct AuthCodeCredentials: Encodable {
  34. let sessionID: String
  35. let credentialURI: String
  36. }
  37. // TODO(andrewheard): Investigate using an enum for credentials to represent oneof semantics.
  38. let implicitCredentials: ImplicitCredentials?
  39. let authCodeCredentials: AuthCodeCredentials?
  40. init(providerID: String, implicitCredentials: ImplicitCredentials) {
  41. self.providerID = providerID
  42. self.implicitCredentials = implicitCredentials
  43. authCodeCredentials = nil
  44. }
  45. init(providerID: String, authCodeCredentials: AuthCodeCredentials) {
  46. self.providerID = providerID
  47. implicitCredentials = nil
  48. self.authCodeCredentials = authCodeCredentials
  49. }
  50. }
  51. // MARK: - Response Models
  52. /// Response that encapsulates an Auth Exchange access token.
  53. ///
  54. /// This is the return value for an `ExchangeInstallationAuthTokenRequest` or
  55. /// `ExchangeCustomTokenRequest`.
  56. struct ExchangeTokenResponse: Decodable, Equatable {
  57. /// A container for a Firebase access token and the duration of time until it expires.
  58. struct AuthExchangeToken: Decodable, Equatable {
  59. /// A signed [JWT](https://tools.ietf.org/html/rfc7519) containing claims that identify a user.
  60. let accessToken: String
  61. /// The duration of time until the `accessToken` expires, approximately relative to the time
  62. /// this response was received.
  63. let timeToLive: ProtobufDuration
  64. enum CodingKeys: String, CodingKey {
  65. case accessToken
  66. case timeToLive = "ttl"
  67. }
  68. }
  69. /// An Auth Exchange access token that can be used to access Firebase services.
  70. let token: AuthExchangeToken
  71. }
  72. /// Model of a `google.protobuf.Duration`, which represents a time span.
  73. ///
  74. /// A Protocol Buffer
  75. /// [`Duration`](https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#duration)
  76. /// represents a signed, fixed-length span of time represented as a count of seconds and fractions
  77. /// of seconds at nanosecond resolution. It is independent of any calendar and concepts like "day"
  78. /// or "month".
  79. struct ProtobufDuration: Equatable {
  80. private static let nanosecondsPerSecond = 1e9 // 1 x 10⁹ nanoseconds / second
  81. /// The number of seconds in the time span.
  82. let seconds: Int64
  83. /// The fraction of a second, at nanosecond resolution, in addition to `seconds` in the time span.
  84. let nanoseconds: Int32
  85. /// Floating-point representation of the time span in seconds.
  86. var duration: TimeInterval {
  87. return TimeInterval(seconds) + Double(nanoseconds) / ProtobufDuration.nanosecondsPerSecond
  88. }
  89. /// Creates a new instance from a JSON string representation of a `Duration`.
  90. ///
  91. /// - parameter json: a string with the format "`{seconds}.{nanoseconds}s`", with nanoseconds
  92. /// expressed as fractional seconds.
  93. init(json: String) throws {
  94. (seconds, nanoseconds) = try parseDuration(text: json)
  95. }
  96. }
  97. extension ProtobufDuration: Decodable {
  98. init(from decoder: Decoder) throws {
  99. let durationString = try decoder.singleValueContainer().decode(String.self)
  100. try self.init(json: durationString)
  101. }
  102. }