IdentityToolkitRequest.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. func containsPostBody() -> Bool {
  51. true
  52. }
  53. func queryParams() -> String {
  54. return ""
  55. }
  56. /// Returns the request's full URL.
  57. func requestURL() -> URL {
  58. let apiProtocol: String
  59. let apiHostAndPathPrefix: String
  60. let urlString: String
  61. let emulatorHostAndPort = _requestConfiguration.emulatorHostAndPort
  62. if useIdentityPlatform {
  63. if let emulatorHostAndPort = emulatorHostAndPort {
  64. apiProtocol = kHttpProtocol
  65. apiHostAndPathPrefix = "\(emulatorHostAndPort)/\(kIdentityPlatformAPIHost)"
  66. } else if useStaging {
  67. apiHostAndPathPrefix = kIdentityPlatformStagingAPIHost
  68. apiProtocol = kHttpsProtocol
  69. } else {
  70. apiHostAndPathPrefix = kIdentityPlatformAPIHost
  71. apiProtocol = kHttpsProtocol
  72. }
  73. urlString = "\(apiProtocol)//\(apiHostAndPathPrefix)/v2/\(endpoint)?key=\(apiKey)"
  74. } else {
  75. if let emulatorHostAndPort = emulatorHostAndPort {
  76. apiProtocol = kHttpProtocol
  77. apiHostAndPathPrefix = "\(emulatorHostAndPort)/\(kFirebaseAuthAPIHost)"
  78. } else if useStaging {
  79. apiProtocol = kHttpsProtocol
  80. apiHostAndPathPrefix = kFirebaseAuthStagingAPIHost
  81. } else {
  82. apiProtocol = kHttpsProtocol
  83. apiHostAndPathPrefix = kFirebaseAuthAPIHost
  84. }
  85. urlString =
  86. "\(apiProtocol)//\(apiHostAndPathPrefix)/identitytoolkit/v3/relyingparty/\(endpoint)?key=\(apiKey)"
  87. }
  88. guard let returnURL = URL(string: "\(urlString)\(queryParams())") else {
  89. fatalError("Internal Auth error: Failed to generate URL for \(urlString)")
  90. }
  91. return returnURL
  92. }
  93. /// Returns the request's configuration.
  94. func requestConfiguration() -> AuthRequestConfiguration {
  95. _requestConfiguration
  96. }
  97. // MARK: Internal API for development
  98. static var host: String { gAPIHost }
  99. static func setHost(_ host: String) {
  100. gAPIHost = host
  101. }
  102. }