14
14
15
15
import Foundation
16
16
17
+ import FirebaseAppCheck
17
18
import FirebaseAuth
18
19
import FirebaseCoreInternal
19
20
import FirebaseSharedSwift
@@ -40,9 +41,14 @@ actor GrpcClient: CustomStringConvertible {
40
41
41
42
private let auth : Auth
42
43
44
+ private let appCheckEnabled = false
45
+
46
+ private let appCheck : AppCheck ?
47
+
43
48
private enum RequestHeaders {
44
49
static let googRequestParamsHeader = " x-goog-request-params "
45
50
static let authorizationHeader = " x-firebase-auth-token "
51
+ static let appCheckHeader = " X-Firebase-AppCheck "
46
52
}
47
53
48
54
private let googRequestHeaderValue : String
@@ -67,11 +73,13 @@ actor GrpcClient: CustomStringConvertible {
67
73
} ( )
68
74
69
75
init ( projectId: String , settings: DataConnectSettings , connectorConfig: ConnectorConfig ,
70
- auth: Auth ) {
76
+ auth: Auth ,
77
+ appCheck: AppCheck ? ) {
71
78
self . projectId = projectId
72
79
serverSettings = settings
73
80
self . connectorConfig = connectorConfig
74
81
self . auth = auth
82
+ self . appCheck = appCheck
75
83
76
84
connectorName =
77
85
" projects/ \( projectId) /locations/ \( connectorConfig. location) /services/ \( connectorConfig. serviceId) /connectors/ \( connectorConfig. connector) "
@@ -173,12 +181,11 @@ actor GrpcClient: CustomStringConvertible {
173
181
174
182
headers. add ( name: RequestHeaders . googRequestParamsHeader, value: googRequestHeaderValue)
175
183
176
- // add token if available
177
-
184
+ // Add Auth token if available
178
185
do {
179
186
if let token = try await auth. currentUser? . getIDToken ( ) {
180
187
headers. add ( name: RequestHeaders . authorizationHeader, value: " \( token) " )
181
- print ( " added token \( token) " )
188
+ print ( " added Auth token \( token) " )
182
189
183
190
} else {
184
191
FirebaseLogger . dataConnect. debug ( " No auth token available. Not adding auth header. " )
@@ -188,6 +195,25 @@ actor GrpcClient: CustomStringConvertible {
188
195
. debug ( " Cannot get auth token successfully due to: \( error) . Not adding auth header. " )
189
196
}
190
197
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
+
191
217
let options = CallOptions ( customMetadata: headers)
192
218
return options
193
219
}
0 commit comments