FunctionsContext.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. // TODO: Implement async await version
  35. // @available(macOS 10.15.0, *)
  36. // internal func getContext() async throws -> FunctionsContext {
  37. // return FunctionsContext(authToken: nil, fcmToken: nil, appCheckToken: nil)
  38. //
  39. // }
  40. func getContext(options: HTTPSCallableOptions? = nil,
  41. _ completion: @escaping ((FunctionsContext, Error?) -> Void)) {
  42. let dispatchGroup = DispatchGroup()
  43. var authToken: String?
  44. var appCheckToken: String?
  45. var error: Error?
  46. var limitedUseAppCheckToken: String?
  47. if let auth {
  48. dispatchGroup.enter()
  49. auth.getToken(forcingRefresh: false) { token, authError in
  50. authToken = token
  51. error = authError
  52. dispatchGroup.leave()
  53. }
  54. }
  55. if let appCheck {
  56. dispatchGroup.enter()
  57. if options?.requireLimitedUseAppCheckTokens == true {
  58. appCheck.getLimitedUseToken? { tokenResult in
  59. // Send only valid token to functions.
  60. if tokenResult.error == nil {
  61. limitedUseAppCheckToken = tokenResult.token
  62. }
  63. dispatchGroup.leave()
  64. }
  65. } else {
  66. appCheck.getToken(forcingRefresh: false) { tokenResult in
  67. // Send only valid token to functions.
  68. if tokenResult.error == nil {
  69. appCheckToken = tokenResult.token
  70. }
  71. dispatchGroup.leave()
  72. }
  73. }
  74. }
  75. dispatchGroup.notify(queue: .main) {
  76. let context = FunctionsContext(authToken: authToken,
  77. fcmToken: self.messaging?.fcmToken,
  78. appCheckToken: appCheckToken,
  79. limitedUseAppCheckToken: limitedUseAppCheckToken)
  80. completion(context, error)
  81. }
  82. }
  83. }