Search in sources :

Example 16 with UserSessionRepresentation

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());
}
Also used : UserSessionRepresentation(org.keycloak.representations.idm.UserSessionRepresentation) OAuthClient(org.keycloak.testsuite.util.OAuthClient) AccessTokenResponse(org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse) UserResource(org.keycloak.admin.client.resource.UserResource) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest) Test(org.junit.Test)

Aggregations

UserSessionRepresentation (org.keycloak.representations.idm.UserSessionRepresentation)16 Test (org.junit.Test)11 ClientsResource (org.keycloak.admin.client.resource.ClientsResource)6 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)6 UserResource (org.keycloak.admin.client.resource.UserResource)4 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)4 Map (java.util.Map)3 AuthzClient (org.keycloak.authorization.client.AuthzClient)3 AuthenticatedClientSessionModel (org.keycloak.models.AuthenticatedClientSessionModel)3 URI (java.net.URI)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Objects (java.util.Objects)2 Response (javax.ws.rs.core.Response)2 RealmResource (org.keycloak.admin.client.resource.RealmResource)2 UsersResource (org.keycloak.admin.client.resource.UsersResource)2 ResponseType (org.keycloak.dom.saml.v2.protocol.ResponseType)2 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)2 OAuthClient (org.keycloak.testsuite.util.OAuthClient)2 AccessTokenResponse (org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse)2