IdentityToolkitRequest.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 kHttpsProtocol = "https:"
  16. private let kHttpProtocol = "http:"
  17. private let kEmulatorHostAndPrefixFormat = "%@/%@"
  18. private let gAPIHost = "www.googleapis.com"
  19. private let kFirebaseAuthAPIHost = "www.googleapis.com"
  20. private let kIdentityPlatformAPIHost = "identitytoolkit.googleapis.com"
  21. private let kFirebaseAuthStagingAPIHost = "staging-www.sandbox.googleapis.com"
  22. private let kIdentityPlatformStagingAPIHost =
  23. "staging-identitytoolkit.sandbox.googleapis.com"
  24. /// Represents a request to an identity toolkit endpoint.
  25. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  26. open class IdentityToolkitRequest {
  27. /// Gets the RPC's endpoint.
  28. public let endpoint: String
  29. /// Gets the client's API key used for the request.
  30. public var apiKey: String
  31. /// The tenant ID of the request. nil if none is available.
  32. public let tenantID: String?
  33. let _requestConfiguration: AuthRequestConfiguration
  34. let _useIdentityPlatform: Bool
  35. let _useStaging: Bool
  36. public init(endpoint: String, requestConfiguration: AuthRequestConfiguration,
  37. useIdentityPlatform: Bool = false, useStaging: Bool = false) {
  38. self.endpoint = endpoint
  39. apiKey = requestConfiguration.apiKey
  40. _requestConfiguration = requestConfiguration
  41. _useIdentityPlatform = useIdentityPlatform
  42. _useStaging = useStaging
  43. tenantID = requestConfiguration.auth?.tenantID
  44. }
  45. public func containsPostBody() -> Bool {
  46. true
  47. }
  48. /// Returns the request's full URL.
  49. public func requestURL() -> URL {
  50. let apiProtocol: String
  51. let apiHostAndPathPrefix: String
  52. let urlString: String
  53. let emulatorHostAndPort = _requestConfiguration.emulatorHostAndPort
  54. if _useIdentityPlatform {
  55. if let emulatorHostAndPort = emulatorHostAndPort {
  56. apiProtocol = kHttpProtocol
  57. apiHostAndPathPrefix = "\(emulatorHostAndPort)/\(kIdentityPlatformAPIHost)"
  58. } else if _useStaging {
  59. apiHostAndPathPrefix = kIdentityPlatformStagingAPIHost
  60. apiProtocol = kHttpsProtocol
  61. } else {
  62. apiHostAndPathPrefix = kIdentityPlatformAPIHost
  63. apiProtocol = kHttpsProtocol
  64. }
  65. urlString = "\(apiProtocol)//\(apiHostAndPathPrefix)/v2/\(endpoint)?key=\(apiKey)"
  66. } else {
  67. if let emulatorHostAndPort = emulatorHostAndPort {
  68. apiProtocol = kHttpProtocol
  69. apiHostAndPathPrefix = "\(emulatorHostAndPort)/\(kFirebaseAuthAPIHost)"
  70. } else if _useStaging {
  71. apiProtocol = kHttpsProtocol
  72. apiHostAndPathPrefix = kFirebaseAuthStagingAPIHost
  73. } else {
  74. apiProtocol = kHttpsProtocol
  75. apiHostAndPathPrefix = kFirebaseAuthAPIHost
  76. }
  77. urlString =
  78. "\(apiProtocol)//\(apiHostAndPathPrefix)/identitytoolkit/v3/relyingparty/\(endpoint)?key=\(apiKey)"
  79. }
  80. return URL(string: urlString)!
  81. }
  82. /// Returns the request's configuration.
  83. public func requestConfiguration() -> AuthRequestConfiguration {
  84. _requestConfiguration
  85. }
  86. }