IdentityToolkitRequest.swift 4.4 KB

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