mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cognito): honour case-insensitive sign-in configuration (#7831)
This commit is contained in:
1 parent
7748d2b51f
commit
faf9c4e64d
9 files changed
+531
-21
No files matched your search
@@ -0,0 +1,85 @@
|
||||
package io.floci.aws.services.cognito;
|
||||
|
||||
import io.floci.aws.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.floci.aws.services.cognito.CognitoRestAssuredUtils.cognitoAction;
|
||||
import static io.floci.aws.services.cognito.CognitoRestAssuredUtils.cognitoJson;
|
||||
import static org.hamcrest.Matchers.emptyOrNullString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
@QuarkusTest
|
||||
class CognitoEmailCaseIntegrationTest {
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void initiateAuth_caseInsensitiveEmailPool_acceptsMixedCaseEmail() throws Exception {
|
||||
String poolId = cognitoJson("CreateUserPool", """
|
||||
{
|
||||
"PoolName": "email-case-%s",
|
||||
"UsernameAttributes": ["email"],
|
||||
"UsernameConfiguration": { "CaseSensitive": false }
|
||||
}
|
||||
""".formatted(UUID.randomUUID())).path("UserPool").path("Id").asText();
|
||||
try {
|
||||
String clientId = cognitoJson("CreateUserPoolClient", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"ClientName": "email-case-client",
|
||||
"ExplicitAuthFlows": ["ALLOW_USER_PASSWORD_AUTH"]
|
||||
}
|
||||
""".formatted(poolId)).path("UserPoolClient").path("ClientId").asText();
|
||||
String username = cognitoJson("AdminCreateUser", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"Username": "fixture@example.com",
|
||||
"TemporaryPassword": "Temporary-password123!",
|
||||
"MessageAction": "SUPPRESS",
|
||||
"UserAttributes": [
|
||||
{ "Name": "email", "Value": "fixture@example.com" },
|
||||
{ "Name": "email_verified", "Value": "true" }
|
||||
]
|
||||
}
|
||||
""".formatted(poolId)).path("User").path("Username").asText();
|
||||
cognitoAction("AdminSetUserPassword", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"Username": "%s",
|
||||
"Password": "Valid-password123!",
|
||||
"Permanent": true
|
||||
}
|
||||
""".formatted(poolId, username)).then().statusCode(200);
|
||||
|
||||
for (String email : List.of("fixture@example.com", "Fixture@Example.com")) {
|
||||
cognitoAction("InitiateAuth", """
|
||||
{
|
||||
"ClientId": "%s",
|
||||
"AuthFlow": "USER_PASSWORD_AUTH",
|
||||
"AuthParameters": {
|
||||
"USERNAME": "%s",
|
||||
"PASSWORD": "Valid-password123!"
|
||||
}
|
||||
}
|
||||
""".formatted(clientId, email))
|
||||
.then().statusCode(200)
|
||||
.body("AuthenticationResult.ExpiresIn", equalTo(3600))
|
||||
.body("AuthenticationResult.AccessToken", not(emptyOrNullString()))
|
||||
.body("AuthenticationResult.IdToken", not(emptyOrNullString()));
|
||||
}
|
||||
} finally {
|
||||
cognitoAction("DeleteUserPool", """
|
||||
{ "UserPoolId": "%s" }
|
||||
""".formatted(poolId)).then().statusCode(200);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,10 @@ import io.floci.aws.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
@@ -37,6 +40,44 @@ class CognitoMessageMfaIntegrationTest {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"false", "true", "default"})
|
||||
void smsChallengeRespectsUsernameCaseSensitivity(String setting) throws Exception {
|
||||
String configuration = "default".equals(setting) ? ""
|
||||
: ",\"UsernameConfiguration\":{\"CaseSensitive\":" + setting + "}";
|
||||
String poolId = cognitoJson("CreateUserPool", "{\"PoolName\":\"MfaCasePool\"" + configuration + "}")
|
||||
.path("UserPool").path("Id").asText();
|
||||
try {
|
||||
cognitoJson("SetUserPoolMfaConfig", """
|
||||
{"UserPoolId":"%s","MfaConfiguration":"ON",%s}
|
||||
""".formatted(poolId, SMS_CONFIG));
|
||||
String clientId = createClient(poolId);
|
||||
User user = createUser(poolId);
|
||||
User variant = new User(user.username().toUpperCase(Locale.ROOT), user.phone(), user.email());
|
||||
JsonNode challenge = passwordLogin(clientId, "false".equals(setting) ? variant : user);
|
||||
assertEquals("SMS_MFA", challenge.path("ChallengeName").asText());
|
||||
String code = CognitoCodes.sixDigitCode(latestSms(user.phone()));
|
||||
cognitoAction("RespondToAuthChallenge", """
|
||||
{"ClientId":"%s","ChallengeName":"SMS_MFA","Session":"%s",
|
||||
"ChallengeResponses":{"SMS_MFA_CODE":"%s"}}
|
||||
""".formatted(clientId, challenge.path("Session").asText(), code))
|
||||
.then().statusCode(400).body("__type", equalTo("NotAuthorizedException"));
|
||||
cognitoAction("RespondToAuthChallenge", respond(clientId, "SMS_MFA", challenge,
|
||||
new User("other", user.phone(), user.email()), "SMS_MFA_CODE", code))
|
||||
.then().statusCode(400).body("__type", equalTo("NotAuthorizedException"));
|
||||
if (!"false".equals(setting)) {
|
||||
cognitoAction("RespondToAuthChallenge", respond(clientId, "SMS_MFA", challenge, variant,
|
||||
"SMS_MFA_CODE", code)).then().statusCode(400)
|
||||
.body("__type", equalTo("NotAuthorizedException"));
|
||||
}
|
||||
JsonNode result = cognitoJson("RespondToAuthChallenge", respond(clientId, "SMS_MFA", challenge,
|
||||
"false".equals(setting) ? variant : user, "SMS_MFA_CODE", code));
|
||||
assertFalse(result.path("AuthenticationResult").path("AccessToken").asText().isEmpty());
|
||||
} finally {
|
||||
cognitoAction("DeleteUserPool", "{\"UserPoolId\":\"" + poolId + "\"}").then().statusCode(200);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void optionalSmsMfaChallengesOnceTurnedOnAndVerifiesThePhone() throws Exception {
|
||||
String poolId = createPool("OPTIONAL", SMS_CONFIG);
|
||||
|
||||
@@ -5977,6 +5977,230 @@ class CognitoServiceTest {
|
||||
return service.createUserPool(req, "us-east-1");
|
||||
}
|
||||
|
||||
@Nested
|
||||
class CaseInsensitiveSignIn {
|
||||
private UserPool pool() {
|
||||
return service.createUserPool(Map.of("PoolName", "case-insensitive",
|
||||
"UsernameAttributes", List.of("email"),
|
||||
"UsernameConfiguration", Map.of("CaseSensitive", false)), "us-east-1");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void emailLookupRespectsExplicitCaseSensitivity(boolean caseSensitive) {
|
||||
UserPool pool = pool();
|
||||
pool.setUsernameConfiguration(Map.of("CaseSensitive", caseSensitive));
|
||||
CognitoUser user = service.adminCreateUser(pool.getId(), "Fixture@Example.com", Map.of(), null);
|
||||
assertEquals(user.getUsername(), service.adminGetUser(pool.getId(), "Fixture@Example.com").getUsername());
|
||||
if (caseSensitive) {
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.adminGetUser(pool.getId(), "fixture@example.com"));
|
||||
assertEquals("UserNotFoundException", error.getErrorCode());
|
||||
} else {
|
||||
assertEquals(user.getUsername(), service.adminGetUser(pool.getId(), "fixture@example.com").getUsername());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void omittedCaseSensitivityKeepsExactEmailMatching() {
|
||||
UserPool pool = createEmailAliasPool();
|
||||
service.adminCreateUser(pool.getId(), "fixture@example.com", Map.of(), null);
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.adminGetUser(pool.getId(), "Fixture@Example.com"));
|
||||
assertEquals("UserNotFoundException", error.getErrorCode());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void passwordAndSrpAuthKeepCanonicalIdentity(boolean admin) throws Exception {
|
||||
UserPool pool = pool();
|
||||
String password = "Valid-password123!";
|
||||
CognitoUser user = service.adminCreateUser(pool.getId(), "fixture@example.com", Map.of(), null);
|
||||
service.adminSetUserPassword(pool.getId(), user.getUsername(), password, true);
|
||||
UserPoolClient client = service.createUserPoolClient(pool.getId(), "client", true, false, List.of(), List.of());
|
||||
client.setExplicitAuthFlows(List.of("ALLOW_USER_PASSWORD_AUTH", "ALLOW_ADMIN_USER_PASSWORD_AUTH",
|
||||
"ALLOW_USER_SRP_AUTH"));
|
||||
String supplied = "Fixture@Example.com";
|
||||
String secretHash = secretHash(client, supplied);
|
||||
Map<String, String> params = Map.of("USERNAME", supplied, "PASSWORD", password, "SECRET_HASH", secretHash);
|
||||
Map<String, Object> authenticated = initiateAuth(admin, pool, client, "USER_PASSWORD_AUTH", params);
|
||||
Map<?, ?> tokens = (Map<?, ?>) authenticated.get("AuthenticationResult");
|
||||
String accessToken = (String) tokens.get("AccessToken");
|
||||
JsonNode claims = new ObjectMapper().readTree(Base64.getUrlDecoder().decode(accessToken.split("\\.")[1]));
|
||||
assertEquals(user.getUsername(), claims.path("sub").asText());
|
||||
assertEquals(user.getUsername(), claims.path("username").asText());
|
||||
|
||||
Map<String, String> srpParams = Map.of("USERNAME", supplied,
|
||||
"SRP_A", CognitoSrpTestClient.publicA(), "SECRET_HASH", secretHash);
|
||||
Map<String, Object> started = initiateAuth(admin, pool, client, "USER_SRP_AUTH", srpParams);
|
||||
Map<?, ?> challenge = (Map<?, ?>) started.get("ChallengeParameters");
|
||||
assertEquals(user.getUsername(), challenge.get("USER_ID_FOR_SRP"));
|
||||
Map<String, String> proof = new HashMap<>(CognitoSrpTestClient.proof(
|
||||
pool.getId(), user.getUsername(), password, challenge));
|
||||
proof.put("SECRET_HASH", secretHash(client, user.getUsername()));
|
||||
Map<String, Object> completed = respondToChallenge(admin, pool, client, "PASSWORD_VERIFIER", (String) started.get("Session"), proof);
|
||||
assertNotNull(completed.get("AuthenticationResult"));
|
||||
}
|
||||
|
||||
private Map<String, Object> initiateAuth(boolean admin, UserPool pool, UserPoolClient client,
|
||||
String flow, Map<String, String> params) {
|
||||
return admin ? service.adminInitiateAuth(pool.getId(), client.getClientId(),
|
||||
"USER_PASSWORD_AUTH".equals(flow) ? "ADMIN_USER_PASSWORD_AUTH" : flow, params)
|
||||
: service.initiateAuth(client.getClientId(), flow, params);
|
||||
}
|
||||
|
||||
private Map<String, Object> respondToChallenge(boolean admin, UserPool pool, UserPoolClient client,
|
||||
String challenge, String session, Map<String, String> responses) {
|
||||
return admin ? service.adminRespondToAuthChallenge(pool.getId(), client.getClientId(),
|
||||
challenge, session, responses)
|
||||
: service.respondToAuthChallenge(client.getClientId(), challenge, session, responses);
|
||||
}
|
||||
|
||||
private String secretHash(UserPoolClient client, String username) throws Exception {
|
||||
Mac mac = Mac.getInstance("HmacSHA256");
|
||||
mac.init(new SecretKeySpec(client.getClientSecret().getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
|
||||
return Base64.getEncoder().encodeToString(
|
||||
mac.doFinal((username + client.getClientId()).getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"false,false", "true,false", "false,true", "true,true", "false,default", "true,default"})
|
||||
void srpChallengeRespectsUsernameCaseSensitivity(boolean admin, String setting) throws Exception {
|
||||
UserPool pool = service.createUserPool(Map.of("PoolName", "classic-srp-case"), "us-east-1");
|
||||
if (!"default".equals(setting)) {
|
||||
pool.setUsernameConfiguration(Map.of("CaseSensitive", Boolean.parseBoolean(setting)));
|
||||
}
|
||||
String password = "Valid-password123!";
|
||||
CognitoUser user = service.adminCreateUser(pool.getId(), "Fixture", Map.of(), null);
|
||||
service.adminSetUserPassword(pool.getId(), user.getUsername(), password, true);
|
||||
UserPoolClient client = service.createUserPoolClient(pool.getId(), "client", true, false, List.of(), List.of());
|
||||
client.setExplicitAuthFlows(List.of("ALLOW_USER_SRP_AUTH"));
|
||||
String supplied = "false".equals(setting) ? "fixture" : user.getUsername();
|
||||
Map<String, Object> started = initiateAuth(admin, pool, client, "USER_SRP_AUTH",
|
||||
Map.of("USERNAME", supplied, "SRP_A", CognitoSrpTestClient.publicA(),
|
||||
"SECRET_HASH", secretHash(client, supplied)));
|
||||
Map<?, ?> challenge = (Map<?, ?>) started.get("ChallengeParameters");
|
||||
assertEquals("Fixture", challenge.get("USER_ID_FOR_SRP"));
|
||||
String session = (String) started.get("Session");
|
||||
Map<String, String> proof = new HashMap<>(CognitoSrpTestClient.proof(
|
||||
pool.getId(), user.getUsername(), password, challenge));
|
||||
proof.remove("USERNAME");
|
||||
assertEquals("InvalidParameterException", assertThrows(AwsException.class,
|
||||
() -> respondToChallenge(admin, pool, client, "PASSWORD_VERIFIER", session, proof)).getErrorCode());
|
||||
proof.put("USERNAME", "other");
|
||||
proof.put("SECRET_HASH", secretHash(client, "other"));
|
||||
assertEquals("NotAuthorizedException", assertThrows(AwsException.class,
|
||||
() -> respondToChallenge(admin, pool, client, "PASSWORD_VERIFIER", session, proof)).getErrorCode());
|
||||
proof.put("USERNAME", "fixture");
|
||||
proof.put("SECRET_HASH", secretHash(client, "fixture"));
|
||||
if (!"false".equals(setting)) {
|
||||
assertEquals("NotAuthorizedException", assertThrows(AwsException.class,
|
||||
() -> respondToChallenge(admin, pool, client, "PASSWORD_VERIFIER", session, proof)).getErrorCode());
|
||||
proof.put("USERNAME", user.getUsername());
|
||||
proof.put("SECRET_HASH", secretHash(client, user.getUsername()));
|
||||
}
|
||||
Map<?, ?> tokens = (Map<?, ?>) respondToChallenge(admin, pool, client, "PASSWORD_VERIFIER", session, proof).get("AuthenticationResult");
|
||||
String accessToken = (String) tokens.get("AccessToken");
|
||||
JsonNode claims = new ObjectMapper().readTree(Base64.getUrlDecoder().decode(accessToken.split("\\.")[1]));
|
||||
assertEquals("Fixture", claims.path("username").asText());
|
||||
assertEquals(user.getAttributes().get("sub"), claims.path("sub").asText());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"false,false", "true,false", "false,true", "true,true", "false,default", "true,default"})
|
||||
void newPasswordChallengeRespectsUsernameCaseSensitivity(boolean admin, String setting) throws Exception {
|
||||
UserPool pool = service.createUserPool(Map.of("PoolName", "classic-password-case"), "us-east-1");
|
||||
if (!"default".equals(setting)) {
|
||||
pool.setUsernameConfiguration(Map.of("CaseSensitive", Boolean.parseBoolean(setting)));
|
||||
}
|
||||
String temporaryPassword = "Temporary-password123!";
|
||||
CognitoUser user = service.adminCreateUser(pool.getId(), "Fixture", Map.of(), temporaryPassword);
|
||||
UserPoolClient client = service.createUserPoolClient(pool.getId(), "client", true, false, List.of(), List.of());
|
||||
client.setExplicitAuthFlows(List.of("ALLOW_USER_PASSWORD_AUTH", "ALLOW_ADMIN_USER_PASSWORD_AUTH"));
|
||||
String supplied = "false".equals(setting) ? "fixture" : user.getUsername();
|
||||
Map<String, Object> started = initiateAuth(admin, pool, client, "USER_PASSWORD_AUTH",
|
||||
Map.of("USERNAME", supplied, "PASSWORD", temporaryPassword,
|
||||
"SECRET_HASH", secretHash(client, supplied)));
|
||||
assertEquals("NEW_PASSWORD_REQUIRED", started.get("ChallengeName"));
|
||||
String session = (String) started.get("Session");
|
||||
String newPassword = "Permanent-password123!";
|
||||
Map<String, String> responses = new HashMap<>(Map.of("NEW_PASSWORD", newPassword));
|
||||
assertEquals("NotAuthorizedException", assertThrows(AwsException.class,
|
||||
() -> respondToChallenge(admin, pool, client, "NEW_PASSWORD_REQUIRED", session, responses))
|
||||
.getErrorCode());
|
||||
responses.put("USERNAME", "other");
|
||||
responses.put("SECRET_HASH", secretHash(client, "other"));
|
||||
assertEquals("NotAuthorizedException", assertThrows(AwsException.class,
|
||||
() -> respondToChallenge(admin, pool, client, "NEW_PASSWORD_REQUIRED", session, responses))
|
||||
.getErrorCode());
|
||||
responses.put("USERNAME", "fixture");
|
||||
responses.put("SECRET_HASH", secretHash(client, "fixture"));
|
||||
if (!"false".equals(setting)) {
|
||||
assertEquals("NotAuthorizedException", assertThrows(AwsException.class,
|
||||
() -> respondToChallenge(admin, pool, client, "NEW_PASSWORD_REQUIRED", session, responses))
|
||||
.getErrorCode());
|
||||
responses.put("USERNAME", user.getUsername());
|
||||
responses.put("SECRET_HASH", secretHash(client, user.getUsername()));
|
||||
}
|
||||
Map<?, ?> tokens = (Map<?, ?>) respondToChallenge(admin, pool, client, "NEW_PASSWORD_REQUIRED",
|
||||
session, responses).get("AuthenticationResult");
|
||||
assertNotNull(tokens.get("AccessToken"));
|
||||
assertEquals("Fixture", service.adminGetUser(pool.getId(), supplied).getUsername());
|
||||
assertNotNull(initiateAuth(admin, pool, client, "USER_PASSWORD_AUTH",
|
||||
Map.of("USERNAME", supplied, "PASSWORD", newPassword, "SECRET_HASH", secretHash(client, supplied)))
|
||||
.get("AuthenticationResult"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicateEmailCreationAndUpdatesUseTheSameComparison() {
|
||||
UserPool pool = pool();
|
||||
CognitoUser user = service.adminCreateUser(pool.getId(), "fixture@example.com",
|
||||
Map.of("email_verified", "true"), null);
|
||||
AwsException duplicate = assertThrows(AwsException.class,
|
||||
() -> service.adminCreateUser(pool.getId(), "Fixture@Example.com", Map.of(), null));
|
||||
assertEquals("AliasExistsException", duplicate.getErrorCode());
|
||||
UserPoolClient client = service.createUserPoolClient(pool.getId(), "client", false, false, List.of(), List.of());
|
||||
AwsException signup = assertThrows(AwsException.class, () -> service.signUp(client.getClientId(),
|
||||
"Fixture@Example.com", "Valid-password123!", Map.of(), Map.of(), Map.of()));
|
||||
assertEquals("UsernameExistsException", signup.getErrorCode());
|
||||
|
||||
CognitoUser other = service.adminCreateUser(pool.getId(), "other@example.com", Map.of(), null);
|
||||
AwsException update = assertThrows(AwsException.class, () -> service.adminUpdateUserAttributes(
|
||||
pool.getId(), other.getUsername(), Map.of("email", "Fixture@Example.com")));
|
||||
assertEquals("AliasExistsException", update.getErrorCode());
|
||||
service.adminSetUserPassword(pool.getId(), other.getUsername(), "Valid-password123!", true);
|
||||
client.setExplicitAuthFlows(List.of("ALLOW_USER_PASSWORD_AUTH"));
|
||||
Map<?, ?> tokens = (Map<?, ?>) service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
|
||||
Map.of("USERNAME", other.getUsername(), "PASSWORD", "Valid-password123!"))
|
||||
.get("AuthenticationResult");
|
||||
AwsException selfUpdate = assertThrows(AwsException.class, () -> service.updateUserAttributes(
|
||||
(String) tokens.get("AccessToken"), Map.of("email", "Fixture@Example.com")));
|
||||
assertEquals("AliasExistsException", selfUpdate.getErrorCode());
|
||||
service.adminUpdateUserAttributes(pool.getId(), user.getUsername(), Map.of("email", "Changed@Example.com"));
|
||||
assertEquals(user.getUsername(), service.adminGetUser(pool.getId(), "changed@example.com").getUsername());
|
||||
assertThrows(AwsException.class, () -> service.adminGetUser(pool.getId(), "fixture@example.com"));
|
||||
assertEquals(user.getUsername(), service.adminGetUser(pool.getId(), user.getUsername()).getAttributes().get("sub"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void classicUsernameAndPreferredUsernameAliasRespectTheSetting() {
|
||||
UserPool pool = service.createUserPool(Map.of("PoolName", "classic-case-insensitive",
|
||||
"AliasAttributes", List.of("preferred_username"),
|
||||
"UsernameConfiguration", Map.of("CaseSensitive", false)), "us-east-1");
|
||||
CognitoUser user = service.adminCreateUser(pool.getId(), "Fixture",
|
||||
Map.of("preferred_username", "Nickname"), null);
|
||||
assertEquals(user.getUsername(), service.adminGetUser(pool.getId(), "fixture").getUsername());
|
||||
assertEquals(user.getUsername(), service.adminGetUser(pool.getId(), "NICKNAME").getUsername());
|
||||
AwsException duplicate = assertThrows(AwsException.class,
|
||||
() -> service.adminCreateUser(pool.getId(), "FIXTURE", Map.of(), null));
|
||||
assertEquals("UsernameExistsException", duplicate.getErrorCode());
|
||||
UserPoolClient client = service.createUserPoolClient(pool.getId(), "client", false, false, List.of(), List.of());
|
||||
AwsException signup = assertThrows(AwsException.class, () -> service.signUp(client.getClientId(),
|
||||
"FIXTURE", "Valid-password123!", Map.of(), Map.of(), Map.of()));
|
||||
assertEquals("UsernameExistsException", signup.getErrorCode());
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isUuid(String value) {
|
||||
try {
|
||||
UUID.fromString(value);
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.time.Clock;
|
||||
@@ -398,9 +399,12 @@ class CognitoSignUpTriggerReentrancyTest {
|
||||
deliveredCode.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void preSignUpRejectsSameUsernameAdminCreationWithoutBlockingTheTrigger() throws Exception {
|
||||
@ParameterizedTest
|
||||
@CsvSource({"true, alice", "false, alice", "false, ALICE"})
|
||||
void preSignUpRejectsSameUsernameAdminCreationWithoutBlockingTheTrigger(
|
||||
boolean caseSensitive, String overlappingUsername) throws Exception {
|
||||
UserPool pool = pool(Map.of("PreSignUp", FUNCTION));
|
||||
pool.setUsernameConfiguration(Map.of("CaseSensitive", caseSensitive));
|
||||
UserPoolClient client = client(pool);
|
||||
AtomicReference<Throwable> callbackFailure = new AtomicReference<>();
|
||||
AtomicReference<AwsException> duplicateFailure = new AtomicReference<>();
|
||||
@@ -410,7 +414,7 @@ class CognitoSignUpTriggerReentrancyTest {
|
||||
if ("PreSignUp_SignUp".equals(event.path("triggerSource").asText())) {
|
||||
callback(() -> {
|
||||
AwsException duplicate = assertThrows(AwsException.class,
|
||||
() -> service.adminCreateUser(pool.getId(), "alice",
|
||||
() -> service.adminCreateUser(pool.getId(), overlappingUsername,
|
||||
Map.of("name", "created by trigger"), null, "SUPPRESS"));
|
||||
assertEquals("UsernameExistsException", duplicate.getErrorCode());
|
||||
duplicateFailure.set(duplicate);
|
||||
|
||||
@@ -12,6 +12,8 @@ import io.floci.aws.services.cognito.model.UserPoolClient;
|
||||
import io.floci.aws.testing.MutableClock;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
@@ -21,6 +23,7 @@ import java.security.MessageDigest;
|
||||
import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -108,6 +111,43 @@ class CognitoTotpMfaTest {
|
||||
assertNotNull(((Map<?, ?>) completed.get("AuthenticationResult")).get("AccessToken"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"false", "true", "default"})
|
||||
void totpChallengesRespectUsernameCaseSensitivity(String setting) {
|
||||
if (!"default".equals(setting)) {
|
||||
pool.setUsernameConfiguration(Map.of("CaseSensitive", Boolean.parseBoolean(setting)));
|
||||
}
|
||||
String supplied = "false".equals(setting) ? "ALICE" : USERNAME;
|
||||
String session = (String) service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
|
||||
Map.of("USERNAME", supplied, "PASSWORD", PASSWORD)).get("Session");
|
||||
Map<String, Object> association = service.associateSoftwareToken(null, session);
|
||||
String secret = (String) association.get("SecretCode");
|
||||
String code = CognitoTotp.code(secret, clock.instant());
|
||||
String verified = (String) service.verifySoftwareToken(null, (String) association.get("Session"), code)
|
||||
.get("Session");
|
||||
assertTotpUsernameMatching(setting, "MFA_SETUP", verified, Map.of());
|
||||
String later = (String) passwordLogin().get("Session");
|
||||
assertTotpUsernameMatching(setting, "SOFTWARE_TOKEN_MFA", later, Map.of("SOFTWARE_TOKEN_MFA_CODE", code));
|
||||
}
|
||||
|
||||
private void assertTotpUsernameMatching(String setting, String challenge, String session,
|
||||
Map<String, String> fields) {
|
||||
Map<String, String> responses = new HashMap<>(fields);
|
||||
assertEquals("NotAuthorizedException", assertThrows(AwsException.class,
|
||||
() -> service.respondToAuthChallenge(client.getClientId(), challenge, session, responses)).getErrorCode());
|
||||
responses.put("USERNAME", "other");
|
||||
assertEquals("NotAuthorizedException", assertThrows(AwsException.class,
|
||||
() -> service.respondToAuthChallenge(client.getClientId(), challenge, session, responses)).getErrorCode());
|
||||
responses.put("USERNAME", "ALICE");
|
||||
if (!"false".equals(setting)) {
|
||||
assertEquals("NotAuthorizedException", assertThrows(AwsException.class,
|
||||
() -> service.respondToAuthChallenge(client.getClientId(), challenge, session, responses)).getErrorCode());
|
||||
responses.put("USERNAME", USERNAME);
|
||||
}
|
||||
assertNotNull(service.respondToAuthChallenge(client.getClientId(), challenge, session, responses)
|
||||
.get("AuthenticationResult"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void disabledUserCannotCompleteMfaSetupChallenge() {
|
||||
String loginSession = (String) passwordLogin().get("Session");
|
||||
|
||||
Reference in new issue
Block a user