IdentityToolkitRequest.swift 4.1 KB

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