IdentityToolkitRequest.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. /** @class FIRIdentityToolkitRequest
  25. @brief Represents a request to an identity toolkit endpoint.
  26. */
  27. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  28. @objc(FIRIdentityToolkitRequest) open class IdentityToolkitRequest: NSObject {
  29. /** @property endpoint
  30. @brief Gets the RPC's endpoint.
  31. */
  32. @objc public let endpoint: String
  33. /** @property APIKey
  34. @brief Gets the client's API key used for the request.
  35. */
  36. @objc(APIKey) public var apiKey: String
  37. /** @property tenantID
  38. @brief The tenant ID of the request. nil if none is available.
  39. */
  40. @objc public let tenantID: String?
  41. let _requestConfiguration: AuthRequestConfiguration
  42. let _useIdentityPlatform: Bool
  43. let _useStaging: Bool
  44. @objc public init(endpoint: String, requestConfiguration: AuthRequestConfiguration,
  45. useIdentityPlatform: Bool = false, useStaging: Bool = false) {
  46. self.endpoint = endpoint
  47. apiKey = requestConfiguration.apiKey
  48. _requestConfiguration = requestConfiguration
  49. _useIdentityPlatform = useIdentityPlatform
  50. _useStaging = useStaging
  51. tenantID = requestConfiguration.auth?.tenantID
  52. }
  53. @objc public func containsPostBody() -> Bool {
  54. true
  55. }
  56. /** @fn requestURL
  57. @brief Gets the request's full URL.
  58. */
  59. @objc public func requestURL() -> URL {
  60. let apiProtocol: String
  61. let apiHostAndPathPrefix: String
  62. let urlString: String
  63. let emulatorHostAndPort = _requestConfiguration.emulatorHostAndPort
  64. if _useIdentityPlatform {
  65. if let emulatorHostAndPort = emulatorHostAndPort {
  66. apiProtocol = kHttpProtocol
  67. apiHostAndPathPrefix = "\(emulatorHostAndPort)/\(kIdentityPlatformAPIHost)"
  68. } else if _useStaging {
  69. apiHostAndPathPrefix = kIdentityPlatformStagingAPIHost
  70. apiProtocol = kHttpsProtocol
  71. } else {
  72. apiHostAndPathPrefix = kIdentityPlatformAPIHost
  73. apiProtocol = kHttpsProtocol
  74. }
  75. urlString = "\(apiProtocol)//\(apiHostAndPathPrefix)/v2/\(endpoint)?key=\(apiKey)"
  76. } else {
  77. if let emulatorHostAndPort = emulatorHostAndPort {
  78. apiProtocol = kHttpProtocol
  79. apiHostAndPathPrefix = "\(emulatorHostAndPort)/\(kFirebaseAuthAPIHost)"
  80. } else if _useStaging {
  81. apiProtocol = kHttpsProtocol
  82. apiHostAndPathPrefix = kFirebaseAuthStagingAPIHost
  83. } else {
  84. apiProtocol = kHttpsProtocol
  85. apiHostAndPathPrefix = kFirebaseAuthAPIHost
  86. }
  87. urlString =
  88. "\(apiProtocol)//\(apiHostAndPathPrefix)/identitytoolkit/v3/relyingparty/\(endpoint)?key=\(apiKey)"
  89. }
  90. return URL(string: urlString)!
  91. }
  92. /** @fn requestConfiguration
  93. @brief Gets the request's configuration.
  94. */
  95. @objc public func requestConfiguration() -> AuthRequestConfiguration {
  96. _requestConfiguration
  97. }
  98. }