Skip to content

Commit 36d766f

Browse files
authored
Merge branch 'main' into sdhuka/fix-endpoint-test
2 parents 4ea193d + b075845 commit 36d766f

File tree

4 files changed

+150
-11
lines changed

4 files changed

+150
-11
lines changed

aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/RealAWSCognitoAuthPlugin.kt

+47-5
Original file line numberDiff line numberDiff line change
@@ -464,19 +464,37 @@ internal class RealAWSCognitoAuthPlugin(
464464
onError: Consumer<AuthException>
465465
) {
466466
authStateMachine.getCurrentState { authState ->
467+
val signInOptions = options as? AWSCognitoAuthSignInOptions ?: AWSCognitoAuthSignInOptions.builder()
468+
.authFlowType(configuration.authFlowType)
469+
.build()
467470
when (authState.authNState) {
468471
is AuthenticationState.NotConfigured -> onError.accept(
469472
InvalidUserPoolConfigurationException()
470473
)
471474
// Continue sign in
472-
is AuthenticationState.SignedOut, is AuthenticationState.Configured -> {
473-
val signInOptions = options as? AWSCognitoAuthSignInOptions ?: AWSCognitoAuthSignInOptions.builder()
474-
.authFlowType(configuration.authFlowType)
475-
.build()
476-
475+
is AuthenticationState.SignedOut,
476+
is AuthenticationState.Configured -> {
477477
_signIn(username, password, signInOptions, onSuccess, onError)
478478
}
479479
is AuthenticationState.SignedIn -> onError.accept(SignedInException())
480+
is AuthenticationState.SigningIn -> {
481+
val token = StateChangeListenerToken()
482+
authStateMachine.listen(
483+
token,
484+
{ authState ->
485+
when (authState.authNState) {
486+
is AuthenticationState.SignedOut -> {
487+
authStateMachine.cancel(token)
488+
_signIn(username, password, signInOptions, onSuccess, onError)
489+
}
490+
else -> Unit
491+
}
492+
},
493+
{
494+
authStateMachine.send(AuthenticationEvent(AuthenticationEvent.EventType.CancelSignIn()))
495+
}
496+
)
497+
}
480498
else -> onError.accept(InvalidStateException())
481499
}
482500
}
@@ -706,6 +724,30 @@ internal class RealAWSCognitoAuthPlugin(
706724
)
707725
}
708726
is AuthenticationState.SignedIn -> onError.accept(SignedInException())
727+
is AuthenticationState.SigningIn -> {
728+
val token = StateChangeListenerToken()
729+
authStateMachine.listen(
730+
token,
731+
{ authState ->
732+
when (authState.authNState) {
733+
is AuthenticationState.SignedOut -> {
734+
authStateMachine.cancel(token)
735+
_signInWithHostedUI(
736+
callingActivity = callingActivity,
737+
options = options,
738+
onSuccess = onSuccess,
739+
onError = onError,
740+
provider = provider
741+
)
742+
}
743+
else -> Unit
744+
}
745+
},
746+
{
747+
authStateMachine.send(AuthenticationEvent(AuthenticationEvent.EventType.CancelSignIn()))
748+
}
749+
)
750+
}
709751
else -> onError.accept(InvalidStateException())
710752
}
711753
}

aws-auth-cognito/src/test/java/com/amplifyframework/auth/cognito/RealAWSCognitoAuthPluginTest.kt

