Files
firebase-ios-sdk/FirebaseFunctions/Sources/Internal/FunctionsContext.swift
T

103 lines
3.2 KiB
Swift
Raw Normal View History

// Copyright 2022 Google LLC
2022-01-26 15:24:08 -05:00
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2022-01-26 15:24:08 -05:00
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2022-01-26 15:24:08 -05:00
import FirebaseAppCheckInterop
import FirebaseAuthInterop
import FirebaseMessagingInterop
2023-11-21 12:12:05 -08:00
import Foundation
2022-01-26 15:24:08 -05:00
/// FunctionsContext is a helper class for gathering metadata for a function call.
2023-11-21 12:12:05 -08:00
class FunctionsContext: NSObject {
2022-01-26 15:24:08 -05:00
let authToken: String?
let fcmToken: String?
let appCheckToken: String?
let limitedUseAppCheckToken: String?
2022-01-26 15:24:08 -05:00
init(authToken: String?, fcmToken: String?, appCheckToken: String?,
limitedUseAppCheckToken: String?) {
2022-01-26 15:24:08 -05:00
self.authToken = authToken
self.fcmToken = fcmToken
self.appCheckToken = appCheckToken
self.limitedUseAppCheckToken = limitedUseAppCheckToken
2022-01-26 15:24:08 -05:00
}
}
2023-11-21 12:12:05 -08:00
class FunctionsContextProvider: NSObject {
2022-01-26 15:24:08 -05:00
private var auth: AuthInterop?
private var messaging: MessagingInterop?
private var appCheck: AppCheckInterop?
2022-01-26 15:24:08 -05:00
init(auth: AuthInterop?, messaging: MessagingInterop?, appCheck: AppCheckInterop?) {
self.auth = auth
self.messaging = messaging
self.appCheck = appCheck
}
// TODO: Implement async await version
// @available(macOS 10.15.0, *)
// internal func getContext() async throws -> FunctionsContext {
// return FunctionsContext(authToken: nil, fcmToken: nil, appCheckToken: nil)
//
// }
2023-11-21 12:12:05 -08:00
func getContext(options: HTTPSCallableOptions? = nil,
_ completion: @escaping ((FunctionsContext, Error?) -> Void)) {
2022-01-26 15:24:08 -05:00
let dispatchGroup = DispatchGroup()
var authToken: String?
var appCheckToken: String?
var error: Error?
var limitedUseAppCheckToken: String?
2022-01-26 15:24:08 -05:00
if let auth {
2022-01-26 15:24:08 -05:00
dispatchGroup.enter()
auth.getToken(forcingRefresh: false) { token, authError in
authToken = token
error = authError
2022-01-26 15:24:08 -05:00
dispatchGroup.leave()
}
}
if let appCheck {
2022-01-26 15:24:08 -05:00
dispatchGroup.enter()
if options?.requireLimitedUseAppCheckTokens == true {
appCheck.getLimitedUseToken? { tokenResult in
// Send only valid token to functions.
if tokenResult.error == nil {
limitedUseAppCheckToken = tokenResult.token
}
dispatchGroup.leave()
}
} else {
appCheck.getToken(forcingRefresh: false) { tokenResult in
// Send only valid token to functions.
if tokenResult.error == nil {
appCheckToken = tokenResult.token
}
dispatchGroup.leave()
}
2022-01-26 15:24:08 -05:00
}
}
dispatchGroup.notify(queue: .main) {
let context = FunctionsContext(authToken: authToken,
fcmToken: self.messaging?.fcmToken,
appCheckToken: appCheckToken,
limitedUseAppCheckToken: limitedUseAppCheckToken)
2022-01-26 15:24:08 -05:00
completion(context, error)
}
}
}