Skip to content

Commit

Permalink
Add Appcheck for Dataconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
cherylEnkidu committed Jul 17, 2024
1 parent 88d0bef commit 5183bad
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
7 changes: 5 additions & 2 deletions FirebaseDataConnect/Sources/DataConnect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import Foundation

import FirebaseAppCheck
import FirebaseAuth
import FirebaseCore

Expand Down Expand Up @@ -59,7 +60,8 @@ public class DataConnect {
projectId: projectID,
settings: settings,
connectorConfig: connectorConfig,
auth: Auth.auth(app: app)
auth: Auth.auth(app: app),
appCheck: AppCheck.appCheck(app: app)
)

operationsManager = OperationsManager(grpcClient: grpcClient)
Expand All @@ -80,7 +82,8 @@ public class DataConnect {
projectId: projectID,
settings: settings,
connectorConfig: connectorConfig,
auth: Auth.auth(app: app)
auth: Auth.auth(app: app),
appCheck: AppCheck.appCheck(app: app)
)
operationsManager = OperationsManager(grpcClient: grpcClient)
}
Expand Down
34 changes: 25 additions & 9 deletions FirebaseDataConnect/Sources/Internal/GrpcClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import Foundation

import FirebaseAppCheck
import FirebaseAuth
import FirebaseCoreInternal
import FirebaseSharedSwift
Expand All @@ -23,7 +24,6 @@ import NIOHPACK
import NIOPosix
import OSLog
import SwiftProtobuf
import FirebaseAppCheck

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
actor GrpcClient: CustomStringConvertible {
Expand All @@ -40,13 +40,13 @@ actor GrpcClient: CustomStringConvertible {
private let connectorName: String // Fully qualified connector name

private let auth: Auth
private let appCheck: AppCheck

private let appCheck: AppCheck?

private enum RequestHeaders {
static let googRequestParamsHeader = "x-goog-request-params"
static let authorizationHeader = "x-firebase-auth-token"
static let appCheckHeader = "x-firebase-appcheck"
static let appCheckHeader = "X-Firebase-AppCheck"
}

private let googRequestHeaderValue: String
Expand All @@ -72,12 +72,12 @@ actor GrpcClient: CustomStringConvertible {

init(projectId: String, settings: DataConnectSettings, connectorConfig: ConnectorConfig,
auth: Auth,
appCheck: AppCheck) {
appCheck: AppCheck?) {
self.projectId = projectId
serverSettings = settings
self.connectorConfig = connectorConfig
self.auth = auth
self.appCheck = appCheck
self.appCheck = appCheck

connectorName =
"projects/\(projectId)/locations/\(connectorConfig.location)/services/\(connectorConfig.serviceId)/connectors/\(connectorConfig.connector)"
Expand Down Expand Up @@ -179,12 +179,11 @@ actor GrpcClient: CustomStringConvertible {

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

// add token if available

// Add Auth token if available
do {
if let token = try await auth.currentUser?.getIDToken() {
headers.add(name: RequestHeaders.authorizationHeader, value: "\(token)")
print("added token \(token)")
print("added Auth token \(token)")

} else {
FirebaseLogger.dataConnect.debug("No auth token available. Not adding auth header.")
Expand All @@ -194,6 +193,23 @@ actor GrpcClient: CustomStringConvertible {
.debug("Cannot get auth token successfully due to: \(error). Not adding auth header.")
}

// Add AppCheck token if available
do {
if let token = try await appCheck?.token(forcingRefresh: false) {
headers.add(name: RequestHeaders.appCheckHeader, value: "\(token)")
FirebaseLogger.dataConnect
.debug("App Check token added: \(token)")
} else {
FirebaseLogger.dataConnect
.debug("App Check token unavailable. Not adding App Check header.")
}
} catch {
FirebaseLogger.dataConnect
.debug(
"Cannot get App Check token successfully due to: \(error). Not adding App Check header."
)
}

let options = CallOptions(customMetadata: headers)
return options
}
Expand Down
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ let package = Package(
dependencies: [
.product(name: "GRPC", package: "grpc-swift"),
"FirebaseAuth",
"FirebaseAppCheck",
"FirebaseCore",
"FirebaseCoreExtension",
"FirebaseSharedSwift",
Expand Down

0 comments on commit 5183bad

Please sign in to comment.