Search in sources :

Example 6 with UserSessionRepresentation

use of org.keycloak.representations.idm.UserSessionRepresentation in project keycloak by keycloak.

the class AuthzClientCredentialsTest method testReusingAccessAndRefreshTokens.

private void testReusingAccessAndRefreshTokens(int expectedUserSessionsCount) throws Exception {
    ClientsResource clients = getAdminClient().realm("authz-test-session").clients();
    ClientRepresentation clientRepresentation = clients.findByClientId("resource-server-test").get(0);
    List<UserSessionRepresentation> userSessions = clients.get(clientRepresentation.getId()).getUserSessions(-1, -1);
    assertEquals(0, userSessions.size());
    AuthzClient authzClient = getAuthzClient("default-session-keycloak.json");
    ProtectionResource protection = authzClient.protection();
    protection.resource().findByName("Default Resource");
    userSessions = clients.get(clientRepresentation.getId()).getUserSessions(null, null);
    assertEquals(expectedUserSessionsCount, userSessions.size());
    Thread.sleep(2000);
    protection = authzClient.protection();
    protection.resource().findByName("Default Resource");
    userSessions = clients.get(clientRepresentation.getId()).getUserSessions(null, null);
    assertEquals(expectedUserSessionsCount, userSessions.size());
}
Also used : UserSessionRepresentation(org.keycloak.representations.idm.UserSessionRepresentation) ProtectionResource(org.keycloak.authorization.client.resource.ProtectionResource) AuthzClient(org.keycloak.authorization.client.AuthzClient) ClientsResource(org.keycloak.admin.client.resource.ClientsResource) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation)

Example 7 with UserSessionRepresentation

use of org.keycloak.representations.idm.UserSessionRepresentation in project keycloak by keycloak.

the class AuthzClientCredentialsTest method testPermissionWhenResourceServerIsCurrentUser.

@Test
public void testPermissionWhenResourceServerIsCurrentUser() throws Exception {
    ClientsResource clients = getAdminClient().realm("authz-test-session").clients();
    ClientRepresentation clientRepresentation = clients.findByClientId("resource-server-test").get(0);
    List<UserSessionRepresentation> userSessions = clients.get(clientRepresentation.getId()).getUserSessions(-1, -1);
    assertEquals(0, userSessions.size());
    AuthzClient authzClient = getAuthzClient("default-session-keycloak.json");
    org.keycloak.authorization.client.resource.AuthorizationResource authorization = authzClient.authorization(authzClient.obtainAccessToken().getToken());
    AuthorizationResponse response = authorization.authorize();
    AccessToken accessToken = toAccessToken(response.getToken());
    assertEquals(1, accessToken.getAuthorization().getPermissions().size());
    assertEquals("Default Resource", accessToken.getAuthorization().getPermissions().iterator().next().getResourceName());
}
Also used : UserSessionRepresentation(org.keycloak.representations.idm.UserSessionRepresentation) AuthzClient(org.keycloak.authorization.client.AuthzClient) AccessToken(org.keycloak.representations.AccessToken) ClientsResource(org.keycloak.admin.client.resource.ClientsResource) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) AuthorizationResponse(org.keycloak.representations.idm.authorization.AuthorizationResponse) Test(org.junit.Test)

Example 8 with UserSessionRepresentation

use of org.keycloak.representations.idm.UserSessionRepresentation in project keycloak by keycloak.

the class ClientResource method toUserSessionRepresentation.

/**
 * Converts the specified {@link UserSessionModel} into a {@link UserSessionRepresentation}.
 *
 * @param userSession the model to be converted.
 * @return a reference to the constructed representation.
 */
private UserSessionRepresentation toUserSessionRepresentation(final UserSessionModel userSession) {
    UserSessionRepresentation rep = ModelToRepresentation.toRepresentation(userSession);
    // Update lastSessionRefresh with the timestamp from clientSession
    Map.Entry<String, AuthenticatedClientSessionModel> result = userSession.getAuthenticatedClientSessions().entrySet().stream().filter(entry -> Objects.equals(client.getId(), entry.getKey())).findFirst().orElse(null);
    if (result != null) {
        rep.setLastAccess(Time.toMillis(result.getValue().getTimestamp()));
    }
    return rep;
}
Also used : UserSessionRepresentation(org.keycloak.representations.idm.UserSessionRepresentation) AuthenticatedClientSessionModel(org.keycloak.models.AuthenticatedClientSessionModel) Map(java.util.Map) HashMap(java.util.HashMap)

