use of org.keycloak.representations.idm.authorization.RolePolicyRepresentation in project keycloak by keycloak.
the class RolePolicyForm method toRepresentation.
public RolePolicyRepresentation toRepresentation() {
RolePolicyRepresentation representation = new RolePolicyRepresentation();
representation.setName(UIUtils.getTextInputValue(name));
representation.setDescription(UIUtils.getTextInputValue(description));
representation.setLogic(Logic.valueOf(UIUtils.getTextFromElement(logic.getFirstSelectedOption()).toUpperCase()));
Set<RolePolicyRepresentation.RoleDefinition> roles = realmRoleSelect.getSelected();
roles.addAll(clientRoleSelect.getSelected());
representation.setRoles(roles);
return representation;
}
use of org.keycloak.representations.idm.authorization.RolePolicyRepresentation in project keycloak by keycloak.
the class AuthzCleanupTest method createRolePolicy.
private static Policy createRolePolicy(AuthorizationProvider authz, ResourceServer resourceServer, String roleName) {
RolePolicyRepresentation representation = new RolePolicyRepresentation();
representation.setName(roleName);
representation.setType("role");
representation.setDecisionStrategy(DecisionStrategy.UNANIMOUS);
representation.setLogic(Logic.POSITIVE);
representation.addRole(roleName, true);
return authz.getStoreFactory().getPolicyStore().create(representation, resourceServer);
}
use of org.keycloak.representations.idm.authorization.RolePolicyRepresentation in project keycloak by keycloak.
the class PolicyEnforcerTest method initAuthorizationSettings.
private void initAuthorizationSettings(ClientResource clientResource) {
if (clientResource.authorization().resources().findByName("Resource A").isEmpty()) {
JSPolicyRepresentation jsPolicy = new JSPolicyRepresentation();
jsPolicy.setName("Always Grant Policy");
StringBuilder code = new StringBuilder();
code.append("$evaluation.grant();");
jsPolicy.setCode(code.toString());
clientResource.authorization().policies().js().create(jsPolicy).close();
RolePolicyRepresentation rolePolicy = new RolePolicyRepresentation();
rolePolicy.setName("Only User Policy");
rolePolicy.addRole("user");
clientResource.authorization().policies().role().create(rolePolicy).close();
createResource(clientResource, "Resource A", "/api/resourcea");
ResourcePermissionRepresentation permission = new ResourcePermissionRepresentation();
permission.setName("Resource A Permission");
permission.addResource("Resource A");
permission.addPolicy(jsPolicy.getName());
clientResource.authorization().permissions().resource().create(permission).close();
}
if (clientResource.authorization().resources().findByName("Resource B").isEmpty()) {
JSPolicyRepresentation policy = new JSPolicyRepresentation();
policy.setName("Always Deny Policy");
StringBuilder code = new StringBuilder();
code.append("$evaluation.deny();");
policy.setCode(code.toString());
clientResource.authorization().policies().js().create(policy).close();
createResource(clientResource, "Resource B", "/api/resourceb");
ResourcePermissionRepresentation permission = new ResourcePermissionRepresentation();
permission.setName("Resource B Permission");
permission.addResource("Resource B");
permission.addPolicy(policy.getName());
clientResource.authorization().permissions().resource().create(permission).close();
}
if (clientResource.authorization().resources().findByName("Root").isEmpty()) {
createResource(clientResource, "Root", "/*");
}
}
use of org.keycloak.representations.idm.authorization.RolePolicyRepresentation in project keycloak by keycloak.
the class RolePolicyManagementTest method testRealmAndClientRoles.
@Test
public void testRealmAndClientRoles() throws InterruptedException {
authorizationPage.navigateTo();
RolePolicyRepresentation expected = new RolePolicyRepresentation();
expected.setName("Test Realm And Client Role Policy");
expected.setDescription("description");
String clientId = newClient.getClientId();
expected.addRole("Realm Role A");
expected.addRole("Realm Role C");
expected.addClientRole(clientId, "Client Role A");
expected.addClientRole(clientId, "Client Role B");
expected.addClientRole(clientId, "Client Role C");
expected = createPolicy(expected);
expected.setRoles(expected.getRoles().stream().filter(roleDefinition -> !roleDefinition.getId().contains("Client Role B") && !roleDefinition.getId().contains("Realm Role A")).collect(Collectors.toSet()));
authorizationPage.navigateTo();
authorizationPage.authorizationTabs().policies().update(expected.getName(), expected);
assertAlertSuccess();
authorizationPage.navigateTo();
RolePolicy actual = authorizationPage.authorizationTabs().policies().name(expected.getName());
expected = assertPolicy(expected, actual);
expected.getRoles().forEach(roleDefinition -> {
if (roleDefinition.getId().equals("Realm Role C")) {
roleDefinition.setRequired(true);
}
});
authorizationPage.navigateTo();
authorizationPage.authorizationTabs().policies().update(expected.getName(), expected);
assertAlertSuccess();
expected = assertPolicy(expected, actual);
expected.getRoles().clear();
expected.addClientRole(clientId, "Client Role B", true);
authorizationPage.navigateTo();
authorizationPage.authorizationTabs().policies().update(expected.getName(), expected);
assertAlertSuccess();
assertPolicy(expected, actual);
}
use of org.keycloak.representations.idm.authorization.RolePolicyRepresentation in project keycloak by keycloak.
the class RolePolicyManagementTest method testUpdateRealmRoles.
@Test
public void testUpdateRealmRoles() throws InterruptedException {
authorizationPage.navigateTo();
RolePolicyRepresentation expected = new RolePolicyRepresentation();
expected.setName("Test Update Realm Role Policy");
expected.setDescription("description");
expected.addRole("Realm Role A");
expected.addRole("Realm Role B");
expected.addRole("Realm Role C");
expected = createPolicy(expected);
String previousName = expected.getName();
expected.setName("Changed Test Realm Role Policy");
expected.setDescription("Changed description");
expected.setLogic(Logic.NEGATIVE);
expected.setRoles(expected.getRoles().stream().filter(roleDefinition -> !roleDefinition.getId().equals("Realm Role B")).collect(Collectors.toSet()));
authorizationPage.navigateTo();
authorizationPage.authorizationTabs().policies().update(previousName, expected);
assertAlertSuccess();
authorizationPage.navigateTo();
RolePolicy actual = authorizationPage.authorizationTabs().policies().name(expected.getName());
expected = assertPolicy(expected, actual);
expected.getRoles().iterator().next().setRequired(true);
authorizationPage.navigateTo();
authorizationPage.authorizationTabs().policies().update(expected.getName(), expected);
assertAlertSuccess();
expected = assertPolicy(expected, actual);
expected.getRoles().clear();
expected.addRole("Realm Role B", true);
authorizationPage.navigateTo();
authorizationPage.authorizationTabs().policies().update(expected.getName(), expected);
assertAlertSuccess();
assertPolicy(expected, actual);
}
Aggregations