|
| 1 | +/* |
| 2 | + * Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | +package com.amplifyframework.auth.cognito |
| 16 | + |
| 17 | +import android.content.Context |
| 18 | +import androidx.test.core.app.ApplicationProvider |
| 19 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 20 | +import com.amplifyframework.auth.AuthUserAttribute |
| 21 | +import com.amplifyframework.auth.AuthUserAttributeKey |
| 22 | +import com.amplifyframework.auth.MFAType |
| 23 | +import com.amplifyframework.auth.cognito.test.R |
| 24 | +import com.amplifyframework.auth.options.AuthSignUpOptions |
| 25 | +import com.amplifyframework.auth.result.step.AuthSignInStep |
| 26 | +import com.amplifyframework.core.Amplify |
| 27 | +import com.amplifyframework.core.AmplifyConfiguration |
| 28 | +import com.amplifyframework.core.category.CategoryConfiguration |
| 29 | +import com.amplifyframework.core.category.CategoryType |
| 30 | +import com.amplifyframework.logging.AndroidLoggingPlugin |
| 31 | +import com.amplifyframework.logging.LogLevel |
| 32 | +import com.amplifyframework.testutils.sync.SynchronousAuth |
| 33 | +import dev.robinohs.totpkt.otp.totp.TotpGenerator |
| 34 | +import dev.robinohs.totpkt.otp.totp.timesupport.generateCode |
| 35 | +import java.util.Random |
| 36 | +import java.util.UUID |
| 37 | +import java.util.concurrent.CountDownLatch |
| 38 | +import java.util.concurrent.TimeUnit |
| 39 | +import org.junit.After |
| 40 | +import org.junit.Assert |
| 41 | +import org.junit.Before |
| 42 | +import org.junit.Test |
| 43 | +import org.junit.runner.RunWith |
| 44 | + |
| 45 | +@RunWith(AndroidJUnit4::class) |
| 46 | +class AWSCognitoAuthPluginTOTPTests { |
| 47 | + |
| 48 | + private lateinit var authPlugin: AWSCognitoAuthPlugin |
| 49 | + private lateinit var synchronousAuth: SynchronousAuth |
| 50 | + private val password = UUID.randomUUID().toString() |
| 51 | + private val userName = "testUser${Random().nextInt()}" |
| 52 | + private val email = "$userName@testdomain.com" |
| 53 | + |
| 54 | + @Before |
| 55 | + fun setup() { |
| 56 | + val context = ApplicationProvider.getApplicationContext<Context>() |
| 57 | + Amplify.addPlugin(AndroidLoggingPlugin(LogLevel.VERBOSE)) |
| 58 | + val config = AmplifyConfiguration.fromConfigFile(context, R.raw.amplifyconfiguration_totp) |
| 59 | + val authConfig: CategoryConfiguration = config.forCategoryType(CategoryType.AUTH) |
| 60 | + val authConfigJson = authConfig.getPluginConfig("awsCognitoAuthPlugin") |
| 61 | + authPlugin = AWSCognitoAuthPlugin() |
| 62 | + authPlugin.configure(authConfigJson, context) |
| 63 | + synchronousAuth = SynchronousAuth.delegatingTo(authPlugin) |
| 64 | + signUpNewUser(userName, password, email) |
| 65 | + synchronousAuth.signOut() |
| 66 | + } |
| 67 | + |
| 68 | + @After |
| 69 | + fun tearDown() { |
| 70 | + synchronousAuth.deleteUser() |
| 71 | + } |
| 72 | + |
| 73 | + /* |
| 74 | + * This test signs up a new user and goes thru successful MFA Setup process. |
| 75 | + * */ |
| 76 | + @Test |
| 77 | + fun mfa_setup() { |
| 78 | + val result = synchronousAuth.signIn(userName, password) |
| 79 | + Assert.assertEquals(AuthSignInStep.CONTINUE_SIGN_IN_WITH_TOTP_SETUP, result.nextStep.signInStep) |
| 80 | + val otp = TotpGenerator().generateCode( |
| 81 | + result.nextStep.totpSetupDetails!!.sharedSecret.toByteArray(), |
| 82 | + System.currentTimeMillis() |
| 83 | + ) |
| 84 | + synchronousAuth.confirmSignIn(otp) |
| 85 | + val currentUser = synchronousAuth.currentUser |
| 86 | + Assert.assertEquals(userName.lowercase(), currentUser.username) |
| 87 | + } |
| 88 | + |
| 89 | + /* |
| 90 | + * This test signs up a new user, enter incorrect MFA code during verification and |
| 91 | + * then enter correct OTP code to successfully set TOTP MFA. |
| 92 | + * */ |
| 93 | + @Test |
| 94 | + fun mfasetup_with_incorrect_otp() { |
| 95 | + val result = synchronousAuth.signIn(userName, password) |
| 96 | + Assert.assertEquals(AuthSignInStep.CONTINUE_SIGN_IN_WITH_TOTP_SETUP, result.nextStep.signInStep) |
| 97 | + try { |
| 98 | + synchronousAuth.confirmSignIn("123456") |
| 99 | + } catch (e: Exception) { |
| 100 | + Assert.assertEquals("Code mismatch", e.cause?.message) |
| 101 | + val otp = TotpGenerator().generateCode( |
| 102 | + result.nextStep.totpSetupDetails!!.sharedSecret.toByteArray(), |
| 103 | + System.currentTimeMillis() |
| 104 | + ) |
| 105 | + synchronousAuth.confirmSignIn(otp) |
| 106 | + val currentUser = synchronousAuth.currentUser |
| 107 | + Assert.assertEquals(userName.lowercase(), currentUser.username) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /* |
| 112 | + * This test signs up a new user, successfully setup MFA, sign-out and then goes thru sign-in with TOTP. |
| 113 | + * */ |
| 114 | + @Test |
| 115 | + fun signIn_with_totp_after_mfa_setup() { |
| 116 | + val result = synchronousAuth.signIn(userName, password) |
| 117 | + Assert.assertEquals(AuthSignInStep.CONTINUE_SIGN_IN_WITH_TOTP_SETUP, result.nextStep.signInStep) |
| 118 | + val otp = TotpGenerator().generateCode( |
| 119 | + result.nextStep.totpSetupDetails!!.sharedSecret.toByteArray() |
| 120 | + ) |
| 121 | + synchronousAuth.confirmSignIn(otp) |
| 122 | + synchronousAuth.signOut() |
| 123 | + |
| 124 | + val signInResult = synchronousAuth.signIn(userName, password) |
| 125 | + Assert.assertEquals(AuthSignInStep.CONFIRM_SIGN_IN_WITH_TOTP_CODE, signInResult.nextStep.signInStep) |
| 126 | + val otpCode = TotpGenerator().generateCode( |
| 127 | + result.nextStep.totpSetupDetails!!.sharedSecret.toByteArray(), |
| 128 | + System.currentTimeMillis() + 30 * 1000 // 30 sec is added to generate new OTP code |
| 129 | + ) |
| 130 | + synchronousAuth.confirmSignIn(otpCode) |
| 131 | + val currentUser = synchronousAuth.currentUser |
| 132 | + Assert.assertEquals(userName.lowercase(), currentUser.username) |
| 133 | + } |
| 134 | + |
| 135 | + /* |
| 136 | + * This test signs up a new user, successfully setup MFA, update user attribute to add phone number, |
| 137 | + * sign-out the user, goes thru MFA selection flow during sign-in, select TOTP MFA type, |
| 138 | + * successfully sign-in using TOTP |
| 139 | + * */ |
| 140 | + @Test |
| 141 | + fun select_mfa_type() { |
| 142 | + val result = synchronousAuth.signIn(userName, password) |
| 143 | + Assert.assertEquals(AuthSignInStep.CONTINUE_SIGN_IN_WITH_TOTP_SETUP, result.nextStep.signInStep) |
| 144 | + val otp = TotpGenerator().generateCode( |
| 145 | + result.nextStep.totpSetupDetails!!.sharedSecret.toByteArray() |
| 146 | + ) |
| 147 | + synchronousAuth.confirmSignIn(otp) |
| 148 | + synchronousAuth.updateUserAttribute(AuthUserAttribute(AuthUserAttributeKey.phoneNumber(), "+19876543210")) |
| 149 | + updateMFAPreference(MFAPreference.Enabled, MFAPreference.Enabled) |
| 150 | + synchronousAuth.signOut() |
| 151 | + val signInResult = synchronousAuth.signIn(userName, password) |
| 152 | + Assert.assertEquals(AuthSignInStep.CONTINUE_SIGN_IN_WITH_MFA_SELECTION, signInResult.nextStep.signInStep) |
| 153 | + val totpSignInResult = synchronousAuth.confirmSignIn(MFAType.TOTP.value) |
| 154 | + Assert.assertEquals(AuthSignInStep.CONFIRM_SIGN_IN_WITH_TOTP_CODE, totpSignInResult.nextStep.signInStep) |
| 155 | + val otpCode = TotpGenerator().generateCode( |
| 156 | + result.nextStep.totpSetupDetails!!.sharedSecret.toByteArray(), |
| 157 | + System.currentTimeMillis() + 30 * 1000 // 30 sec is added to generate new OTP code |
| 158 | + ) |
| 159 | + synchronousAuth.confirmSignIn(otpCode) |
| 160 | + val currentUser = synchronousAuth.currentUser |
| 161 | + Assert.assertEquals(userName.lowercase(), currentUser.username) |
| 162 | + } |
| 163 | + |
| 164 | + private fun signUpNewUser(userName: String, password: String, email: String) { |
| 165 | + val options = AuthSignUpOptions.builder() |
| 166 | + .userAttributes( |
| 167 | + listOf( |
| 168 | + AuthUserAttribute(AuthUserAttributeKey.email(), email) |
| 169 | + ) |
| 170 | + ).build() |
| 171 | + synchronousAuth.signUp(userName, password, options) |
| 172 | + } |
| 173 | + |
| 174 | + private fun updateMFAPreference(sms: MFAPreference, totp: MFAPreference) { |
| 175 | + val latch = CountDownLatch(1) |
| 176 | + authPlugin.updateMFAPreference(sms, totp, { latch.countDown() }, { latch.countDown() }) |
| 177 | + latch.await(5, TimeUnit.SECONDS) |
| 178 | + } |
| 179 | +} |
0 commit comments