Skip to content

armgit / floci

Public Imported from GitHub
0

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

feat: emit jti/origin_jti and support RevokeToken (#1705)

Abhigya28252763f527
diff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.java
index 4869427f32..3f3ade3bb3 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.java
@@ -92,6 +92,7 @@ public class CognitoJsonHandler {
case "AdminRemoveUserFromGroup" -> handleAdminRemoveUserFromGroup(request);
case "AdminListGroupsForUser" -> handleAdminListGroupsForUser(request);
case "GetTokensFromRefreshToken" -> handleGetTokensFromRefreshToken(request);
+ case "RevokeToken" -> handleRevokeToken(request);
case "ListUserPoolClientSecrets" -> handleListUserPoolClientSecrets(request);
case "AddUserPoolClientSecret" -> handleAddUserPoolClientSecret(request);
case "DeleteUserPoolClientSecret" -> handleDeleteUserPoolClientSecret(request);
@@ -451,6 +452,15 @@ public class CognitoJsonHandler {
return Response.ok(objectMapper.valueToTree(result)).build();
}
+ private Response handleRevokeToken(JsonNode request) {
+ service.revokeToken(
+ request.path("ClientId").asText(null),
+ request.path("Token").asText(null),
+ request.path("ClientSecret").asText(null)
+ );
+ return Response.ok(objectMapper.createObjectNode()).build();
+ }
+
private Response handleInitiateAuth(JsonNode request) {
Map<String, String> params = new HashMap<>();
request.path("AuthParameters").fields().forEachRemaining(e -> params.put(e.getKey(), e.getValue().asText()));
diff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java
index 37efd3579b..78716df602 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java
@@ -1309,13 +1309,13 @@ public class CognitoService {
String username = extractUsernameFromToken(accessToken);
String poolId = extractPoolIdFromToken(accessToken);
String jti = extractJtiFromToken(accessToken);
-
+
if (username == null || poolId == null) {
throw new AwsException("NotAuthorizedException", "Invalid access token", 400);
}
-
- // Check if access token has been revoked
+
validateTokenNotRevoked(jti, poolId, "access");
+ validateOriginJtiNotRevoked(accessToken, poolId);
Long iat = extractIatFromToken(accessToken);
validateUserNotGloballySignedOut(username, poolId, "access", iat != null ? iat : 0L);
@@ -1373,13 +1373,13 @@ public class CognitoService {
String username = extractUsernameFromToken(accessToken);
String poolId = extractPoolIdFromToken(accessToken);
String jti = extractJtiFromToken(accessToken);
-
+
if (username == null || poolId == null || jti == null) {
throw new AwsException("NotAuthorizedException", "Invalid access token", 400);
}
-
- // Check if access token has been revoked
+
validateTokenNotRevoked(jti, poolId, "access");
+ validateOriginJtiNotRevoked(accessToken, poolId);
Long iat = extractIatFromToken(accessToken);
validateUserNotGloballySignedOut(username, poolId, "access", iat != null ? iat : 0L);
@@ -1396,13 +1396,13 @@ public class CognitoService {
String username = extractUsernameFromToken(accessToken);
String poolId = extractPoolIdFromToken(accessToken);
String jti = extractJtiFromToken(accessToken);
-
+
if (username == null || poolId == null) {
throw new AwsException("NotAuthorizedException", "Invalid access token", 400);
}
-
- // Check if access token has been revoked
+
validateTokenNotRevoked(jti, poolId, "access");
+ validateOriginJtiNotRevoked(accessToken, poolId);
Long iat = extractIatFromToken(accessToken);
validateUserNotGloballySignedOut(username, poolId, "access", iat != null ? iat : 0L);
@@ -1413,13 +1413,13 @@ public class CognitoService {
String username = extractUsernameFromToken(accessToken);
String poolId = extractPoolIdFromToken(accessToken);
String jti = extractJtiFromToken(accessToken);
-
+
if (username == null || poolId == null) {
throw new AwsException("NotAuthorizedException", "Invalid access token", 400);
}
-
- // Check if access token has been revoked
+
validateTokenNotRevoked(jti, poolId, "access");
+ validateOriginJtiNotRevoked(accessToken, poolId);
Long iat = extractIatFromToken(accessToken);
validateUserNotGloballySignedOut(username, poolId, "access", iat != null ? iat : 0L);
@@ -1516,15 +1516,63 @@ public class CognitoService {
return result;
}
+ public void revokeToken(String clientId, String token, String clientSecret) {
+ if (token == null || token.isBlank()) {
+ throw new AwsException("InvalidParameterException", "Token is required", 400);
+ }
+ if (clientId == null || clientId.isBlank()) {
+ throw new AwsException("InvalidParameterException", "ClientId is required", 400);
+ }
+
+ UserPoolClient client = findClientById(clientId);
+
+ // Authenticate the caller before disclosing configuration state: a confidential client must
+ // present a valid secret first, matching AWS which validates identity ahead of feature checks.
+ String secret = client.getClientSecret();
+ if (secret != null && !secret.isBlank() && !secret.equals(clientSecret)) {
+ throw new AwsException("UnauthorizedException", "Invalid client secret", 403);
+ }
+
+ if (!isTokenRevocationEnabled(client)) {
+ throw new AwsException("UnsupportedOperationException",
+ "Please enable token revocation before revoking tokens for this client", 400);
+ }
+
+ String[] parts = parseRefreshToken(token);
+ if (parts == null) {
+ throw new AwsException("UnsupportedTokenTypeException",
+ "Only refresh tokens can be revoked", 400);
+ }
+
+ String poolId = parts[0];
+ String username = parts[1];
+ String tokenClientId = parts[2];
+ String familyId = parts.length > 4 && !parts[4].isEmpty() ? parts[4] : null;
+
+ if (!clientId.equals(tokenClientId)) {
+ throw new AwsException("UnauthorizedException", "Refresh token was not issued to this client", 403);
+ }
+ if (familyId == null) {
+ return; // Nothing keyed to revoke (legacy token); revocation is idempotent.
+ }
+
+ long nowMs = System.currentTimeMillis();
+ long expiresAtSeconds = nowMs / 1000L + (365L * 24L * 60L * 60L);
+ RevokedTokenInfo info = new RevokedTokenInfo(familyId, "refresh", username, poolId, nowMs, expiresAtSeconds);
+ revokedTokenStore.put(revokedTokenKey(poolId, familyId), info);
+ LOG.infov("RevokeToken: revoked refresh token family {0} for user {1} in pool {2}", familyId, username, poolId);
+ }
+
Map<String, Object> generateAuthResult(CognitoUser user, UserPool pool, UserPoolClient client, ClaimsOverride override) {
- return generateAuthResult(user, pool, client, override, null);
+ String originJti = UUID.randomUUID().toString();
+ return generateAuthResult(user, pool, client, override, originJti);
}
Map<String, Object> generateAuthResult(CognitoUser user, UserPool pool, UserPoolClient client, ClaimsOverride override, String originJti) {
Map<String, Object> auth = new HashMap<>();
auth.put("AccessToken", generateSignedJwt(user, pool, "access", client, override, originJti));
auth.put("IdToken", generateSignedJwt(user, pool, "id", client, override, originJti));
- auth.put("RefreshToken", buildRefreshToken(pool.getId(), user.getUsername(), client.getClientId()));
+ auth.put("RefreshToken", buildRefreshToken(pool.getId(), user.getUsername(), client.getClientId(), originJti));
auth.put("ExpiresIn", resolveAccessTokenLifetimeSeconds(client));
auth.put("TokenType", "Bearer");
return auth;
@@ -1557,8 +1605,7 @@ public class CognitoService {
String jti = UUID.randomUUID().toString();
claims.put("jti", jti);
- // Add origin_jti for access and ID tokens derived from refresh tokens
- if (("access".equals(type) || "id".equals(type)) && originJti != null) {
+ if (("access".equals(type) || "id".equals(type)) && originJti != null && isTokenRevocationEnabled(client)) {
claims.put("origin_jti", originJti);
}
@@ -2115,12 +2162,18 @@ public class CognitoService {
}
}
- String buildRefreshToken(String poolId, String username, String clientId) {
+ String buildRefreshToken(String poolId, String username, String clientId, String originJti) {
long issuedAt = System.currentTimeMillis();
- String raw = poolId + "|" + username + "|" + clientId + "|" + issuedAt + "|" + UUID.randomUUID();
+ String familyId = originJti != null ? originJti : UUID.randomUUID().toString();
+ String raw = poolId + "|" + username + "|" + clientId + "|" + issuedAt + "|" + familyId;
return Base64.getEncoder().withoutPadding().encodeToString(raw.getBytes(StandardCharsets.UTF_8));
}
+ private boolean isTokenRevocationEnabled(UserPoolClient client) {
+ return client == null || client.getEnableTokenRevocation() == null
+ || Boolean.TRUE.equals(client.getEnableTokenRevocation());
+ }
+
String[] parseRefreshToken(String refreshToken) {
try {
byte[] decoded = Base64.getDecoder().decode(refreshToken);
@@ -2228,6 +2281,17 @@ public class CognitoService {
}
}
+ private String extractOriginJtiFromToken(String token) {
+ try {
+ String[] parts = token.split("\\.");
+ if (parts.length < 2) return null;
+ String payloadJson = new String(Base64.getUrlDecoder().decode(parts[1]), StandardCharsets.UTF_8);
+ return extractJsonField(payloadJson, "origin_jti");
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
private Long extractIatFromToken(String token) {
try {
String[] parts = token.split("\\.");
@@ -2295,6 +2359,13 @@ public class CognitoService {
}
}
+ private void validateOriginJtiNotRevoked(String accessToken, String poolId) {
+ String originJti = extractOriginJtiFromToken(accessToken);
+ if (originJti != null) {
+ validateTokenNotRevoked(originJti, poolId, "access");
+ }
+ }
+
/**
* Check if a user has been globally signed out (affects all their tokens).
* This method should be called in addition to validateTokenNotRevoked.
diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/OriginJtiIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/OriginJtiIntegrationTest.java
new file mode 100644
index 0000000000..ec9e4cde5e
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/cognito/OriginJtiIntegrationTest.java
@@ -0,0 +1,365 @@
+package io.github.hectorvent.floci.services.cognito;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
+import io.quarkus.test.junit.QuarkusTest;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Order;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestMethodOrder;
+
+import java.util.Base64;
+
+import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoAction;
+import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoJson;
+import static org.junit.jupiter.api.Assertions.*;
+
+/**
+ * Integration tests for the Cognito-native {@code jti} and {@code origin_jti} token claims.
+ * <p>
+ * Real AWS Cognito stamps every access and ID token with a unique {@code jti} and a per
+ * refresh-token-family {@code origin_jti} whenever token revocation is enabled on the app
+ * client (the default). Downstream stateless authorizers key their deny-list on
+ * {@code origin_jti}, so it must stay identical across a refresh of the same session and
+ * differ between independent logins.
+ * <p>
+ * Provisioning runs in an {@code @Order(1)} test rather than {@code @BeforeAll} because a
+ * {@code @QuarkusTest} HTTP endpoint is only guaranteed reachable inside test methods. The
+ * remaining ordered tests form a session lifecycle (login → refresh → revoke); each guards
+ * the shared state it depends on via {@link #requireSetup()} / {@link #requireInitialAuthState()}
+ * so a failure in an earlier step reports a clear cause instead of a misleading assertion.
+ */
+@QuarkusTest
+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
+class OriginJtiIntegrationTest {
+
+ private static final ObjectMapper MAPPER = new ObjectMapper();
+
+ private static String poolId;
+ private static String clientId;
+ private static String refreshToken;
+
+ private static String originJti1;
+ private static String accessJti1;
+
+ private static final String USERNAME = "bob";
+ private static final String PASSWORD = "Password123!";
+
+ @BeforeAll
+ static void configureRestAssured() {
+ RestAssuredJsonUtils.configureAwsContentTypes();
+ }
+
+ // ─── helpers ────────────────────────────────────────────────────────────
+
+ /** Decodes the JWT payload (second segment) into a JSON tree. */
+ private static JsonNode decodeJwtPayload(String jwt) throws Exception {
+ String[] segments = jwt.split("\\.");
+ assertEquals(3, segments.length, "JWT must have three dot-separated segments");
+ byte[] payload = Base64.getUrlDecoder().decode(segments[1]);
+ return MAPPER.readTree(payload);
+ }
+
+ /** POSTs without asserting 200 so error responses can be inspected. */
+ private static JsonNode cognitoJsonAny(String action, String body) throws Exception {
+ return MAPPER.readTree(cognitoAction(action, body).then().extract().asString());
+ }
+
+ private static JsonNode passwordLogin() throws Exception {
+ return cognitoJson("InitiateAuth", """
+ {
+ "ClientId": "%s",
+ "AuthFlow": "USER_PASSWORD_AUTH",
+ "AuthParameters": {"USERNAME": "%s", "PASSWORD": "%s"}
+ }
+ """.formatted(clientId, USERNAME, PASSWORD));
+ }
+
+ /** Guards the pool/client provisioned by setupPoolAndUser so a setup failure reads clearly. */
+ private static void requireSetup() {
+ assertNotNull(poolId, "setupPoolAndUser (Order 1) must run first to create the user pool");
+ assertNotNull(clientId, "setupPoolAndUser (Order 1) must run first to create the app client");
+ }
+
+ /** Guards the state handed forward by the initial-auth test so an earlier failure reads clearly. */
+ private static void requireInitialAuthState() {
+ assertNotNull(refreshToken, "initialAuthEmitsJtiAndOriginJti (Order 2) must run first to set refreshToken");
+ assertFalse(refreshToken.isBlank(), "refreshToken from the initial authentication must not be blank");
+ assertNotNull(originJti1, "initialAuthEmitsJtiAndOriginJti (Order 2) must run first to set origin_jti");
+ }
+
+ // ─── setup ──────────────────────────────────────────────────────────────
+
+ @Test
+ @Order(1)
+ void setupPoolAndUser() throws Exception {
+ JsonNode pool = cognitoJson("CreateUserPool", """
+ {"PoolName":"OriginJtiTestPool"}
+ """);
+ poolId = pool.path("UserPool").path("Id").asText();
+ assertFalse(poolId.isBlank(), "Pool ID must not be blank");
+
+ JsonNode client = cognitoJson("CreateUserPoolClient", """
+ {
+ "UserPoolId": "%s",
+ "ClientName": "origin-jti-client",
+ "ExplicitAuthFlows": ["ALLOW_USER_PASSWORD_AUTH", "ALLOW_REFRESH_TOKEN_AUTH"]
+ }
+ """.formatted(poolId));
+ clientId = client.path("UserPoolClient").path("ClientId").asText();
+ assertFalse(clientId.isBlank(), "Client ID must not be blank");
+
+ cognitoAction("AdminCreateUser", """
+ {"UserPoolId":"%s","Username":"%s"}
+ """.formatted(poolId, USERNAME)).then().statusCode(200);
+
+ cognitoAction("AdminSetUserPassword", """
+ {"UserPoolId":"%s","Username":"%s","Password":"%s","Permanent":true}
+ """.formatted(poolId, USERNAME, PASSWORD)).then().statusCode(200);
+ }
+
+ @Test
+ @Order(2)
+ void initialAuthEmitsJtiAndOriginJti() throws Exception {
+ requireSetup();
+ JsonNode auth = passwordLogin();
+ JsonNode result = auth.path("AuthenticationResult");
+
+ refreshToken = result.path("RefreshToken").asText();
+ assertFalse(refreshToken.isBlank(), "RefreshToken must be present after sign-in");
+
+ JsonNode access = decodeJwtPayload(result.path("AccessToken").asText());
+ JsonNode id = decodeJwtPayload(result.path("IdToken").asText());
+
+ assertFalse(access.path("jti").asText().isBlank(), "Access token must contain jti");
+ assertFalse(access.path("origin_jti").asText().isBlank(), "Access token must contain origin_jti");
+ assertFalse(id.path("jti").asText().isBlank(), "ID token must contain jti");
+ assertFalse(id.path("origin_jti").asText().isBlank(), "ID token must contain origin_jti");
+
+ assertEquals(access.path("origin_jti").asText(), id.path("origin_jti").asText(),
+ "Access and ID tokens from the same authentication must share origin_jti");
+ assertNotEquals(access.path("jti").asText(), id.path("jti").asText(),
+ "Access and ID tokens must each carry their own unique jti");
+
+ originJti1 = access.path("origin_jti").asText();
+ accessJti1 = access.path("jti").asText();
+ }
+
+ @Test
+ @Order(3)
+ void describeClientReportsEnableTokenRevocation() throws Exception {
+ requireSetup();
+ JsonNode described = cognitoJson("DescribeUserPoolClient", """
+ {"UserPoolId":"%s","ClientId":"%s"}
+ """.formatted(poolId, clientId));
+
+ assertTrue(described.path("UserPoolClient").path("EnableTokenRevocation").asBoolean(),
+ "EnableTokenRevocation should default to true and be reported by DescribeUserPoolClient");
+ }
+
+ @Test
+ @Order(4)
+ void refreshKeepsOriginJtiStableAndRotatesJti() throws Exception {
+ requireSetup();
+ requireInitialAuthState();
+ JsonNode refreshed = cognitoJson("InitiateAuth", """
+ {
+ "ClientId": "%s",
+ "AuthFlow": "REFRESH_TOKEN_AUTH",
+ "AuthParameters": {"REFRESH_TOKEN": "%s"}
+ }
+ """.formatted(clientId, refreshToken));
+
+ JsonNode access = decodeJwtPayload(refreshed.path("AuthenticationResult").path("AccessToken").asText());
+
+ assertEquals(originJti1, access.path("origin_jti").asText(),
+ "origin_jti must stay identical across a refresh of the same session");
+ assertNotEquals(accessJti1, access.path("jti").asText(),
+ "jti must be freshly generated on every token issuance");
+ }
+
+ @Test
+ @Order(5)
+ void getTokensFromRefreshTokenKeepsOriginJti() throws Exception {
+ requireSetup();
+ requireInitialAuthState();
+ JsonNode refreshed = cognitoJson("GetTokensFromRefreshToken", """
+ {"ClientId":"%s","RefreshToken":"%s"}
+ """.formatted(clientId, refreshToken));
+
+ JsonNode access = decodeJwtPayload(refreshed.path("AuthenticationResult").path("AccessToken").asText());
+ assertEquals(originJti1, access.path("origin_jti").asText(),
+ "origin_jti must also stay stable through the GetTokensFromRefreshToken path");
+ }
+
+ @Test
+ @Order(6)
+ void secondLoginUsesDifferentOriginJti() throws Exception {
+ requireSetup();
+ requireInitialAuthState();
+ JsonNode auth = passwordLogin();
+ JsonNode access = decodeJwtPayload(auth.path("AuthenticationResult").path("AccessToken").asText());
+
+ assertFalse(access.path("origin_jti").asText().isBlank(), "Second login must also carry origin_jti");
+ assertNotEquals(originJti1, access.path("origin_jti").asText(),
+ "Each independent login must start a new revocation family with a distinct origin_jti");
+ }
+
+ @Test
+ @Order(7)
+ void clientWithRevocationDisabledOmitsOriginJti() throws Exception {
+ requireSetup();
+ JsonNode client = cognitoJson("CreateUserPoolClient", """
+ {
+ "UserPoolId": "%s",
+ "ClientName": "no-revocation-client",
+ "ExplicitAuthFlows": ["ALLOW_USER_PASSWORD_AUTH", "ALLOW_REFRESH_TOKEN_AUTH"],
+ "EnableTokenRevocation": false
+ }
+ """.formatted(poolId));
+ String disabledClientId = client.path("UserPoolClient").path("ClientId").asText();
+
+ assertFalse(client.path("UserPoolClient").path("EnableTokenRevocation").asBoolean(),
+ "EnableTokenRevocation=false must round-trip through CreateUserPoolClient");
+
+ JsonNode auth = cognitoJson("InitiateAuth", """
+ {
+ "ClientId": "%s",
+ "AuthFlow": "USER_PASSWORD_AUTH",
+ "AuthParameters": {"USERNAME": "%s", "PASSWORD": "%s"}
+ }
+ """.formatted(disabledClientId, USERNAME, PASSWORD));
+
+ JsonNode access = decodeJwtPayload(auth.path("AuthenticationResult").path("AccessToken").asText());
+ assertFalse(access.path("jti").asText().isBlank(), "jti is always emitted");
+ assertTrue(access.path("origin_jti").isMissingNode(),
+ "origin_jti must be omitted when token revocation is disabled on the client");
+ }
+
+ @Test
+ @Order(8)
+ void revokeTokenInvalidatesRefreshFamily() throws Exception {
+ requireSetup();
+ JsonNode auth = passwordLogin();
+ String freshRefresh = auth.path("AuthenticationResult").path("RefreshToken").asText();
+
+ // Sanity: the fresh refresh token works before revocation.
+ JsonNode ok = cognitoJson("InitiateAuth", """
+ {"ClientId":"%s","AuthFlow":"REFRESH_TOKEN_AUTH","AuthParameters":{"REFRESH_TOKEN":"%s"}}
+ """.formatted(clientId, freshRefresh));
+ assertFalse(ok.path("AuthenticationResult").path("AccessToken").asText().isBlank(),
+ "Refresh must work before RevokeToken");
+
+ cognitoAction("RevokeToken", """
+ {"ClientId":"%s","Token":"%s"}
+ """.formatted(clientId, freshRefresh)).then().statusCode(200);
+
+ JsonNode rejected = cognitoJsonAny("InitiateAuth", """
+ {"ClientId":"%s","AuthFlow":"REFRESH_TOKEN_AUTH","AuthParameters":{"REFRESH_TOKEN":"%s"}}
+ """.formatted(clientId, freshRefresh));
+ assertEquals("NotAuthorizedException", rejected.path("__type").asText(),
+ "REFRESH_TOKEN_AUTH must fail after RevokeToken, body was: " + rejected);
+
+ JsonNode rejected2 = cognitoJsonAny("GetTokensFromRefreshToken", """
+ {"ClientId":"%s","RefreshToken":"%s"}
+ """.formatted(clientId, freshRefresh));
+ assertEquals("NotAuthorizedException", rejected2.path("__type").asText(),
+ "GetTokensFromRefreshToken must also fail after RevokeToken");
+ }
+
+ @Test
+ @Order(9)
+ void revokeTokenRejectsNonRefreshToken() throws Exception {
+ requireSetup();
+ JsonNode body = cognitoJsonAny("RevokeToken", """
+ {"ClientId":"%s","Token":"not-a-refresh-token"}
+ """.formatted(clientId));
+ assertEquals("UnsupportedTokenTypeException", body.path("__type").asText(),
+ "Only refresh tokens may be revoked, body was: " + body);
+ }
+
+ @Test
+ @Order(10)
+ void revokeTokenUnsupportedWhenRevocationDisabled() throws Exception {
+ requireSetup();
+ JsonNode client = cognitoJson("CreateUserPoolClient", """
+ {
+ "UserPoolId": "%s",
+ "ClientName": "revoke-disabled-client",
+ "ExplicitAuthFlows": ["ALLOW_USER_PASSWORD_AUTH", "ALLOW_REFRESH_TOKEN_AUTH"],
+ "EnableTokenRevocation": false
+ }
+ """.formatted(poolId));
+ String disabledClientId = client.path("UserPoolClient").path("ClientId").asText();
+
+ JsonNode auth = cognitoJson("InitiateAuth", """
+ {
+ "ClientId": "%s",
+ "AuthFlow": "USER_PASSWORD_AUTH",
+ "AuthParameters": {"USERNAME": "%s", "PASSWORD": "%s"}
+ }
+ """.formatted(disabledClientId, USERNAME, PASSWORD));
+ String disabledRefresh = auth.path("AuthenticationResult").path("RefreshToken").asText();
+
+ JsonNode body = cognitoJsonAny("RevokeToken", """
+ {"ClientId":"%s","Token":"%s"}
+ """.formatted(disabledClientId, disabledRefresh));
+ assertEquals("UnsupportedOperationException", body.path("__type").asText(),
+ "RevokeToken must be rejected when the client disables token revocation, body was: " + body);
+ }
+
+ @Test
+ @Order(11)
+ void revokeTokenChecksClientSecretBeforeRevocationConfig() throws Exception {
+ requireSetup();
+ // Confidential client with revocation disabled: an invalid secret must be rejected with
+ // NotAuthorizedException *before* the UnsupportedOperationException config check, so that
+ // revocation state is never disclosed to an unauthenticated caller (matches AWS ordering).
+ JsonNode client = cognitoJson("CreateUserPoolClient", """
+ {
+ "UserPoolId": "%s",
+ "ClientName": "confidential-disabled-client",
+ "GenerateSecret": true,
+ "ExplicitAuthFlows": ["ALLOW_USER_PASSWORD_AUTH", "ALLOW_REFRESH_TOKEN_AUTH"],
+ "EnableTokenRevocation": false
+ }
+ """.formatted(poolId));
+ String confidentialClientId = client.path("UserPoolClient").path("ClientId").asText();
+ assertFalse(client.path("UserPoolClient").path("ClientSecret").asText().isBlank(),
+ "Confidential client must be issued a secret");
+
+ JsonNode body = cognitoJsonAny("RevokeToken", """
+ {"ClientId":"%s","Token":"any-token","ClientSecret":"wrong-secret"}
+ """.formatted(confidentialClientId));
+ assertEquals("UnauthorizedException", body.path("__type").asText(),
+ "Caller identity must be validated before the revocation-enabled check, body was: " + body);
+ }
+
+ @Test
+ @Order(12)
+ void revokeTokenInvalidatesAccessTokens() throws Exception {
+ requireSetup();
+ JsonNode auth = passwordLogin();
+ String freshAccess = auth.path("AuthenticationResult").path("AccessToken").asText();
+ String freshRefresh = auth.path("AuthenticationResult").path("RefreshToken").asText();
+
+ // GetUser must succeed before revocation.
+ JsonNode ok = cognitoJsonAny("GetUser", """
+ {"AccessToken":"%s"}
+ """.formatted(freshAccess));
+ assertFalse(ok.has("__type"), "GetUser must succeed before RevokeToken, body was: " + ok);
+
+ cognitoAction("RevokeToken", """
+ {"ClientId":"%s","Token":"%s"}
+ """.formatted(clientId, freshRefresh)).then().statusCode(200);
+
+ // Access token issued alongside the revoked refresh token must now be rejected.
+ JsonNode rejected = cognitoJsonAny("GetUser", """
+ {"AccessToken":"%s"}
+ """.formatted(freshAccess));
+ assertEquals("NotAuthorizedException", rejected.path("__type").asText(),
+ "GetUser must fail after RevokeToken invalidates the origin_jti family, body was: " + rejected);
+ }
+}