mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(cognito): skip the primary recovery attribute as an MFA factor (#7191)
This commit is contained in:
3 files changed
+152
-2
No files matched your search
+27
-2
@@ -2357,20 +2357,45 @@ final class CognitoAuthFlowHandler {
|
||||
* users of such a pool choose only which one is preferred. An SMS factor needs a pool that offers SMS
|
||||
* MFA and the user's phone number; an email factor needs the pool's EmailMfaConfiguration and the
|
||||
* user's email address.
|
||||
* A pool that requires MFA does not assign either one when its attribute is the primary account-recovery
|
||||
* destination, as in AWS, unless that would leave the user with no factor and no software-token setup.
|
||||
* A factor the user turned on is never left out.
|
||||
*/
|
||||
private List<String> mfaFactors(UserPool pool, CognitoUser user) {
|
||||
List<String> factors = mfaFactors(pool, user, true);
|
||||
// Leaving the recovery attribute out must never leave a pool that requires MFA with no factor at
|
||||
// all: completePrimaryAuth would then issue tokens without a code. When software-token setup is not
|
||||
// available as the alternative, offer the factors the filter dropped.
|
||||
if (factors.isEmpty() && "ON".equals(pool.getMfaConfiguration())
|
||||
&& !Boolean.TRUE.equals(pool.getSoftwareTokenMfaEnabled())) {
|
||||
return mfaFactors(pool, user, false);
|
||||
}
|
||||
return factors;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param skipRecoveryAttribute leave out an email or phone factor that the pool would only assign because
|
||||
* MFA is required, when that attribute is the primary recovery destination.
|
||||
* A factor the user turned on themselves is always kept: AWS prompts for the
|
||||
* user's enabled, preferred factor before it works out an assignment.
|
||||
*/
|
||||
private List<String> mfaFactors(UserPool pool, CognitoUser user, boolean skipRecoveryAttribute) {
|
||||
boolean required = "ON".equals(pool.getMfaConfiguration());
|
||||
boolean messaging = service.verificationServicesConfigured();
|
||||
boolean smsEnabled = user.getSmsMfaSettings() != null && user.getSmsMfaSettings().isEnabled();
|
||||
boolean emailEnabled = user.getEmailMfaSettings() != null && user.getEmailMfaSettings().isEnabled();
|
||||
List<String> factors = new ArrayList<>();
|
||||
if (messaging && smsMfaOffered(pool) && user.getAttributes().get("phone_number") != null
|
||||
&& (required || (user.getSmsMfaSettings() != null && user.getSmsMfaSettings().isEnabled()))) {
|
||||
&& (smsEnabled || (required
|
||||
&& (!skipRecoveryAttribute || service.isNotPrimaryRecoveryAttribute(pool, "phone_number"))))) {
|
||||
factors.add("SMS_MFA");
|
||||
}
|
||||
if (required ? CognitoService.softwareTokenMfaActive(pool, user) : CognitoService.softwareTokenMfaEnabled(user)) {
|
||||
factors.add("SOFTWARE_TOKEN_MFA");
|
||||
}
|
||||
if (messaging && pool.getEmailMfaConfiguration() != null && user.getAttributes().get("email") != null
|
||||
&& (required || (user.getEmailMfaSettings() != null && user.getEmailMfaSettings().isEnabled()))) {
|
||||
&& (emailEnabled || (required
|
||||
&& (!skipRecoveryAttribute || service.isNotPrimaryRecoveryAttribute(pool, "email"))))) {
|
||||
factors.add("EMAIL_OTP");
|
||||
}
|
||||
return factors;
|
||||
|
||||
@@ -6593,6 +6593,23 @@ public class CognitoService implements ResourceProvider {
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether {@code attribute} ({@code email} or {@code phone_number}) is <em>not</em> the pool's primary
|
||||
* self-service account-recovery destination, i.e. not the highest-priority mechanism of its
|
||||
* AccountRecoverySetting. A pool with no AccountRecoverySetting has no primary destination here.
|
||||
* AWS does not assign the primary destination as an email or SMS MFA factor, since a recovery code sent
|
||||
* there would bypass the factor.
|
||||
*/
|
||||
boolean isNotPrimaryRecoveryAttribute(UserPool pool, String attribute) {
|
||||
List<String> mechanisms = accountRecoveryMechanisms(pool);
|
||||
if (mechanisms.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
String primary = mechanisms.get(0);
|
||||
return (!"email".equals(attribute) || !"verified_email".equals(primary))
|
||||
&& (!"phone_number".equals(attribute) || !"verified_phone_number".equals(primary));
|
||||
}
|
||||
|
||||
private List<String> accountRecoveryMechanisms(UserPool pool) {
|
||||
Map<String, Object> setting = pool.getAccountRecoverySetting();
|
||||
if (setting == null) {
|
||||
|
||||
+108
@@ -255,6 +255,102 @@ class CognitoMessageMfaIntegrationTest {
|
||||
.body("$", not(hasKey("SmsMfaConfiguration")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void requiredMfaSkipsTheEmailThatIsThePrimaryRecoveryDestination() throws Exception {
|
||||
String poolId = createPoolWithRecovery("ON", "verified_email", "verified_phone_number",
|
||||
EMAIL_CONFIG + ",\"SoftwareTokenMfaConfiguration\":{\"Enabled\":true}");
|
||||
String clientId = createClient(poolId);
|
||||
User user = createUser(poolId);
|
||||
|
||||
// Email is where password-recovery codes go, so it cannot also be the MFA factor: the user sets up TOTP.
|
||||
JsonNode challenge = passwordLogin(clientId, user);
|
||||
assertEquals("MFA_SETUP", challenge.path("ChallengeName").asText());
|
||||
assertEquals("[\"SOFTWARE_TOKEN_MFA\"]", challenge.path("ChallengeParameters").path("MFAS_CAN_SETUP").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void requiredMfaUsesEmailWhenPhoneIsThePrimaryRecoveryDestination() throws Exception {
|
||||
String poolId = createPoolWithRecovery("ON", "verified_phone_number", "verified_email",
|
||||
EMAIL_CONFIG + ",\"SoftwareTokenMfaConfiguration\":{\"Enabled\":true}");
|
||||
String clientId = createClient(poolId);
|
||||
User user = createUser(poolId);
|
||||
|
||||
JsonNode challenge = passwordLogin(clientId, user);
|
||||
assertEquals("EMAIL_OTP", challenge.path("ChallengeName").asText());
|
||||
String code = CognitoCodes.sixDigitCode(latestEmail(user.email()).path("Body").path("text_part").asText());
|
||||
JsonNode signedIn = cognitoJson("RespondToAuthChallenge", respond(clientId, "EMAIL_OTP", challenge, user,
|
||||
"EMAIL_OTP_CODE", code));
|
||||
assertFalse(signedIn.path("AuthenticationResult").path("AccessToken").asText().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void requiredMfaOffersOnlyTheFactorThatIsNotThePrimaryRecoveryDestination() throws Exception {
|
||||
String poolId = createPoolWithRecovery("ON", "verified_email", "verified_phone_number",
|
||||
SMS_CONFIG + "," + EMAIL_CONFIG);
|
||||
String clientId = createClient(poolId);
|
||||
User user = createUser(poolId);
|
||||
|
||||
// Both attributes are present and both factors are on, but email is the recovery destination: only SMS is left.
|
||||
JsonNode challenge = passwordLogin(clientId, user);
|
||||
assertEquals("SMS_MFA", challenge.path("ChallengeName").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void requiredMfaStillAsksForTheRecoveryEmailWhenNothingElseIsLeft() throws Exception {
|
||||
// No software-token MFA, so excluding the recovery email would leave no factor and issue tokens.
|
||||
String poolId = createPoolWithRecovery("ON", "verified_email", "verified_phone_number", EMAIL_CONFIG);
|
||||
String clientId = createClient(poolId);
|
||||
User user = createUser(poolId);
|
||||
|
||||
JsonNode challenge = passwordLogin(clientId, user);
|
||||
assertEquals("EMAIL_OTP", challenge.path("ChallengeName").asText());
|
||||
assertTrue(challenge.path("AuthenticationResult").isMissingNode(), "no tokens before the code");
|
||||
String code = CognitoCodes.sixDigitCode(latestEmail(user.email()).path("Body").path("text_part").asText());
|
||||
JsonNode signedIn = cognitoJson("RespondToAuthChallenge", respond(clientId, "EMAIL_OTP", challenge, user,
|
||||
"EMAIL_OTP_CODE", code));
|
||||
assertFalse(signedIn.path("AuthenticationResult").path("AccessToken").asText().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void requiredMfaStillAsksForTheRecoveryPhoneWhenNothingElseIsLeft() throws Exception {
|
||||
String poolId = createPoolWithRecovery("ON", "verified_phone_number", "verified_email", SMS_CONFIG);
|
||||
String clientId = createClient(poolId);
|
||||
User user = createUser(poolId);
|
||||
|
||||
JsonNode challenge = passwordLogin(clientId, user);
|
||||
assertEquals("SMS_MFA", challenge.path("ChallengeName").asText());
|
||||
assertTrue(challenge.path("AuthenticationResult").isMissingNode(), "no tokens before the code");
|
||||
}
|
||||
|
||||
@Test
|
||||
void optionalMfaStillAsksForAFactorTheUserTurnedOnEvenIfItIsTheRecoveryEmail() throws Exception {
|
||||
String poolId = createPoolWithRecovery("OPTIONAL", "verified_email", "verified_phone_number", EMAIL_CONFIG);
|
||||
String clientId = createClient(poolId);
|
||||
User user = createUser(poolId);
|
||||
assertTrue(passwordLogin(clientId, user).path("ChallengeName").isMissingNode(),
|
||||
"MFA is optional and the user has not turned it on");
|
||||
cognitoAction("AdminSetUserMFAPreference", """
|
||||
{"UserPoolId":"%s","Username":"%s","EmailMfaSettings":{"Enabled":true}}
|
||||
""".formatted(poolId, user.username())).then().statusCode(200);
|
||||
|
||||
JsonNode challenge = passwordLogin(clientId, user);
|
||||
assertEquals("EMAIL_OTP", challenge.path("ChallengeName").asText());
|
||||
assertTrue(challenge.path("AuthenticationResult").isMissingNode(), "no tokens before the code");
|
||||
}
|
||||
|
||||
@Test
|
||||
void requiredMfaHonoursARecoveryEmailTheUserTurnedOnOverSoftwareTokenSetup() throws Exception {
|
||||
String poolId = createPoolWithRecovery("ON", "verified_email", "verified_phone_number",
|
||||
EMAIL_CONFIG + ",\"SoftwareTokenMfaConfiguration\":{\"Enabled\":true}");
|
||||
String clientId = createClient(poolId);
|
||||
User user = createUser(poolId);
|
||||
cognitoAction("AdminSetUserMFAPreference", """
|
||||
{"UserPoolId":"%s","Username":"%s","EmailMfaSettings":{"Enabled":true,"PreferredMfa":true}}
|
||||
""".formatted(poolId, user.username())).then().statusCode(200);
|
||||
|
||||
assertEquals("EMAIL_OTP", passwordLogin(clientId, user).path("ChallengeName").asText());
|
||||
}
|
||||
|
||||
private record User(String username, String phone, String email) {}
|
||||
|
||||
private static User createUser(String poolId) throws Exception {
|
||||
@@ -281,6 +377,18 @@ class CognitoMessageMfaIntegrationTest {
|
||||
return poolId;
|
||||
}
|
||||
|
||||
private static String createPoolWithRecovery(String mfaConfiguration, String primary, String secondary,
|
||||
String factorConfiguration) throws Exception {
|
||||
String poolId = cognitoJson("CreateUserPool", """
|
||||
{"PoolName":"MessageMfaRecoveryPool","AccountRecoverySetting":{"RecoveryMechanisms":[
|
||||
{"Priority":1,"Name":"%s"},{"Priority":2,"Name":"%s"}]}}
|
||||
""".formatted(primary, secondary)).path("UserPool").path("Id").asText();
|
||||
cognitoJson("SetUserPoolMfaConfig", """
|
||||
{"UserPoolId":"%s","MfaConfiguration":"%s",%s}
|
||||
""".formatted(poolId, mfaConfiguration, factorConfiguration));
|
||||
return poolId;
|
||||
}
|
||||
|
||||
private static String createClient(String poolId) throws Exception {
|
||||
return cognitoJson("CreateUserPoolClient", """
|
||||
{"UserPoolId":"%s","ClientName":"message-mfa-client",
|
||||
|
||||
Reference in new issue
Block a user