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