Search in sources :

Example 6 with AbstractPolicyRepresentation

use of org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation in project keycloak by keycloak.

the class Permissions method update.

public void update(String name, AbstractPolicyRepresentation representation, boolean save) {
    for (WebElement row : permissions().rows()) {
        PolicyRepresentation actual = permissions().toRepresentation(row);
        if (actual.getName().equalsIgnoreCase(name)) {
            clickLink(row.findElements(tagName("a")).get(0));
            WaitUtils.waitForPageToLoad();
            String type = representation.getType();
            if ("resource".equals(type)) {
                resourcePermission.form().populate((ResourcePermissionRepresentation) representation, save);
            } else if ("scope".equals(type)) {
                scopePermission.form().populate((ScopePermissionRepresentation) representation, save);
            }
            return;
        }
    }
}
Also used : AbstractPolicyRepresentation(org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation) PolicyRepresentation(org.keycloak.representations.idm.authorization.PolicyRepresentation) WebElement(org.openqa.selenium.WebElement) ScopePermissionRepresentation(org.keycloak.representations.idm.authorization.ScopePermissionRepresentation)

Example 7 with AbstractPolicyRepresentation

use of org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation in project keycloak by keycloak.

the class PolicyResourceService method update.

@PUT
@Consumes("application/json")
@Produces("application/json")
@NoCache
public Response update(String payload) {
    if (auth != null) {
        this.auth.realm().requireManageAuthorization();
    }
    AbstractPolicyRepresentation representation = doCreateRepresentation(payload);
    if (policy == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    representation.setId(policy.getId());
    RepresentationToModel.toModel(representation, authorization, policy);
    audit(representation, OperationType.UPDATE);
    return Response.status(Status.CREATED).build();
}
Also used : AbstractPolicyRepresentation(org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) NoCache(org.jboss.resteasy.annotations.cache.NoCache) PUT(javax.ws.rs.PUT)

Example 8 with AbstractPolicyRepresentation

use of org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation in project keycloak by keycloak.

the class PolicyService method create.

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@NoCache
public Response create(String payload, @Context KeycloakSession session) {
    if (auth != null) {
        this.auth.realm().requireManageAuthorization();
    }
    AbstractPolicyRepresentation representation = doCreateRepresentation(payload);
    Policy policy = create(representation);
    representation.setId(policy.getId());
    audit(representation, representation.getId(), OperationType.CREATE, session);
    return Response.status(Status.CREATED).entity(representation).build();
}
Also used : AbstractPolicyRepresentation(org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation) Policy(org.keycloak.authorization.model.Policy) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Example 9 with AbstractPolicyRepresentation

use of org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation in project keycloak by keycloak.

the class PolicyTypeService method doCreateRepresentation.

@Override
protected AbstractPolicyRepresentation doCreateRepresentation(String payload) {
    PolicyProviderFactory provider = getPolicyProviderFactory(type);
    Class<? extends AbstractPolicyRepresentation> representationType = provider.getRepresentationType();
    if (representationType == null) {
        throw new RuntimeException("Policy provider for type [" + type + "] returned a null representation type.");
    }
    AbstractPolicyRepresentation representation;
    try {
        representation = JsonSerialization.readValue(payload, representationType);
    } catch (IOException e) {
        throw new RuntimeException("Failed to deserialize JSON using policy provider for type [" + type + "].", e);
    }
    representation.setType(type);
    return representation;
}
Also used : AbstractPolicyRepresentation(org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation) PolicyProviderFactory(org.keycloak.authorization.policy.provider.PolicyProviderFactory) IOException(java.io.IOException)

Example 10 with AbstractPolicyRepresentation

use of org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation in project keycloak by keycloak.

the class RepresentationToModel method toModel.

public static Policy toModel(AbstractPolicyRepresentation representation, AuthorizationProvider authorization, Policy model) {
    model.setName(representation.getName());
    model.setDescription(representation.getDescription());
    model.setDecisionStrategy(representation.getDecisionStrategy());
    model.setLogic(representation.getLogic());
    Set resources = representation.getResources();
    Set scopes = representation.getScopes();
    Set policies = representation.getPolicies();
    if (representation instanceof PolicyRepresentation) {
        PolicyRepresentation policy = PolicyRepresentation.class.cast(representation);
        if (resources == null) {
            String resourcesConfig = policy.getConfig().get("resources");
            if (resourcesConfig != null) {
                try {
                    resources = JsonSerialization.readValue(resourcesConfig, Set.class);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        if (scopes == null) {
            String scopesConfig = policy.getConfig().get("scopes");
            if (scopesConfig != null) {
                try {
                    scopes = JsonSerialization.readValue(scopesConfig, Set.class);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        if (policies == null) {
            String policiesConfig = policy.getConfig().get("applyPolicies");
            if (policiesConfig != null) {
                try {
                    policies = JsonSerialization.readValue(policiesConfig, Set.class);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        model.setConfig(policy.getConfig());
    }
    StoreFactory storeFactory = authorization.getStoreFactory();
    updateResources(resources, model, storeFactory);
    updateScopes(scopes, model, storeFactory);
    updateAssociatedPolicies(policies, model, storeFactory);
    PolicyProviderFactory provider = authorization.getProviderFactory(model.getType());
    if (representation instanceof PolicyRepresentation) {
        provider.onImport(model, PolicyRepresentation.class.cast(representation), authorization);
    } else if (representation.getId() == null) {
        provider.onCreate(model, representation, authorization);
    } else {
        provider.onUpdate(model, representation, authorization);
    }
    representation.setId(model.getId());
    return model;
}
Also used : AbstractPolicyRepresentation(org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation) PolicyRepresentation(org.keycloak.representations.idm.authorization.PolicyRepresentation) Set(java.util.Set) HashSet(java.util.HashSet) PolicyProviderFactory(org.keycloak.authorization.policy.provider.PolicyProviderFactory) ArtifactBindingUtils.computeArtifactBindingIdentifierString(org.keycloak.protocol.saml.util.ArtifactBindingUtils.computeArtifactBindingIdentifierString) IOException(java.io.IOException) StoreFactory(org.keycloak.authorization.store.StoreFactory)

Aggregations

AbstractPolicyRepresentation (org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation)10 Policy (org.keycloak.authorization.model.Policy)4 IOException (java.io.IOException)3 ClientPolicyRepresentation (org.keycloak.representations.idm.authorization.ClientPolicyRepresentation)3 GroupPolicyRepresentation (org.keycloak.representations.idm.authorization.GroupPolicyRepresentation)3 JSPolicyRepresentation (org.keycloak.representations.idm.authorization.JSPolicyRepresentation)3 PolicyRepresentation (org.keycloak.representations.idm.authorization.PolicyRepresentation)3 RolePolicyRepresentation (org.keycloak.representations.idm.authorization.RolePolicyRepresentation)3 UserPolicyRepresentation (org.keycloak.representations.idm.authorization.UserPolicyRepresentation)3 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Consumes (javax.ws.rs.Consumes)2 Produces (javax.ws.rs.Produces)2 NoCache (org.jboss.resteasy.annotations.cache.NoCache)2 Scope (org.keycloak.authorization.model.Scope)2 PolicyProviderFactory (org.keycloak.authorization.policy.provider.PolicyProviderFactory)2 PolicyStore (org.keycloak.authorization.store.PolicyStore)2 WebElement (org.openqa.selenium.WebElement)2 List (java.util.List)1 Map (java.util.Map)1