Skip to content

Commit ce9fb34

Browse files
authored
Add appcheck for Dataconnect (#13302)
1 parent c4809b9 commit ce9fb34

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

FirebaseDataConnect/Sources/DataConnect.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import Foundation
1616

17+
import FirebaseAppCheck
1718
import FirebaseAuth
1819
import FirebaseCore
1920

@@ -64,7 +65,8 @@ public class DataConnect {
6465
projectId: projectID,
6566
settings: settings,
6667
connectorConfig: connectorConfig,
67-
auth: Auth.auth(app: app)
68+
auth: Auth.auth(app: app),
69+
appCheck: AppCheck.appCheck(app: app)
6870
)
6971

7072
operationsManager = OperationsManager(grpcClient: grpcClient)
@@ -85,7 +87,8 @@ public class DataConnect {
8587
projectId: projectID,
8688
settings: settings,
8789
connectorConfig: connectorConfig,
88-
auth: Auth.auth(app: app)
90+
auth: Auth.auth(app: app),
91+
appCheck: AppCheck.appCheck(app: app)
8992
)
9093
operationsManager = OperationsManager(grpcClient: grpcClient)
9194
}

FirebaseDataConnect/Sources/Internal/GrpcClient.swift

+30-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import Foundation
1616

17+
import FirebaseAppCheck
1718
import FirebaseAuth
1819
import FirebaseCoreInternal
1920
import FirebaseSharedSwift
@@ -40,9 +41,14 @@ actor GrpcClient: CustomStringConvertible {
4041

4142
private let auth: Auth
4243

44+
private let appCheckEnabled = false
45+
46+
private let appCheck: AppCheck?
47+
4348
private enum RequestHeaders {
4449
static let googRequestParamsHeader = "x-goog-request-params"
4550
static let authorizationHeader = "x-firebase-auth-token"
51+
static let appCheckHeader = "X-Firebase-AppCheck"
4652
}
4753

4854
private let googRequestHeaderValue: String
@@ -67,11 +73,13 @@ actor GrpcClient: CustomStringConvertible {
6773
}()
6874

6975
init(projectId: String, settings: DataConnectSettings, connectorConfig: ConnectorConfig,
70-
auth: Auth) {
76+
auth: Auth,
77+
appCheck: AppCheck?) {
7178
self.projectId = projectId
7279
serverSettings = settings
7380
self.connectorConfig = connectorConfig
7481
self.auth = auth
82+
self.appCheck = appCheck
7583

7684
connectorName =
7785
"projects/\(projectId)/locations/\(connectorConfig.location)/services/\(connectorConfig.serviceId)/connectors/\(connectorConfig.connector)"
@@ -173,12 +181,11 @@ actor GrpcClient: CustomStringConvertible {
173181

174182
headers.add(name: RequestHeaders.googRequestParamsHeader, value: googRequestHeaderValue)
175183

176-
// add token if available
177-
184+
// Add Auth token if available
178185
do {
179186
if let token = try await auth.currentUser?.getIDToken() {
180187
headers.add(name: RequestHeaders.authorizationHeader, value: "\(token)")
181-
print("added token \(token)")
188+
print("added Auth token \(token)")
182189

183190
} else {
184191
FirebaseLogger.dataConnect.debug("No auth token available. Not adding auth header.")
@@ -188,6 +195,25 @@ actor GrpcClient: CustomStringConvertible {
188195
.debug("Cannot get auth token successfully due to: \(error). Not adding auth header.")
189196
}
190197

198+
// Add AppCheck token if available
199+
if appCheckEnabled {
200+
do {
201+
if let token = try await appCheck?.token(forcingRefresh: false) {
202+
headers.add(name: RequestHeaders.appCheckHeader, value: "\(token)")
203+
FirebaseLogger.dataConnect
204+
.debug("App Check token added: \(token)")
205+
} else {
206+
FirebaseLogger.dataConnect
207+
.debug("App Check token unavailable. Not adding App Check header.")
208+
}
209+
} catch {
210+
FirebaseLogger.dataConnect
211+
.debug(
212+
"Cannot get App Check token successfully due to: \(error). Not adding App Check header."
213+
)
214+
}
215+
}
216+
191217
let options = CallOptions(customMetadata: headers)
192218
return options
193219
}

Package.swift

+1
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,7 @@ let package = Package(
671671
dependencies: [
672672
.product(name: "GRPC", package: "grpc-swift"),
673673
"FirebaseAuth",
674+
"FirebaseAppCheck",
674675
"FirebaseCore",
675676
"FirebaseCoreExtension",
676677
"FirebaseSharedSwift",

0 commit comments

Comments
 (0)