feat(cognito): enforce app client ExplicitAuthFlows in InitiateAuth and AdminInitiateAuth (#3974)

ExplicitAuthFlows was stored but never checked, so a client that only allowed SRP still accepted USER_PASSWORD_AUTH. When a client lists flows, reject any flow it does not enable with InvalidParameterException. Legacy values (USER_PASSWORD_AUTH, ADMIN_NO_SRP_AUTH, CUSTOM_AUTH_FLOW_ONLY) are honored, and clients created without a list stay ungated.

The Java compatibility test signed in with ADMIN_USER_PASSWORD_AUTH on a client that never enabled it; add ALLOW_ADMIN_USER_PASSWORD_AUTH to that client.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-20 21:04:55 -05:00
1 parent e14221fe62
commit a12bf8c96c
4 files changed
+199 -11

No files matched your search

@@ -122,6 +122,7 @@ class CognitoFeaturesTest {
.clientName("compat-test-client")
.explicitAuthFlows(
ExplicitAuthFlowsType.ALLOW_USER_PASSWORD_AUTH,
ExplicitAuthFlowsType.ALLOW_ADMIN_USER_PASSWORD_AUTH,
ExplicitAuthFlowsType.ALLOW_REFRESH_TOKEN_AUTH));
clientId = resp.userPoolClient().clientId();
assertThat(clientId).isNotBlank();
+6
View File
@@ -210,6 +210,12 @@ challenge. It requires the user pool's tier to be Essentials or higher. `WEB_AUT
Any other `AuthFlow` value is rejected with `InvalidParameterException` and no tokens are issued.
When an app client sets `ExplicitAuthFlows`, only the flows it lists are accepted (`ALLOW_USER_PASSWORD_AUTH`,
`ALLOW_USER_SRP_AUTH`, `ALLOW_CUSTOM_AUTH`, `ALLOW_USER_AUTH`, `ALLOW_ADMIN_USER_PASSWORD_AUTH`,
`ALLOW_REFRESH_TOKEN_AUTH`, and the legacy `USER_PASSWORD_AUTH`, `ADMIN_NO_SRP_AUTH` and
`CUSTOM_AUTH_FLOW_ONLY`); any other flow fails with `InvalidParameterException`. A client created without `ExplicitAuthFlows` is not gated, unlike AWS, which defaults
such a client to SRP, custom and refresh auth.
## User Attribute Update Verification
`CreateUserPool`, `UpdateUserPool`, and `DescribeUserPool` support
@@ -95,11 +95,26 @@ final class CognitoAuthFlowHandler {
UserPool pool = service.describeUserPool(client.getUserPoolId());
return switch (authFlow) {
case "USER_PASSWORD_AUTH" -> authenticateWithPassword(pool, client, authParameters, clientMetadata);
case "REFRESH_TOKEN_AUTH", "REFRESH_TOKEN" -> handleRefreshToken(pool, client, authParameters, clientMetadata);
case "USER_SRP_AUTH" -> handleUserSrpAuth(pool, client, authParameters, clientMetadata);
case "CUSTOM_AUTH" -> handleCustomAuth(pool, client, authParameters, clientMetadata);
case "USER_AUTH" -> handleUserAuth(pool, client, authParameters, clientMetadata);
case "USER_PASSWORD_AUTH" -> {
requireFlowEnabled(client, authFlow, "ALLOW_USER_PASSWORD_AUTH", "USER_PASSWORD_AUTH");
yield authenticateWithPassword(pool, client, authParameters, clientMetadata);
}
case "REFRESH_TOKEN_AUTH", "REFRESH_TOKEN" -> {
requireRefreshEnabled(client, authFlow);
yield handleRefreshToken(pool, client, authParameters, clientMetadata);
}
case "USER_SRP_AUTH" -> {
requireFlowEnabled(client, authFlow, "ALLOW_USER_SRP_AUTH");
yield handleUserSrpAuth(pool, client, authParameters, clientMetadata);
}
case "CUSTOM_AUTH" -> {
requireFlowEnabled(client, authFlow, "ALLOW_CUSTOM_AUTH", "CUSTOM_AUTH_FLOW_ONLY");
yield handleCustomAuth(pool, client, authParameters, clientMetadata);
}
case "USER_AUTH" -> {
requireFlowEnabled(client, authFlow, "ALLOW_USER_AUTH");
yield handleUserAuth(pool, client, authParameters, clientMetadata);
}
default -> throw unsupportedAuthFlow(authFlow);
};
}
@@ -123,16 +138,69 @@ final class CognitoAuthFlowHandler {
}
return switch (authFlow) {
case "ADMIN_USER_PASSWORD_AUTH", "ADMIN_NO_SRP_AUTH", "USER_PASSWORD_AUTH" ->
authenticateWithPassword(pool, client, authParameters, clientMetadata);
case "REFRESH_TOKEN_AUTH", "REFRESH_TOKEN" -> handleRefreshToken(pool, client, authParameters, clientMetadata);
case "ADMIN_USER_SRP_AUTH", "USER_SRP_AUTH" -> handleUserSrpAuth(pool, client, authParameters, clientMetadata);
case "CUSTOM_AUTH" -> handleCustomAuth(pool, client, authParameters, clientMetadata);
case "USER_AUTH" -> handleUserAuth(pool, client, authParameters, clientMetadata);
case "ADMIN_USER_PASSWORD_AUTH" -> {
requireFlowEnabled(client, authFlow, "ALLOW_ADMIN_USER_PASSWORD_AUTH", "ADMIN_NO_SRP_AUTH");
yield authenticateWithPassword(pool, client, authParameters, clientMetadata);
}
case "ADMIN_NO_SRP_AUTH" -> {
requireFlowEnabled(client, authFlow, "ALLOW_ADMIN_USER_PASSWORD_AUTH", "ADMIN_NO_SRP_AUTH");
yield authenticateWithPassword(pool, client, authParameters, clientMetadata);
}
case "USER_PASSWORD_AUTH" -> {
requireFlowEnabled(client, authFlow, "ALLOW_USER_PASSWORD_AUTH", "USER_PASSWORD_AUTH",
"ALLOW_ADMIN_USER_PASSWORD_AUTH", "ADMIN_NO_SRP_AUTH");
yield authenticateWithPassword(pool, client, authParameters, clientMetadata);
}
case "REFRESH_TOKEN_AUTH", "REFRESH_TOKEN" -> {
requireRefreshEnabled(client, authFlow);
yield handleRefreshToken(pool, client, authParameters, clientMetadata);
}
case "ADMIN_USER_SRP_AUTH", "USER_SRP_AUTH" -> {
requireFlowEnabled(client, authFlow, "ALLOW_USER_SRP_AUTH");
yield handleUserSrpAuth(pool, client, authParameters, clientMetadata);
}
case "CUSTOM_AUTH" -> {
requireFlowEnabled(client, authFlow, "ALLOW_CUSTOM_AUTH", "CUSTOM_AUTH_FLOW_ONLY");
yield handleCustomAuth(pool, client, authParameters, clientMetadata);
}
case "USER_AUTH" -> {
requireFlowEnabled(client, authFlow, "ALLOW_USER_AUTH");
yield handleUserAuth(pool, client, authParameters, clientMetadata);
}
default -> throw unsupportedAuthFlow(authFlow);
};
}
/**
* Enforces the app client's {@code ExplicitAuthFlows}. A client with no explicit list is left
* ungated so clients created without one keep working; once a list is set, the flow must be
* covered by one of {@code acceptedValues}.
*/
private static void requireFlowEnabled(UserPoolClient client, String authFlow, String... acceptedValues) {
List<String> enabled = client.getExplicitAuthFlows();
if (enabled == null || enabled.isEmpty()) {
return;
}
for (String accepted : acceptedValues) {
if (enabled.contains(accepted)) {
return;
}
}
throw new AwsException("InvalidParameterException", authFlow + " flow not enabled for this client", 400);
}
/**
* Refresh has to be listed explicitly via {@code ALLOW_REFRESH_TOKEN_AUTH}, except on clients that
* only use legacy (non {@code ALLOW_}) values, where refresh was never gated.
*/
private static void requireRefreshEnabled(UserPoolClient client, String authFlow) {
List<String> enabled = client.getExplicitAuthFlows();
if (enabled == null || enabled.stream().noneMatch(flow -> flow.startsWith("ALLOW_"))) {
return;
}
requireFlowEnabled(client, authFlow, "ALLOW_REFRESH_TOKEN_AUTH");
}
private static AwsException unsupportedAuthFlow(String authFlow) {
return new AwsException("InvalidParameterException", "Unsupported AuthFlow: " + authFlow, 400);
}
@@ -204,6 +204,119 @@ class CognitoServiceTest {
assertEquals("USERNAME and SRP_A are required", exception.getMessage());
}
@Test
void initiateAuthRejectsFlowsTheClientDoesNotAllow() {
UserPool pool = createPoolAndUser();
UserPoolClient client = clientWithFlows(pool, "ALLOW_USER_SRP_AUTH", "ALLOW_REFRESH_TOKEN_AUTH");
Map<String, String> passwordParams = Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!");
AwsException exception = assertThrows(AwsException.class, () ->
service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH", passwordParams));
assertEquals("InvalidParameterException", exception.getErrorCode());
assertEquals("USER_PASSWORD_AUTH flow not enabled for this client", exception.getMessage());
assertThrows(AwsException.class, () ->
service.initiateAuth(client.getClientId(), "CUSTOM_AUTH", Map.of("USERNAME", "alice")));
}
@Test
void initiateAuthAllowsFlowsTheClientEnables() {
UserPool pool = createPoolAndUser();
UserPoolClient client = clientWithFlows(pool, "ALLOW_USER_PASSWORD_AUTH");
Map<String, Object> result = service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!"));
assertTrue(result.containsKey("AuthenticationResult"));
}
@Test
void initiateAuthHonorsLegacyExplicitAuthFlowValues() {
UserPool pool = createPoolAndUser();
UserPoolClient client = clientWithFlows(pool, "USER_PASSWORD_AUTH");
Map<String, Object> result = service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!"));
assertTrue(result.containsKey("AuthenticationResult"));
}
@Test
void refreshTokenAuthRequiresAllowRefreshTokenAuthWhenClientUsesAllowValues() {
UserPool pool = createPoolAndUser();
UserPoolClient client = clientWithFlows(pool, "ALLOW_USER_PASSWORD_AUTH");
AwsException exception = assertThrows(AwsException.class, () ->
service.initiateAuth(client.getClientId(), "REFRESH_TOKEN_AUTH",
Map.of("REFRESH_TOKEN", "irrelevant")));
assertEquals("REFRESH_TOKEN_AUTH flow not enabled for this client", exception.getMessage());
}
@Test
void adminInitiateAuthRejectsFlowsTheClientDoesNotAllow() {
UserPool pool = createPoolAndUser();
UserPoolClient client = clientWithFlows(pool, "ALLOW_USER_SRP_AUTH");
Map<String, String> passwordParams = Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!");
for (String flow : List.of("ADMIN_USER_PASSWORD_AUTH", "ADMIN_NO_SRP_AUTH", "USER_PASSWORD_AUTH")) {
AwsException exception = assertThrows(AwsException.class, () ->
service.adminInitiateAuth(pool.getId(), client.getClientId(), flow, passwordParams));
assertEquals("InvalidParameterException", exception.getErrorCode());
assertEquals(flow + " flow not enabled for this client", exception.getMessage());
}
}
@Test
void adminInitiateAuthAllowsAdminPasswordFlowWhenEnabled() {
UserPool pool = createPoolAndUser();
UserPoolClient client = clientWithFlows(pool, "ALLOW_ADMIN_USER_PASSWORD_AUTH");
Map<String, String> passwordParams = Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!");
for (String flow : List.of("ADMIN_USER_PASSWORD_AUTH", "ADMIN_NO_SRP_AUTH")) {
Map<String, Object> result = service.adminInitiateAuth(pool.getId(), client.getClientId(), flow,
passwordParams);
assertTrue(result.containsKey("AuthenticationResult"));
}
}
@Test
void userAuthRequiresAllowUserAuthWhenClientListsFlows() {
UserPool pool = createPoolAndUser();
UserPoolClient client = clientWithFlows(pool, "ALLOW_USER_PASSWORD_AUTH", "ALLOW_USER_SRP_AUTH");
Map<String, String> params = Map.of("USERNAME", "alice");
AwsException initiate = assertThrows(AwsException.class, () ->
service.initiateAuth(client.getClientId(), "USER_AUTH", params));
assertEquals("InvalidParameterException", initiate.getErrorCode());
assertEquals("USER_AUTH flow not enabled for this client", initiate.getMessage());
AwsException admin = assertThrows(AwsException.class, () ->
service.adminInitiateAuth(pool.getId(), client.getClientId(), "USER_AUTH", params));
assertEquals("InvalidParameterException", admin.getErrorCode());
assertEquals("USER_AUTH flow not enabled for this client", admin.getMessage());
}
@Test
void userAuthAllowedWhenClientEnablesAllowUserAuth() {
UserPool pool = createPoolAndUser();
UserPoolClient client = clientWithFlows(pool, "ALLOW_USER_AUTH");
Map<String, String> params = Map.of("USERNAME", "alice");
Map<String, Object> initiate = service.initiateAuth(client.getClientId(), "USER_AUTH", params);
assertEquals("SELECT_CHALLENGE", initiate.get("ChallengeName"));
Map<String, Object> admin = service.adminInitiateAuth(pool.getId(), client.getClientId(), "USER_AUTH",
params);
assertEquals("SELECT_CHALLENGE", admin.get("ChallengeName"));
}
private UserPoolClient clientWithFlows(UserPool pool, String... explicitAuthFlows) {
return service.createUserPoolClient(pool.getId(), "flows-client", false, false, List.of(), List.of(),
null, List.of(), null, List.of(explicitAuthFlows), null, null, List.of(), null, List.of(), null,
null, null, List.of(), null, null);
}
@ParameterizedTest
@CsvSource({
"Short1!a",