Skip to content

Commit

Permalink
Add userAttributes to confirmSignIn call
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerjroach committed Nov 17, 2023
1 parent 644be2d commit 58d6823
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -785,12 +785,15 @@ internal class RealAWSCognitoAuthPlugin(
},
{
val awsCognitoConfirmSignInOptions = options as? AWSCognitoAuthConfirmSignInOptions
val metadata = awsCognitoConfirmSignInOptions?.metadata ?: emptyMap()
val userAttributes = awsCognitoConfirmSignInOptions?.userAttributes ?: emptyList()
when (signInState) {
is SignInState.ResolvingChallenge -> {
val event = SignInChallengeEvent(
SignInChallengeEvent.EventType.VerifyChallengeAnswer(
challengeResponse,
awsCognitoConfirmSignInOptions?.metadata ?: mapOf()
metadata,
userAttributes
)
)
authStateMachine.send(event)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.amplifyframework.auth.cognito.actions
import aws.sdk.kotlin.services.cognitoidentityprovider.model.ChallengeNameType
import aws.sdk.kotlin.services.cognitoidentityprovider.model.ResourceNotFoundException
import aws.sdk.kotlin.services.cognitoidentityprovider.respondToAuthChallenge
import com.amplifyframework.auth.AuthUserAttribute
import com.amplifyframework.auth.cognito.AuthEnvironment
import com.amplifyframework.auth.cognito.helpers.AuthHelper
import com.amplifyframework.auth.cognito.helpers.SignInChallengeHelper
Expand All @@ -32,9 +33,11 @@ import com.amplifyframework.statemachine.codegen.events.SignInChallengeEvent
internal object SignInChallengeCognitoActions : SignInChallengeActions {
private const val KEY_SECRET_HASH = "SECRET_HASH"
private const val KEY_USERNAME = "USERNAME"
private const val KEY_PREFIX_USER_ATTRIBUTE = "userAttributes."
override fun verifyChallengeAuthAction(
answer: String,
metadata: Map<String, String>,
attributes: List<AuthUserAttribute>,
challenge: AuthChallenge
): Action = Action<AuthEnvironment>("VerifySignInChallenge") { id, dispatcher ->
logger.verbose("$id Starting execution")
Expand All @@ -50,6 +53,12 @@ internal object SignInChallengeCognitoActions : SignInChallengeActions {
challengeResponses[responseKey] = answer
}

challengeResponses.putAll(
attributes.map {
Pair("${KEY_PREFIX_USER_ATTRIBUTE}${it.key.keyString}", it.value)
}
)

val secretHash = AuthHelper.getSecretHash(
username,
configuration.userPool?.appClient,
Expand Down Expand Up @@ -90,6 +99,7 @@ internal object SignInChallengeCognitoActions : SignInChallengeActions {
SignInChallengeEvent.EventType.RetryVerifyChallengeAnswer(
answer,
metadata,
attributes,
challenge
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@

package com.amplifyframework.statemachine.codegen.actions

import com.amplifyframework.auth.AuthUserAttribute
import com.amplifyframework.statemachine.Action
import com.amplifyframework.statemachine.codegen.data.AuthChallenge

internal interface SignInChallengeActions {
fun verifyChallengeAuthAction(
answer: String,
metadata: Map<String, String>,
userAttributes: List<AuthUserAttribute>,
challenge: AuthChallenge
): Action
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@

package com.amplifyframework.statemachine.codegen.events

import com.amplifyframework.auth.AuthUserAttribute
import com.amplifyframework.statemachine.StateMachineEvent
import com.amplifyframework.statemachine.codegen.data.AuthChallenge
import java.util.Date

internal class SignInChallengeEvent(val eventType: EventType, override val time: Date? = null) : StateMachineEvent {
sealed class EventType {
data class WaitForAnswer(val challenge: AuthChallenge, val hasNewResponse: Boolean = false) : EventType()
data class VerifyChallengeAnswer(val answer: String, val metadata: Map<String, String>) : EventType()
data class VerifyChallengeAnswer(
val answer: String,
val metadata: Map<String, String>,
val userAttributes: List<AuthUserAttribute>
) : EventType()

data class RetryVerifyChallengeAnswer(
val answer: String,
val metadata: Map<String, String>,
val userAttributes: List<AuthUserAttribute>,
val authChallenge: AuthChallenge
) : EventType()
data class FinalizeSignIn(val accessToken: String) : EventType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ internal sealed class SignInChallengeState : State {
is WaitingForAnswer -> when (challengeEvent) {
is SignInChallengeEvent.EventType.VerifyChallengeAnswer -> {
val action = challengeActions.verifyChallengeAuthAction(
challengeEvent.answer, challengeEvent.metadata, oldState.challenge
challengeEvent.answer,
challengeEvent.metadata,
challengeEvent.userAttributes,
oldState.challenge
)
StateResolution(Verifying(oldState.challenge.challengeName), listOf(action))
}
Expand All @@ -78,7 +81,10 @@ internal sealed class SignInChallengeState : State {
}
is SignInChallengeEvent.EventType.RetryVerifyChallengeAnswer -> {
val action = challengeActions.verifyChallengeAuthAction(
challengeEvent.answer, challengeEvent.metadata, challengeEvent.authChallenge
challengeEvent.answer,
challengeEvent.metadata,
challengeEvent.userAttributes,
challengeEvent.authChallenge,
)
StateResolution(Verifying(challengeEvent.authChallenge.challengeName), listOf(action))
}
Expand All @@ -92,7 +98,10 @@ internal sealed class SignInChallengeState : State {
when (challengeEvent) {
is SignInChallengeEvent.EventType.VerifyChallengeAnswer -> {
val action = challengeActions.verifyChallengeAuthAction(
challengeEvent.answer, challengeEvent.metadata, oldState.challenge
challengeEvent.answer,
challengeEvent.metadata,
challengeEvent.userAttributes,
oldState.challenge,
)
StateResolution(Verifying(oldState.challenge.challengeName), listOf(action))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,9 @@ open class StateTransitionTestBase {
mockSignInChallengeActions.verifyChallengeAuthAction(
MockitoHelper.anyObject(),
MockitoHelper.anyObject(),
MockitoHelper.anyObject()
)
MockitoHelper.anyObject(),
MockitoHelper.anyObject(),
)
)
.thenReturn(
Action { dispatcher, _ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,13 @@ class StateTransitionTests : StateTransitionTestBase() {
SignInChallengeEvent(
SignInChallengeEvent.EventType.RetryVerifyChallengeAnswer(
"test",
mapOf(),
emptyMap(),
emptyList(),
AuthChallenge(
ChallengeNameType.CustomChallenge.toString(),
"Test",
"session_mock_value",
mapOf()
emptyMap(),
)
)
)
Expand All @@ -401,7 +402,8 @@ class StateTransitionTests : StateTransitionTestBase() {
SignInChallengeEvent(
SignInChallengeEvent.EventType.VerifyChallengeAnswer(
"test",
mapOf()
emptyMap(),
emptyList()
)
)
)
Expand Down Expand Up @@ -481,7 +483,7 @@ class StateTransitionTests : StateTransitionTestBase() {
challengeState?.apply {
stateMachine.send(
SignInChallengeEvent(
SignInChallengeEvent.EventType.VerifyChallengeAnswer("test", mapOf())
SignInChallengeEvent.EventType.VerifyChallengeAnswer("test", emptyMap(), emptyList())
)
)
}
Expand Down

0 comments on commit 58d6823

Please sign in to comment.