AuthBackendRPCIssuer.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2024 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 FirebaseCore
  15. import FirebaseCoreExtension
  16. import Foundation
  17. #if COCOAPODS
  18. @preconcurrency import GTMSessionFetcher
  19. #else
  20. @preconcurrency import GTMSessionFetcherCore
  21. #endif
  22. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  23. protocol AuthBackendRPCIssuerProtocol: Sendable {
  24. /// Asynchronously send a HTTP request.
  25. /// - Parameter request: The request to be made.
  26. /// - Parameter body: Request body.
  27. /// - Parameter contentType: Content type of the body.
  28. /// - Parameter completionHandler: Handles HTTP response. Invoked asynchronously
  29. /// on the auth global work queue in the future.
  30. func asyncCallToURL<T: AuthRPCRequest>(with request: T,
  31. body: Data?,
  32. contentType: String) async -> (Data?, Error?)
  33. }
  34. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  35. final class AuthBackendRPCIssuer: AuthBackendRPCIssuerProtocol {
  36. let fetcherService: GTMSessionFetcherService
  37. init() {
  38. fetcherService = GTMSessionFetcherService()
  39. fetcherService.userAgent = AuthBackend.authUserAgent()
  40. fetcherService.callbackQueue = kAuthGlobalWorkQueue
  41. // Avoid reusing the session to prevent
  42. // https://github.com/firebase/firebase-ios-sdk/issues/1261
  43. fetcherService.reuseSession = false
  44. }
  45. func asyncCallToURL<T: AuthRPCRequest>(with request: T,
  46. body: Data?,
  47. contentType: String) async -> (Data?, Error?) {
  48. let requestConfiguration = request.requestConfiguration()
  49. let request = await AuthBackend.request(
  50. for: request.requestURL(),
  51. httpMethod: body == nil ? "GET" : "POST",
  52. contentType: contentType,
  53. requestConfiguration: requestConfiguration
  54. )
  55. let fetcher = fetcherService.fetcher(with: request)
  56. if let _ = requestConfiguration.emulatorHostAndPort {
  57. fetcher.allowLocalhostRequest = true
  58. fetcher.allowedInsecureSchemes = ["http"]
  59. }
  60. fetcher.bodyData = body
  61. return await withUnsafeContinuation { continuation in
  62. fetcher.beginFetch { data, error in
  63. continuation.resume(returning: (data, error))
  64. }
  65. }
  66. }
  67. }