FunctionsContext.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. // `getLimitedUseToken(completion:)` is an optional protocol method.
  59. // If it’s not implemented, we still need to leave the dispatch group.
  60. if let limitedUseTokenClosure = appCheck.getLimitedUseToken {
  61. limitedUseTokenClosure { tokenResult in
  62. // Send only valid token to functions.
  63. if tokenResult.error == nil {
  64. limitedUseAppCheckToken = tokenResult.token
  65. }
  66. dispatchGroup.leave()
  67. }
  68. } else {
  69. dispatchGroup.leave()
  70. }
  71. } else {
  72. appCheck.getToken(forcingRefresh: false) { tokenResult in
  73. // Send only valid token to functions.
  74. if tokenResult.error == nil {
  75. appCheckToken = tokenResult.token
  76. }
  77. dispatchGroup.leave()
  78. }
  79. }
  80. }
  81. dispatchGroup.notify(queue: .main) {
  82. let context = FunctionsContext(authToken: authToken,
  83. fcmToken: self.messaging?.fcmToken,
  84. appCheckToken: appCheckToken,
  85. limitedUseAppCheckToken: limitedUseAppCheckToken)
  86. completion(context, error)
  87. }
  88. }
  89. }