From a12bf8c96ccd45a727dd51ab412fff0a9abdbe21 Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Sun, 20 Sep 2026 22:04:55 -0400 Subject: [PATCH] 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. --- .../com/floci/test/CognitoFeaturesTest.java | 1 + docs/services/cognito.md | 6 + .../cognito/CognitoAuthFlowHandler.java | 90 ++++++++++++-- .../services/cognito/CognitoServiceTest.java | 113 ++++++++++++++++++ 4 files changed, 199 insertions(+), 11 deletions(-) diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CognitoFeaturesTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CognitoFeaturesTest.java index 7ac7c30feb..7b459c5b63 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CognitoFeaturesTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CognitoFeaturesTest.java @@ -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(); diff --git a/docs/services/cognito.md b/docs/services/cognito.md index dd74c1ab6b..bc1022257a 100644 --- a/docs/services/cognito.md +++ b/docs/services/cognito.md @@ -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 diff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoAuthFlowHandler.java b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoAuthFlowHandler.java index 478fb78e97..56bb18d616 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoAuthFlowHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoAuthFlowHandler.java @@ -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 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 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); } diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.java index 137353e7b0..0d6f767daa 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.java @@ -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 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 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 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 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 passwordParams = Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!"); + + for (String flow : List.of("ADMIN_USER_PASSWORD_AUTH", "ADMIN_NO_SRP_AUTH")) { + Map 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 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 params = Map.of("USERNAME", "alice"); + + Map initiate = service.initiateAuth(client.getClientId(), "USER_AUTH", params); + assertEquals("SELECT_CHALLENGE", initiate.get("ChallengeName")); + + Map 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",