mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix: resolve Cognito auth, token, and user lookup issues (#218 #220 #228 #229 #233 #234 #235) (#279)
This commit is contained in:
1 parent
b723a9c519
commit
5e8b39c1aa
7 files changed
+1625
-34
No files matched your search
+178
@@ -331,6 +331,173 @@ class CognitoIntegrationTest {
|
||||
.statusCode(404);
|
||||
}
|
||||
|
||||
// ── Issue #228: AccessToken contains client_id ─────────────────────
|
||||
|
||||
@Test
|
||||
@Order(30)
|
||||
void accessTokenContainsClientId() throws Exception {
|
||||
String token = initiateAuthAndGetAccessToken();
|
||||
JsonNode payload = decodeJwtPayload(token);
|
||||
assertEquals(clientId, payload.path("client_id").asText(),
|
||||
"AccessToken must contain client_id matching the requesting client");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(31)
|
||||
void idTokenDoesNotContainClientId() throws Exception {
|
||||
JsonNode auth = cognitoJson("InitiateAuth", """
|
||||
{
|
||||
"ClientId": "%s",
|
||||
"AuthFlow": "USER_PASSWORD_AUTH",
|
||||
"AuthParameters": { "USERNAME": "%s", "PASSWORD": "%s" }
|
||||
}
|
||||
""".formatted(clientId, username, password));
|
||||
String idToken = auth.path("AuthenticationResult").path("IdToken").asText();
|
||||
JsonNode payload = decodeJwtPayload(idToken);
|
||||
assertTrue(payload.path("client_id").isMissingNode(),
|
||||
"IdToken should not contain client_id");
|
||||
}
|
||||
|
||||
// ── Issue #229: Password verification ──────────────────────────────
|
||||
|
||||
@Test
|
||||
@Order(40)
|
||||
void initiateAuthRejectsWrongPassword() {
|
||||
cognitoAction("InitiateAuth", """
|
||||
{
|
||||
"ClientId": "%s",
|
||||
"AuthFlow": "USER_PASSWORD_AUTH",
|
||||
"AuthParameters": { "USERNAME": "%s", "PASSWORD": "WrongPassword!" }
|
||||
}
|
||||
""".formatted(clientId, username))
|
||||
.then()
|
||||
.statusCode(400);
|
||||
}
|
||||
|
||||
// ── Issue #220: Lookup by sub and email ─────────────────────────────
|
||||
|
||||
@Test
|
||||
@Order(50)
|
||||
void adminGetUserBySubUuid() throws Exception {
|
||||
JsonNode user = cognitoJson("AdminGetUser", """
|
||||
{ "UserPoolId": "%s", "Username": "%s" }
|
||||
""".formatted(poolId, username));
|
||||
String sub = null;
|
||||
for (JsonNode attr : user.path("UserAttributes")) {
|
||||
if ("sub".equals(attr.path("Name").asText())) {
|
||||
sub = attr.path("Value").asText();
|
||||
break;
|
||||
}
|
||||
}
|
||||
assertNotNull(sub, "User should have a sub attribute");
|
||||
|
||||
JsonNode bySubUser = cognitoJson("AdminGetUser", """
|
||||
{ "UserPoolId": "%s", "Username": "%s" }
|
||||
""".formatted(poolId, sub));
|
||||
assertEquals(username, bySubUser.path("Username").asText(),
|
||||
"AdminGetUser with sub UUID should return the same user");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(51)
|
||||
void adminGetUserByEmailAlias() throws Exception {
|
||||
JsonNode byEmail = cognitoJson("AdminGetUser", """
|
||||
{ "UserPoolId": "%s", "Username": "%s" }
|
||||
""".formatted(poolId, username));
|
||||
assertEquals(username, byEmail.path("Username").asText());
|
||||
}
|
||||
|
||||
// ── Issue #233: ListUsers Filter ─────────────────────────────────────
|
||||
|
||||
@Test
|
||||
@Order(60)
|
||||
void listUsersFilterByEmailExactMatch() throws Exception {
|
||||
JsonNode resp = cognitoJson("ListUsers", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"Filter": "email = \\"%s\\""
|
||||
}
|
||||
""".formatted(poolId, username));
|
||||
assertEquals(1, resp.path("Users").size(),
|
||||
"Filter by email should return exactly one matching user");
|
||||
assertEquals(username, resp.path("Users").get(0).path("Username").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(61)
|
||||
void listUsersFilterByEmailPrefixStartsWith() throws Exception {
|
||||
String prefix = username.substring(0, 5);
|
||||
JsonNode resp = cognitoJson("ListUsers", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"Filter": "email ^= \\"%s\\""
|
||||
}
|
||||
""".formatted(poolId, prefix));
|
||||
assertTrue(resp.path("Users").size() >= 1,
|
||||
"Prefix filter should return at least the test user");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(62)
|
||||
void listUsersNoFilterReturnsAll() throws Exception {
|
||||
JsonNode resp = cognitoJson("ListUsers", """
|
||||
{ "UserPoolId": "%s" }
|
||||
""".formatted(poolId));
|
||||
assertTrue(resp.path("Users").size() >= 1);
|
||||
}
|
||||
|
||||
// ── Issue #234: GetTokensFromRefreshToken ────────────────────────────
|
||||
|
||||
@Test
|
||||
@Order(70)
|
||||
void getTokensFromRefreshTokenReturnsAccessAndIdToken() throws Exception {
|
||||
JsonNode authResp = cognitoJson("InitiateAuth", """
|
||||
{
|
||||
"ClientId": "%s",
|
||||
"AuthFlow": "USER_PASSWORD_AUTH",
|
||||
"AuthParameters": { "USERNAME": "%s", "PASSWORD": "%s" }
|
||||
}
|
||||
""".formatted(clientId, username, password));
|
||||
String refreshToken = authResp.path("AuthenticationResult").path("RefreshToken").asText();
|
||||
assertNotNull(refreshToken);
|
||||
|
||||
JsonNode refreshResp = cognitoJson("GetTokensFromRefreshToken", """
|
||||
{
|
||||
"ClientId": "%s",
|
||||
"RefreshToken": "%s"
|
||||
}
|
||||
""".formatted(clientId, refreshToken));
|
||||
|
||||
assertNotNull(refreshResp.path("AuthenticationResult").path("AccessToken").asText(null));
|
||||
assertNotNull(refreshResp.path("AuthenticationResult").path("IdToken").asText(null));
|
||||
assertTrue(refreshResp.path("AuthenticationResult").path("RefreshToken").isMissingNode(),
|
||||
"GetTokensFromRefreshToken should not return a new RefreshToken");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(71)
|
||||
void refreshTokenAuthFlowReturnsNewTokens() throws Exception {
|
||||
JsonNode authResp = cognitoJson("InitiateAuth", """
|
||||
{
|
||||
"ClientId": "%s",
|
||||
"AuthFlow": "USER_PASSWORD_AUTH",
|
||||
"AuthParameters": { "USERNAME": "%s", "PASSWORD": "%s" }
|
||||
}
|
||||
""".formatted(clientId, username, password));
|
||||
String refreshToken = authResp.path("AuthenticationResult").path("RefreshToken").asText();
|
||||
|
||||
JsonNode refreshed = cognitoJson("InitiateAuth", """
|
||||
{
|
||||
"ClientId": "%s",
|
||||
"AuthFlow": "REFRESH_TOKEN_AUTH",
|
||||
"AuthParameters": { "REFRESH_TOKEN": "%s" }
|
||||
}
|
||||
""".formatted(clientId, refreshToken));
|
||||
|
||||
assertNotNull(refreshed.path("AuthenticationResult").path("AccessToken").asText(null));
|
||||
assertNotNull(refreshed.path("AuthenticationResult").path("IdToken").asText(null));
|
||||
}
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────
|
||||
|
||||
private static Response cognitoAction(String action, String body) {
|
||||
@@ -380,6 +547,17 @@ class CognitoIntegrationTest {
|
||||
return signature.verify(Base64.getUrlDecoder().decode(padBase64(parts[2])));
|
||||
}
|
||||
|
||||
private static String initiateAuthAndGetAccessToken() throws Exception {
|
||||
JsonNode auth = cognitoJson("InitiateAuth", """
|
||||
{
|
||||
"ClientId": "%s",
|
||||
"AuthFlow": "USER_PASSWORD_AUTH",
|
||||
"AuthParameters": { "USERNAME": "%s", "PASSWORD": "%s" }
|
||||
}
|
||||
""".formatted(clientId, username, password));
|
||||
return auth.path("AuthenticationResult").path("AccessToken").asText();
|
||||
}
|
||||
|
||||
private static String padBase64(String value) {
|
||||
int remainder = value.length() % 4;
|
||||
if (remainder == 0) {
|
||||
|
||||
@@ -397,6 +397,254 @@ class CognitoServiceTest {
|
||||
() -> service.adminUserGlobalSignOut(pool.getId(), "ghost"));
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Issue #229 — password verification
|
||||
// =========================================================================
|
||||
|
||||
@Test
|
||||
void initiateAuthRejectsAnyPasswordWhenNoHashSet() {
|
||||
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
|
||||
service.adminCreateUser(pool.getId(), "bob", Map.of("email", "bob@example.com"), null);
|
||||
UserPoolClient client = service.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of());
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
|
||||
Map.of("USERNAME", "bob", "PASSWORD", "anything")));
|
||||
assertEquals("NotAuthorizedException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void initiateAuthWorksAfterPasswordIsSet() {
|
||||
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
|
||||
service.adminCreateUser(pool.getId(), "bob", Map.of("email", "bob@example.com"), null);
|
||||
service.adminSetUserPassword(pool.getId(), "bob", "Perm1!", true);
|
||||
UserPoolClient client = service.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of());
|
||||
|
||||
Map<String, Object> result = service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
|
||||
Map.of("USERNAME", "bob", "PASSWORD", "Perm1!"));
|
||||
assertNotNull(((Map<String, Object>) result.get("AuthenticationResult")).get("AccessToken"));
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Issue #235 — AdminSetUserPassword(Permanent=false) changes the password
|
||||
// =========================================================================
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void adminSetUserPasswordPermanentFalseChangesPassword() {
|
||||
UserPool pool = createPoolAndUser(); // alice has permanent "Perm1234!"
|
||||
UserPoolClient client = service.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of());
|
||||
|
||||
service.adminSetUserPassword(pool.getId(), "alice", "NewTemp1!", false);
|
||||
|
||||
// Old password now rejected
|
||||
assertThrows(AwsException.class, () ->
|
||||
service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
|
||||
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!")));
|
||||
|
||||
// New temp password triggers NEW_PASSWORD_REQUIRED challenge
|
||||
Map<String, Object> result = service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
|
||||
Map.of("USERNAME", "alice", "PASSWORD", "NewTemp1!"));
|
||||
assertEquals("NEW_PASSWORD_REQUIRED", result.get("ChallengeName"));
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Issue #228 — AccessToken contains client_id claim
|
||||
// =========================================================================
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void accessTokenContainsClientId() {
|
||||
UserPool pool = createPoolAndUser();
|
||||
UserPoolClient client = service.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of());
|
||||
|
||||
Map<String, Object> authResult = service.initiateAuth(
|
||||
client.getClientId(), "USER_PASSWORD_AUTH",
|
||||
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!"));
|
||||
Map<String, Object> auth = (Map<String, Object>) authResult.get("AuthenticationResult");
|
||||
String accessToken = (String) auth.get("AccessToken");
|
||||
|
||||
String payloadJson = new String(Base64.getUrlDecoder().decode(accessToken.split("\\.")[1]),
|
||||
StandardCharsets.UTF_8);
|
||||
assertTrue(payloadJson.contains("\"client_id\":\"" + client.getClientId() + "\""),
|
||||
"AccessToken should contain client_id claim matching the requesting client");
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void idTokenDoesNotContainClientId() {
|
||||
UserPool pool = createPoolAndUser();
|
||||
UserPoolClient client = service.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of());
|
||||
|
||||
Map<String, Object> authResult = service.initiateAuth(
|
||||
client.getClientId(), "USER_PASSWORD_AUTH",
|
||||
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!"));
|
||||
Map<String, Object> auth = (Map<String, Object>) authResult.get("AuthenticationResult");
|
||||
String idToken = (String) auth.get("IdToken");
|
||||
|
||||
String payloadJson = new String(Base64.getUrlDecoder().decode(idToken.split("\\.")[1]),
|
||||
StandardCharsets.UTF_8);
|
||||
assertFalse(payloadJson.contains("\"client_id\""),
|
||||
"IdToken should not contain client_id claim");
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Issue #220 — adminGetUser resolves sub UUID and email aliases
|
||||
// =========================================================================
|
||||
|
||||
@Test
|
||||
void adminGetUserBySubUuid() {
|
||||
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
|
||||
service.adminCreateUser(pool.getId(), "bob", Map.of("email", "bob@example.com"), null);
|
||||
|
||||
String sub = service.adminGetUser(pool.getId(), "bob").getAttributes().get("sub");
|
||||
assertNotNull(sub);
|
||||
|
||||
CognitoUser found = service.adminGetUser(pool.getId(), sub);
|
||||
assertEquals("bob", found.getUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminGetUserByEmailAlias() {
|
||||
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
|
||||
service.adminCreateUser(pool.getId(), "bob", Map.of("email", "bob@example.com"), null);
|
||||
|
||||
CognitoUser found = service.adminGetUser(pool.getId(), "bob@example.com");
|
||||
assertEquals("bob", found.getUsername());
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Issue #233 — listUsers Filter
|
||||
// =========================================================================
|
||||
|
||||
@Test
|
||||
void listUsersNoFilterReturnsAll() {
|
||||
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
|
||||
service.adminCreateUser(pool.getId(), "user1", Map.of("email", "user1@example.com"), null);
|
||||
service.adminCreateUser(pool.getId(), "user2", Map.of("email", "user2@example.com"), null);
|
||||
|
||||
assertEquals(2, service.listUsers(pool.getId(), null).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listUsersFilterBySubExactMatch() {
|
||||
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
|
||||
service.adminCreateUser(pool.getId(), "user1", Map.of("email", "user1@example.com"), null);
|
||||
service.adminCreateUser(pool.getId(), "user2", Map.of("email", "user2@example.com"), null);
|
||||
|
||||
String sub2 = service.adminGetUser(pool.getId(), "user2").getAttributes().get("sub");
|
||||
List<CognitoUser> result = service.listUsers(pool.getId(), "sub = \"" + sub2 + "\"");
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertEquals("user2", result.get(0).getUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listUsersFilterByEmailExactMatch() {
|
||||
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
|
||||
service.adminCreateUser(pool.getId(), "user1", Map.of("email", "user1@example.com"), null);
|
||||
service.adminCreateUser(pool.getId(), "user2", Map.of("email", "user2@example.com"), null);
|
||||
|
||||
List<CognitoUser> result = service.listUsers(pool.getId(), "email = \"user1@example.com\"");
|
||||
assertEquals(1, result.size());
|
||||
assertEquals("user1", result.get(0).getUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listUsersFilterByEmailPrefix() {
|
||||
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
|
||||
service.adminCreateUser(pool.getId(), "user1", Map.of("email", "alice@example.com"), null);
|
||||
service.adminCreateUser(pool.getId(), "user2", Map.of("email", "bob@example.com"), null);
|
||||
service.adminCreateUser(pool.getId(), "user3", Map.of("email", "alice2@example.com"), null);
|
||||
|
||||
List<CognitoUser> result = service.listUsers(pool.getId(), "email ^= \"alice\"");
|
||||
assertEquals(2, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listUsersFilterNoMatchReturnsEmpty() {
|
||||
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
|
||||
service.adminCreateUser(pool.getId(), "user1", Map.of("email", "user1@example.com"), null);
|
||||
|
||||
List<CognitoUser> result = service.listUsers(pool.getId(), "email = \"nobody@example.com\"");
|
||||
assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Issue #234 — GetTokensFromRefreshToken
|
||||
// =========================================================================
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void refreshTokenIsStructuredAndDecodable() {
|
||||
UserPool pool = createPoolAndUser();
|
||||
UserPoolClient client = service.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of());
|
||||
|
||||
Map<String, Object> authResult = service.initiateAuth(
|
||||
client.getClientId(), "USER_PASSWORD_AUTH",
|
||||
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!"));
|
||||
Map<String, Object> auth = (Map<String, Object>) authResult.get("AuthenticationResult");
|
||||
String refreshToken = (String) auth.get("RefreshToken");
|
||||
|
||||
assertNotNull(refreshToken);
|
||||
// Should be parseable as base64 structured token
|
||||
String decoded = new String(Base64.getDecoder().decode(refreshToken), StandardCharsets.UTF_8);
|
||||
String[] parts = decoded.split("\\|", 4);
|
||||
assertEquals(4, parts.length, "Refresh token should encode 4 pipe-separated fields");
|
||||
assertEquals(pool.getId(), parts[0]);
|
||||
assertEquals("alice", parts[1]);
|
||||
assertEquals(client.getClientId(), parts[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void getTokensFromRefreshTokenReturnsNewAccessAndIdTokens() {
|
||||
UserPool pool = createPoolAndUser();
|
||||
UserPoolClient client = service.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of());
|
||||
|
||||
Map<String, Object> authResult = service.initiateAuth(
|
||||
client.getClientId(), "USER_PASSWORD_AUTH",
|
||||
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!"));
|
||||
String refreshToken = (String) ((Map<String, Object>) authResult.get("AuthenticationResult")).get("RefreshToken");
|
||||
|
||||
Map<String, Object> refreshResult = service.getTokensFromRefreshToken(client.getClientId(), refreshToken);
|
||||
Map<String, Object> refreshAuth = (Map<String, Object>) refreshResult.get("AuthenticationResult");
|
||||
|
||||
assertNotNull(refreshAuth.get("AccessToken"), "Should return a new AccessToken");
|
||||
assertNotNull(refreshAuth.get("IdToken"), "Should return a new IdToken");
|
||||
assertNull(refreshAuth.get("RefreshToken"), "GetTokensFromRefreshToken should not return a new RefreshToken");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getTokensFromRefreshTokenInvalidTokenThrows() {
|
||||
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
|
||||
UserPoolClient client = service.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of());
|
||||
|
||||
assertThrows(AwsException.class, () ->
|
||||
service.getTokensFromRefreshToken(client.getClientId(), "not-a-valid-refresh-token"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void refreshTokenAuthFlowReturnsNewTokens() {
|
||||
UserPool pool = createPoolAndUser();
|
||||
UserPoolClient client = service.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of());
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> firstAuth = (Map<String, Object>) service.initiateAuth(
|
||||
client.getClientId(), "USER_PASSWORD_AUTH",
|
||||
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!")).get("AuthenticationResult");
|
||||
String refreshToken = (String) firstAuth.get("RefreshToken");
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> refreshed = (Map<String, Object>) service.initiateAuth(
|
||||
client.getClientId(), "REFRESH_TOKEN_AUTH",
|
||||
Map.of("REFRESH_TOKEN", refreshToken)).get("AuthenticationResult");
|
||||
|
||||
assertNotNull(refreshed.get("AccessToken"));
|
||||
assertNotNull(refreshed.get("IdToken"));
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// deleteUserPool cascades groups
|
||||
// =========================================================================
|
||||
|
||||
Reference in new issue
Block a user