Search in sources :

Example 6 with RolePoliciesResource

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();
    });
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) RolePolicyRepresentation(org.keycloak.representations.idm.authorization.RolePolicyRepresentation) RoleScopeResource(org.keycloak.admin.client.resource.RoleScopeResource) UsersResource(org.keycloak.admin.client.resource.UsersResource) RolePoliciesResource(org.keycloak.admin.client.resource.RolePoliciesResource) RolesResource(org.keycloak.admin.client.resource.RolesResource) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Example 7 with RolePoliciesResource

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"));
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) RolePolicyRepresentation(org.keycloak.representations.idm.authorization.RolePolicyRepresentation) UserPolicyRepresentation(org.keycloak.representations.idm.authorization.UserPolicyRepresentation) RolePoliciesResource(org.keycloak.admin.client.resource.RolePoliciesResource) PoliciesResource(org.keycloak.admin.client.resource.PoliciesResource) RolePoliciesResource(org.keycloak.admin.client.resource.RolePoliciesResource) RolesResource(org.keycloak.admin.client.resource.RolesResource) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) ResourcesResource(org.keycloak.admin.client.resource.ResourcesResource) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) Before(org.junit.Before)

Example 8 with RolePoliciesResource

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();
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) RolePolicyRepresentation(org.keycloak.representations.idm.authorization.RolePolicyRepresentation) RealmResource(org.keycloak.admin.client.resource.RealmResource) UserPolicyRepresentation(org.keycloak.representations.idm.authorization.UserPolicyRepresentation) UsersResource(org.keycloak.admin.client.resource.UsersResource) ClientsResource(org.keycloak.admin.client.resource.ClientsResource) RolePoliciesResource(org.keycloak.admin.client.resource.RolePoliciesResource) PoliciesResource(org.keycloak.admin.client.resource.PoliciesResource) RolePoliciesResource(org.keycloak.admin.client.resource.RolePoliciesResource) RolesResource(org.keycloak.admin.client.resource.RolesResource) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) Before(org.junit.Before)

Aggregations

RolePoliciesResource (org.keycloak.admin.client.resource.RolePoliciesResource)8 RolePolicyRepresentation (org.keycloak.representations.idm.authorization.RolePolicyRepresentation)8 AuthorizationResource (org.keycloak.admin.client.resource.AuthorizationResource)6 RoleRepresentation (org.keycloak.representations.idm.RoleRepresentation)5 Test (org.junit.Test)4 RolePolicyResource (org.keycloak.admin.client.resource.RolePolicyResource)4 RolesResource (org.keycloak.admin.client.resource.RolesResource)4 Response (javax.ws.rs.core.Response)3 Before (org.junit.Before)3 PoliciesResource (org.keycloak.admin.client.resource.PoliciesResource)3 UserPolicyRepresentation (org.keycloak.representations.idm.authorization.UserPolicyRepresentation)3 ResourcesResource (org.keycloak.admin.client.resource.ResourcesResource)2 UsersResource (org.keycloak.admin.client.resource.UsersResource)2 ResourceRepresentation (org.keycloak.representations.idm.authorization.ResourceRepresentation)2 NotFoundException (javax.ws.rs.NotFoundException)1 ClientsResource (org.keycloak.admin.client.resource.ClientsResource)1 PolicyResource (org.keycloak.admin.client.resource.PolicyResource)1 RealmResource (org.keycloak.admin.client.resource.RealmResource)1 RoleScopeResource (org.keycloak.admin.client.resource.RoleScopeResource)1 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)1