FunctionsContext.swift 3.3 KB

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