Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(cognito): reject unrecognized AuthFlow instead of issuing tokens (#3923)

Priamo Germosen61f67cc83466
diff --git a/compatibility-tests/sdk-test-python/tests/test_cognito.py b/compatibility-tests/sdk-test-python/tests/test_cognito.py
index d1b9164896..53e8e80191 100644
--- a/compatibility-tests/sdk-test-python/tests/test_cognito.py
+++ b/compatibility-tests/sdk-test-python/tests/test_cognito.py
@@ -135,13 +135,16 @@ class TestCognitoAuth:
Username=username,
UserAttributes=[{"Name": "email", "Value": "[email protected]"}],
)
+ cognito_client.admin_set_user_password(
+ UserPoolId=pool_id, Username=username, Password="Pytest#1234", Permanent=True
+ )
try:
response = cognito_client.admin_initiate_auth(
UserPoolId=pool_id,
ClientId=client_id,
AuthFlow="ADMIN_NO_SRP_AUTH",
- AuthParameters={"USERNAME": username, "PASSWORD": "any"},
+ AuthParameters={"USERNAME": username, "PASSWORD": "Pytest#1234"},
)
access_token = response["AuthenticationResult"]["AccessToken"]
assert access_token
@@ -171,12 +174,15 @@ class TestCognitoAuth:
Username=username,
UserAttributes=[{"Name": "email", "Value": "[email protected]"}],
)
+ cognito_client.admin_set_user_password(
+ UserPoolId=pool_id, Username=username, Password="Pytest#1234", Permanent=True
+ )
auth_response = cognito_client.admin_initiate_auth(
UserPoolId=pool_id,
ClientId=client_id,
AuthFlow="ADMIN_NO_SRP_AUTH",
- AuthParameters={"USERNAME": username, "PASSWORD": "any"},
+ AuthParameters={"USERNAME": username, "PASSWORD": "Pytest#1234"},
)
access_token = auth_response["AuthenticationResult"]["AccessToken"]
diff --git a/docs/services/cognito.md b/docs/services/cognito.md
index 22ea28bef4..b986806ed4 100644
--- a/docs/services/cognito.md
+++ b/docs/services/cognito.md
@@ -195,6 +195,15 @@ further divergences, both deliberate:
|--------|-------------|
| ListUsers | Lists users stored in a user pool. |
+## Supported AuthFlow Values
+
+`InitiateAuth` accepts `USER_PASSWORD_AUTH`, `USER_SRP_AUTH`, `CUSTOM_AUTH`, `REFRESH_TOKEN_AUTH` and
+`REFRESH_TOKEN`. `AdminInitiateAuth` accepts `ADMIN_USER_PASSWORD_AUTH`, `ADMIN_NO_SRP_AUTH`,
+`ADMIN_USER_SRP_AUTH`, `USER_PASSWORD_AUTH`, `CUSTOM_AUTH`, `REFRESH_TOKEN_AUTH` and `REFRESH_TOKEN`.
+
+Any other `AuthFlow` value, including the choice-based `USER_AUTH` flow, is rejected with
+`InvalidParameterException` and no tokens are issued. `USER_AUTH` is not implemented yet.
+
## 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 29ddca047f..253014c70c 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
@@ -88,17 +88,7 @@ final class CognitoAuthFlowHandler {
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);
- default -> {
- String username = authParameters.get("USERNAME");
- if (username == null) {
- throw new AwsException("InvalidParameterException", "USERNAME is required", 400);
- }
- CognitoUser user = service.adminGetUser(pool.getId(), username);
- Map<String, Object> result = new HashMap<>();
- result.put("AuthenticationResult",
- issueTokens(pool, client, user, "TokenGeneration_Authentication", clientMetadata));
- yield result;
- }
+ default -> throw unsupportedAuthFlow(authFlow);
};
}
@@ -121,21 +111,19 @@ final class CognitoAuthFlowHandler {
}
return switch (authFlow) {
- case "ADMIN_USER_PASSWORD_AUTH", "USER_PASSWORD_AUTH" ->
+ 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" -> handleUserSrpAuth(pool, client, authParameters, clientMetadata);
case "CUSTOM_AUTH" -> handleCustomAuth(pool, client, authParameters, clientMetadata);
- default -> {
- CognitoUser user = service.adminGetUser(userPoolId, username);
- Map<String, Object> result = new HashMap<>();
- result.put("AuthenticationResult",
- issueTokens(pool, client, user, "TokenGeneration_Authentication", clientMetadata));
- yield result;
- }
+ default -> throw unsupportedAuthFlow(authFlow);
};
}
+ private static AwsException unsupportedAuthFlow(String authFlow) {
+ return new AwsException("InvalidParameterException", "Unsupported AuthFlow: " + authFlow, 400);
+ }
+
Map<String, Object> respondToAuthChallenge(String clientId, String challengeName, String session,
Map<String, String> responses, Map<String, String> clientMetadata) {
UserPoolClient client = service.findClientById(clientId);
diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java
index 6faae904e2..94212904f0 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java
@@ -134,6 +134,77 @@ class CognitoIntegrationTest {
.body("AuthenticationResult.RefreshToken", org.hamcrest.Matchers.notNullValue());
}
+ @Test
+ @Order(2)
+ void initiateAuthRejectsUnrecognizedAuthFlow() {
+ cognitoAction("InitiateAuth", """
+ {
+ "ClientId": "%s",
+ "AuthFlow": "UNKNOWN_FLOW",
+ "AuthParameters": {
+ "USERNAME": "%s"
+ }
+ }
+ """.formatted(clientId, USERNAME))
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("InvalidParameterException"))
+ .body("AuthenticationResult", org.hamcrest.Matchers.nullValue());
+ }
+
+ @Test
+ @Order(2)
+ void adminInitiateAuthRejectsUnrecognizedAuthFlow() {
+ cognitoAction("AdminInitiateAuth", """
+ {
+ "UserPoolId": "%s",
+ "ClientId": "%s",
+ "AuthFlow": "UNKNOWN_FLOW",
+ "AuthParameters": {
+ "USERNAME": "%s"
+ }
+ }
+ """.formatted(poolId, clientId, USERNAME))
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("InvalidParameterException"))
+ .body("AuthenticationResult", org.hamcrest.Matchers.nullValue());
+ }
+
+ @Test
+ @Order(2)
+ void adminInitiateAuthNoSrpFlowRequiresThePassword() {
+ cognitoAction("AdminInitiateAuth", """
+ {
+ "UserPoolId": "%s",
+ "ClientId": "%s",
+ "AuthFlow": "ADMIN_NO_SRP_AUTH",
+ "AuthParameters": {
+ "USERNAME": "%s",
+ "PASSWORD": "wrong-password"
+ }
+ }
+ """.formatted(poolId, clientId, USERNAME))
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("NotAuthorizedException"));
+
+ cognitoAction("AdminInitiateAuth", """
+ {
+ "UserPoolId": "%s",
+ "ClientId": "%s",
+ "AuthFlow": "ADMIN_NO_SRP_AUTH",
+ "AuthParameters": {
+ "USERNAME": "%s",
+ "PASSWORD": "%s"
+ }
+ }
+ """.formatted(poolId, clientId, USERNAME, PASSWORD))
+ .then()
+ .statusCode(200)
+ .body("AuthenticationResult.AccessToken", org.hamcrest.Matchers.notNullValue());
+ }
+
@Test
@Order(3)
void authTokensAreSignedWithPublishedRsaJwksKey() throws Exception {
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 f832de46e7..b498bca91a 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
@@ -147,6 +147,47 @@ class CognitoServiceTest {
assertEquals(List.of("email"), pool.getUsernameAttributes());
}
+ @Test
+ void initiateAuthRejectsUnrecognizedAuthFlowWithoutIssuingTokens() {
+ UserPool pool = createPoolAndUser();
+ UserPoolClient client = service.createUserPoolClient(
+ pool.getId(), "client", false, false, List.of(), List.of());
+
+ AwsException exception = assertThrows(AwsException.class, () ->
+ service.initiateAuth(client.getClientId(), "UNKNOWN_FLOW", Map.of("USERNAME", "alice")));
+
+ assertEquals("InvalidParameterException", exception.getErrorCode());
+ }
+
+ @Test
+ void adminInitiateAuthRejectsUnrecognizedAuthFlowWithoutIssuingTokens() {
+ UserPool pool = createPoolAndUser();
+ UserPoolClient client = service.createUserPoolClient(
+ pool.getId(), "client", false, false, List.of(), List.of());
+
+ AwsException exception = assertThrows(AwsException.class, () ->
+ service.adminInitiateAuth(pool.getId(), client.getClientId(), "UNKNOWN_FLOW",
+ Map.of("USERNAME", "alice")));
+
+ assertEquals("InvalidParameterException", exception.getErrorCode());
+ }
+
+ @Test
+ void adminInitiateAuthNoSrpFlowRequiresTheCorrectPassword() {
+ UserPool pool = createPoolAndUser();
+ UserPoolClient client = service.createUserPoolClient(
+ pool.getId(), "client", false, false, List.of(), List.of());
+
+ AwsException exception = assertThrows(AwsException.class, () ->
+ service.adminInitiateAuth(pool.getId(), client.getClientId(), "ADMIN_NO_SRP_AUTH",
+ Map.of("USERNAME", "alice", "PASSWORD", "wrong")));
+ assertEquals("NotAuthorizedException", exception.getErrorCode());
+
+ Map<String, Object> result = service.adminInitiateAuth(pool.getId(), client.getClientId(),
+ "ADMIN_NO_SRP_AUTH", Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!"));
+ assertTrue(result.containsKey("AuthenticationResult"));
+ }
+
@ParameterizedTest
@CsvSource({
"Short1!a",