Skip to content

feat(authenticator): Add support for Email MFA #199

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

Merged
merged 11 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
@@ -0,0 +1,114 @@
/*
* Copyright 2024 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.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.amplifyframework.ui.authenticator.ui

import com.amplifyframework.auth.AuthCodeDeliveryDetails
import com.amplifyframework.ui.authenticator.ScreenshotTestBase
import com.amplifyframework.ui.authenticator.SignInConfirmMfaState
import com.amplifyframework.ui.authenticator.enums.AuthenticatorInitialStep
import com.amplifyframework.ui.authenticator.enums.AuthenticatorStep
import com.amplifyframework.ui.authenticator.forms.FieldConfig
import com.amplifyframework.ui.authenticator.forms.FieldError
import com.amplifyframework.ui.authenticator.forms.FieldKey
import com.amplifyframework.ui.authenticator.mockFieldData
import com.amplifyframework.ui.authenticator.mockFieldState
import com.amplifyframework.ui.authenticator.mockForm
import org.junit.Test

class SignInConfirmMfa : ScreenshotTestBase() {

@Test
fun sign_in_confirm_email_mfa_default() {
screenshot {
SignInConfirmMfa(
mockSignInConfirmMfaState(
AuthCodeDeliveryDetails(
"[email protected]",
AuthCodeDeliveryDetails.DeliveryMedium.EMAIL
)
)
)
}
}

@Test
fun sign_in_confirm_email_mfa_incorrect_code() {
screenshot {
SignInConfirmMfa(
mockSignInConfirmMfaState(
AuthCodeDeliveryDetails(
"[email protected]",
AuthCodeDeliveryDetails.DeliveryMedium.EMAIL
),
"123456",
FieldError.ConfirmationCodeIncorrect
)
)
}
}

@Test
fun sign_in_confirm_sms_mfa_default() {
screenshot {
SignInConfirmMfa(
mockSignInConfirmMfaState(
AuthCodeDeliveryDetails(
"123-123-1234",
AuthCodeDeliveryDetails.DeliveryMedium.SMS
)
)
)
}
}

@Test
fun sign_in_confirm_sms_mfa_incorrect_code() {
screenshot {
SignInConfirmMfa(
mockSignInConfirmMfaState(
AuthCodeDeliveryDetails(
"123-123-1234",
AuthCodeDeliveryDetails.DeliveryMedium.SMS
),
"123456",
FieldError.ConfirmationCodeIncorrect
)
)
}
}

private fun mockSignInConfirmMfaState(
deliveryDetails: AuthCodeDeliveryDetails,
confirmationCode: String = "",
fieldError: FieldError? = null
) = object : SignInConfirmMfaState {
override val form = mockForm(
mockFieldData(
config = FieldConfig.Text(FieldKey.ConfirmationCode),
state = mockFieldState(content = confirmationCode, error = fieldError)
)
)
override val deliveryDetails: AuthCodeDeliveryDetails
get() = deliveryDetails

override fun moveTo(step: AuthenticatorInitialStep) {}
override suspend fun confirmSignIn() {
TODO("Not yet implemented")
}

override val step = AuthenticatorStep.SignInContinueWithEmailSetup
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2024 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.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.amplifyframework.ui.authenticator.ui

import com.amplifyframework.ui.authenticator.ScreenshotTestBase
import com.amplifyframework.ui.authenticator.SignInContinueWithEmailSetupState
import com.amplifyframework.ui.authenticator.enums.AuthenticatorInitialStep
import com.amplifyframework.ui.authenticator.enums.AuthenticatorStep
import com.amplifyframework.ui.authenticator.forms.FieldConfig
import com.amplifyframework.ui.authenticator.forms.FieldKey
import com.amplifyframework.ui.authenticator.mockFieldData
import com.amplifyframework.ui.authenticator.mockFieldState
import com.amplifyframework.ui.authenticator.mockForm
import org.junit.Test

class SignInContinueWithEmailSetupScreenshots : ScreenshotTestBase() {

@Test
fun default_state() {
screenshot {
SignInContinueWithEmailSetup(mockSignInContinueWithEmailSetupState())
}
}

private fun mockSignInContinueWithEmailSetupState() = object : SignInContinueWithEmailSetupState {
override val form = mockForm(
mockFieldData(
config = FieldConfig.Text(FieldKey.Email),
state = mockFieldState(content = "[email protected]")
)
)
override fun moveTo(step: AuthenticatorInitialStep) {}
override suspend fun continueSignIn() {}
override val step = AuthenticatorStep.SignInContinueWithEmailSetup
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2024 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.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.amplifyframework.ui.authenticator.ui

import com.amplifyframework.auth.MFAType
import com.amplifyframework.ui.authenticator.ScreenshotTestBase
import com.amplifyframework.ui.authenticator.SignInContinueWithMfaSetupSelectionState
import com.amplifyframework.ui.authenticator.enums.AuthenticatorInitialStep
import com.amplifyframework.ui.authenticator.enums.AuthenticatorStep
import com.amplifyframework.ui.authenticator.forms.FieldConfig
import com.amplifyframework.ui.authenticator.forms.FieldKey
import com.amplifyframework.ui.authenticator.mockFieldData
import com.amplifyframework.ui.authenticator.mockFieldState
import com.amplifyframework.ui.authenticator.mockForm
import org.junit.Test

class SignInContinueWithMfaSetupSelection : ScreenshotTestBase() {

@Test
fun default_state() {
screenshot {
SignInContinueWithMfaSetupSelection(mockSignInContinueWithMfaSetupSelectionState())
}
}

private fun mockSignInContinueWithMfaSetupSelectionState() = object : SignInContinueWithMfaSetupSelectionState {
override val form = mockForm(
mockFieldData(
config = FieldConfig.Text(FieldKey.MfaSelection),
state = mockFieldState(content = "EMAIL_OTP")
)
)
override val allowedMfaTypes: Set<MFAType>
get() = setOf(MFAType.TOTP, MFAType.SMS, MFAType.EMAIL)

override fun moveTo(step: AuthenticatorInitialStep) {}
override suspend fun continueSignIn() {}
override val step = AuthenticatorStep.SignInContinueWithEmailSetup
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,53 @@ interface SignInContinueWithTotpSetupState : AuthenticatorStepState {
suspend fun continueSignIn()
}

/**
* The user has completed the initial Sign In step, and must setup their desired MFA method to continue.
*/
@Stable
interface SignInContinueWithMfaSetupSelectionState : AuthenticatorStepState {
/**
* The input form state holder for this step.
*/
val form: MutableFormState

/**
* The set of [MFAType] that could be used to continue this sign in.
*/
val allowedMfaTypes: Set<MFAType>

/**
* Move the user to a different [AuthenticatorInitialStep].
*/
fun moveTo(step: AuthenticatorInitialStep)

/**
* Continue the user's sign in using the information entered into the [form].
*/
suspend fun continueSignIn()
}

