Skip to content

Commit 2b4947e

Browse files
sdhukaDhuka
andauthored
feat: Update SignIn API to accept authFlowType (#2846)
Co-authored-by: Dhuka <[email protected]>
1 parent 5d00416 commit 2b4947e

File tree

2 files changed

+61
-17
lines changed

2 files changed

+61
-17
lines changed

aws-android-sdk-mobile-client/src/main/java/com/amazonaws/mobile/client/AWSMobileClient.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
import com.amazonaws.services.cognitoidentity.model.NotAuthorizedException;
110110
import com.amazonaws.services.cognitoidentityprovider.AmazonCognitoIdentityProvider;
111111
import com.amazonaws.services.cognitoidentityprovider.AmazonCognitoIdentityProviderClient;
112+
import com.amazonaws.services.cognitoidentityprovider.model.AuthFlowType;
112113
import com.amazonaws.services.cognitoidentityprovider.model.GlobalSignOutRequest;
113114
import com.amazonaws.services.cognitoidentityprovider.model.InvalidUserPoolConfigurationException;
114115
import com.amazonaws.util.StringUtils;
@@ -1192,7 +1193,19 @@ public void signIn(final String username,
11921193
final Callback<SignInResult> callback) {
11931194

11941195
final InternalCallback<SignInResult> internalCallback = new InternalCallback<SignInResult>(callback);
1195-
internalCallback.async(_signIn(username, password, validationData, clientMetadata, internalCallback));
1196+
internalCallback.async(_signIn(username, password, validationData, clientMetadata, null, internalCallback));
1197+
}
1198+
1199+
@AnyThread
1200+
public void signIn(final String username,
1201+
final String password,
1202+
final Map<String, String> validationData,
1203+
final Map<String, String> clientMetadata,
1204+
final AuthFlowType authFlowType,
1205+
final Callback<SignInResult> callback) {
1206+
1207+
final InternalCallback<SignInResult> internalCallback = new InternalCallback<SignInResult>(callback);
1208+
internalCallback.async(_signIn(username, password, validationData, clientMetadata, authFlowType, internalCallback));
11961209
}
11971210

11981211
@WorkerThread
@@ -1209,13 +1222,25 @@ public SignInResult signIn(final String username,
12091222
final Map<String, String> clientMetadata) throws Exception {
12101223

12111224
final InternalCallback<SignInResult> internalCallback = new InternalCallback<SignInResult>();
1212-
return internalCallback.await(_signIn(username, password, validationData, clientMetadata, internalCallback));
1225+
return internalCallback.await(_signIn(username, password, validationData, clientMetadata, null, internalCallback));
1226+
}
1227+
1228+
@WorkerThread
1229+
public SignInResult signIn(final String username,
1230+
final String password,
1231+
final Map<String, String> validationData,
1232+
final Map<String, String> clientMetadata,
1233+
final AuthFlowType authFlowType) throws Exception {
1234+
1235+
final InternalCallback<SignInResult> internalCallback = new InternalCallback<SignInResult>();
1236+
return internalCallback.await(_signIn(username, password, validationData, clientMetadata, authFlowType, internalCallback));
12131237
}
12141238

12151239
private Runnable _signIn(final String username,
12161240
final String password,
12171241
final Map<String, String> validationData,
12181242
final Map<String, String> clientMetadata,
1243+
final AuthFlowType authFlowType,
12191244
final Callback<SignInResult> callback) {
12201245

12211246
this.signInCallback = callback;
@@ -1264,18 +1289,19 @@ public void getAuthenticationDetails(AuthenticationContinuation authenticationCo
12641289
awsConfiguration.optJsonObject(AUTH_KEY).has("authenticationFlowType");
12651290

12661291
try {
1267-
String authFlowType = authFlowTypeInConfig ?
1268-
awsConfiguration.optJsonObject(AUTH_KEY).getString("authenticationFlowType") :
1269-
null;
1270-
if (authFlowTypeInConfig && AUTH_TYPE_INIT_CUSTOM_AUTH.equals(authFlowType)) {
1292+
String resolvedAuthFlowType = authFlowType != null ? authFlowType.name() : null;
1293+
if (resolvedAuthFlowType == null && authFlowTypeInConfig) {
1294+
resolvedAuthFlowType = awsConfiguration.optJsonObject(AUTH_KEY).getString("authenticationFlowType");
1295+
}
1296+
if (resolvedAuthFlowType != null && AUTH_TYPE_INIT_CUSTOM_AUTH.equals(resolvedAuthFlowType)) {
12711297
// If there's a value in the config and it's CUSTOM_AUTH, we'll
12721298
// use one of the below constructors depending on what's passed in.
12731299
if (password != null) {
12741300
authenticationContinuation.setAuthenticationDetails(new AuthenticationDetails(username, password, authParameters, validationData));
12751301
} else {
12761302
authenticationContinuation.setAuthenticationDetails(new AuthenticationDetails(username, authParameters, validationData));
12771303
}
1278-
} else if (authFlowTypeInConfig && AUTH_TYPE_INIT_USER_PASSWORD.equals(authFlowType)) {
1304+
} else if (resolvedAuthFlowType != null && AUTH_TYPE_INIT_USER_PASSWORD.equals(resolvedAuthFlowType)) {
12791305
// If there's a value in the config and it's USER_PASSWORD_AUTH, set the auth type (challenge name)
12801306
// to be USER_PASSWORD.
12811307
AuthenticationDetails authenticationDetails = new AuthenticationDetails(username, password, validationData);
@@ -1703,7 +1729,7 @@ public void deleteUser(final Callback<Void> callback) {
17031729
final InternalCallback<Void> internalCallback = new InternalCallback<>(callback);
17041730
internalCallback.async(_deleteUser(internalCallback));
17051731
}
1706-
1732+
17071733
private Runnable _deleteUser(final Callback<Void> callback) {
17081734
return () -> {
17091735
if (userpool == null) {

aws-android-sdk-mobile-client/src/test/java/com/amazonaws/mobile/client/AWSMobileClientAuthFlowSettingTest.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.amazonaws.mobileconnectors.cognitoidentityprovider.continuations.AuthenticationDetails;
2828
import com.amazonaws.mobileconnectors.cognitoidentityprovider.handlers.AuthenticationHandler;
2929

30+
import com.amazonaws.services.cognitoidentityprovider.model.AuthFlowType;
3031
import org.json.JSONException;
3132
import org.json.JSONObject;
3233
import org.junit.Before;
@@ -82,54 +83,71 @@ public void beforeEachTest() {
8283
public void testUserPasswordAuth() throws JSONException, InterruptedException {
8384
String flowFromConfig = "USER_PASSWORD_AUTH";
8485
String expectedAuthType = "USER_PASSWORD";
85-
verifyScenario(flowFromConfig, expectedAuthType);
86+
verifyScenario(flowFromConfig, null, expectedAuthType);
8687
}
8788

8889
@Test
8990
public void testUserSrpAuth() throws JSONException, InterruptedException {
9091
String flowFromConfig = "USER_SRP_AUTH";
9192
String expectedAuthType = "PASSWORD_VERIFIER";
92-
verifyScenario(flowFromConfig, expectedAuthType);
93+
verifyScenario(flowFromConfig, null, expectedAuthType);
9394
}
9495

9596
@Test
9697
public void testCustomAuth() throws JSONException, InterruptedException {
9798
String flowFromConfig = "CUSTOM_AUTH";
9899
String expectedAuthType = "CUSTOM_CHALLENGE";
99-
verifyScenario(flowFromConfig, expectedAuthType);
100+
verifyScenario(flowFromConfig, null, expectedAuthType);
100101
}
101102

102103
@Test
103104
public void testNoAuthFlowInConfig() throws JSONException, InterruptedException {
104105
String flowFromConfig = null;
105106
String expectedAuthType = "PASSWORD_VERIFIER";
106-
verifyScenario(flowFromConfig, expectedAuthType);
107+
verifyScenario(flowFromConfig, null, expectedAuthType);
108+
}
109+
110+
@Test
111+
public void testSwitchAuthFlowInAPI() throws JSONException, InterruptedException {
112+
String flowFromConfig = "USER_SRP_AUTH";
113+
String expectedAuthType = "CUSTOM_CHALLENGE";
114+
verifyScenario(flowFromConfig, "CUSTOM_AUTH", expectedAuthType);
115+
}
116+
117+
@Test
118+
public void testSwitchFromNoAuthFlowInConfig() throws JSONException, InterruptedException {
119+
String flowFromConfig = null;
120+
String expectedAuthType = "USER_PASSWORD";
121+
verifyScenario(flowFromConfig, "USER_PASSWORD_AUTH", expectedAuthType);
107122
}
108123

109124
/**
110125
* Verify that the correct auth type (aka challenge name) is passed in based on the
111126
* auth flow type from the config file.
112-
* @param authFlowType The auth flow type from the config.
127+
* @param configAuthFlowType The auth flow type from the config.
113128
* @param expectedAuthType The auth type expected.
129+
* @param overridenAuthFlowType The authType passed to the API
114130
* @throws JSONException Not expected.
115131
* @throws InterruptedException Not expected.
116132
*/
117-
private void verifyScenario(String authFlowType, String expectedAuthType) throws JSONException, InterruptedException {
133+
private void verifyScenario(String configAuthFlowType, String overridenAuthFlowType, String expectedAuthType) throws JSONException, InterruptedException {
118134
AuthenticationContinuation mockContinuation = setupMockContinuation();
119135
CognitoUserPool mockUserPool = setupMockUserPool(mockContinuation);
120-
initMobileClientAndWait(authFlowType, mockUserPool);
121-
signinAndWait();
136+
initMobileClientAndWait(configAuthFlowType, mockUserPool);
137+
signinAndWait(overridenAuthFlowType);
122138
ArgumentCaptor<AuthenticationDetails> argumentCaptor = ArgumentCaptor.forClass(AuthenticationDetails.class);
123139
verify(mockContinuation).setAuthenticationDetails(argumentCaptor.capture());
124140
AuthenticationDetails actualAuthDetails = argumentCaptor.getValue();
125141
assertEquals(expectedAuthType, actualAuthDetails.getAuthenticationType());
126142
}
127143

128-
private void signinAndWait() throws InterruptedException {
144+
private void signinAndWait(String overridenAuthFlowType) throws InterruptedException {
145+
AuthFlowType overridenAuthFlowEnum = overridenAuthFlowType == null ? null : AuthFlowType.valueOf(overridenAuthFlowType);
129146
mobileClient.signIn("test",
130147
"fakepassword",
131148
Collections.emptyMap(),
132149
Collections.emptyMap(),
150+
overridenAuthFlowEnum,
133151
new Callback<SignInResult>() {
134152
@Override
135153
public void onResult(SignInResult result) {

0 commit comments

Comments
 (0)