FunctionsContext.swift 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. func context(options: HTTPSCallableOptions?) async throws -> FunctionsContext {
  35. async let authToken = auth?.getToken(forcingRefresh: false)
  36. async let appCheckToken = getAppCheckToken(options: options)
  37. async let limitedUseAppCheckToken = getLimitedUseAppCheckToken(options: options)
  38. // Only `authToken` is throwing, but the formatter script removes the `try`
  39. // from `try authToken` and puts it in front of the initializer call.
  40. return try await FunctionsContext(
  41. authToken: authToken,
  42. fcmToken: messaging?.fcmToken,
  43. appCheckToken: appCheckToken,
  44. limitedUseAppCheckToken: limitedUseAppCheckToken
  45. )
  46. }
  47. private func getAppCheckToken(options: HTTPSCallableOptions?) async -> String? {
  48. guard
  49. options?.requireLimitedUseAppCheckTokens != true,
  50. let tokenResult = await appCheck?.getToken(forcingRefresh: false)
  51. else { return nil }
  52. // The placeholder token should be used in the case of App Check error.
  53. return tokenResult.token
  54. }
  55. private func getLimitedUseAppCheckToken(options: HTTPSCallableOptions?) async -> String? {
  56. // At the moment, `await` doesn’t get along with Objective-C’s optional protocol methods.
  57. await withCheckedContinuation { (continuation: CheckedContinuation<String?, Never>) in
  58. guard
  59. options?.requireLimitedUseAppCheckTokens == true,
  60. let appCheck,
  61. // `getLimitedUseToken(completion:)` is an optional protocol method. Optional binding
  62. // is performed to make sure `continuation` is called even if the method’s not implemented.
  63. let limitedUseTokenClosure = appCheck.getLimitedUseToken
  64. else {
  65. return continuation.resume(returning: nil)
  66. }
  67. limitedUseTokenClosure { tokenResult in
  68. // The placeholder token should be used in the case of App Check error.
  69. continuation.resume(returning: tokenResult.token)
  70. }
  71. }
  72. }
  73. }