use of org.keycloak.admin.client.resource.AuthorizationResource in project keycloak by keycloak.
the class ClientPolicyManagementTest method testUpdate.
@Test
public void testUpdate() {
AuthorizationResource authorization = getClient().authorization();
ClientPolicyRepresentation representation = new ClientPolicyRepresentation();
representation.setName("Update Test Client Policy");
representation.setDescription("description");
representation.setDecisionStrategy(DecisionStrategy.CONSENSUS);
representation.setLogic(Logic.NEGATIVE);
representation.addClient("Client A");
representation.addClient("Client B");
representation.addClient("Client C");
assertCreated(authorization, representation);
representation.setName("changed");
representation.setDescription("changed");
representation.setDecisionStrategy(DecisionStrategy.AFFIRMATIVE);
representation.setLogic(Logic.POSITIVE);
representation.setClients(representation.getClients().stream().filter(userName -> !userName.equals("Client A")).collect(Collectors.toSet()));
ClientPoliciesResource policies = authorization.policies().client();
ClientPolicyResource permission = policies.findById(representation.getId());
permission.update(representation);
assertRepresentation(representation, permission);
representation.setClients(representation.getClients().stream().filter(userName -> !userName.equals("Client C")).collect(Collectors.toSet()));
permission.update(representation);
assertRepresentation(representation, permission);
}
use of org.keycloak.admin.client.resource.AuthorizationResource in project keycloak by keycloak.
the class ExportAuthorizationSettingsTest method testRoleBasedPolicyWithMultipleRoles.
// KEYCLOAK-4983
@Test
public void testRoleBasedPolicyWithMultipleRoles() {
ClientResource clientResource = getClientResource();
AuthorizationResource authorizationResource = clientResource.authorization();
testRealmResource().clients().create(ClientBuilder.create().clientId("test-client-1").build()).close();
testRealmResource().clients().create(ClientBuilder.create().clientId("test-client-2").build()).close();
ClientRepresentation client1 = getClientByClientId("test-client-1");
ClientRepresentation client2 = getClientByClientId("test-client-2");
testRealmResource().clients().get(client1.getId()).roles().create(RoleBuilder.create().name("client-role").build());
testRealmResource().clients().get(client2.getId()).roles().create(RoleBuilder.create().name("client-role").build());
RoleRepresentation role1 = testRealmResource().clients().get(client1.getId()).roles().get("client-role").toRepresentation();
RoleRepresentation role2 = testRealmResource().clients().get(client2.getId()).roles().get("client-role").toRepresentation();
PolicyRepresentation policy = new PolicyRepresentation();
policy.setName("role-based-policy");
policy.setType("role");
Map<String, String> config = new HashMap<>();
config.put("roles", "[{\"id\":\"" + role1.getId() + "\"},{\"id\":\"" + role2.getId() + "\"}]");
policy.setConfig(config);
try (Response create = authorizationResource.policies().create(policy)) {
Assert.assertEquals(Status.CREATED, create.getStatusInfo());
}
// export authorization settings
ResourceServerRepresentation exportSettings = authorizationResource.exportSettings();
boolean found = false;
for (PolicyRepresentation p : exportSettings.getPolicies()) {
if (p.getName().equals("role-based-policy")) {
found = true;
Assert.assertTrue(p.getConfig().get("roles").contains("test-client-1/client-role") && p.getConfig().get("roles").contains("test-client-2/client-role"));
}
}
if (!found) {
Assert.fail("Policy \"role-based-policy\" was not found in exported settings.");
}
}
use of org.keycloak.admin.client.resource.AuthorizationResource in project keycloak by keycloak.
the class ExportAuthorizationSettingsTest method testResourceBasedPermission.
// KEYCLOAK-4341
@Test
public void testResourceBasedPermission() throws Exception {
String permissionName = "resource-based-permission";
ClientResource clientResource = getClientResource();
AuthorizationResource authorizationResource = clientResource.authorization();
// get Default Resource
List<ResourceRepresentation> resources = authorizationResource.resources().findByName("Default Resource");
Assert.assertTrue(resources.size() == 1);
ResourceRepresentation resource = resources.get(0);
// get Default Policy
PolicyRepresentation policy = authorizationResource.policies().findByName("Default Policy");
// create Resource-based permission and add default policy/resource
ResourcePermissionRepresentation permission = new ResourcePermissionRepresentation();
permission.setName(permissionName);
permission.addPolicy(policy.getId());
permission.addResource(resource.getId());
Response create = authorizationResource.permissions().resource().create(permission);
try {
Assert.assertEquals(Status.CREATED, create.getStatusInfo());
} finally {
create.close();
}
// export authorization settings
ResourceServerRepresentation exportSettings = authorizationResource.exportSettings();
// check exported settings contains both resources/applyPolicies
boolean found = false;
for (PolicyRepresentation p : exportSettings.getPolicies()) {
if (p.getName().equals(permissionName)) {
found = true;
Assert.assertEquals("[\"Default Resource\"]", p.getConfig().get("resources"));
Assert.assertEquals("[\"Default Policy\"]", p.getConfig().get("applyPolicies"));
}
}
Assert.assertTrue("Permission \"role-based-permission\" was not found.", found);
}
use of org.keycloak.admin.client.resource.AuthorizationResource in project keycloak by keycloak.
the class GroupPolicyManagementTest method testDeleteGroupAndPolicy.
@Test
public void testDeleteGroupAndPolicy() {
AuthorizationResource authorization = getClient().authorization();
GroupPolicyRepresentation representation = new GroupPolicyRepresentation();
representation.setName(UUID.randomUUID().toString());
representation.setDescription("description");
representation.setDecisionStrategy(DecisionStrategy.CONSENSUS);
representation.setLogic(Logic.NEGATIVE);
representation.setGroupsClaim("groups");
representation.addGroupPath("/Group G", true);
assertCreated(authorization, representation);
GroupsResource groups = getRealm().groups();
GroupRepresentation group = groups.groups("Group G", null, null).get(0);
groups.group(group.getId()).remove();
try {
getClient().authorization().policies().group().findByName(representation.getName());
} catch (NotFoundException e) {
}
representation.getGroups().clear();
representation.addGroupPath("/Group H/Group I/Group K");
representation.addGroupPath("/Group F");
assertCreated(authorization, representation);
group = groups.groups("Group K", null, null).get(0);
groups.group(group.getId()).remove();
GroupPolicyRepresentation policy = getClient().authorization().policies().group().findByName(representation.getName());
assertNotNull(policy);
assertEquals(1, policy.getGroups().size());
}
use of org.keycloak.admin.client.resource.AuthorizationResource in project keycloak by keycloak.
the class GroupPolicyManagementTest method testCreate.
@Test
public void testCreate() {
AuthorizationResource authorization = getClient().authorization();
GroupPolicyRepresentation representation = new GroupPolicyRepresentation();
representation.setName("Group Policy");
representation.setDescription("description");
representation.setDecisionStrategy(DecisionStrategy.CONSENSUS);
representation.setLogic(Logic.NEGATIVE);
representation.setGroupsClaim("groups");
representation.addGroupPath("/Group A/Group B/Group C", true);
representation.addGroupPath("Group F");
assertCreated(authorization, representation);
}
Aggregations