IdentityToolkitRequest.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 var 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. class IdentityToolkitRequest {
  27. /// Gets the RPC's endpoint.
  28. let endpoint: String
  29. /// Gets the client's API key used for the request.
  30. var apiKey: String
  31. /// The tenant ID of the request. nil if none is available.
  32. let tenantID: String?
  33. /** @property useIdentityPlatform
  34. @brief The toggle of using Identity Platform endpoints.
  35. */
  36. let useIdentityPlatform: Bool
  37. /** @property useStaging
  38. @brief The toggle of using staging endpoints.
  39. */
  40. let useStaging: Bool
  41. /** @property clientType
  42. @brief The type of the client that the request sent from, which should be CLIENT_TYPE_IOS;
  43. */
  44. var clientType: String
  45. private let _requestConfiguration: AuthRequestConfiguration
  46. init(endpoint: String, requestConfiguration: AuthRequestConfiguration,
  47. useIdentityPlatform: Bool = false, useStaging: Bool = false) {
  48. self.endpoint = endpoint
  49. apiKey = requestConfiguration.apiKey
  50. _requestConfiguration = requestConfiguration
  51. self.useIdentityPlatform = useIdentityPlatform
  52. self.useStaging = useStaging
  53. clientType = "CLIENT_TYPE_IOS"
  54. tenantID = requestConfiguration.auth?.tenantID
  55. }
  56. func containsPostBody() -> Bool {
  57. true
  58. }
  59. func queryParams() -> String {
  60. return ""
  61. }
  62. /// Returns the request's full URL.
  63. func requestURL() -> URL {
  64. let apiProtocol: String
  65. let apiHostAndPathPrefix: String
  66. let urlString: String
  67. let emulatorHostAndPort = _requestConfiguration.emulatorHostAndPort
  68. if useIdentityPlatform {
  69. if let emulatorHostAndPort = emulatorHostAndPort {
  70. apiProtocol = kHttpProtocol
  71. apiHostAndPathPrefix = "\(emulatorHostAndPort)/\(kIdentityPlatformAPIHost)"
  72. } else if useStaging {
  73. apiHostAndPathPrefix = kIdentityPlatformStagingAPIHost
  74. apiProtocol = kHttpsProtocol
  75. } else {
  76. apiHostAndPathPrefix = kIdentityPlatformAPIHost
  77. apiProtocol = kHttpsProtocol
  78. }
  79. urlString = "\(apiProtocol)//\(apiHostAndPathPrefix)/v2/\(endpoint)?key=\(apiKey)"
  80. } else {
  81. if let emulatorHostAndPort = emulatorHostAndPort {
  82. apiProtocol = kHttpProtocol
  83. apiHostAndPathPrefix = "\(emulatorHostAndPort)/\(kFirebaseAuthAPIHost)"
  84. } else if useStaging {
  85. apiProtocol = kHttpsProtocol
  86. apiHostAndPathPrefix = kFirebaseAuthStagingAPIHost
  87. } else {
  88. apiProtocol = kHttpsProtocol
  89. apiHostAndPathPrefix = kFirebaseAuthAPIHost
  90. }
  91. urlString =
  92. "\(apiProtocol)//\(apiHostAndPathPrefix)/identitytoolkit/v3/relyingparty/\(endpoint)?key=\(apiKey)"
  93. }
  94. guard let returnURL = URL(string: "\(urlString)\(queryParams())") else {
  95. fatalError("Internal Auth error: Failed to generate URL for \(urlString)")
  96. }
  97. return returnURL
  98. }
  99. /// Returns the request's configuration.
  100. func requestConfiguration() -> AuthRequestConfiguration {
  101. _requestConfiguration
  102. }
  103. // MARK: Internal API for development
  104. static var host: String { gAPIHost }
  105. static func setHost(_ host: String) {
  106. gAPIHost = host
  107. }
  108. }