/**
* The user has completed the initial Sign In step, and must setup email MFA method to continue.
*/
@Stable
interface SignInContinueWithEmailSetupState : AuthenticatorStepState {
/**
* The input form state holder for this step.
*/
val form: MutableFormState

/**
* Move the user to a different [AuthenticatorInitialStep].
*/
fun moveTo(step: AuthenticatorInitialStep)

/**
* Continue the user's sign in using the information entered into the [form].
*/
suspend fun continueSignIn()
}

/**
* The user has completed the initial Sign In step, and must select their desired MFA method to continue.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,35 @@ internal class AuthenticatorViewModel(
moveTo(newState)
}

private suspend fun handleMfaSetupSelectionRequired(
username: String,
password: String,
allowedMfaTypes: Set<MFAType>?
) {
if (allowedMfaTypes.isNullOrEmpty()) {
handleGeneralFailure(AuthException("Missing allowedMfaTypes", "Please open a bug with Amplify"))
return
}

moveTo(
stateFactory.newSignInContinueWithMfaSetupSelectionState(
allowedMfaTypes = allowedMfaTypes,
onSubmit = { mfaType -> confirmSignIn(username, password, mfaType) }
)
)
}

private suspend fun handleEmailMfaSetupRequired(
username: String,
password: String
) {
moveTo(
stateFactory.newSignInContinueWithEmailSetupState(
onSubmit = { mfaType -> confirmSignIn(username, password, mfaType) }
)
)
}

private suspend fun handleMfaSelectionRequired(
username: String,
password: String,
Expand All @@ -349,7 +378,8 @@ internal class AuthenticatorViewModel(
private suspend fun handleSignInSuccess(username: String, password: String, result: AuthSignInResult) {
when (val nextStep = result.nextStep.signInStep) {
AuthSignInStep.DONE -> checkVerificationMechanisms()
AuthSignInStep.CONFIRM_SIGN_IN_WITH_SMS_MFA_CODE -> moveTo(
AuthSignInStep.CONFIRM_SIGN_IN_WITH_SMS_MFA_CODE,
AuthSignInStep.CONFIRM_SIGN_IN_WITH_OTP -> moveTo(
stateFactory.newSignInMfaState(
result.nextStep.codeDeliveryDetails
) { confirmationCode -> confirmSignIn(username, password, confirmationCode) }
Expand All @@ -373,6 +403,10 @@ internal class AuthenticatorViewModel(
AuthSignInStep.CONFIRM_SIGN_UP -> handleUnconfirmedSignIn(username, password)
AuthSignInStep.CONTINUE_SIGN_IN_WITH_MFA_SELECTION ->
handleMfaSelectionRequired(username, password, result.nextStep.allowedMFATypes)
AuthSignInStep.CONTINUE_SIGN_IN_WITH_MFA_SETUP_SELECTION ->
handleMfaSetupSelectionRequired(username, password, result.nextStep.allowedMFATypes)
AuthSignInStep.CONTINUE_SIGN_IN_WITH_EMAIL_MFA_SETUP ->
handleEmailMfaSetupRequired(username, password)
AuthSignInStep.CONTINUE_SIGN_IN_WITH_TOTP_SETUP ->
handleTotpSetupRequired(username, password, result.nextStep.totpSetupDetails)
AuthSignInStep.CONFIRM_SIGN_IN_WITH_TOTP_CODE -> moveTo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ abstract class AuthenticatorStep internal constructor() {
*/
object SignInContinueWithTotpSetup : AuthenticatorStep()

