Skip to content

Commit 54bc9b1

Browse files
authored
fix(aws-android-sdk-auth-userpools): Check actual password requirements in drop-in UI (#3588)
* Check actual password requirements in drop-in UI * Also read password length when changing password.
1 parent ff6e88c commit 54bc9b1

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

aws-android-sdk-auth-userpools/src/main/java/com/amazonaws/mobile/auth/userpools/CognitoUserPoolsSignInProvider.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import com.amazonaws.mobile.auth.core.internal.util.ViewHelper;
5353

5454
import org.json.JSONException;
55+
import org.json.JSONObject;
5556

5657
import java.util.HashSet;
5758
import java.util.Set;
@@ -62,6 +63,9 @@
6263
import static com.amazonaws.mobile.auth.userpools.CognitoUserPoolsSignInProvider.AttributeKeys.USERNAME;
6364
import static com.amazonaws.mobile.auth.userpools.CognitoUserPoolsSignInProvider.AttributeKeys.VERIFICATION_CODE;
6465

66+
import androidx.annotation.NonNull;
67+
import androidx.annotation.Nullable;
68+
6569
/**
6670
* Manages sign-in using Cognito User Pools.
6771
*/
@@ -408,10 +412,14 @@ public void handleActivityResult(final int requestCode,
408412
password = data.getStringExtra(PASSWORD);
409413
verificationCode = data.getStringExtra(VERIFICATION_CODE);
410414

411-
if (password.length() < PASSWORD_MIN_LENGTH) {
415+
Integer minimumPasswordLength = getMinimumPasswordLength(awsConfiguration);
416+
if (minimumPasswordLength != null && password.length() < minimumPasswordLength) {
412417
ViewHelper.showDialog(activity, activity.getString(R.string.title_activity_forgot_password),
413-
activity.getString(R.string.password_change_failed)
414-
+ " " + activity.getString(R.string.password_length_validation_failed));
418+
activity.getString(R.string.password_change_failed)
419+
+ " " + activity.getString(
420+
R.string.password_length_validation_failed_variable,
421+
minimumPasswordLength
422+
));
415423
return;
416424
}
417425

@@ -444,7 +452,7 @@ public void handleActivityResult(final int requestCode,
444452

445453
if (verificationCode.length() < 1) {
446454
ViewHelper.showDialog(activity, activity.getString(R.string.title_activity_mfa),
447-
activity.getString(R.string.mfa_failed)
455+
activity.getString(R.string.mfa_failed)
448456
+ " " + activity.getString(R.string.mfa_code_empty));
449457
return;
450458
}
@@ -469,7 +477,7 @@ public void handleActivityResult(final int requestCode,
469477

470478
if (verificationCode.length() < 1) {
471479
ViewHelper.showDialog(activity, activity.getString(R.string.title_activity_sign_up_confirm),
472-
activity.getString(R.string.sign_up_confirm_title)
480+
activity.getString(R.string.sign_up_confirm_title)
473481
+ " " + activity.getString(R.string.sign_up_confirm_code_missing));
474482
return;
475483
}
@@ -697,4 +705,13 @@ static int getBackgroundColor() {
697705
static String getFontFamily() {
698706
return fontFamily;
699707
}
708+
709+
@Nullable
710+
static Integer getMinimumPasswordLength(@NonNull final AWSConfiguration configuration) {
711+
JSONObject auth = configuration.optJsonObject("Auth");
712+
if (auth == null) return null;
713+
JSONObject passwordSettings = auth.optJSONObject("passwordProtectionSettings");
714+
if (passwordSettings == null) return null;
715+
return passwordSettings.optInt("passwordPolicyMinLength");
716+
}
700717
}

aws-android-sdk-auth-userpools/src/main/java/com/amazonaws/mobile/auth/userpools/SignUpActivity.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
import static com.amazonaws.mobile.auth.userpools.CognitoUserPoolsSignInProvider.AttributeKeys.*;
3939
import static com.amazonaws.mobile.auth.userpools.CognitoUserPoolsSignInProvider.getErrorMessageFromException;
4040

41+
import androidx.annotation.Nullable;
42+
43+
import org.json.JSONObject;
44+
4145
/**
4246
* Activity to prompt for account sign up information.
4347
*/
@@ -47,6 +51,7 @@ public class SignUpActivity extends Activity {
4751

4852
private SignUpView signUpView;
4953
private CognitoUserPool mUserPool;
54+
private AWSConfiguration configuration;
5055

5156
/**
5257
* Starts a {@link SignUpActivity}
@@ -67,7 +72,8 @@ protected void onCreate(final Bundle savedInstanceState) {
6772
signUpView = (SignUpView) findViewById(R.id.signup_view);
6873

6974
Context appContext = getApplicationContext();
70-
mUserPool = new CognitoUserPool(appContext, new AWSConfiguration(appContext));
75+
configuration = new AWSConfiguration(appContext);
76+
mUserPool = new CognitoUserPool(appContext, configuration);
7177

7278
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
7379
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
@@ -94,13 +100,16 @@ public void signUp(final View view) {
94100
Log.d(LOG_TAG, "email = " + email);
95101
Log.d(LOG_TAG, "phone = " + phone);
96102

103+
104+
final Integer minimumPasswordLength = CognitoUserPoolsSignInProvider.getMinimumPasswordLength(configuration);
105+
97106
if (username.isEmpty()) {
98107
showError(getString(R.string.sign_up_username_missing));
99108
return;
100109
}
101110

102-
if (password.length() < 6) {
103-
showError(getString(R.string.password_length_validation_failed));
111+
if (minimumPasswordLength != null && password.length() < minimumPasswordLength) {
112+
showError(getString(R.string.password_length_validation_failed_variable, minimumPasswordLength));
104113
return;
105114
}
106115

aws-android-sdk-auth-userpools/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<string name="mfa_code_empty">MFA Code is empty.</string>
4444
<string name="mfa_failed">MFA Failed.</string>
4545
<string name="password_length_validation_failed">Password should have 6 or more characters.</string>
46+
<string name="password_length_validation_failed_variable">Password should have %d or more characters.</string>
4647
<string name="sign_up_username_missing">Missing username.</string>
4748
<string name="sign_up_confirm_code_missing">Sign Up Confirmation code is missing.</string>
4849
<string name="sign_up_in_progress">Sign up in progress</string>

0 commit comments

Comments
 (0)