use of org.keycloak.representations.idm.UserSessionRepresentation in project keycloak by keycloak.
the class TokenRevocationTest method testRevokeToken.
@Test
public void testRevokeToken() throws Exception {
oauth.clientSessionState("client-session");
OAuthClient.AccessTokenResponse tokenResponse1 = login("test-app", "test-user@localhost", "password");
OAuthClient.AccessTokenResponse tokenResponse2 = login("test-app-scope", "test-user@localhost", "password");
UserResource testUser = realm.users().get(realm.users().search("test-user@localhost").get(0).getId());
List<UserSessionRepresentation> userSessions = testUser.getUserSessions();
assertEquals(1, userSessions.size());
Map<String, String> clients = userSessions.get(0).getClients();
assertEquals("test-app", clients.get(realm.clients().findByClientId("test-app").get(0).getId()));
assertEquals("test-app-scope", clients.get(realm.clients().findByClientId("test-app-scope").get(0).getId()));
isTokenEnabled(tokenResponse1, "test-app");
isTokenEnabled(tokenResponse2, "test-app-scope");
oauth.clientId("test-app");
CloseableHttpResponse response = oauth.doTokenRevoke(tokenResponse1.getRefreshToken(), "refresh_token", "password");
assertThat(response, Matchers.statusCodeIsHC(Status.OK));
userSessions = testUser.getUserSessions();
assertEquals(1, userSessions.size());
clients = userSessions.get(0).getClients();
assertNull(clients.get(realm.clients().findByClientId("test-app").get(0).getId()));
assertEquals("test-app-scope", clients.get(realm.clients().findByClientId("test-app-scope").get(0).getId()));
isTokenDisabled(tokenResponse1, "test-app");
isTokenEnabled(tokenResponse2, "test-app-scope");
// Revoke second token and assert no sessions for testUser
response = oauth.doTokenRevoke(tokenResponse2.getRefreshToken(), "refresh_token", "password");
assertThat(response, Matchers.statusCodeIsHC(Status.OK));
userSessions = testUser.getUserSessions();
assertEquals(0, userSessions.size());
}
Aggregations