Skip to content

Commit

Permalink
Update signInMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerjroach committed Nov 26, 2024
1 parent 58429a9 commit c7bd1dd
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ internal object SRPCognitoActions : SRPActions {
override fun verifyPasswordSRPAction(
challengeParameters: Map<String, String>,
metadata: Map<String, String>,
session: String?
session: String?,
signInMethod: SignInMethod
) =
Action<AuthEnvironment>("VerifyPasswordSRP") { id, dispatcher ->
logger.verbose("$id Starting execution")
Expand Down Expand Up @@ -294,7 +295,7 @@ internal object SRPCognitoActions : SRPActions {
session = response.session,
challengeParameters = response.challengeParameters,
authenticationResult = response.authenticationResult,
signInMethod = SignInMethod.ApiBased(SignInMethod.ApiBased.AuthType.USER_SRP_AUTH)
signInMethod = signInMethod
)
} else {
throw ServiceException(
Expand All @@ -313,7 +314,7 @@ internal object SRPCognitoActions : SRPActions {
)
)
)
SRPEvent(SRPEvent.EventType.RetryRespondPasswordVerifier(challengeParams, metadata, session))
SRPEvent(SRPEvent.EventType.RetryRespondPasswordVerifier(challengeParams, metadata, session, signInMethod))
} else {
val errorEvent = SRPEvent(SRPEvent.EventType.ThrowPasswordVerifierError(e))
logger.verbose("$id Sending event ${errorEvent.type}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ internal object SetupTOTPCognitoActions : SetupTOTPActions {
)
} catch (exception: Exception) {
SetupTOTPEvent(
SetupTOTPEvent.EventType.ThrowAuthError(exception, eventType.username, eventType.session, eventType.signInMethod)
SetupTOTPEvent.EventType.ThrowAuthError(
exception, eventType.username, eventType.session, eventType.signInMethod
)
)
}
dispatcher.send(evt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ internal object SignInCognitoActions : SignInActions {
override fun initResolveChallenge(event: SignInEvent.EventType.ReceivedChallenge) =
Action<AuthEnvironment>("InitResolveChallenge") { id, dispatcher ->
logger.verbose("$id Starting execution")
val evt = SignInChallengeEvent(SignInChallengeEvent.EventType.WaitForAnswer(event.challenge, event.signInMethod, true))
val evt = SignInChallengeEvent(
SignInChallengeEvent.EventType.WaitForAnswer(event.challenge, event.signInMethod, true)
)
logger.verbose("$id Sending event ${evt.type}")
dispatcher.send(evt)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package com.amplifyframework.auth.cognito.helpers

import aws.sdk.kotlin.services.cognitoidentityprovider.model.AuthFlowType as CognitoAuthFlowType
import com.amplifyframework.auth.cognito.options.AuthFlowType
import com.amplifyframework.statemachine.codegen.data.SignInMethod

internal fun AuthFlowType.toCognitoType() = when (this) {
AuthFlowType.USER_SRP_AUTH -> CognitoAuthFlowType.UserSrpAuth
Expand All @@ -25,3 +26,12 @@ internal fun AuthFlowType.toCognitoType() = when (this) {
AuthFlowType.USER_PASSWORD_AUTH -> CognitoAuthFlowType.UserPasswordAuth
AuthFlowType.USER_AUTH -> CognitoAuthFlowType.UserAuth
}

internal fun AuthFlowType.toSignInMethod() = when (this) {
AuthFlowType.USER_SRP_AUTH -> SignInMethod.ApiBased(SignInMethod.ApiBased.AuthType.USER_SRP_AUTH)
AuthFlowType.CUSTOM_AUTH -> SignInMethod.ApiBased(SignInMethod.ApiBased.AuthType.CUSTOM_AUTH)
AuthFlowType.CUSTOM_AUTH_WITH_SRP -> SignInMethod.ApiBased(SignInMethod.ApiBased.AuthType.CUSTOM_AUTH)
AuthFlowType.CUSTOM_AUTH_WITHOUT_SRP -> SignInMethod.ApiBased(SignInMethod.ApiBased.AuthType.CUSTOM_AUTH)
AuthFlowType.USER_PASSWORD_AUTH -> SignInMethod.ApiBased(SignInMethod.ApiBased.AuthType.USER_PASSWORD_AUTH)
AuthFlowType.USER_AUTH -> SignInMethod.ApiBased(SignInMethod.ApiBased.AuthType.USER_AUTH)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.amplifyframework.statemachine.codegen.actions

import com.amplifyframework.statemachine.Action
import com.amplifyframework.statemachine.codegen.data.SignInMethod
import com.amplifyframework.statemachine.codegen.events.SRPEvent

internal interface SRPActions {
Expand All @@ -24,6 +25,7 @@ internal interface SRPActions {
fun verifyPasswordSRPAction(
challengeParameters: Map<String, String>,
metadata: Map<String, String>,
session: String?
session: String?,
signInMethod: SignInMethod
): Action
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.amplifyframework.statemachine.codegen.events
import com.amplifyframework.auth.cognito.options.AuthFlowType
import com.amplifyframework.statemachine.StateMachineEvent
import com.amplifyframework.statemachine.codegen.data.AuthChallenge
import com.amplifyframework.statemachine.codegen.data.SignInMethod
import java.util.Date

internal class SRPEvent(val eventType: EventType, override val time: Date? = null) :
Expand All @@ -44,7 +45,8 @@ internal class SRPEvent(val eventType: EventType, override val time: Date? = nul
data class RetryRespondPasswordVerifier(
val challengeParameters: Map<String, String>,
val metadata: Map<String, String>,
val session: String?
val session: String?,
val signInMethod: SignInMethod
) : EventType()

data class ThrowAuthError(val exception: Exception) : EventType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ internal class SetupTOTPEvent(val eventType: EventType, override val time: Date?
val challengeParams: Map<String, String>?,
val signInMethod: SignInMethod
) : EventType()
data class ThrowAuthError(val exception: Exception, val username: String, val session: String?, val signInMethod: SignInMethod) : EventType()
data class ThrowAuthError(
val exception: Exception,
val username: String,
val session: String?,
val signInMethod: SignInMethod
) : EventType()
data class VerifyChallengeAnswer(
val answer: String,
val username: String,
Expand All @@ -43,7 +48,11 @@ internal class SetupTOTPEvent(val eventType: EventType, override val time: Date?
) :
EventType()

data class RespondToAuthChallenge(val username: String, val session: String?, val signInMethod: SignInMethod) : EventType()
data class RespondToAuthChallenge(
val username: String,
val session: String?,
val signInMethod: SignInMethod
) : EventType()
data class Verified(val id: String = "") : EventType()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@

package com.amplifyframework.statemachine.codegen.states

import com.amplifyframework.auth.cognito.helpers.toSignInMethod
import com.amplifyframework.statemachine.State
import com.amplifyframework.statemachine.StateMachineEvent
import com.amplifyframework.statemachine.StateMachineResolver
import com.amplifyframework.statemachine.StateResolution
import com.amplifyframework.statemachine.codegen.actions.SRPActions
import com.amplifyframework.statemachine.codegen.data.SignInMethod
import com.amplifyframework.statemachine.codegen.events.SRPEvent

internal sealed class SRPSignInState : State {
data class NotStarted(val id: String = "") : SRPSignInState()
data class InitiatingSRPA(val id: String = "") : SRPSignInState()
data class InitiatingSRPA(val signInMethod: SignInMethod) : SRPSignInState()
data class RespondingPasswordVerifier(val id: String = "") : SRPSignInState()
data class SignedIn(val id: String = "") : SRPSignInState()
data class Cancelling(val id: String = "") : SRPSignInState()
Expand All @@ -44,18 +46,18 @@ internal sealed class SRPSignInState : State {
is NotStarted -> when (srpEvent) {
is SRPEvent.EventType.InitiateSRP -> {
val action = srpActions.initiateSRPAuthAction(srpEvent)
StateResolution(InitiatingSRPA(), listOf(action))
StateResolution(InitiatingSRPA(srpEvent.authFlowType.toSignInMethod()), listOf(action))
}
is SRPEvent.EventType.InitiateSRPWithCustom -> {
val action = srpActions.initiateSRPWithCustomAuthAction(srpEvent)
StateResolution(InitiatingSRPA(), listOf(action))
StateResolution(InitiatingSRPA(SignInMethod.ApiBased(SignInMethod.ApiBased.AuthType.CUSTOM_AUTH)), listOf(action))
}
else -> defaultResolution
}
is InitiatingSRPA -> when (srpEvent) {
is SRPEvent.EventType.RespondPasswordVerifier -> {
val action = srpActions.verifyPasswordSRPAction(
srpEvent.challengeParameters, srpEvent.metadata, srpEvent.session
srpEvent.challengeParameters, srpEvent.metadata, srpEvent.session, oldState.signInMethod
)
StateResolution(RespondingPasswordVerifier(), listOf(action))
}
Expand All @@ -66,7 +68,7 @@ internal sealed class SRPSignInState : State {
is RespondingPasswordVerifier -> when (srpEvent) {
is SRPEvent.EventType.RetryRespondPasswordVerifier -> {
val action = srpActions.verifyPasswordSRPAction(
srpEvent.challengeParameters, srpEvent.metadata, srpEvent.session
srpEvent.challengeParameters, srpEvent.metadata, srpEvent.session, srpEvent.signInMethod
)
StateResolution(RespondingPasswordVerifier(), listOf(action))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ class SetupTOTPCognitoActionsTest {
initiateAction.execute(dispatcher, authEnvironment)

val expectedEvent = SetupTOTPEvent(
SetupTOTPEvent.EventType.ThrowAuthError(serviceException, "USERNAME", "SESSION", SignInMethod.ApiBased(SignInMethod.ApiBased.AuthType.USER_SRP_AUTH)
SetupTOTPEvent.EventType.ThrowAuthError(
serviceException,
"USERNAME",
"SESSION",
SignInMethod.ApiBased(SignInMethod.ApiBased.AuthType.USER_SRP_AUTH)
)
)
assertEquals(
Expand Down Expand Up @@ -171,7 +175,9 @@ class SetupTOTPCognitoActionsTest {
}
}
val expectedEvent = SetupTOTPEvent(
SetupTOTPEvent.EventType.RespondToAuthChallenge(username, session, SignInMethod.ApiBased(SignInMethod.ApiBased.AuthType.USER_SRP_AUTH))
SetupTOTPEvent.EventType.RespondToAuthChallenge(
username, session, SignInMethod.ApiBased(SignInMethod.ApiBased.AuthType.USER_SRP_AUTH)
)
)

val verifyChallengeAnswerAction = SetupTOTPCognitoActions.verifyChallengeAnswer(
Expand Down Expand Up @@ -268,7 +274,12 @@ class SetupTOTPCognitoActionsTest {
throw serviceException
}
val expectedEvent = SetupTOTPEvent(
SetupTOTPEvent.EventType.ThrowAuthError(serviceException, "USERNAME", "SESSION", SignInMethod.ApiBased(SignInMethod.ApiBased.AuthType.USER_SRP_AUTH))
SetupTOTPEvent.EventType.ThrowAuthError(
serviceException,
"USERNAME",
"SESSION",
SignInMethod.ApiBased(SignInMethod.ApiBased.AuthType.USER_SRP_AUTH)
)
)

val verifyChallengeAnswerAction = SetupTOTPCognitoActions.verifyChallengeAnswer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ object AuthStateJsonGenerator : SerializableProvider {
"CODE_DELIVERY_DESTINATION" to "+12345678900"
),
),
SignInMethod.ApiBased(SignInMethod.ApiBased.AuthType.CUSTOM_AUTH)
SignInMethod.ApiBased(SignInMethod.ApiBased.AuthType.USER_SRP_AUTH)
)
)
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ object SignInTestCaseGenerator : SerializableProvider {
),
validations = listOf(
mockedSignInSuccessExpectation,
ExpectationShapes.State("SignedIn_SessionEstablished.json")
ExpectationShapes.State("SignedIn_SessionEstablished_User_Auth.json")
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"description": "Test that confirmsignin secondary challenge processes the custom challenge returned",
"preConditions": {
"amplify-configuration": "authconfiguration.json",
"state": "SigningIn_SigningIn.json",
"state": "SigningIn_SigningIn_Custom.json",
"mockedResponses": [
{
"type": "cognitoIdentityProvider",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
},
{
"type": "state",
"expectedState": "SignedIn_SessionEstablished.json"
"expectedState": "SignedIn_SessionEstablished_User_Auth.json"
}
]
}

0 comments on commit c7bd1dd

Please sign in to comment.