Skip to content

feat(appcheck): create FirebaseAppCheck.tokenChanges Flow #4806

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions appcheck/firebase-appcheck/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# Unreleased
* [unchanged] Updated to accommodate the release of the updated
[database] Kotlin extensions library.

## Kotlin
* [feature] Added
[`FirebaseAppCheck.tokenChanges`](/docs/reference/kotlin/com/google/firebase/appcheck/ktx/package-summary#tokenChanges)
Kotlin Flow to listen to changes in the token state.

# 16.1.2
* [unchanged] Updated to keep [app_check] SDK versions aligned.
Expand Down
1 change: 1 addition & 0 deletions appcheck/firebase-appcheck/ktx/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package com.google.firebase.appcheck.ktx {
method @NonNull public static operator String component1(@NonNull com.google.firebase.appcheck.AppCheckToken);
method public static operator long component2(@NonNull com.google.firebase.appcheck.AppCheckToken);
method @NonNull public static com.google.firebase.appcheck.FirebaseAppCheck getAppCheck(@NonNull com.google.firebase.ktx.Firebase);
method @NonNull public static kotlinx.coroutines.flow.Flow<com.google.firebase.appcheck.AppCheckToken> getTokenChanges(@NonNull com.google.firebase.appcheck.FirebaseAppCheck);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ import com.google.firebase.BuildConfig
import com.google.firebase.FirebaseApp
import com.google.firebase.appcheck.AppCheckToken
import com.google.firebase.appcheck.FirebaseAppCheck
import com.google.firebase.appcheck.FirebaseAppCheck.AppCheckListener
import com.google.firebase.components.Component
import com.google.firebase.components.ComponentRegistrar
import com.google.firebase.ktx.Firebase
import com.google.firebase.platforminfo.LibraryVersionComponent
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.channels.trySendBlocking
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.callbackFlow

/** Returns the [FirebaseAppCheck] instance of the default [FirebaseApp]. */
val Firebase.appCheck: FirebaseAppCheck
Expand All @@ -30,6 +37,24 @@ val Firebase.appCheck: FirebaseAppCheck
/** Returns the [FirebaseAppCheck] instance of a given [FirebaseApp]. */
fun Firebase.appCheck(app: FirebaseApp) = FirebaseAppCheck.getInstance(app)

/**
* Registers an [AppCheckListener] to changes in the token state. This [Flow] should be used ONLY if
* you need to authorize requests to a non-Firebase backend. Requests to Firebase backends are
* authorized automatically if configured.
*
* Back-pressure is handled by dropping the oldest value in the buffer on overflow.
*/
val FirebaseAppCheck.tokenChanges: Flow<AppCheckToken>
get() =
callbackFlow {
val tokenListener = AppCheckListener { appCheckToken -> trySendBlocking(appCheckToken) }

addAppCheckListener(tokenListener)

awaitClose { removeAppCheckListener(tokenListener) }
}
.buffer(capacity = Channel.CONFLATED)

/**
* Destructuring declaration for [AppCheckToken] to provide token.
*
Expand Down