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());
}
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());
}
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;
}
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();
}
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));
}
Aggregations