Skip to content

Commit 3d95744

Browse files
authored
Merge 77fd50a into 4f4ede7
2 parents 4f4ede7 + 77fd50a commit 3d95744

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

firebase-functions/ktx/api.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package com.google.firebase.functions.ktx {
66
method @NonNull public static com.google.firebase.functions.FirebaseFunctions functions(@NonNull com.google.firebase.ktx.Firebase, @NonNull com.google.firebase.FirebaseApp app);
77
method @NonNull public static com.google.firebase.functions.FirebaseFunctions functions(@NonNull com.google.firebase.ktx.Firebase, @NonNull com.google.firebase.FirebaseApp app, @NonNull String regionOrCustomDomain);
88
method @NonNull public static com.google.firebase.functions.FirebaseFunctions getFunctions(@NonNull com.google.firebase.ktx.Firebase);
9+
method @NonNull public static com.google.firebase.functions.HttpsCallableReference getHttpsCallable(@NonNull com.google.firebase.functions.FirebaseFunctions, @NonNull String name, @NonNull kotlin.jvm.functions.Function1<? super com.google.firebase.functions.HttpsCallableOptions.Builder,kotlin.Unit> init);
10+
method @NonNull public static com.google.firebase.functions.HttpsCallableReference getHttpsCallableFromUrl(@NonNull com.google.firebase.functions.FirebaseFunctions, @NonNull java.net.URL url, @NonNull kotlin.jvm.functions.Function1<? super com.google.firebase.functions.HttpsCallableOptions.Builder,kotlin.Unit> init);
911
}
1012

1113
}

firebase-functions/ktx/src/main/kotlin/com/google/firebase/functions/ktx/Functions.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ import com.google.firebase.FirebaseApp
1919
import com.google.firebase.components.Component
2020
import com.google.firebase.components.ComponentRegistrar
2121
import com.google.firebase.functions.FirebaseFunctions
22+
import com.google.firebase.functions.HttpsCallableOptions
23+
import com.google.firebase.functions.HttpsCallableReference
2224
import com.google.firebase.ktx.Firebase
2325
import com.google.firebase.platforminfo.LibraryVersionComponent
26+
import java.net.URL
2427

2528
/** Returns the [FirebaseFunctions] instance of the default [FirebaseApp]. */
2629
val Firebase.functions: FirebaseFunctions
@@ -45,3 +48,23 @@ class FirebaseFunctionsKtxRegistrar : ComponentRegistrar {
4548
override fun getComponents(): List<Component<*>> =
4649
listOf(LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME))
4750
}
51+
52+
/** Returns a reference to the Callable HTTPS trigger with the given name and call options. */
53+
fun FirebaseFunctions.getHttpsCallable(
54+
name: String,
55+
init: HttpsCallableOptions.Builder.() -> Unit
56+
): HttpsCallableReference {
57+
val builder = HttpsCallableOptions.Builder()
58+
builder.init()
59+
return getHttpsCallable(name, builder.build())
60+
}
61+
62+
/** Returns a reference to the Callable HTTPS trigger with the given URL and call options. */
63+
fun FirebaseFunctions.getHttpsCallableFromUrl(
64+
url: URL,
65+
init: HttpsCallableOptions.Builder.() -> Unit
66+
): HttpsCallableReference {
67+
val builder = HttpsCallableOptions.Builder()
68+
builder.init()
69+
return getHttpsCallableFromUrl(url, builder.build())
70+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.functions
16+
17+
/**
18+
* Returns true if the {@link HttpsCallableReference} is configured to use FAC limited-use tokens.
19+
*/
20+
fun HttpsCallableReference.usesLimitedUseFacTokens() = options.getLimitedUseAppCheckTokens()

firebase-functions/ktx/src/test/kotlin/com/google/firebase/functions/ktx/FunctionsTests.kt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ import com.google.common.truth.Truth.assertThat
1919
import com.google.firebase.FirebaseApp
2020
import com.google.firebase.FirebaseOptions
2121
import com.google.firebase.functions.FirebaseFunctions
22+
import com.google.firebase.functions.usesLimitedUseFacTokens
2223
import com.google.firebase.ktx.Firebase
2324
import com.google.firebase.ktx.app
2425
import com.google.firebase.ktx.initialize
2526
import com.google.firebase.platforminfo.UserAgentPublisher
27+
import java.net.URL
2628
import org.junit.After
2729
import org.junit.Before
2830
import org.junit.Test
@@ -100,3 +102,50 @@ class LibraryVersionTest : BaseTestCase() {
100102
assertThat(publisher.userAgent).contains(LIBRARY_NAME)
101103
}
102104
}
105+
106+
@RunWith(RobolectricTestRunner::class)
107+
class AppCheckLimitedUseTest : BaseTestCase() {
108+
@Test
109+
fun `FirebaseFunctions#getHttpsCallable should not use limited-use tokens by default`() {
110+
val callable = Firebase.functions.getHttpsCallable("function")
111+
assertThat(callable.usesLimitedUseFacTokens()).isFalse()
112+
}
113+
114+
@Test
115+
fun `FirebaseFunctions#getHttpsCallable should build callable with FAC settings (when true)`() {
116+
val callable =
117+
Firebase.functions.getHttpsCallable("function") { limitedUseAppCheckTokens = true }
118+
assertThat(callable.usesLimitedUseFacTokens()).isTrue()
119+
}
120+
121+
@Test
122+
fun `FirebaseFunctions#getHttpsCallable should build callable with FAC settings (when false)`() {
123+
val callable =
124+
Firebase.functions.getHttpsCallable("function") { limitedUseAppCheckTokens = false }
125+
assertThat(callable.usesLimitedUseFacTokens()).isFalse()
126+
}
127+
128+
@Test
129+
fun `FirebaseFunctions#getHttpsCallableFromUrl should not use limited-use tokens by default`() {
130+
val callable = Firebase.functions.getHttpsCallableFromUrl(URL("https://functions.test"))
131+
assertThat(callable.usesLimitedUseFacTokens()).isFalse()
132+
}
133+
134+
@Test
135+
fun `FirebaseFunctions#getHttpsCallableFromUrl callable with FAC settings (when true)`() {
136+
val callable =
137+
Firebase.functions.getHttpsCallableFromUrl(URL("https://functions.test")) {
138+
limitedUseAppCheckTokens = true
139+
}
140+
assertThat(callable.usesLimitedUseFacTokens()).isTrue()
141+
}
142+
143+
@Test
144+
fun `FirebaseFunctions#getHttpsCallableFromUrl callable with FAC settings (when false)`() {
145+
val callable =
146+
Firebase.functions.getHttpsCallableFromUrl(URL("https://functions.test")) {
147+
limitedUseAppCheckTokens = false
148+
}
149+
assertThat(callable.usesLimitedUseFacTokens()).isFalse()
150+
}
151+
}

0 commit comments

Comments
 (0)