KEYCLOAK-738 Possible NPE in MemUserSessionProvider.getUserSessions

This commit is contained in:
mposolda 2014-10-07 18:38:08 +02:00
parent 2e76be45fa
commit 6522455b44
3 changed files with 17 additions and 19 deletions

View file

@ -121,7 +121,7 @@ public class MemUserSessionProvider implements UserSessionProvider {
for (ClientSessionEntity s : clientSessions.values()) { for (ClientSessionEntity s : clientSessions.values()) {
String realmId = realm.getId(); String realmId = realm.getId();
String clientId = client.getId(); String clientId = client.getId();
if (s.getSession().getRealm().equals(realmId) && s.getClientId().equals(clientId)) { if (s.getSession() != null && s.getSession().getRealm().equals(realmId) && s.getClientId().equals(clientId)) {
if (!userSessionEntities.contains(s.getSession())) { if (!userSessionEntities.contains(s.getSession())) {
userSessionEntities.add(s.getSession()); userSessionEntities.add(s.getSession());
} }

View file

@ -40,7 +40,6 @@ import org.keycloak.models.UserSessionModel;
import org.keycloak.protocol.oidc.OpenIDConnectService; import org.keycloak.protocol.oidc.OpenIDConnectService;
import org.keycloak.protocol.oidc.TokenManager; import org.keycloak.protocol.oidc.TokenManager;
import org.keycloak.representations.AccessToken; import org.keycloak.representations.AccessToken;
import org.keycloak.representations.adapters.action.SessionStats;
import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.services.managers.RealmManager; import org.keycloak.services.managers.RealmManager;
import org.keycloak.services.resources.admin.AdminRoot; import org.keycloak.services.resources.admin.AdminRoot;
@ -158,17 +157,17 @@ public class AdapterTest {
Client client = ClientBuilder.newClient(); Client client = ClientBuilder.newClient();
UriBuilder authBase = UriBuilder.fromUri("http://localhost:8081/auth"); UriBuilder authBase = UriBuilder.fromUri("http://localhost:8081/auth");
WebTarget adminTarget = client.target(AdminRoot.realmsUrl(authBase)).path("demo"); WebTarget adminTarget = client.target(AdminRoot.realmsUrl(authBase)).path("demo");
Map<String, SessionStats> stats = adminTarget.path("session-stats").request() Map<String, Integer> stats = adminTarget.path("application-session-stats").request()
.header(HttpHeaders.AUTHORIZATION, "Bearer " + adminToken) .header(HttpHeaders.AUTHORIZATION, "Bearer " + adminToken)
.get(new GenericType<Map<String, SessionStats>>() { .get(new GenericType<Map<String, Integer>>() {
}); });
SessionStats custStats = stats.get("customer-portal"); Integer custSessionsCount = stats.get("customer-portal");
Assert.assertNotNull(custStats); Assert.assertNotNull(custSessionsCount);
Assert.assertEquals(1, custStats.getActiveSessions()); Assert.assertTrue(1 == custSessionsCount);
SessionStats prodStats = stats.get("product-portal"); Integer prodStatsCount = stats.get("product-portal");
Assert.assertNotNull(prodStats); Assert.assertNotNull(prodStatsCount);
Assert.assertEquals(1, prodStats.getActiveSessions()); Assert.assertTrue(1 == prodStatsCount);
client.close(); client.close();

View file

@ -35,7 +35,6 @@ import org.keycloak.models.UserSessionModel;
import org.keycloak.protocol.oidc.OpenIDConnectService; import org.keycloak.protocol.oidc.OpenIDConnectService;
import org.keycloak.protocol.oidc.TokenManager; import org.keycloak.protocol.oidc.TokenManager;
import org.keycloak.representations.AccessToken; import org.keycloak.representations.AccessToken;
import org.keycloak.representations.adapters.action.SessionStats;
import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.services.managers.RealmManager; import org.keycloak.services.managers.RealmManager;
import org.keycloak.services.resources.admin.AdminRoot; import org.keycloak.services.resources.admin.AdminRoot;
@ -132,16 +131,16 @@ public class RelativeUriAdapterTest {
Client client = ClientBuilder.newClient(); Client client = ClientBuilder.newClient();
UriBuilder authBase = UriBuilder.fromUri("http://localhost:8081/auth"); UriBuilder authBase = UriBuilder.fromUri("http://localhost:8081/auth");
WebTarget adminTarget = client.target(AdminRoot.realmsUrl(authBase)).path("demo"); WebTarget adminTarget = client.target(AdminRoot.realmsUrl(authBase)).path("demo");
Map<String, SessionStats> stats = adminTarget.path("session-stats").request() Map<String, Integer> stats = adminTarget.path("application-session-stats").request()
.header(HttpHeaders.AUTHORIZATION, "Bearer " + adminToken) .header(HttpHeaders.AUTHORIZATION, "Bearer " + adminToken)
.get(new GenericType<Map<String, SessionStats>>(){}); .get(new GenericType<Map<String, Integer>>(){});
SessionStats custStats = stats.get("customer-portal"); Integer custSessionsCount = stats.get("customer-portal");
Assert.assertNotNull(custStats); Assert.assertNotNull(custSessionsCount);
Assert.assertEquals(1, custStats.getActiveSessions()); Assert.assertTrue(1 == custSessionsCount);
SessionStats prodStats = stats.get("product-portal"); Integer prodStatsCount = stats.get("product-portal");
Assert.assertNotNull(prodStats); Assert.assertNotNull(prodStatsCount);
Assert.assertEquals(1, prodStats.getActiveSessions()); Assert.assertTrue(1 == prodStatsCount);
client.close(); client.close();