-
Notifications
You must be signed in to change notification settings - Fork 125
fix(auth): Fix confirm signin when incorrect MFA code is entered #2286
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
Changes from all commits
9dcc7f6
06b3fae
735bb2c
ec8df34
a151860
2cddb87
dadd049
ad5a10d
6353e11
4a0d605
8c75154
5bd5172
2fe92aa
93b79cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -588,11 +588,17 @@ internal class RealAWSCognitoAuthPlugin( | |
authStateMachine.getCurrentState { authState -> | ||
val authNState = authState.authNState | ||
val signInState = (authNState as? AuthenticationState.SigningIn)?.signInState | ||
when ((signInState as? SignInState.ResolvingChallenge)?.challengeState) { | ||
is SignInChallengeState.WaitingForAnswer -> { | ||
_confirmSignIn(challengeResponse, options, onSuccess, onError) | ||
if (signInState is SignInState.ResolvingChallenge) { | ||
when (signInState.challengeState) { | ||
is SignInChallengeState.WaitingForAnswer -> { | ||
_confirmSignIn(challengeResponse, options, onSuccess, onError) | ||
} | ||
else -> { | ||
onError.accept(InvalidStateException()) | ||
} | ||
} | ||
else -> onError.accept(InvalidStateException()) | ||
} else { | ||
onError.accept(InvalidStateException()) | ||
} | ||
} | ||
} | ||
|
@@ -610,7 +616,6 @@ internal class RealAWSCognitoAuthPlugin( | |
val authNState = authState.authNState | ||
val authZState = authState.authZState | ||
val signInState = (authNState as? AuthenticationState.SigningIn)?.signInState | ||
val challengeState = (signInState as? SignInState.ResolvingChallenge)?.challengeState | ||
when { | ||
authNState is AuthenticationState.SignedIn && | ||
authZState is AuthorizationState.SessionEstablished -> { | ||
|
@@ -625,21 +630,34 @@ internal class RealAWSCognitoAuthPlugin( | |
signInState is SignInState.Error -> { | ||
authStateMachine.cancel(token) | ||
onError.accept( | ||
CognitoAuthExceptionConverter.lookup(signInState.exception, "Confirm Sign in failed.") | ||
CognitoAuthExceptionConverter.lookup( | ||
signInState.exception, "Confirm Sign in failed." | ||
) | ||
) | ||
} | ||
signInState is SignInState.ResolvingChallenge && | ||
signInState.challengeState is SignInChallengeState.Error -> { | ||
authStateMachine.cancel(token) | ||
onError.accept( | ||
CognitoAuthExceptionConverter.lookup( | ||
( | ||
signInState.challengeState as SignInChallengeState.Error | ||
).exception, | ||
"Confirm Sign in failed." | ||
) | ||
) | ||
} | ||
} | ||
}, | ||
{ | ||
val awsCognitoConfirmSignInOptions = options as? AWSCognitoAuthConfirmSignInOptions | ||
val event = SignInChallengeEvent( | ||
SignInChallengeEvent.EventType.VerifyChallengeAnswer( | ||
challengeResponse, | ||
awsCognitoConfirmSignInOptions?.metadata ?: mapOf() | ||
) | ||
}, { | ||
val awsCognitoConfirmSignInOptions = options as? AWSCognitoAuthConfirmSignInOptions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation looks off, I wonder how it passed lint check. |
||
val event = SignInChallengeEvent( | ||
SignInChallengeEvent.EventType.VerifyChallengeAnswer( | ||
challengeResponse, | ||
awsCognitoConfirmSignInOptions?.metadata ?: mapOf() | ||
) | ||
authStateMachine.send(event) | ||
} | ||
) | ||
authStateMachine.send(event) | ||
} | ||
) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,5 +1,5 @@ | ||||||||||
/* | ||||||||||
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||||||||||
* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||||||||||
* | ||||||||||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||||||||||
* You may not use this file except in compliance with the License. | ||||||||||
|
@@ -26,7 +26,6 @@ import com.amplifyframework.statemachine.codegen.actions.SignInChallengeActions | |||||||||
import com.amplifyframework.statemachine.codegen.data.AuthChallenge | ||||||||||
import com.amplifyframework.statemachine.codegen.events.CustomSignInEvent | ||||||||||
import com.amplifyframework.statemachine.codegen.events.SignInChallengeEvent | ||||||||||
import com.amplifyframework.statemachine.codegen.events.SignInEvent | ||||||||||
|
||||||||||
internal object SignInChallengeCognitoActions : SignInChallengeActions { | ||||||||||
private const val KEY_SECRET_HASH = "SECRET_HASH" | ||||||||||
|
@@ -81,20 +80,25 @@ internal object SignInChallengeCognitoActions : SignInChallengeActions { | |||||||||
) | ||||||||||
) | ||||||||||
} catch (e: Exception) { | ||||||||||
SignInEvent(SignInEvent.EventType.ThrowError(e)) | ||||||||||
SignInChallengeEvent(SignInChallengeEvent.EventType.ThrowError(e, challenge)) | ||||||||||
} | ||||||||||
logger.verbose("$id Sending event ${evt.type}") | ||||||||||
dispatcher.send(evt) | ||||||||||
} | ||||||||||
|
||||||||||
override fun resetToWaitingForAnswer( | ||||||||||
event: SignInChallengeEvent.EventType.ThrowError, | ||||||||||
challenge: AuthChallenge | ||||||||||
): Action = Action<AuthEnvironment>("ResetToWaitingForAnswer") { id, dispatcher -> | ||||||||||
logger.verbose("$id Starting execution") | ||||||||||
dispatcher.send(SignInChallengeEvent(SignInChallengeEvent.EventType.WaitForAnswer(challenge))) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit. |
||||||||||
} | ||||||||||
|
||||||||||
private fun getChallengeResponseKey(challengeName: String): String? { | ||||||||||
val VALUE_ANSWER = "ANSWER" | ||||||||||
val VALUE_SMS_MFA = "SMS_MFA_CODE" | ||||||||||
val VALUE_NEW_PASSWORD = "NEW_PASSWORD" | ||||||||||
return when (ChallengeNameType.fromValue(challengeName)) { | ||||||||||
is ChallengeNameType.SmsMfa -> VALUE_SMS_MFA | ||||||||||
is ChallengeNameType.NewPasswordRequired -> VALUE_NEW_PASSWORD | ||||||||||
is ChallengeNameType.CustomChallenge -> VALUE_ANSWER | ||||||||||
is ChallengeNameType.SmsMfa -> "SMS_MFA_CODE" | ||||||||||
is ChallengeNameType.NewPasswordRequired -> "NEW_PASSWORD" | ||||||||||
is ChallengeNameType.CustomChallenge -> "ANSWER" | ||||||||||
else -> null | ||||||||||
} | ||||||||||
} | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ internal sealed class SignInChallengeState : State { | |
data class WaitingForAnswer(val challenge: AuthChallenge) : SignInChallengeState() | ||
data class Verifying(val id: String = "") : SignInChallengeState() | ||
data class Verified(val id: String = "") : SignInChallengeState() | ||
data class Error(val exception: Exception, val challenge: AuthChallenge) : SignInChallengeState() | ||
|
||
class Resolver(private val challengeActions: SignInChallengeActions) : StateMachineResolver<SignInChallengeState> { | ||
override val defaultState: SignInChallengeState = NotStarted() | ||
|
@@ -58,8 +59,25 @@ internal sealed class SignInChallengeState : State { | |
} | ||
is Verifying -> when (challengeEvent) { | ||
is SignInChallengeEvent.EventType.Verified -> StateResolution(Verified()) | ||
is SignInChallengeEvent.EventType.ThrowError -> { | ||
val action = challengeActions.resetToWaitingForAnswer(challengeEvent, challengeEvent.challenge) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like passing just challengeEvent is enough. |
||
StateResolution(Error(challengeEvent.exception, challengeEvent.challenge), listOf(action)) | ||
} | ||
|
||
else -> defaultResolution | ||
} | ||
is Error -> { | ||
when (challengeEvent) { | ||
is SignInChallengeEvent.EventType.VerifyChallengeAnswer -> { | ||
val action = challengeActions.verifyChallengeAuthAction(challengeEvent, oldState.challenge) | ||
StateResolution(Verifying(oldState.challenge.challengeName), listOf(action)) | ||
} | ||
is SignInChallengeEvent.EventType.WaitForAnswer -> { | ||
gpanshu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
StateResolution(WaitingForAnswer(challengeEvent.challenge)) | ||
} | ||
else -> defaultResolution | ||
} | ||
} | ||
else -> defaultResolution | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"description": "Test that invalid code on confirm SignIn with SMS challenge errors out", | ||
"preConditions": { | ||
"amplify-configuration": "authconfiguration.json", | ||
"state": "SigningIn_SigningIn.json", | ||
"mockedResponses": [ | ||
{ | ||
"type": "cognitoIdentityProvider", | ||
"apiName": "respondToAuthChallenge", | ||
"responseType": "failure", | ||
"response": { | ||
"errorType": "CodeMismatchException", | ||
"errorMessage": "Confirmation code entered is not correct." | ||
} | ||
} | ||
] | ||
}, | ||
"api": { | ||
"name": "confirmSignIn", | ||
"params": { | ||
"challengeResponse": "000000" | ||
}, | ||
"options": { | ||
} | ||
}, | ||
"validations": [ | ||
{ | ||
"type": "amplify", | ||
"apiName": "confirmSignIn", | ||
"responseType": "failure", | ||
"response": { | ||
"errorType": "CodeMismatchException", | ||
"errorMessage": "Confirmation code entered is not correct.", | ||
"recoverySuggestion": "Enter correct confirmation code.", | ||
"cause": { | ||
"errorType": "CodeMismatchException", | ||
"errorMessage": "Confirmation code entered is not correct." | ||
} | ||
} | ||
} | ||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like code paths are still the same. This change looks unnecessary.