Skip to content

Commit ca14fed

Browse files
authored
fix(auth): Don't throw if the configuration timeout is exceeded (#3321)
1 parent 10d1107 commit ca14fed

3 files changed

Lines changed: 39 additions & 4 deletions

File tree

aws-auth-cognito/api/aws-auth-cognito.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public final class com/amplifyframework/auth/cognito/AWSCognitoAuthPlugin : com/
1010
public static final field AWS_COGNITO_AUTH_LOG_NAMESPACE Ljava/lang/String;
1111
public static final field Companion Lcom/amplifyframework/auth/cognito/AWSCognitoAuthPlugin$Companion;
1212
public fun <init> ()V
13+
public synthetic fun <init> (JILkotlin/jvm/internal/DefaultConstructorMarker;)V
1314
public fun associateWebAuthnCredential (Landroid/app/Activity;Lcom/amplifyframework/auth/options/AuthAssociateWebAuthnCredentialsOptions;Lcom/amplifyframework/core/Action;Lcom/amplifyframework/core/Consumer;)V
1415
public fun associateWebAuthnCredential (Landroid/app/Activity;Lcom/amplifyframework/core/Action;Lcom/amplifyframework/core/Consumer;)V
1516
public fun autoSignIn (Lcom/amplifyframework/core/Consumer;Lcom/amplifyframework/core/Consumer;)V

aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/AWSCognitoAuthPlugin.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import com.amplifyframework.core.Consumer
6868
import com.amplifyframework.core.configuration.AmplifyOutputsData
6969
import com.amplifyframework.statemachine.codegen.events.AuthEvent
7070
import com.amplifyframework.statemachine.codegen.states.AuthState
71+
import kotlin.time.Duration
7172
import kotlin.time.Duration.Companion.seconds
7273
import kotlinx.coroutines.CoroutineScope
7374
import kotlinx.coroutines.CoroutineStart
@@ -77,18 +78,22 @@ import kotlinx.coroutines.channels.Channel
7778
import kotlinx.coroutines.channels.consumeEach
7879
import kotlinx.coroutines.launch
7980
import kotlinx.coroutines.runBlocking
80-
import kotlinx.coroutines.withTimeout
81+
import kotlinx.coroutines.withTimeoutOrNull
8182
import org.json.JSONObject
8283

8384
/**
8485
* A Cognito implementation of the Auth Plugin.
8586
*/
86-
class AWSCognitoAuthPlugin : AuthPlugin<AWSCognitoAuthService>() {
87+
class AWSCognitoAuthPlugin internal constructor(
88+
private val configurationTimeout: Duration = 10.seconds
89+
) : AuthPlugin<AWSCognitoAuthService>() {
8790
companion object {
8891
const val AWS_COGNITO_AUTH_LOG_NAMESPACE = "amplify:aws-cognito-auth:%s"
8992
private const val AWS_COGNITO_AUTH_PLUGIN_KEY = "awsCognitoAuthPlugin"
9093
}
9194

95+
constructor() : this(configurationTimeout = 10.seconds)
96+
9297
private val logger = authLogger()
9398

9499
@VisibleForTesting
@@ -127,9 +132,10 @@ class AWSCognitoAuthPlugin : AuthPlugin<AWSCognitoAuthService>() {
127132
}
128133

129134
override fun initialize(context: Context) {
130-
// Block until the state machine is in the configured state.
135+
// Wait up to the configured timeout for the state machine to reach Configured, but do not throw on timeout.
136+
// This matches legacy behavior where initialization proceeds regardless of whether configuration completes.
131137
runBlocking {
132-
withTimeout(10.seconds) {
138+
withTimeoutOrNull(configurationTimeout) {
133139
suspendWhileConfiguring()
134140
}
135141
}

aws-auth-cognito/src/test/java/com/amplifyframework/auth/cognito/AWSCognitoAuthPluginTest.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,15 @@ import com.amplifyframework.auth.result.AuthSignUpResult
5454
import com.amplifyframework.auth.result.AuthUpdateAttributeResult
5555
import com.amplifyframework.core.Action
5656
import com.amplifyframework.core.Consumer
57+
import com.amplifyframework.statemachine.codegen.states.AuthState
5758
import io.kotest.matchers.shouldBe
5859
import io.kotest.matchers.string.shouldNotBeBlank
5960
import io.mockk.coVerify
6061
import io.mockk.every
6162
import io.mockk.mockk
6263
import io.mockk.verify
64+
import kotlin.time.Duration.Companion.milliseconds
65+
import kotlinx.coroutines.flow.MutableSharedFlow
6366
import org.json.JSONObject
6467
import org.junit.Before
6568
import org.junit.Test
@@ -899,4 +902,29 @@ class AWSCognitoAuthPluginTest {
899902
authPlugin.handleWebUISignInResponse(null)
900903
coVerify(timeout = CHANNEL_TIMEOUT) { useCase.execute(null) }
901904
}
905+
906+
@Test
907+
fun `initialize completes when state machine reaches Configured`() {
908+
val plugin = AWSCognitoAuthPlugin(configurationTimeout = 100.milliseconds)
909+
plugin.authStateMachine = mockk(relaxed = true)
910+
911+
val stateFlow = MutableSharedFlow<AuthState>(replay = 1)
912+
stateFlow.tryEmit(AuthState.Configured(null, null, null))
913+
every { plugin.authStateMachine.state } returns stateFlow
914+
915+
plugin.initialize(mockk())
916+
}
917+
918+
@Test
919+
fun `initialize does not throw when state machine does not reach Configured within timeout`() {
920+
val plugin = AWSCognitoAuthPlugin(configurationTimeout = 100.milliseconds)
921+
plugin.authStateMachine = mockk(relaxed = true)
922+
923+
val stateFlow = MutableSharedFlow<AuthState>(replay = 1)
924+
stateFlow.tryEmit(AuthState.ConfiguringAuth())
925+
every { plugin.authStateMachine.state } returns stateFlow
926+
927+
// Should complete without throwing despite the state never reaching Configured
928+
plugin.initialize(mockk())
929+
}
902930
}

0 commit comments

Comments
 (0)