Skip to content

Kotlin implementation for Https callable functions #5009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions firebase-functions/ktx/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package com.google.firebase.functions.ktx {
method @NonNull public static com.google.firebase.functions.FirebaseFunctions functions(@NonNull com.google.firebase.ktx.Firebase, @NonNull com.google.firebase.FirebaseApp app);
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);
method @NonNull public static com.google.firebase.functions.FirebaseFunctions getFunctions(@NonNull com.google.firebase.ktx.Firebase);
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);
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);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import com.google.firebase.FirebaseApp
import com.google.firebase.components.Component
import com.google.firebase.components.ComponentRegistrar
import com.google.firebase.functions.FirebaseFunctions
import com.google.firebase.functions.HttpsCallableOptions
import com.google.firebase.functions.HttpsCallableReference
import com.google.firebase.ktx.Firebase
import com.google.firebase.platforminfo.LibraryVersionComponent
import java.net.URL

/** Returns the [FirebaseFunctions] instance of the default [FirebaseApp]. */
val Firebase.functions: FirebaseFunctions
Expand All @@ -45,3 +48,23 @@ class FirebaseFunctionsKtxRegistrar : ComponentRegistrar {
override fun getComponents(): List<Component<*>> =
listOf(LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME))
}

/** Returns a reference to the Callable HTTPS trigger with the given name and call options. */
fun FirebaseFunctions.getHttpsCallable(
name: String,
init: HttpsCallableOptions.Builder.() -> Unit
): HttpsCallableReference {
val builder = HttpsCallableOptions.Builder()
builder.init()
return getHttpsCallable(name, builder.build())
}

/** Returns a reference to the Callable HTTPS trigger with the given URL and call options. */
fun FirebaseFunctions.getHttpsCallableFromUrl(
url: URL,
init: HttpsCallableOptions.Builder.() -> Unit
): HttpsCallableReference {
val builder = HttpsCallableOptions.Builder()
builder.init()
return getHttpsCallableFromUrl(url, builder.build())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.functions

/**
* Returns true if the {@link HttpsCallableReference} is configured to use FAC limited-use tokens.
*/
fun HttpsCallableReference.usesLimitedUseFacTokens() = options.getLimitedUseAppCheckTokens()
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ import com.google.common.truth.Truth.assertThat
import com.google.firebase.FirebaseApp
import com.google.firebase.FirebaseOptions
import com.google.firebase.functions.FirebaseFunctions
import com.google.firebase.functions.usesLimitedUseFacTokens
import com.google.firebase.ktx.Firebase
import com.google.firebase.ktx.app
import com.google.firebase.ktx.initialize
import com.google.firebase.platforminfo.UserAgentPublisher
import java.net.URL
import org.junit.After
import org.junit.Before
import org.junit.Test
Expand Down Expand Up @@ -100,3 +102,50 @@ class LibraryVersionTest : BaseTestCase() {
assertThat(publisher.userAgent).contains(LIBRARY_NAME)
}
}

@RunWith(RobolectricTestRunner::class)
class AppCheckLimitedUseTest : BaseTestCase() {
@Test
fun `FirebaseFunctions#getHttpsCallable should not use limited-use tokens by default`() {
val callable = Firebase.functions.getHttpsCallable("function")
assertThat(callable.usesLimitedUseFacTokens()).isFalse()
}

@Test
fun `FirebaseFunctions#getHttpsCallable should build callable with FAC settings (when true)`() {
val callable =
Firebase.functions.getHttpsCallable("function") { limitedUseAppCheckTokens = true }
assertThat(callable.usesLimitedUseFacTokens()).isTrue()
}

@Test
fun `FirebaseFunctions#getHttpsCallable should build callable with FAC settings (when false)`() {
val callable =
Firebase.functions.getHttpsCallable("function") { limitedUseAppCheckTokens = false }
assertThat(callable.usesLimitedUseFacTokens()).isFalse()
}

@Test
fun `FirebaseFunctions#getHttpsCallableFromUrl should not use limited-use tokens by default`() {
val callable = Firebase.functions.getHttpsCallableFromUrl(URL("https://functions.test"))
assertThat(callable.usesLimitedUseFacTokens()).isFalse()
}

@Test
fun `FirebaseFunctions#getHttpsCallableFromUrl callable with FAC settings (when true)`() {
val callable =
Firebase.functions.getHttpsCallableFromUrl(URL("https://functions.test")) {
limitedUseAppCheckTokens = true
}
assertThat(callable.usesLimitedUseFacTokens()).isTrue()
}

@Test
fun `FirebaseFunctions#getHttpsCallableFromUrl callable with FAC settings (when false)`() {
val callable =
Firebase.functions.getHttpsCallableFromUrl(URL("https://functions.test")) {
limitedUseAppCheckTokens = false
}
assertThat(callable.usesLimitedUseFacTokens()).isFalse()
}
}