FunctionsContext.swift 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2022 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. @preconcurrency import FirebaseAppCheckInterop
  15. @preconcurrency import FirebaseAuthInterop
  16. @preconcurrency import FirebaseMessagingInterop
  17. import Foundation
  18. /// `FunctionsContext` is a helper object that holds metadata for a function call.
  19. struct FunctionsContext {
  20. let authToken: String?
  21. let fcmToken: String?
  22. let appCheckToken: String?
  23. let limitedUseAppCheckToken: String?
  24. }
  25. struct FunctionsContextProvider: Sendable {
  26. private let auth: AuthInterop?
  27. private let messaging: MessagingInterop?
  28. private let appCheck: AppCheckInterop?
  29. init(auth: AuthInterop?, messaging: MessagingInterop?, appCheck: AppCheckInterop?) {
  30. self.auth = auth
  31. self.messaging = messaging
  32. self.appCheck = appCheck
  33. }
  34. @available(iOS 13, macCatalyst 13, macOS 10.15, tvOS 13, watchOS 7, *)
  35. func context(options: HTTPSCallableOptions?) async throws -> FunctionsContext {
  36. async let authToken = auth?.getToken(forcingRefresh: false)
  37. async let appCheckToken = getAppCheckToken(options: options)
  38. async let limitedUseAppCheckToken = getLimitedUseAppCheckToken(options: options)
  39. // Only `authToken` is throwing, but the formatter script removes the `try`
  40. // from `try authToken` and puts it in front of the initializer call.
  41. return try await FunctionsContext(
  42. authToken: authToken,
  43. fcmToken: messaging?.fcmToken,
  44. appCheckToken: appCheckToken,
  45. limitedUseAppCheckToken: limitedUseAppCheckToken
  46. )
  47. }
  48. @available(iOS 13, macCatalyst 13, macOS 10.15, tvOS 13, watchOS 7, *)
  49. private func getAppCheckToken(options: HTTPSCallableOptions?) async -> String? {
  50. guard
  51. options?.requireLimitedUseAppCheckTokens != true,
  52. let tokenResult = await appCheck?.getToken(forcingRefresh: false)
  53. else { return nil }
  54. // The placeholder token should be used in the case of App Check error.
  55. return tokenResult.token
  56. }
  57. @available(iOS 13, macCatalyst 13, macOS 10.15, tvOS 13, watchOS 7, *)
  58. private func getLimitedUseAppCheckToken(options: HTTPSCallableOptions?) async -> String? {
  59. // At the moment, `await` doesn’t get along with Objective-C’s optional protocol methods.
  60. await withCheckedContinuation { (continuation: CheckedContinuation<String?, Never>) in
  61. guard
  62. options?.requireLimitedUseAppCheckTokens == true,
  63. let appCheck,
  64. // `getLimitedUseToken(completion:)` is an optional protocol method. Optional binding
  65. // is performed to make sure `continuation` is called even if the method’s not implemented.
  66. let limitedUseTokenClosure = appCheck.getLimitedUseToken
  67. else {
  68. return continuation.resume(returning: nil)
  69. }
  70. limitedUseTokenClosure { tokenResult in
  71. // The placeholder token should be used in the case of App Check error.
  72. continuation.resume(returning: tokenResult.token)
  73. }
  74. }
  75. }
  76. }