Skip to content
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

fix(auth): Fix for missing session variable when custom auth is performed #2335

Merged
merged 6 commits into from
Mar 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
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ internal object SRPCognitoActions : SRPActions {
params.plus(KEY_DEVICE_KEY to it)
} ?: params

SRPEvent(SRPEvent.EventType.RespondPasswordVerifier(challengeParams, event.metadata))
SRPEvent(
SRPEvent.EventType.RespondPasswordVerifier(
challengeParams, event.metadata, initiateAuthResponse.session
)
)
} ?: throw Exception("Auth challenge parameters are empty.")
else -> throw Exception("Not yet implemented.")
}
Expand Down Expand Up @@ -136,7 +140,11 @@ internal object SRPCognitoActions : SRPActions {
params.plus(KEY_DEVICE_KEY to it)
} ?: params

SRPEvent(SRPEvent.EventType.RespondPasswordVerifier(challengeParams, event.metadata))
SRPEvent(
SRPEvent.EventType.RespondPasswordVerifier(
challengeParams, event.metadata, initiateAuthResponse.session
)
)
} ?: throw ServiceException(
"Auth challenge parameters are empty.",
AmplifyException.TODO_RECOVERY_SUGGESTION
Expand Down Expand Up @@ -193,6 +201,7 @@ internal object SRPCognitoActions : SRPActions {
clientId = configuration.userPool.appClient
challengeResponses = challengeParams
clientMetadata = event.metadata
session = event.session
pinpointEndpointId?.let { analyticsMetadata { analyticsEndpointId = it } }
encodedContextData?.let { userContextData { encodedData = it } }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ internal class SRPEvent(val eventType: EventType, override val time: Date? = nul
) : EventType()
data class RespondPasswordVerifier(
val challengeParameters: Map<String, String>,
val metadata: Map<String, String>
val metadata: Map<String, String>,
val session: String?
) : EventType()

data class ThrowAuthError(val exception: Exception) : EventType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import com.amplifyframework.auth.cognito.exceptions.configuration.InvalidUserPoo
import com.amplifyframework.auth.cognito.helpers.AuthHelper
import com.amplifyframework.auth.cognito.helpers.SRPHelper
import com.amplifyframework.auth.cognito.options.AWSCognitoAuthResendUserAttributeConfirmationCodeOptions
import com.amplifyframework.auth.cognito.options.AWSCognitoAuthSignInOptions
import com.amplifyframework.auth.cognito.options.AWSCognitoAuthUpdateUserAttributeOptions
import com.amplifyframework.auth.cognito.options.AWSCognitoAuthUpdateUserAttributesOptions
import com.amplifyframework.auth.cognito.options.AuthFlowType
Expand Down Expand Up @@ -220,6 +221,31 @@ class RealAWSCognitoAuthPluginTest {
verify(exactly = 0) { onSuccess.accept(any()) }
}

@Test
fun testCustomSignInWithSRPSucceedsWithChallenge() {
// GIVEN
val onSuccess = mockk<Consumer<AuthSignInResult>>()
val onError = mockk<Consumer<AuthException>>(relaxed = true)
val currentAuthState = mockk<AuthState> {
every { authNState } returns AuthenticationState.SignedOut(mockk())
}
every { authStateMachine.getCurrentState(captureLambda()) } answers {
lambda<(AuthState) -> Unit>().invoke(currentAuthState)
}

// WHEN
plugin.signIn(
"username",
"password",
AWSCognitoAuthSignInOptions.builder().authFlowType(AuthFlowType.CUSTOM_AUTH_WITH_SRP).build(),
onSuccess,
onError
)

// THEN
verify(exactly = 0) { onSuccess.accept(any()) }
}

@Test
fun testConfirmSignUpFailsIfNotConfigured() {
// GIVEN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,13 @@ open class StateTransitionTestBase {
Mockito.`when`(mockSRPActions.initiateSRPAuthAction(MockitoHelper.anyObject()))
.thenReturn(
Action { dispatcher, _ ->
dispatcher.send(SRPEvent(SRPEvent.EventType.RespondPasswordVerifier(mapOf(), mapOf())))
dispatcher.send(
SRPEvent(
SRPEvent.EventType.RespondPasswordVerifier(
mapOf(), mapOf(), "sample_session"
)
)
)
}
)

Expand Down