Search in sources :

Example 1 with RolePoliciesResource

use of org.keycloak.admin.client.resource.RolePoliciesResource in project keycloak by keycloak.

the class ScopePermissionManagementTest 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();
    authorization.scopes().create(new ScopeRepresentation("Scope A"));
    authorization.scopes().create(new ScopeRepresentation("Scope B"));
    authorization.scopes().create(new ScopeRepresentation("Scope C"));
    ResourcesResource resources = authorization.resources();
    resources.create(new ResourceRepresentation("Resource A", "Scope A"));
    resources.create(new ResourceRepresentation("Resource B", "Scope B", "Scope C"));
}
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) ScopeRepresentation(org.keycloak.representations.idm.authorization.ScopeRepresentation) 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 2 with RolePoliciesResource

use of org.keycloak.admin.client.resource.RolePoliciesResource in project keycloak by keycloak.

the class RolePolicyManagementTest method testGenericConfig.

@Test
public void testGenericConfig() {
    AuthorizationResource authorization = getClient().authorization();
    RolePolicyRepresentation representation = new RolePolicyRepresentation();
    representation.setName("Test Generic Config  Permission");
    representation.addRole("Role A", false);
    RolePoliciesResource policies = authorization.policies().role();
    try (Response response = policies.create(representation)) {
        RolePolicyRepresentation created = response.readEntity(RolePolicyRepresentation.class);
        PolicyResource policy = authorization.policies().policy(created.getId());
        PolicyRepresentation genericConfig = policy.toRepresentation();
        assertNotNull(genericConfig.getConfig());
        assertNotNull(genericConfig.getConfig().get("roles"));
        RoleRepresentation role = getRealm().roles().get("Role A").toRepresentation();
        assertTrue(genericConfig.getConfig().get("roles").contains(role.getId()));
    }
}
Also used : Response(javax.ws.rs.core.Response) RolePolicyRepresentation(org.keycloak.representations.idm.authorization.RolePolicyRepresentation) PolicyRepresentation(org.keycloak.representations.idm.authorization.PolicyRepresentation) RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) RolePolicyRepresentation(org.keycloak.representations.idm.authorization.RolePolicyRepresentation) RolePolicyResource(org.keycloak.admin.client.resource.RolePolicyResource) PolicyResource(org.keycloak.admin.client.resource.PolicyResource) RolePoliciesResource(org.keycloak.admin.client.resource.RolePoliciesResource) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) Test(org.junit.Test)

Example 3 with RolePoliciesResource

use of org.keycloak.admin.client.resource.RolePoliciesResource in project keycloak by keycloak.

the class RolePolicyManagementTest method testUpdate.

@Test
public void testUpdate() {
    AuthorizationResource authorization = getClient().authorization();
    RolePolicyRepresentation representation = new RolePolicyRepresentation();
    representation.setName("Update Test Role Policy");
    representation.setDescription("description");
    representation.setDecisionStrategy(DecisionStrategy.CONSENSUS);
    representation.setLogic(Logic.NEGATIVE);
    representation.addRole("Role A", false);
    representation.addRole("Role B", true);
    representation.addRole("Role C", false);
    assertCreated(authorization, representation);
    representation.setName("changed");
    representation.setDescription("changed");
    representation.setDecisionStrategy(DecisionStrategy.AFFIRMATIVE);
    representation.setLogic(Logic.POSITIVE);
    representation.setRoles(representation.getRoles().stream().filter(roleDefinition -> !roleDefinition.getId().equals("Resource A")).collect(Collectors.toSet()));
    RolePoliciesResource policies = authorization.policies().role();
    RolePolicyResource permission = policies.findById(representation.getId());
    permission.update(representation);
    assertRepresentation(representation, permission);
    for (RolePolicyRepresentation.RoleDefinition roleDefinition : representation.getRoles()) {
        if (roleDefinition.getId().equals("Role B")) {
            roleDefinition.setRequired(false);
        }
        if (roleDefinition.getId().equals("Role C")) {
            roleDefinition.setRequired(true);
        }
    }
    permission.update(representation);
    assertRepresentation(representation, permission);
}
Also used : RolePolicyRepresentation(org.keycloak.representations.idm.authorization.RolePolicyRepresentation) RolePoliciesResource(org.keycloak.admin.client.resource.RolePoliciesResource) RolePolicyResource(org.keycloak.admin.client.resource.RolePolicyResource) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) Test(org.junit.Test)

Example 4 with RolePoliciesResource

use of org.keycloak.admin.client.resource.RolePoliciesResource in project keycloak by keycloak.

the class RolePolicyManagementTest method assertCreated.

private void assertCreated(AuthorizationResource authorization, RolePolicyRepresentation representation) {
    RolePoliciesResource permissions = authorization.policies().role();
    try (Response response = permissions.create(representation)) {
        RolePolicyRepresentation created = response.readEntity(RolePolicyRepresentation.class);
        RolePolicyResource permission = permissions.findById(created.getId());
        assertRepresentation(representation, permission);
    }
}
Also used : Response(javax.ws.rs.core.Response) RolePolicyRepresentation(org.keycloak.representations.idm.authorization.RolePolicyRepresentation) RolePoliciesResource(org.keycloak.admin.client.resource.RolePoliciesResource) RolePolicyResource(org.keycloak.admin.client.resource.RolePolicyResource)

Example 5 with RolePoliciesResource

use of org.keycloak.admin.client.resource.RolePoliciesResource in project keycloak by keycloak.

the class RolePolicyManagementTest method testDelete.

@Test
public void testDelete() {
    AuthorizationResource authorization = getClient().authorization();
    RolePolicyRepresentation representation = new RolePolicyRepresentation();
    representation.setName("Test Delete Permission");
    representation.addRole("Role A", false);
    RolePoliciesResource policies = authorization.policies().role();
    try (Response response = policies.create(representation)) {
        RolePolicyRepresentation created = response.readEntity(RolePolicyRepresentation.class);
        policies.findById(created.getId()).remove();
        RolePolicyResource removed = policies.findById(created.getId());
        try {
            removed.toRepresentation();
            fail("Permission not removed");
        } catch (NotFoundException ignore) {
        }
    }
}
Also used : Response(javax.ws.rs.core.Response) RolePolicyRepresentation(org.keycloak.representations.idm.authorization.RolePolicyRepresentation) RolePoliciesResource(org.keycloak.admin.client.resource.RolePoliciesResource) RolePolicyResource(org.keycloak.admin.client.resource.RolePolicyResource) NotFoundException(javax.ws.rs.NotFoundException) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) Test(org.junit.Test)

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