Example 9 with UserSessionRepresentation

use of org.keycloak.representations.idm.UserSessionRepresentation in project keycloak by keycloak.

the class SessionTest method testGetUserSessions.

@Test
public void testGetUserSessions() {
    // List<java.util.Map<String, String>> stats = this.testRealmResource().getClientSessionStats();
    ClientResource account = findClientResourceById("account");
    testRealmAccountManagementPage.navigateTo();
    loginPage.form().login(testUser);
    List<UserSessionRepresentation> sessions = account.getUserSessions(0, 5);
    assertEquals(1, sessions.size());
    UserSessionRepresentation rep = sessions.get(0);
    UserRepresentation testUserRep = getFullUserRep(testUser.getUsername());
    assertEquals(testUserRep.getId(), rep.getUserId());
    assertEquals(testUserRep.getUsername(), rep.getUsername());
    String clientId = account.toRepresentation().getId();
    assertEquals("account", rep.getClients().get(clientId));
    assertNotNull(rep.getIpAddress());
    assertNotNull(rep.getLastAccess());
    assertNotNull(rep.getStart());
    testRealmAccountManagementPage.signOut();
}
Also used : UserSessionRepresentation(org.keycloak.representations.idm.UserSessionRepresentation) ClientResource(org.keycloak.admin.client.resource.ClientResource) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Example 10 with UserSessionRepresentation

use of org.keycloak.representations.idm.UserSessionRepresentation in project keycloak by keycloak.

the class ClientTest method offlineUserSessions.

@Test
public void offlineUserSessions() throws IOException {
    ClientRepresentation client = createAppClient();
    String id = client.getId();
    Response response = realm.users().create(UserBuilder.create().username("testuser").build());
    String userId = ApiUtil.getCreatedId(response);
    response.close();
    realm.users().get(userId).resetPassword(CredentialBuilder.create().password("password").build());
    Map<String, Long> offlineSessionCount = realm.clients().get(id).getOfflineSessionCount();
    assertEquals(new Long(0), offlineSessionCount.get("count"));
    List<UserSessionRepresentation> userSessions = realm.users().get(userId).getOfflineSessions(id);
    assertEquals("There should be no offline sessions", 0, userSessions.size());
    oauth.realm(REALM_NAME);
    oauth.redirectUri(client.getRedirectUris().get(0));
    oauth.scope(OAuth2Constants.OFFLINE_ACCESS);
    oauth.doLogin("testuser", "password");
    AccessTokenResponse accessTokenResponse = oauth.doAccessTokenRequest(oauth.getCurrentQuery().get("code"), "secret");
    assertEquals(200, accessTokenResponse.getStatusCode());
    offlineSessionCount = realm.clients().get(id).getOfflineSessionCount();
    assertEquals(new Long(1), offlineSessionCount.get("count"));
    List<UserSessionRepresentation> offlineUserSessions = realm.clients().get(id).getOfflineUserSessions(0, 100);
    assertEquals(1, offlineUserSessions.size());
    assertEquals("testuser", offlineUserSessions.get(0).getUsername());
    org.hamcrest.MatcherAssert.assertThat(offlineUserSessions.get(0).getLastAccess(), allOf(greaterThan(Time.currentTimeMillis() - 10000L), lessThan(Time.currentTimeMillis())));
    userSessions = realm.users().get(userId).getOfflineSessions(id);
    assertEquals("There should be one offline session", 1, userSessions.size());
    assertOfflineSession(offlineUserSessions.get(0), userSessions.get(0));
}
Also used : AccessTokenResponse(org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse) Response(javax.ws.rs.core.Response) UserSessionRepresentation(org.keycloak.representations.idm.UserSessionRepresentation) AccessTokenResponse(org.keycloak.testsuite.util.OAuthClient.AccessTokenResponse) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) 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