/**
* The user has completed the initial Sign In step, and must register an email MFA to continue.
*/
object SignInContinueWithEmailSetup : AuthenticatorStep()

/**
* The user has completed the initial Sign In step, but is required to setup a MFA type to continue.
*/
object SignInContinueWithMfaSetupSelection : AuthenticatorStep()

/**
* The user has completed the initial Sign In step, and must select their desired MFA method to continue.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2024 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.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.amplifyframework.ui.authenticator.states

import com.amplifyframework.ui.authenticator.SignInContinueWithEmailSetupState
import com.amplifyframework.ui.authenticator.enums.AuthenticatorInitialStep
import com.amplifyframework.ui.authenticator.enums.AuthenticatorStep
import com.amplifyframework.ui.authenticator.forms.FieldKey

internal class SignInContinueWithEmailSetupStateImpl(
private val onSubmit: suspend (email: String) -> Unit,
private val onMoveTo: (step: AuthenticatorInitialStep) -> Unit
) : BaseStateImpl(), SignInContinueWithEmailSetupState {

init {
form.addFields {
email(required = true)
}
}

override fun moveTo(step: AuthenticatorInitialStep) = onMoveTo(step)

override suspend fun continueSignIn() = doSubmit {
val email = form.getTrimmed(FieldKey.Email)!!
onSubmit(email)
}

override val step = AuthenticatorStep.SignInContinueWithEmailSetup
}
Loading