Repository navigation
feat(core): add interop token lookups for Auth, App Check and FCM #18948
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
Draft
jonathanedey
wants to merge
13
commits into
main
Choose a base branch
from
jedey/core/interop-tokens
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,514
−3
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
11b2ba3
feat(core): add the interop token API to the platform interface
jonathanedey 2e40493
feat(core): look up Auth, App Check and FCM tokens on Android
jonathanedey e4e4182
feat(core): look up Auth, App Check and FCM tokens on Apple
jonathanedey 8c3e7e5
test(core): cover the interop tokens in the tests app core suite
jonathanedey 30ae2ba
feat(core): look up Auth, App Check and FCM tokens on web
jonathanedey e12caa0
test(core): cover the interop tokens on web in the core suite
jonathanedey 791675e
fix(core): answer null for the interop tokens on Windows
jonathanedey 337207b
chore(core): trim the interop token comments to the reasons
jonathanedey 41e3ccc
fix(core): report a missing app and a tokenless App Check answer
jonathanedey a73cc70
docs(core): describe the interop token behavior on web
jonathanedey c16f2b8
Merge remote-tracking branch 'origin/main' into jedey/core/interop-to…
jonathanedey 3c61d36
fix(core): build the interop token code on the merged toolchain
jonathanedey ef4aceb
test(core): skip the token checks per test on Windows
jonathanedey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...ase_core/android/src/main/kotlin/io/flutter/plugins/firebase/core/InteropTokenProvider.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // Copyright 2026 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
| package io.flutter.plugins.firebase.core | ||
|
|
||
| import com.google.android.gms.tasks.Task | ||
| import com.google.android.gms.tasks.TaskCompletionSource | ||
| import com.google.android.gms.tasks.Tasks | ||
| import com.google.firebase.FirebaseApp | ||
| import com.google.firebase.appcheck.interop.InteropAppCheckTokenProvider | ||
| import com.google.firebase.auth.internal.InternalAuthProvider | ||
| import com.google.firebase.iid.internal.FirebaseInstanceIdInternal | ||
| import com.google.firebase.internal.api.FirebaseNoSignedInUserException | ||
| import java.util.concurrent.Executor | ||
|
|
||
| /** | ||
| * Fetches the Auth, App Check and FCM tokens through the interop components the SDKs register in | ||
| * the app's component container, as the Functions SDK does. A null token means the SDK isn't linked | ||
| * or has nothing to give, never failure. | ||
| */ | ||
| class InteropTokenProvider( | ||
| private val getComponent: (Class<*>) -> Any?, | ||
| private val executor: Executor, | ||
| ) { | ||
| companion object { | ||
| /** Looks tokens up in the component container of the app named [appName]. */ | ||
| fun forApp(appName: String, executor: Executor): InteropTokenProvider { | ||
| val app = FirebaseApp.getInstance(appName) | ||
| return InteropTokenProvider({ app.get(it) }, executor) | ||
| } | ||
| } | ||
|
|
||
| private inline fun <reified T> get(): T? = getComponent(T::class.java) as? T | ||
|
|
||
| /** The signed-in user's ID token, or null when Auth isn't linked or nobody is signed in. */ | ||
| fun getAuthIdToken(): Task<String?> { | ||
| val auth = get<InternalAuthProvider>() ?: return Tasks.forResult(null) | ||
| return auth.getAccessToken(false).continueWith(executor) { task -> | ||
| if (task.isSuccessful) { | ||
| task.result?.token | ||
| } else { | ||
| when (val exception = task.exception) { | ||
| // Signed out is an answer, not a failure. | ||
| is FirebaseNoSignedInUserException -> null | ||
| else -> throw exception ?: IllegalStateException("getAccessToken failed") | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * App Check's answer, or null when it isn't linked. Before activation the SDK answers with its | ||
| * placeholder token and an error. | ||
| */ | ||
| fun getAppCheckToken(limitedUse: Boolean): Task<CoreAppCheckInteropToken?> { | ||
| val appCheck = get<InteropAppCheckTokenProvider>() ?: return Tasks.forResult(null) | ||
| val tokenTask = if (limitedUse) appCheck.limitedUseToken else appCheck.getToken(false) | ||
| return tokenTask.onSuccessTask(executor) { result -> | ||
| // A result without a token means the SDK changed shape; that's a failure, not "not linked". | ||
| val answer = result ?: throw IllegalStateException("App Check returned no token result") | ||
| Tasks.forResult( | ||
| CoreAppCheckInteropToken( | ||
| token = answer.token, | ||
| error = answer.error?.let { it.message ?: it.toString() }, | ||
| )) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The FCM token, or null when the Instance ID library, the only provider of | ||
| * `FirebaseInstanceIdInternal`, isn't linked. `getToken()` may block on the network, hence the | ||
| * executor. | ||
| */ | ||
| fun getMessagingToken(): Task<String?> { | ||
| val instanceId = get<FirebaseInstanceIdInternal>() ?: return Tasks.forResult(null) | ||
| val taskCompletionSource = TaskCompletionSource<String?>() | ||
| executor.execute { | ||
| try { | ||
| taskCompletionSource.setResult(instanceId.token) | ||
| } catch (e: Exception) { | ||
| taskCompletionSource.setException(e) | ||
| } | ||
| } | ||
| return taskCompletionSource.task | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of manually executing a blocking call on the executor and using a TaskCompletionSource, you can leverage the asynchronous tokenTask property already provided by FirebaseInstanceIdInternal. This avoids blocking a thread in the executor pool and simplifies the implementation.