fix(cognito): add missing UserPoolClient validation (#1394)

* fix(cognito): add missing UserPoolClient validation

* fix(cognito): align user pool client token validity validation with aws
This commit is contained in:
shrimptails-f authored and GitHub committed 2026-06-18 17:56:33 -05:00
1 parent 892f9f7fb8
commit a922d07e77
3 files changed
+552 -30

No files matched your search

@@ -573,8 +573,11 @@ class CognitoIntegrationTest {
"UserPoolId": "%s",
"ClientName": "secret-client",
"GenerateSecret": true,
"AllowedOAuthFlowsUserPoolClient": true,
"AllowedOAuthFlows": ["code"],
"AllowedOAuthScopes": ["openid"]
"AllowedOAuthScopes": ["openid"],
"CallbackURLs": ["https://example.com/callback"],
"DefaultRedirectURI": "https://example.com/callback"
}
""".formatted(poolId));
String secretClientId = secretClient.path("UserPoolClient").path("ClientId").asText();
@@ -646,7 +649,9 @@ class CognitoIntegrationTest {
"ClientName": "updated-name",
"AllowedOAuthFlowsUserPoolClient": true,
"AllowedOAuthFlows": ["code", "implicit"],
"AllowedOAuthScopes": ["email", "openid"]
"AllowedOAuthScopes": ["email", "openid"],
"CallbackURLs": ["https://example.com/callback"],
"DefaultRedirectURI": "https://example.com/callback"
}
""".formatted(poolId, cid));
@@ -1600,7 +1605,7 @@ class CognitoIntegrationTest {
{
"UserPoolId": "%s",
"ClientId": "%s",
"AllowedOAuthFlowsUserPoolClient": false,
"AllowedOAuthFlowsUserPoolClient": true,
"AllowedOAuthFlows": ["implicit"],
"AllowedOAuthScopes": ["email"],
"AnalyticsConfiguration": {
@@ -1641,7 +1646,7 @@ class CognitoIntegrationTest {
""".formatted(extendedPoolId, extendedClientId));
JsonNode described = describeResp.path("UserPoolClient");
assertFalse(described.path("AllowedOAuthFlowsUserPoolClient").asBoolean());
assertTrue(described.path("AllowedOAuthFlowsUserPoolClient").asBoolean());
assertEquals(1, described.path("AllowedOAuthFlows").size());
assertEquals("implicit", described.path("AllowedOAuthFlows").get(0).asText());
assertEquals(1, described.path("AllowedOAuthScopes").size());
@@ -1675,6 +1680,84 @@ class CognitoIntegrationTest {
@Test
@Order(96)
void createUserPoolClientRejectsInvalidTokenValidityConfiguration() throws Exception {
JsonNode poolResponse = cognitoJson("CreateUserPool", """
{
"PoolName": "InvalidTokenValidityPool"
}
""");
String invalidPoolId = poolResponse.path("UserPool").path("Id").asText();
cognitoAction("CreateUserPoolClient", """
{
"UserPoolId": "%s",
"ClientName": "invalid-token-validity-client",
"AccessTokenValidity": -1,
"IdTokenValidity": 0,
"RefreshTokenValidity": -7,
"TokenValidityUnits": {
"AccessToken": "weeks",
"IdToken": "minutes",
"RefreshToken": "days"
}
}
""".formatted(invalidPoolId))
.then()
.statusCode(400);
}
@Test
@Order(97)
void createUserPoolClientRejectsInconsistentOAuthFlowConfiguration() throws Exception {
JsonNode poolResponse = cognitoJson("CreateUserPool", """
{
"PoolName": "InvalidOauthClientPool"
}
""");
String invalidPoolId = poolResponse.path("UserPool").path("Id").asText();
cognitoAction("CreateUserPoolClient", """
{
"UserPoolId": "%s",
"ClientName": "invalid-oauth-client",
"AllowedOAuthFlowsUserPoolClient": false,
"AllowedOAuthFlows": ["code"],
"AllowedOAuthScopes": ["openid"],
"CallbackURLs": ["https://example.com/callback"],
"DefaultRedirectURI": "https://example.com/callback"
}
""".formatted(invalidPoolId))
.then()
.statusCode(400);
}
@Test
@Order(98)
void createUserPoolClientRejectsDefaultRedirectUriNotInCallbackUrls() throws Exception {
JsonNode poolResponse = cognitoJson("CreateUserPool", """
{
"PoolName": "InvalidRedirectClientPool"
}
""");
String invalidPoolId = poolResponse.path("UserPool").path("Id").asText();
cognitoAction("CreateUserPoolClient", """
{
"UserPoolId": "%s",
"ClientName": "invalid-redirect-client",
"AllowedOAuthFlowsUserPoolClient": true,
"AllowedOAuthFlows": ["code"],
"AllowedOAuthScopes": ["openid"],
"CallbackURLs": ["https://example.com/callback"],
"DefaultRedirectURI": "https://different.example.com/callback"
}
""".formatted(invalidPoolId))
.then()
.statusCode(400);
}
@Test
@Order(99)
void configuredTokenValidityControlsAuthResultAndJwtExpiry() throws Exception {
JsonNode poolResponse = cognitoJson("CreateUserPool", """
{
@@ -207,7 +207,22 @@ class CognitoServiceTest {
true,
true,
List.of(" code ", "code", "implicit", "", "implicit"),
new ArrayList<>(java.util.Arrays.asList(" openid ", "openid", "email", null, "email"))
new ArrayList<>(java.util.Arrays.asList(" openid ", "openid", "email", null, "email")),
null,
List.of("https://example.com/callback"),
"https://example.com/callback",
List.of(),
null,
null,
List.of(),
null,
List.of(),
null,
List.of(),
null,
List.of(),
null,
null
);
assertNotNull(client.getClientId());
@@ -245,6 +260,215 @@ class CognitoServiceTest {
assertEquals(Boolean.TRUE, client.getEnableTokenRevocation());
}
@Test
void createUserPoolClientRejectsInvalidTokenValidityConfiguration() {
UserPool pool = service.createUserPool(Map.of("PoolName", "ClientPool"), "us-east-1");
AwsException exception = assertThrows(
AwsException.class,
() -> service.createUserPoolClient(
pool.getId(),
"invalid-token-validity-client",
false,
false,
List.of(),
List.of(),
null,
List.of(),
null,
List.of(),
-1,
0,
List.of(),
null,
List.of(),
-7,
List.of(),
Map.of(
"AccessToken", "weeks",
"IdToken", "minutes",
"RefreshToken", "days"
),
List.of(),
null,
null
)
);
assertEquals("InvalidParameterException", exception.getErrorCode());
}
@Test
void createUserPoolClientAcceptsRefreshTokenValidityZeroAndCoercesToDefault() {
UserPool pool = service.createUserPool(Map.of("PoolName", "ClientPool"), "us-east-1");
UserPoolClient client = service.createUserPoolClient(
pool.getId(),
"refresh-default-client",
false,
false,
List.of(),
List.of(),
null,
List.of(),
null,
List.of(),
null,
null,
List.of(),
null,
List.of(),
0,
List.of(),
null,
List.of(),
null,
null
);
assertEquals(30, client.getRefreshTokenValidity());
}
@Test
void createUserPoolClientRejectsLogoutUrlsWhenOAuthFlowsUserPoolClientIsFalse() {
UserPool pool = service.createUserPool(Map.of("PoolName", "ClientPool"), "us-east-1");
AwsException exception = assertThrows(
AwsException.class,
() -> service.createUserPoolClient(
pool.getId(),
"invalid-logout-client",
false,
false,
List.of(),
List.of(),
null,
List.of(),
null,
List.of(),
null,
null,
List.of("https://example.com/logout"),
null,
List.of(),
null,
List.of(),
null,
List.of(),
null,
null
)
);
assertEquals("InvalidParameterException", exception.getErrorCode());
}
@Test
void createUserPoolClientRejectsMixedCaseTokenValidityUnits() {
UserPool pool = service.createUserPool(Map.of("PoolName", "ClientPool"), "us-east-1");
AwsException exception = assertThrows(
AwsException.class,
() -> service.createUserPoolClient(
pool.getId(),
"invalid-token-unit-client",
false,
false,
List.of(),
List.of(),
null,
List.of(),
null,
List.of(),
1,
1,
List.of(),
null,
List.of(),
7,
List.of(),
Map.of(
"AccessToken", "Hours",
"IdToken", "minutes",
"RefreshToken", "days"
),
List.of(),
null,
null
)
);
assertEquals("InvalidParameterException", exception.getErrorCode());
}
@Test
void createUserPoolClientRejectsInconsistentOAuthFlowConfiguration() {
UserPool pool = service.createUserPool(Map.of("PoolName", "ClientPool"), "us-east-1");
AwsException exception = assertThrows(
AwsException.class,
() -> service.createUserPoolClient(
pool.getId(),
"invalid-oauth-client",
false,
false,
List.of("code"),
List.of("openid"),
null,
List.of("https://example.com/callback"),
"https://example.com/callback",
List.of(),
null,
null,
List.of(),
null,
List.of(),
null,
List.of(),
null,
List.of(),
null,
null
)
);
assertEquals("InvalidParameterException", exception.getErrorCode());
}
@Test
void createUserPoolClientRejectsDefaultRedirectUriNotInCallbackUrls() {
UserPool pool = service.createUserPool(Map.of("PoolName", "ClientPool"), "us-east-1");
AwsException exception = assertThrows(
AwsException.class,
() -> service.createUserPoolClient(
pool.getId(),
"invalid-redirect-client",
false,
true,
List.of("code"),
List.of("openid"),
null,
List.of("https://example.com/callback"),
"https://different.example.com/callback",
List.of(),
null,
null,
List.of(),
null,
List.of(),
null,
List.of(),
null,
List.of(),
null,
null
)
);
assertEquals("InvalidParameterException", exception.getErrorCode());
}
@Test
void updateUserPoolClientAllowsClearingListFieldsWithEmptyArrays() {
UserPool pool = service.createUserPool(Map.of("PoolName", "ClientPool"), "us-east-1");
@@ -253,9 +477,9 @@ class CognitoServiceTest {
pool.getId(),
"client",
false,
false,
List.of(),
List.of(),
true,
List.of("code"),
List.of("openid"),
null,
List.of("https://example.com"),
"https://example.com",
@@ -277,13 +501,13 @@ class CognitoServiceTest {
pool.getId(),
client.getClientId(),
null,
null,
null,
null,
null,
false,
List.of(),
List.of(),
null,
List.of(),
"",
List.of(),
null,
null,
List.of(),
@@ -306,6 +530,61 @@ class CognitoServiceTest {
assertEquals(List.of(), updated.getWriteAttributes());
}
@Test
void updateUserPoolClientAcceptsRefreshTokenValidityZeroAndCoercesToDefault() {
UserPool pool = service.createUserPool(Map.of("PoolName", "ClientPool"), "us-east-1");
UserPoolClient client = service.createUserPoolClient(
pool.getId(),
"client",
false,
false,
List.of(),
List.of(),
null,
List.of(),
null,
List.of(),
null,
null,
List.of(),
null,
List.of(),
7,
List.of(),
null,
List.of(),
null,
null
);
UserPoolClient updated = service.updateUserPoolClient(
pool.getId(),
client.getClientId(),
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
0,
null,
null,
null,
null,
null
);
assertEquals(30, updated.getRefreshTokenValidity());
}
@Test
void createUserPoolWithBlankOverrideIdThrowsValidation() {
AwsException exception = assertThrows(