use of org.keycloak.admin.client.resource.RolePoliciesResource in project keycloak by keycloak.
the class AbstractServletAuthzAdapterTest method testRequiredRole.
@Test
public void testRequiredRole() throws Exception {
performTests(() -> {
login("jdoe", "jdoe");
navigateToUserPremiumPage();
assertWasNotDenied();
RolesResource rolesResource = getClientResource(RESOURCE_SERVER_ID).roles();
rolesResource.create(new RoleRepresentation("required-role", "", false));
RolePolicyRepresentation policy = new RolePolicyRepresentation();
policy.setName("Required Role Policy");
policy.addRole("user_premium", false);
policy.addRole(RESOURCE_SERVER_ID + "/required-role", false);
RolePoliciesResource rolePolicy = getAuthorizationResource().policies().role();
rolePolicy.create(policy);
policy = rolePolicy.findByName(policy.getName());
updatePermissionPolicies("Premium Resource Permission", policy.getName());
login("jdoe", "jdoe");
navigateToUserPremiumPage();
assertWasNotDenied();
policy.getRoles().clear();
policy.addRole("user_premium", false);
policy.addRole(RESOURCE_SERVER_ID + "/required-role", true);
rolePolicy.findById(policy.getId()).update(policy);
login("jdoe", "jdoe");
navigateToUserPremiumPage();
assertWasDenied();
UsersResource users = realmsResouce().realm(REALM_NAME).users();
UserRepresentation user = users.search("jdoe").get(0);
RoleScopeResource roleScopeResource = users.get(user.getId()).roles().clientLevel(getClientResource(RESOURCE_SERVER_ID).toRepresentation().getId());
RoleRepresentation requiredRole = rolesResource.get("required-role").toRepresentation();
roleScopeResource.add(Arrays.asList(requiredRole));
login("jdoe", "jdoe");
navigateToUserPremiumPage();
assertWasNotDenied();
policy.getRoles().clear();
policy.addRole("user_premium", false);
policy.addRole(RESOURCE_SERVER_ID + "/required-role", false);
rolePolicy.findById(policy.getId()).update(policy);
login("jdoe", "jdoe");
navigateToUserPremiumPage();
assertWasNotDenied();
roleScopeResource.remove(Arrays.asList(requiredRole));
login("jdoe", "jdoe");
navigateToUserPremiumPage();
assertWasNotDenied();
});
}
use of org.keycloak.admin.client.resource.RolePoliciesResource in project keycloak by keycloak.
the class ResourcePermissionManagementTest method configureTest.
@Before
public void configureTest() {
super.configureTest();
RolesResource realmRoles = testRealmResource().roles();
realmRoles.create(new RoleRepresentation("Role A", "", false));
realmRoles.create(new RoleRepresentation("Role B", "", false));
RolePolicyRepresentation policyA = new RolePolicyRepresentation();
policyA.setName("Policy A");
policyA.addRole("Role A");
AuthorizationResource authorization = testRealmResource().clients().get(newClient.getId()).authorization();
PoliciesResource policies = authorization.policies();
RolePoliciesResource roles = policies.role();
roles.create(policyA);
RolePolicyRepresentation policyB = new RolePolicyRepresentation();
policyB.setName("Policy B");
policyB.addRole("Role B");
roles.create(policyB);
UserPolicyRepresentation policyC = new UserPolicyRepresentation();
policyC.setName("Policy C");
policyC.addUser("test");
policies.user().create(policyC).close();
ResourcesResource resources = authorization.resources();
resources.create(new ResourceRepresentation("Resource A"));
resources.create(new ResourceRepresentation("Resource B"));
}
use of org.keycloak.admin.client.resource.RolePoliciesResource in project keycloak by keycloak.
the class AggregatePolicyManagementTest method configureTest.
@Before
public void configureTest() {
super.configureTest();
RealmResource realmResource = testRealmResource();
RolesResource realmRoles = realmResource.roles();
realmRoles.create(new RoleRepresentation("Role A", "", false));
realmRoles.create(new RoleRepresentation("Role B", "", false));
UsersResource users = realmResource.users();
users.create(UserBuilder.create().username("user a").build());
ClientsResource clients = realmResource.clients();
clients.create(ClientBuilder.create().clientId("client a").build());
realmResource.groups().add(GroupBuilder.create().name("Group A").build());
RolePolicyRepresentation policyA = new RolePolicyRepresentation();
policyA.setName("Policy A");
policyA.addRole("Role A");
AuthorizationResource authorization = realmResource.clients().get(newClient.getId()).authorization();
PoliciesResource policies = authorization.policies();
RolePoliciesResource roles = policies.role();
roles.create(policyA);
RolePolicyRepresentation policyB = new RolePolicyRepresentation();
policyB.setName("Policy B");
policyB.addRole("Role B");
roles.create(policyB);
UserPolicyRepresentation policyC = new UserPolicyRepresentation();
policyC.setName("Policy C");
policyC.addUser("test");
policies.user().create(policyC).close();
}
Aggregations