FunctionsContext.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 Foundation
  15. import FirebaseAppCheckInterop
  16. import FirebaseAuthInterop
  17. import FirebaseMessagingInterop
  18. /// FunctionsContext is a helper class for gathering metadata for a function call.
  19. internal class FunctionsContext: NSObject {
  20. let authToken: String?
  21. let fcmToken: String?
  22. let appCheckToken: String?
  23. init(authToken: String?, fcmToken: String?, appCheckToken: String?) {
  24. self.authToken = authToken
  25. self.fcmToken = fcmToken
  26. self.appCheckToken = appCheckToken
  27. }
  28. }
  29. internal class FunctionsContextProvider: NSObject {
  30. private var auth: AuthInterop?
  31. private var messaging: MessagingInterop?
  32. private var appCheck: AppCheckInterop?
  33. init(auth: AuthInterop?, messaging: MessagingInterop?, appCheck: AppCheckInterop?) {
  34. self.auth = auth
  35. self.messaging = messaging
  36. self.appCheck = appCheck
  37. }
  38. // TODO: Implement async await version
  39. // @available(macOS 10.15.0, *)
  40. // internal func getContext() async throws -> FunctionsContext {
  41. // return FunctionsContext(authToken: nil, fcmToken: nil, appCheckToken: nil)
  42. //
  43. // }
  44. internal func getContext(_ completion: @escaping ((FunctionsContext, Error?) -> Void)) {
  45. let dispatchGroup = DispatchGroup()
  46. var authToken: String?
  47. var appCheckToken: String?
  48. var error: Error?
  49. if let auth = auth {
  50. dispatchGroup.enter()
  51. auth.getToken(forcingRefresh: false) { token, authError in
  52. authToken = token
  53. error = authError
  54. dispatchGroup.leave()
  55. }
  56. }
  57. if let appCheck = appCheck {
  58. dispatchGroup.enter()
  59. appCheck.getToken(forcingRefresh: false) { tokenResult in
  60. // Send only valid token to functions.
  61. if tokenResult.error == nil {
  62. appCheckToken = tokenResult.token
  63. }
  64. dispatchGroup.leave()
  65. }
  66. }
  67. dispatchGroup.notify(queue: .main) {
  68. let context = FunctionsContext(authToken: authToken,
  69. fcmToken: self.messaging?.fcmToken,
  70. appCheckToken: appCheckToken)
  71. completion(context, error)
  72. }
  73. }
  74. }