Skip to content

Commit

Permalink
dataconnect: DataConnectCredentialsTokenManager: initialize synchrono…
Browse files Browse the repository at this point in the history
…usly to fix race condition (#6448)
  • Loading branch information
dconeybe authored Nov 12, 2024
1 parent 0eff5d3 commit 4e2dcd0
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.firebase.dataconnect

import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.dataconnect.core.FirebaseDataConnectInternal
import com.google.firebase.dataconnect.testutil.DataConnectBackend
import com.google.firebase.dataconnect.testutil.DataConnectIntegrationTestBase
import com.google.firebase.dataconnect.testutil.InProcessDataConnectGrpcServer
Expand Down Expand Up @@ -201,6 +202,7 @@ class AuthIntegrationTest : DataConnectIntegrationTestBase() {
}

private suspend fun signIn() {
(personSchema.dataConnect as FirebaseDataConnectInternal).awaitAuthReady()
val authResult = auth.run { signInAnonymously().await() }
withClue("authResult.user returned from signInAnonymously()") {
authResult.user.shouldNotBeNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,20 @@ internal class DataConnectAppCheck(
blockingDispatcher: CoroutineDispatcher,
logger: Logger,
) :
DataConnectCredentialsTokenManager<InteropAppCheckTokenProvider, AppCheckTokenListener>(
DataConnectCredentialsTokenManager<InteropAppCheckTokenProvider>(
deferredProvider = deferredAppCheckTokenProvider,
parentCoroutineScope = parentCoroutineScope,
blockingDispatcher = blockingDispatcher,
logger = logger,
) {
override fun newTokenListener(): AppCheckTokenListener = AppCheckTokenListenerImpl(logger)
private val appCheckTokenListener = AppCheckTokenListenerImpl(logger)

@DeferredApi
override fun addTokenListener(
provider: InteropAppCheckTokenProvider,
listener: AppCheckTokenListener
) = provider.addAppCheckTokenListener(listener)
override fun addTokenListener(provider: InteropAppCheckTokenProvider) =
provider.addAppCheckTokenListener(appCheckTokenListener)

override fun removeTokenListener(
provider: InteropAppCheckTokenProvider,
listener: AppCheckTokenListener
) = provider.removeAppCheckTokenListener(listener)
override fun removeTokenListener(provider: InteropAppCheckTokenProvider) =
provider.removeAppCheckTokenListener(appCheckTokenListener)

override suspend fun getToken(provider: InteropAppCheckTokenProvider, forceRefresh: Boolean) =
provider.getToken(forceRefresh).await().let { GetTokenResult(it.token) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ internal class DataConnectAuth(
blockingDispatcher: CoroutineDispatcher,
logger: Logger,
) :
DataConnectCredentialsTokenManager<InternalAuthProvider, IdTokenListener>(
DataConnectCredentialsTokenManager<InternalAuthProvider>(
deferredProvider = deferredAuthProvider,
parentCoroutineScope = parentCoroutineScope,
blockingDispatcher = blockingDispatcher,
logger = logger,
) {
override fun newTokenListener(): IdTokenListener = IdTokenListenerImpl(logger)
private val idTokenListener = IdTokenListenerImpl(logger)

@DeferredApi
override fun addTokenListener(provider: InternalAuthProvider, listener: IdTokenListener) =
provider.addIdTokenListener(listener)
override fun addTokenListener(provider: InternalAuthProvider) =
provider.addIdTokenListener(idTokenListener)

override fun removeTokenListener(provider: InternalAuthProvider, listener: IdTokenListener) =
provider.removeIdTokenListener(listener)
override fun removeTokenListener(provider: InternalAuthProvider) =
provider.removeIdTokenListener(idTokenListener)

override suspend fun getToken(provider: InternalAuthProvider, forceRefresh: Boolean) =
provider.getAccessToken(forceRefresh).await().let { GetTokenResult(it.token) }
Expand Down
Loading

0 comments on commit 4e2dcd0

Please sign in to comment.