Skip to content

Commit d119697

Browse files
authored
[FAL] Move useLimitedUseAppCheckTokens property into FirebaseInfo + Update docs (#15234)
1 parent b98d6cb commit d119697

File tree

9 files changed

+32
-51
lines changed

9 files changed

+32
-51
lines changed

FirebaseAI/CHANGELOG.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
# 12.2.0
22
- [feature] Added support for returning thought summaries, which are synthesized
33
versions of a model's internal reasoning process. (#15096)
4-
- [feature] Added a new configuration option to use limited-use App
5-
Check tokens for attesting Firebase AI Logic requests. This enhances
6-
security against replay attacks. To use this feature, configure it
7-
explicitly via the new `useLimitedUseAppCheckTokens` parameter when
8-
initializing `FirebaseAI`. We recommend migrating to limited-use
9-
tokens now, so your app will be ready to take advantage of replay
10-
protection when it becomes available for Firebase AI Logic. (#15099)
4+
- [feature] Added support for limited-use tokens with Firebase App Check. These short-lived tokens
5+
provide greater protection for the APIs that give you access to Gemini and Imagen models. Learn
6+
how to [enable usage of limited-use tokens](https://firebase.google.com/docs/ai-logic/app-check).
7+
(#15099)
118

129
# 12.0.0
1310
- [feature] Added support for Grounding with Google Search. (#15014)

FirebaseAI/Sources/FirebaseAI.swift

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,12 @@ public final class FirebaseAI: Sendable {
3333
/// - backend: The backend API for the Firebase AI SDK; if not specified, uses the default
3434
/// ``Backend/googleAI()`` (Gemini Developer API).
3535
/// - useLimitedUseAppCheckTokens: When sending tokens to the backend, this option enables
36-
/// the usage of App Check's limited-use tokens instead of the standard cached tokens.
37-
///
38-
/// A new limited-use tokens will be generated for each request; providing a smaller attack
39-
/// surface for malicious parties to hijack tokens. When used alongside replay protection,
40-
/// limited-use tokens are also _consumed_ after each request, ensuring they can't be used
41-
/// again.
36+
/// the usage of App Check's limited-use tokens instead of the standard cached tokens. Learn
37+
/// more about [limited-use tokens](https://firebase.google.com/docs/ai-logic/app-check),
38+
/// including their nuances, when to use them, and best practices for integrating them into
39+
/// your app.
4240
///
4341
/// _This flag is set to `false` by default._
44-
///
45-
/// > Important: Replay protection is not currently supported for the FirebaseAI backend.
46-
/// > While this feature is being developed, you can still migrate to using
47-
/// > limited-use tokens. Because limited-use tokens are backwards compatible, you can still
48-
/// > use them without replay protection. Due to their shorter TTL over standard App Check
49-
/// > tokens, they still provide a security benefit.
50-
/// >
5142
/// > Migrating to limited-use tokens sooner minimizes disruption when support for replay
5243
/// > protection is added.
5344
/// - Returns: A `FirebaseAI` instance, configured with the custom `FirebaseApp`.
@@ -110,8 +101,7 @@ public final class FirebaseAI: Sendable {
110101
tools: tools,
111102
toolConfig: toolConfig,
112103
systemInstruction: systemInstruction,
113-
requestOptions: requestOptions,
114-
useLimitedUseAppCheckTokens: useLimitedUseAppCheckTokens
104+
requestOptions: requestOptions
115105
)
116106
}
117107

@@ -147,8 +137,7 @@ public final class FirebaseAI: Sendable {
147137
apiConfig: apiConfig,
148138
generationConfig: generationConfig,
149139
safetySettings: safetySettings,
150-
requestOptions: requestOptions,
151-
useLimitedUseAppCheckTokens: useLimitedUseAppCheckTokens
140+
requestOptions: requestOptions
152141
)
153142
}
154143

@@ -163,8 +152,6 @@ public final class FirebaseAI: Sendable {
163152

164153
let apiConfig: APIConfig
165154

166-
let useLimitedUseAppCheckTokens: Bool
167-
168155
/// A map of active `FirebaseAI` instances keyed by the `FirebaseApp` name and the `location`,
169156
/// in the format `appName:location`.
170157
private nonisolated(unsafe) static var instances: [InstanceKey: FirebaseAI] = [:]
@@ -227,11 +214,11 @@ public final class FirebaseAI: Sendable {
227214
projectID: projectID,
228215
apiKey: apiKey,
229216
firebaseAppID: app.options.googleAppID,
230-
firebaseApp: app
217+
firebaseApp: app,
218+
useLimitedUseAppCheckTokens: useLimitedUseAppCheckTokens
231219
)
232220
self.apiConfig = apiConfig
233221
self.location = location
234-
self.useLimitedUseAppCheckTokens = useLimitedUseAppCheckTokens
235222
}
236223

237224
func modelResourceName(modelName: String) -> String {

FirebaseAI/Sources/FirebaseInfo.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,22 @@ struct FirebaseInfo: Sendable {
2727
let projectID: String
2828
let apiKey: String
2929
let firebaseAppID: String
30+
let useLimitedUseAppCheckTokens: Bool
3031
let app: FirebaseApp
3132

3233
init(appCheck: AppCheckInterop? = nil,
3334
auth: AuthInterop? = nil,
3435
projectID: String,
3536
apiKey: String,
3637
firebaseAppID: String,
37-
firebaseApp: FirebaseApp) {
38+
firebaseApp: FirebaseApp,
39+
useLimitedUseAppCheckTokens: Bool) {
3840
self.appCheck = appCheck
3941
self.auth = auth
4042
self.projectID = projectID
4143
self.apiKey = apiKey
4244
self.firebaseAppID = firebaseAppID
45+
self.useLimitedUseAppCheckTokens = useLimitedUseAppCheckTokens
4346
app = firebaseApp
4447
}
4548
}

FirebaseAI/Sources/GenerativeAIService.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,9 @@ struct GenerativeAIService {
3030

3131
private let urlSession: URLSession
3232

33-
private let useLimitedUseAppCheckTokens: Bool
34-
35-
init(firebaseInfo: FirebaseInfo, urlSession: URLSession, useLimitedUseAppCheckTokens: Bool) {
33+
init(firebaseInfo: FirebaseInfo, urlSession: URLSession) {
3634
self.firebaseInfo = firebaseInfo
3735
self.urlSession = urlSession
38-
self.useLimitedUseAppCheckTokens = useLimitedUseAppCheckTokens
3936
}
4037

4138
func loadRequest<T: GenerativeAIRequest>(request: T) async throws -> T.Response {
@@ -212,7 +209,7 @@ struct GenerativeAIService {
212209

213210
private func fetchAppCheckToken(appCheck: AppCheckInterop) async throws
214211
-> FIRAppCheckTokenResultInterop {
215-
if useLimitedUseAppCheckTokens {
212+
if firebaseInfo.useLimitedUseAppCheckTokens {
216213
if let token = await getLimitedUseAppCheckToken(appCheck: appCheck) {
217214
return token
218215
}
@@ -242,7 +239,7 @@ struct GenerativeAIService {
242239
Never
243240
>) in
244241
guard
245-
useLimitedUseAppCheckTokens,
242+
firebaseInfo.useLimitedUseAppCheckTokens,
246243
// `getLimitedUseToken(completion:)` is an optional protocol method. Optional binding
247244
// is performed to make sure `continuation` is called even if the method’s not implemented.
248245
let limitedUseTokenClosure = appCheck.getLimitedUseToken

FirebaseAI/Sources/GenerativeModel.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ public final class GenerativeModel: Sendable {
7676
/// only text content is supported.
7777
/// - requestOptions: Configuration parameters for sending requests to the backend.
7878
/// - urlSession: The `URLSession` to use for requests; defaults to `URLSession.shared`.
79-
/// - useLimitedUseAppCheckTokens: Use App Check's limited-use tokens instead of the standard
80-
/// cached tokens.
8179
init(modelName: String,
8280
modelResourceName: String,
8381
firebaseInfo: FirebaseInfo,
@@ -88,15 +86,13 @@ public final class GenerativeModel: Sendable {
8886
toolConfig: ToolConfig? = nil,
8987
systemInstruction: ModelContent? = nil,
9088
requestOptions: RequestOptions,
91-
urlSession: URLSession = GenAIURLSession.default,
92-
useLimitedUseAppCheckTokens: Bool = false) {
89+
urlSession: URLSession = GenAIURLSession.default) {
9390
self.modelName = modelName
9491
self.modelResourceName = modelResourceName
9592
self.apiConfig = apiConfig
9693
generativeAIService = GenerativeAIService(
9794
firebaseInfo: firebaseInfo,
98-
urlSession: urlSession,
99-
useLimitedUseAppCheckTokens: useLimitedUseAppCheckTokens
95+
urlSession: urlSession
10096
)
10197
self.generationConfig = generationConfig
10298
self.safetySettings = safetySettings

FirebaseAI/Sources/Types/Public/Imagen/ImagenModel.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,12 @@ public final class ImagenModel {
5353
generationConfig: ImagenGenerationConfig?,
5454
safetySettings: ImagenSafetySettings?,
5555
requestOptions: RequestOptions,
56-
urlSession: URLSession = GenAIURLSession.default,
57-
useLimitedUseAppCheckTokens: Bool = false) {
56+
urlSession: URLSession = GenAIURLSession.default) {
5857
self.modelResourceName = modelResourceName
5958
self.apiConfig = apiConfig
6059
generativeAIService = GenerativeAIService(
6160
firebaseInfo: firebaseInfo,
62-
urlSession: urlSession,
63-
useLimitedUseAppCheckTokens: useLimitedUseAppCheckTokens
61+
urlSession: urlSession
6462
)
6563
self.generationConfig = generationConfig
6664
self.safetySettings = safetySettings

FirebaseAI/Tests/Unit/ChatTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ final class ChatTests: XCTestCase {
6868
projectID: "my-project-id",
6969
apiKey: "API_KEY",
7070
firebaseAppID: "My app ID",
71-
firebaseApp: app
71+
firebaseApp: app,
72+
useLimitedUseAppCheckTokens: false
7273
),
7374
apiConfig: FirebaseAI.defaultVertexAIAPIConfig,
7475
tools: nil,

FirebaseAI/Tests/Unit/GenerativeModelVertexAITests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,13 +507,13 @@ final class GenerativeModelVertexAITests: XCTestCase {
507507
modelName: testModelName,
508508
modelResourceName: testModelResourceName,
509509
firebaseInfo: GenerativeModelTestUtil.testFirebaseInfo(
510-
appCheck: AppCheckInteropFake(token: appCheckToken)
510+
appCheck: AppCheckInteropFake(token: appCheckToken),
511+
useLimitedUseAppCheckTokens: true
511512
),
512513
apiConfig: apiConfig,
513514
tools: nil,
514515
requestOptions: RequestOptions(),
515-
urlSession: urlSession,
516-
useLimitedUseAppCheckTokens: true
516+
urlSession: urlSession
517517
)
518518
MockURLProtocol
519519
.requestHandler = try GenerativeModelTestUtil.httpRequestHandler(

FirebaseAI/Tests/Unit/TestUtilities/GenerativeModelTestUtil.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ enum GenerativeModelTestUtil {
103103

104104
static func testFirebaseInfo(appCheck: AppCheckInterop? = nil,
105105
auth: AuthInterop? = nil,
106-
privateAppID: Bool = false) -> FirebaseInfo {
106+
privateAppID: Bool = false,
107+
useLimitedUseAppCheckTokens: Bool = false) -> FirebaseInfo {
107108
let app = FirebaseApp(instanceWithName: "testApp",
108109
options: FirebaseOptions(googleAppID: "ignore",
109110
gcmSenderID: "ignore"))
@@ -114,7 +115,8 @@ enum GenerativeModelTestUtil {
114115
projectID: "my-project-id",
115116
apiKey: "API_KEY",
116117
firebaseAppID: "My app ID",
117-
firebaseApp: app
118+
firebaseApp: app,
119+
useLimitedUseAppCheckTokens: useLimitedUseAppCheckTokens
118120
)
119121
}
120122
}

0 commit comments

Comments
 (0)