100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Merge commit from fork
Eric Bariauxde89b8d3af27
diff --git a/manager/src/main/java/org/openremote/manager/security/UserResourceImpl.java b/manager/src/main/java/org/openremote/manager/security/UserResourceImpl.javaindex 7cffaa6fc..9f94ea961 100644--- a/manager/src/main/java/org/openremote/manager/security/UserResourceImpl.java+++ b/manager/src/main/java/org/openremote/manager/security/UserResourceImpl.java@@ -100,6 +100,8 @@ public class UserResourceImpl extends ManagerWebResource implements UserResource@Overridepublic User get(RequestParams requestParams, String realm, String userId) {+ throwIfCannotReadUserInRealm(realm, userId);+boolean hasAdminReadRole = hasResourceRole(ClientRole.READ_ADMIN.getValue(), Constants.KEYCLOAK_CLIENT_ID);if (!hasAdminReadRole && !Objects.equals(getUserId(), userId)) {@@ -122,7 +124,7 @@ public class UserResourceImpl extends ManagerWebResource implements UserResourceif (!isAuthenticated()) {throw new ForbiddenException("Must be authenticated");}- return get(requestParams, getRequestRealmName(), getUserId());+ return get(requestParams, getAuthenticatedRealmName(), getUserId());}@Override@@ -278,7 +280,7 @@ public class UserResourceImpl extends ManagerWebResource implements UserResourcethrow new ForbiddenException("Must be authenticated");}- return getUserClientRoles(requestParams, getRequestRealmName(), getUserId(), clientId);+ return getUserClientRoles(requestParams, getAuthenticatedRealmName(), getUserId(), clientId);}@Override@@ -287,11 +289,13 @@ public class UserResourceImpl extends ManagerWebResource implements UserResourcethrow new ForbiddenException("Must be authenticated");}- return getUserRealmRoles(requestParams, getRequestRealmName(), getUserId());+ return getUserRealmRoles(requestParams, getAuthenticatedRealmName(), getUserId());}@Overridepublic String[] getUserClientRoles(@BeanParam RequestParams requestParams, String realm, String userId, String clientId) {+ throwIfCannotReadUserInRealm(realm, userId);+boolean hasAdminReadRole = hasResourceRole(ClientRole.READ_ADMIN.getValue(), Constants.KEYCLOAK_CLIENT_ID);if (!hasAdminReadRole && !Objects.equals(getUserId(), userId)) {@@ -311,6 +315,8 @@ public class UserResourceImpl extends ManagerWebResource implements UserResource@Overridepublic String[] getUserRealmRoles(RequestParams requestParams, String realm, String userId) {+ throwIfCannotReadUserInRealm(realm, userId);+boolean hasAdminReadRole = hasResourceRole(ClientRole.READ_ADMIN.getValue(), Constants.KEYCLOAK_CLIENT_ID);if (!hasAdminReadRole && !Objects.equals(getUserId(), userId)) {@@ -365,6 +371,8 @@ public class UserResourceImpl extends ManagerWebResource implements UserResource@Overridepublic Role[] getClientRoles(RequestParams requestParams, String realm, String clientId) {+ throwIfCannotAdminRealm(realm);+try {return identityService.getIdentityProvider().getClientRoles(realm,@@ -416,6 +424,8 @@ public class UserResourceImpl extends ManagerWebResource implements UserResource@Overridepublic UserSession[] getUserSessions(RequestParams requestParams, String realm, String userId) {+ throwIfCannotReadUserInRealm(realm, userId);+boolean hasAdminReadRole = hasResourceRole(ClientRole.READ_ADMIN.getValue(), Constants.KEYCLOAK_CLIENT_ID);if (!hasAdminReadRole && !Objects.equals(getUserId(), userId)) {@@ -491,6 +501,14 @@ public class UserResourceImpl extends ManagerWebResource implements UserResource}}+ protected void throwIfCannotReadUserInRealm(String realm, String userId) throws WebApplicationException {+ throwIfCannotAdminRealm(realm);++ if (!identityService.getIdentityProvider().isUserInRealm(userId, realm)) {+ throw new NotAllowedException("Cannot read a user in a different realm");+ }+ }+protected Map.Entry<String, String> getCurrentUserAuthInfo() {// Get the current authenticated user informationAuthContext authContext = getAuthContext();diff --git a/test/src/test/groovy/org/openremote/test/users/UserResourceTest.groovy b/test/src/test/groovy/org/openremote/test/users/UserResourceTest.groovyindex 18156ee7f..49002f844 100644--- a/test/src/test/groovy/org/openremote/test/users/UserResourceTest.groovy+++ b/test/src/test/groovy/org/openremote/test/users/UserResourceTest.groovy@@ -97,6 +97,18 @@ class UserResourceTest extends Specification implements ManagerContainerTrait {"testuser3")+ def adminBuildingRoles = identityProvider.getUserClientRoles(+ keycloakTestSetup.realmBuilding.name,+ keycloakTestSetup.testuser4Id,+ KEYCLOAK_CLIENT_ID+ )+ identityProvider.updateUserClientRoles(+ keycloakTestSetup.realmBuilding.name,+ keycloakTestSetup.testuser4Id,+ KEYCLOAK_CLIENT_ID,+ ((adminBuildingRoles as List) + ClientRole.READ_ADMIN.value).unique() as String[]+ )+def adminBuildingAccessToken = authenticate(container,keycloakTestSetup.realmBuilding.name,@@ -265,6 +277,12 @@ class UserResourceTest extends Specification implements ManagerContainerTrait {then: "a not allowed exception should be thrown"thrown(ForbiddenException.class)+ when: "a realm admin tries to read client roles from the master realm"+ adminUserBuildingResource.getClientRoles(null, MASTER_REALM, KEYCLOAK_CLIENT_ID)++ then: "the cross-realm read should be rejected"+ thrown(NotAllowedException)+when: "a new composite role is created by the admin user"List<Role> updatedRoles = new ArrayList<>(Arrays.asList(roles))updatedRoles.add(new Role(@@ -377,6 +395,107 @@ class UserResourceTest extends Specification implements ManagerContainerTrait {testUser2BuildingAccessToken != null}+ def "Get user requires realm administration rights"() {+ when: "a realm admin retrieves a user from their own realm"+ def sameRealmUser = adminUserBuildingResource.get(null, keycloakTestSetup.realmBuilding.name, keycloakTestSetup.testuser2Id)++ then: "the user should be returned"+ sameRealmUser != null+ sameRealmUser.id == keycloakTestSetup.testuser2Id+ sameRealmUser.realm == keycloakTestSetup.realmBuilding.name++ when: "a superuser retrieves a user across realms"+ def crossRealmUser = adminUserResource.get(null, keycloakTestSetup.realmBuilding.name, keycloakTestSetup.testuser2Id)++ then: "the cross-realm read should succeed for the superuser"+ crossRealmUser != null+ crossRealmUser.id == keycloakTestSetup.testuser2Id++ when: "a realm admin tries to retrieve a user from the master realm"+ adminUserBuildingResource.get(null, MASTER_REALM, keycloakTestSetup.testuser1Id)++ then: "the cross-realm read should be rejected"+ thrown(NotAllowedException)++ when: "a realm admin uses their own realm path with a user ID from another realm"+ adminUserBuildingResource.get(null, keycloakTestSetup.realmBuilding.name, keycloakTestSetup.testuser1Id)++ then: "the mismatched user realm should be rejected"+ thrown(NotAllowedException)+ }++ def "Get user client roles requires realm administration rights"() {+ when: "a realm admin retrieves client roles for a user in their own realm"+ def sameRealmRoles = adminUserBuildingResource.getUserClientRoles(null, keycloakTestSetup.realmBuilding.name, keycloakTestSetup.testuser2Id, KEYCLOAK_CLIENT_ID)++ then: "the roles should be returned"+ sameRealmRoles as Set == [+ ClientRole.WRITE_USER.value,+ ClientRole.READ_MAP.value,+ ClientRole.READ_ASSETS.value+ ] as Set++ when: "a superuser retrieves client roles across realms"+ def crossRealmRoles = adminUserResource.getUserClientRoles(null, keycloakTestSetup.realmBuilding.name, keycloakTestSetup.testuser2Id, KEYCLOAK_CLIENT_ID)++ then: "the cross-realm read should succeed for the superuser"+ crossRealmRoles as Set == sameRealmRoles as Set++ when: "a realm admin tries to retrieve client roles for a user in the master realm"+ adminUserBuildingResource.getUserClientRoles(null, MASTER_REALM, keycloakTestSetup.testuser1Id, KEYCLOAK_CLIENT_ID)++ then: "the cross-realm read should be rejected"+ thrown(NotAllowedException)+ }++ def "Get user realm roles requires realm administration rights"() {+ when: "a realm admin retrieves realm roles for a user in their own realm"+ def sameRealmRoles = adminUserBuildingResource.getUserRealmRoles(null, keycloakTestSetup.realmBuilding.name, keycloakTestSetup.testuser3Id)++ then: "the roles should be returned"+ sameRealmRoles.contains(KeycloakTestSetup.REALM_ROLE_TEST)++ when: "a superuser retrieves realm roles across realms"+ def crossRealmRoles = adminUserResource.getUserRealmRoles(null, keycloakTestSetup.realmBuilding.name, keycloakTestSetup.testuser3Id)++ then: "the cross-realm read should succeed for the superuser"+ crossRealmRoles == sameRealmRoles++ when: "a realm admin tries to retrieve realm roles for a user in the master realm"+ adminUserBuildingResource.getUserRealmRoles(null, MASTER_REALM, keycloakTestSetup.testuser1Id)++ then: "the cross-realm read should be rejected"+ thrown(NotAllowedException)+ }++ def "Get user sessions requires realm administration rights"() {+ when: "a realm admin retrieves sessions for a user in their own realm"+ def sameRealmSessions = adminUserBuildingResource.getUserSessions(null, keycloakTestSetup.realmBuilding.name, keycloakTestSetup.testuser2Id)++ then: "the sessions should be returned"+ sameRealmSessions != null++ when: "a superuser retrieves sessions across realms"+ def crossRealmSessions = adminUserResource.getUserSessions(null, keycloakTestSetup.realmBuilding.name, keycloakTestSetup.testuser2Id)++ then: "the cross-realm read should succeed for the superuser"+ crossRealmSessions != null++ when: "a realm admin tries to retrieve sessions for a user in the master realm"+ adminUserBuildingResource.getUserSessions(null, MASTER_REALM, keycloakTestSetup.testuser1Id)++ then: "the cross-realm read should be rejected"+ thrown(NotAllowedException)+ }++ def "Get user sessions rejects users outside the requested realm"() {+ when: "a realm admin uses their own realm path with a user ID from another realm"+ adminUserBuildingResource.getUserSessions(null, keycloakTestSetup.realmBuilding.name, keycloakTestSetup.testuser1Id)++ then: "the mismatched user realm should be rejected"+ thrown(NotAllowedException)+ }+def "Request password reset"() {when: "a user with ${Constants.WRITE_USER_ROLE} permission requests a password reset for themselves"regularUserBuildingResource.requestPasswordResetCurrent(null)