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 9054e30712..7d4c5fa748 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 @@ -328,6 +328,9 @@ final class CognitoAuthFlowHandler { if ("RESET_REQUIRED".equals(user.getUserStatus())) { throw new AwsException("PasswordResetRequiredException", "Password reset required", 400); } + if ("UNCONFIRMED".equals(user.getUserStatus())) { + throw new AwsException("UserNotConfirmedException", "User is not confirmed", 400); + } if (user.getSrpVerifier() == null) { throw new AwsException("NotAuthorizedException", "User does not support SRP auth", 400); } @@ -386,6 +389,9 @@ final class CognitoAuthFlowHandler { if ("RESET_REQUIRED".equals(user.getUserStatus())) { throw new AwsException("PasswordResetRequiredException", "Password reset required", 400); } + if ("UNCONFIRMED".equals(user.getUserStatus())) { + throw new AwsException("UserNotConfirmedException", "User is not confirmed", 400); + } if (user.getSrpVerifier() == null) { throw new AwsException("NotAuthorizedException", "User does not support SRP auth", 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 3fa6f53c20..c5da58ccf6 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 @@ -1313,6 +1313,46 @@ class CognitoServiceTest { assertEquals("bob", params.get("USERNAME")); } + @Test + void initiateAuthWithUserSrpAuthRejectsUnconfirmedUser() { + UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1"); + UserPoolClient client = service.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of()); + service.signUp(client.getClientId(), "bob", "Password123!", Map.of("email", "bob@example.com")); + + AwsException ex = assertThrows(AwsException.class, () -> + service.initiateAuth(client.getClientId(), "USER_SRP_AUTH", + Map.of("USERNAME", "bob", "SRP_A", "ABCDEF1234567890"))); + + assertEquals("UserNotConfirmedException", ex.getErrorCode()); + } + + @Test + void respondToAuthChallengeWithUserSrpAuthRejectsNewlyUnconfirmedUser() { + UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1"); + String password = "Password123!"; + service.adminCreateUser(pool.getId(), "bob", Map.of("email", "bob@example.com"), null); + service.adminSetUserPassword(pool.getId(), "bob", password, true); + UserPoolClient client = service.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of()); + + Map initResult = service.initiateAuth(client.getClientId(), "USER_SRP_AUTH", + Map.of("USERNAME", "bob", "SRP_A", "ABCDEF1234567890")); + String session = (String) initResult.get("Session"); + // Simulate the user becoming unconfirmed after the SRP session is created. + CognitoUser user = service.adminGetUser(pool.getId(), "bob"); + user.setUserStatus("UNCONFIRMED"); + userStore.put(pool.getId() + "::" + user.getUsername(), user); + + AwsException ex = assertThrows(AwsException.class, () -> + service.respondToAuthChallenge(client.getClientId(), "PASSWORD_VERIFIER", session, + Map.of( + "USERNAME", "bob", + "PASSWORD_CLAIM_SIGNATURE", "invalid-sig", + "TIMESTAMP", "Wed Apr 8 12:00:00 UTC 2026" + ))); + + assertEquals("UserNotConfirmedException", ex.getErrorCode()); + } + @Test void respondToAuthChallengeWithInvalidSrpSignatureRejects() { UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");