+4-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package com.amplifyframework.auth.cognito
1818
import aws.sdk.kotlin.services.cognitoidentityprovider.CognitoIdentityProviderClient
1919
import aws.sdk.kotlin.services.cognitoidentityprovider.model.AnalyticsMetadataType
2020
import aws.sdk.kotlin.services.cognitoidentityprovider.model.AttributeType
21-
import aws.sdk.kotlin.services.cognitoidentityprovider.model.ChangePasswordRequest
2221
import aws.sdk.kotlin.services.cognitoidentityprovider.model.ChangePasswordResponse
2322
import aws.sdk.kotlin.services.cognitoidentityprovider.model.CodeDeliveryDetailsType
2423
import aws.sdk.kotlin.services.cognitoidentityprovider.model.CognitoIdentityProviderException
@@ -44,7 +43,6 @@ import com.amplifyframework.auth.AuthException
4443
import com.amplifyframework.auth.AuthUserAttribute
4544
import com.amplifyframework.auth.AuthUserAttributeKey
4645
import com.amplifyframework.auth.cognito.exceptions.configuration.InvalidUserPoolConfigurationException
47-
import com.amplifyframework.auth.cognito.exceptions.invalidstate.SignedInException
4846
import com.amplifyframework.auth.cognito.helpers.AuthHelper
4947
import com.amplifyframework.auth.cognito.helpers.SRPHelper
5048
import com.amplifyframework.auth.cognito.options.AWSCognitoAuthResendUserAttributeConfirmationCodeOptions
@@ -223,6 +221,7 @@ class RealAWSCognitoAuthPluginTest {
223221
val expectedAuthError = InvalidUserPoolConfigurationException()
224222
currentState = AuthenticationState.NotConfigured()
225223

224+
coEvery { authConfiguration.authFlowType } returns AuthFlowType.USER_SRP_AUTH
226225
// WHEN
227226
plugin.signIn("user", "password", AuthSignInOptions.defaults(), onSuccess, onError)
228227

@@ -236,7 +235,7 @@ class RealAWSCognitoAuthPluginTest {
236235
// GIVEN
237236
val onSuccess = mockk<Consumer<AuthSignInResult>>()
238237
val onError = mockk<Consumer<AuthException>>(relaxed = true)
239-
val expectedAuthError = SignedInException()
238+
coEvery { authConfiguration.authFlowType } returns AuthFlowType.USER_SRP_AUTH
240239
currentState = AuthenticationState.SignedIn(
241240
SignedInData(
242241
"userId",
@@ -253,7 +252,7 @@ class RealAWSCognitoAuthPluginTest {
253252

254253
// THEN
255254
verify(exactly = 0) { onSuccess.accept(any()) }
256-
verify { onError.accept(expectedAuthError) }
255+
verify { onError.accept(any()) }
257256
}
258257

259258
@Test
@@ -290,7 +289,7 @@ class RealAWSCognitoAuthPluginTest {
290289
}
291290

292291
coEvery {
293-
authService.cognitoIdentityProviderClient?.changePassword(any<ChangePasswordRequest>())
292+
authService.cognitoIdentityProviderClient?.changePassword(any())
294293
} returns ChangePasswordResponse.invoke { }
295294

296295
// WHEN

aws-auth-cognito/src/test/java/com/amplifyframework/auth/cognito/featuretest/generators/testcasegenerators/SignInTestCaseGenerator.kt

+30-1
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,30 @@ object SignInTestCaseGenerator : SerializableProvider {
271271
)
272272
)
273273

274+
private val signInWhenAlreadySigningInAuthCase = FeatureTestCase(
275+
description = "Test that overriding signIn when already signing in returns success",
276+
preConditions = PreConditions(
277+
"authconfiguration.json",
278+
"SigningIn_SigningIn.json",
279+
mockedResponses = listOf(
280+
mockedInitiateAuthResponse,
281+
mockedSMSChallengeResponse,
282+
)
283+
),
284+
api = API(
285+
AuthAPI.signIn,
286+
params = mapOf(
287+
"username" to username,
288+
"password" to password
289+
).toJsonElement(),
290+
options = JsonObject(emptyMap())
291+
),
292+
validations = listOf(
293+
mockedSignInSMSChallengeExpectation,
294+
ExpectationShapes.State("SigningIn_SigningIn.json")
295+
)
296+
)
297+
274298
private val customAuthCase = FeatureTestCase(
275299
description = "Test that Custom Auth signIn invokes proper cognito request and returns custom challenge",
276300
preConditions = PreConditions(
@@ -325,6 +349,11 @@ object SignInTestCaseGenerator : SerializableProvider {
325349
)
326350

327351
override val serializables: List<Any> = listOf(
328-
baseCase, challengeCase, deviceSRPTestCase, customAuthCase, customAuthWithSRPCase
352+
baseCase,
353+
challengeCase,
354+
deviceSRPTestCase,
355+
customAuthCase,
356+
customAuthWithSRPCase,
357+
signInWhenAlreadySigningInAuthCase
329358
)
330359
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"description": "Test that overriding signIn when already signing in returns success",
3+
"preConditions": {
4+
"amplify-configuration": "authconfiguration.json",
5+
"state": "SigningIn_SigningIn.json",
6+
"mockedResponses": [
7+
{
8+
"type": "cognitoIdentityProvider",
9+
"apiName": "initiateAuth",
10+
"responseType": "success",
11+
"response": {
12+
"challengeName": "PASSWORD_VERIFIER",
13+
"challengeParameters": {
14+
"SALT": "abc",
15+
"SECRET_BLOCK": "secretBlock",
16+
"SRP_B": "def",
17+
"USERNAME": "username",
18+
"USER_ID_FOR_SRP": "userId"
19+
}
20+
}
21+
},
22+
{
23+
"type": "cognitoIdentityProvider",
24+
"apiName": "respondToAuthChallenge",
25+
"responseType": "success",
26+
"response": {
27+
"session": "someSession",
28+
"challengeName": "SMS_MFA",
29+
"challengeParameters": {
30+
"CODE_DELIVERY_DELIVERY_MEDIUM": "SMS",
31+
"CODE_DELIVERY_DESTINATION": "+12345678900"
32+
}
33+
}
34+
}
35+
]
36+
},
37+
"api": {
38+
"name": "signIn",
39+
"params": {
40+
"username": "username",
41+
"password": "password"
42+
},
43+
"options": {
44+
}
45+
},
46+
"validations": [
47+
{
48+
"type": "amplify",
49+
"apiName": "signIn",
50+
"responseType": "success",
51+
"response": {
52+
"isSignedIn": false,
53+
"nextStep": {
54+
"signInStep": "CONFIRM_SIGN_IN_WITH_SMS_MFA_CODE",
55+
"additionalInfo": {
56+
},
57+
"codeDeliveryDetails": {
58+
"destination": "+12345678900",
59+
"deliveryMedium": "SMS"
60+
}
61+
}
62+
}
63+
},
64+
{
65+
"type": "state",
66+
"expectedState": "SigningIn_SigningIn.json"
67+
}
68+
]
69+
}

0 commit comments

Comments
 (0)