-
Couldn't load subscription status.
- Fork 127
feat(auth): Add support for Email MFA #2935
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
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
0b84364
initial email mfa work
tylerjroach 196bf67
remove unnecessary changes
tylerjroach d6c24ff
email mfa
tylerjroach c83b07d
Merge remote-tracking branch 'origin/main' into tjroach/email-mfa
tylerjroach 5b060ad
update to official kotlin sdk email release
tylerjroach 573370a
Update unit tests
vincetran b07a7da
Fix regressions to TOTP Setup
vincetran 7babc43
Fix ktlint issues
vincetran 3026c6a
Add Email MFA integration test and add unit tests
vincetran 803f35e
Merge branch 'main' into tjroach/email-mfa
vincetran b05b957
Fix ktlint issues
vincetran 7a59645
Fix ktlint issues
vincetran 1eeeaf7
Fix ktlint issues
vincetran 68d50d2
Update API dump
vincetran 2d042ec
Update AuthSignInStep
vincetran df76317
Update core.api
vincetran 64e200b
Remove AbortableCountdownLatch and fix a crash
vincetran 9dccb0e
Update test email domain and removed @BeforeClass
vincetran fa3c498
Made a var private
vincetran 3d082c4
Fix a unit test
vincetran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
aws-auth-cognito/src/androidTest/assets/create-mfa-subscription.graphql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| subscription OnCreateMfaInfo { | ||
| onCreateMfaInfo { | ||
| username | ||
| code | ||
| expirationTime | ||
| } | ||
| } |
189 changes: 189 additions & 0 deletions
189
...c/androidTest/java/com/amplifyframework/auth/cognito/AWSCognitoAuthPluginEmailMFATests.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| /* | ||
| * 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.auth.cognito | ||
|
|
||
| import android.content.Context | ||
| import androidx.test.core.app.ApplicationProvider | ||
| import com.amplifyframework.api.aws.AWSApiPlugin | ||
| import com.amplifyframework.api.graphql.SimpleGraphQLRequest | ||
| import com.amplifyframework.auth.AuthUserAttribute | ||
| import com.amplifyframework.auth.AuthUserAttributeKey | ||
| import com.amplifyframework.auth.MFAType | ||
| import com.amplifyframework.auth.cognito.exceptions.service.CodeMismatchException | ||
| import com.amplifyframework.auth.cognito.test.R | ||
| import com.amplifyframework.auth.options.AuthSignUpOptions | ||
| import com.amplifyframework.auth.result.AuthSignUpResult | ||
| import com.amplifyframework.auth.result.step.AuthSignInStep | ||
| import com.amplifyframework.core.configuration.AmplifyOutputs | ||
| import com.amplifyframework.core.configuration.AmplifyOutputsData | ||
| import com.amplifyframework.datastore.generated.model.MfaInfo | ||
| import com.amplifyframework.testutils.Assets | ||
| import com.amplifyframework.testutils.sync.SynchronousAuth | ||
| import java.util.Random | ||
| import java.util.UUID | ||
| import java.util.concurrent.CountDownLatch | ||
| import java.util.concurrent.TimeUnit | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertFailsWith | ||
| import org.junit.After | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
|
|
||
| class AWSCognitoAuthPluginEmailMFATests { | ||
|
|
||
| private val password = "${UUID.randomUUID()}BleepBloop1234!" | ||
| private val userName = "test${Random().nextInt()}" | ||
| private val email = "[email protected]" | ||
|
|
||
| private var authPlugin = AWSCognitoAuthPlugin() | ||
| private var apiPlugin = AWSApiPlugin() | ||
| private lateinit var synchronousAuth: SynchronousAuth | ||
| private var mfaCode = "" | ||
| private var latch: CountDownLatch? = null | ||
|
|
||
| @Before | ||
| fun initializePlugin() { | ||
| val context = ApplicationProvider.getApplicationContext<Context>() | ||
| val config = AmplifyOutputsData | ||
| .deserialize(context, AmplifyOutputs.fromResource(R.raw.amplify_outputs_email_or_totp_mfa)) | ||
|
|
||
| authPlugin.configure(config, context) | ||
| apiPlugin.configure(config, context) | ||
| synchronousAuth = SynchronousAuth.delegatingTo(authPlugin) | ||
|
|
||
| apiPlugin.subscribe( | ||
| SimpleGraphQLRequest<MfaInfo>( | ||
| Assets.readAsString("create-mfa-subscription.graphql"), | ||
| MfaInfo::class.java, | ||
| null | ||
| ), | ||
| { println("====== Subscription Established ======") }, | ||
| { | ||
| println("====== Received some MFA Info ======") | ||
| mfaCode = it.data.code | ||
| latch?.countDown() | ||
| }, | ||
| { println("====== Subscription Failed $it ======") }, | ||
| { } | ||
| ) | ||
| } | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| mfaCode = "" | ||
| synchronousAuth.deleteUser() | ||
| } | ||
|
|
||
| @Test | ||
| fun fresh_email_mfa_setup() { | ||
| // Step 1: Sign up a new user | ||
| signUpNewUser() | ||
|
|
||
| // Step 2: Attempt to sign in with the newly created user | ||
| var signInResult = synchronousAuth.signIn(userName, password) | ||
|
|
||
| // Validation 1: Validate that the next step is MFA Setup Selection | ||
| assertEquals(AuthSignInStep.CONTINUE_SIGN_IN_WITH_MFA_SETUP_SELECTION, signInResult.nextStep.signInStep) | ||
|
|
||
| // Validation 2: Validate that the available MFA choices are Email and TOTP | ||
| assertEquals(setOf(MFAType.EMAIL, MFAType.TOTP), signInResult.nextStep.allowedMFATypes) | ||
|
|
||
| // Step 3: Select "Email" as the MFA to set up | ||
| signInResult = synchronousAuth.confirmSignIn("EMAIL_OTP") | ||
|
|
||
| // Validation 2: Validate that the next step is to input the user's email address | ||
| assertEquals(AuthSignInStep.CONTINUE_SIGN_IN_WITH_EMAIL_MFA_SETUP, signInResult.nextStep.signInStep) | ||
|
|
||
| // Step 4: Input the email address to send the code to then wait for the MFA code | ||
| latch = CountDownLatch(1) | ||
| signInResult = synchronousAuth.confirmSignIn(email) | ||
|
|
||
| // Validation 3: Validate that the next step is to confirm the emailed MFA code | ||
| assertEquals(AuthSignInStep.CONFIRM_SIGN_IN_WITH_OTP, signInResult.nextStep.signInStep) | ||
|
|
||
| // Wait until the MFA code has been received | ||
| latch?.await(20, TimeUnit.SECONDS) | ||
|
|
||
| // Step 5: Input the emailed MFA code for confirmation | ||
| signInResult = synchronousAuth.confirmSignIn(mfaCode) | ||
|
|
||
| // Validation 4: Validate that MFA setup is done | ||
| assertEquals(AuthSignInStep.DONE, signInResult.nextStep.signInStep) | ||
| } | ||
|
|
||
| @Test | ||
| fun sign_in_to_existing_email_mfa() { | ||
| // Step 1: Sign up a new user with an existing email address | ||
| signUpNewUser(email) | ||
|
|
||
| // Step 2: Attempt to sign in with the newly created user | ||
| latch = CountDownLatch(1) | ||
| var signInResult = synchronousAuth.signIn(userName, password) | ||
|
|
||
| // Validation 1: Validate that the next step is to confirm the emailed MFA code | ||
| assertEquals(AuthSignInStep.CONFIRM_SIGN_IN_WITH_OTP, signInResult.nextStep.signInStep) | ||
|
|
||
| // Wait until the MFA code has been received | ||
| latch?.await(20, TimeUnit.SECONDS) | ||
|
|
||
| // Step 4: Input the emailed MFA code for confirmation | ||
| signInResult = synchronousAuth.confirmSignIn(mfaCode) | ||
|
|
||
| // Validation 2: Validate that MFA setup is done | ||
| assertEquals(AuthSignInStep.DONE, signInResult.nextStep.signInStep) | ||
| } | ||
|
|
||
| @Test | ||
| fun use_an_incorrect_MFA_code_then_sign_in_using_the_correct_one() { | ||
| // Step 1: Sign up a new user with an existing email address | ||
| signUpNewUser(email) | ||
|
|
||
| // Step 2: Attempt to sign in with the newly created user | ||
| latch = CountDownLatch(1) | ||
| var signInResult = synchronousAuth.signIn(userName, password) | ||
|
|
||
| // Validation 1: Validate that the next step is to confirm the emailed MFA code | ||
| assertEquals(AuthSignInStep.CONFIRM_SIGN_IN_WITH_OTP, signInResult.nextStep.signInStep) | ||
|
|
||
| // Wait until the MFA code has been received | ||
| latch?.await(20, TimeUnit.SECONDS) | ||
|
|
||
| // Step 4: Input the an incorrect MFA code | ||
| // Validation 2: Validate that an incorrect MFA code throws a CodeMismatchException | ||
| assertFailsWith<CodeMismatchException> { | ||
| signInResult = synchronousAuth.confirmSignIn(mfaCode.reversed()) | ||
| } | ||
|
|
||
| // Step 5: Input the correct MFA code for validation | ||
| signInResult = synchronousAuth.confirmSignIn(mfaCode) | ||
|
|
||
| // Validation 3: Validate that MFA setup is done | ||
| assertEquals(AuthSignInStep.DONE, signInResult.nextStep.signInStep) | ||
| } | ||
|
|
||
| private fun signUpNewUser(email: String? = null): AuthSignUpResult { | ||
| val attributes = if (email == null) { | ||
| emptyList() | ||
| } else { | ||
| listOf(AuthUserAttribute(AuthUserAttributeKey.email(), email)) | ||
| } | ||
| val options = AuthSignUpOptions.builder() | ||
| .userAttributes( | ||
| attributes | ||
| ).build() | ||
| return synchronousAuth.signUp(userName, password, options) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.