FunctionsContext.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. import FirebaseAppCheckInterop
  15. import FirebaseAuthInterop
  16. 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 {
  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. func getContext(options: HTTPSCallableOptions? = nil,
  77. _ completion: @escaping ((FunctionsContext, Error?) -> Void)) {
  78. let dispatchGroup = DispatchGroup()
  79. var authToken: String?
  80. var appCheckToken: String?
  81. var error: Error?
  82. var limitedUseAppCheckToken: String?
  83. if let auth {
  84. dispatchGroup.enter()
  85. auth.getToken(forcingRefresh: false) { token, authError in
  86. authToken = token
  87. error = authError
  88. dispatchGroup.leave()
  89. }
  90. }
  91. if let appCheck {
  92. dispatchGroup.enter()
  93. if options?.requireLimitedUseAppCheckTokens == true {
  94. // `getLimitedUseToken(completion:)` is an optional protocol method.
  95. // If it’s not implemented, we still need to leave the dispatch group.
  96. if let limitedUseTokenClosure = appCheck.getLimitedUseToken {
  97. limitedUseTokenClosure { tokenResult in
  98. // In the case of an error, the token will be the placeholder token.
  99. limitedUseAppCheckToken = tokenResult.token
  100. dispatchGroup.leave()
  101. }
  102. } else {
  103. dispatchGroup.leave()
  104. }
  105. } else {
  106. appCheck.getToken(forcingRefresh: false) { tokenResult in
  107. // In the case of an error, the token will be the placeholder token.
  108. appCheckToken = tokenResult.token
  109. dispatchGroup.leave()
  110. }
  111. }
  112. }
  113. dispatchGroup.notify(queue: .main) {
  114. let context = FunctionsContext(authToken: authToken,
  115. fcmToken: self.messaging?.fcmToken,
  116. appCheckToken: appCheckToken,
  117. limitedUseAppCheckToken: limitedUseAppCheckToken)
  118. completion(context, error)
  119. }
  120. }
  121. }