Search in sources :

Example 6 with PolicyProviderFactory

use of org.keycloak.authorization.policy.provider.PolicyProviderFactory in project keycloak by keycloak.

the class PolicyResourceService method delete.

@DELETE
public Response delete() {
    if (auth != null) {
        this.auth.realm().requireManageAuthorization();
    }
    if (policy == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    StoreFactory storeFactory = authorization.getStoreFactory();
    PolicyStore policyStore = storeFactory.getPolicyStore();
    PolicyProviderFactory resource = getProviderFactory(policy.getType());
    if (resource != null) {
        resource.onRemove(policy, authorization);
    }
    policyStore.delete(policy.getId());
    audit(toRepresentation(policy, authorization), OperationType.DELETE);
    return Response.noContent().build();
}
Also used : PolicyProviderFactory(org.keycloak.authorization.policy.provider.PolicyProviderFactory) PolicyStore(org.keycloak.authorization.store.PolicyStore) StoreFactory(org.keycloak.authorization.store.StoreFactory) DELETE(javax.ws.rs.DELETE)

Example 7 with PolicyProviderFactory

use of org.keycloak.authorization.policy.provider.PolicyProviderFactory 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 8 with PolicyProviderFactory

use of org.keycloak.authorization.policy.provider.PolicyProviderFactory 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

PolicyProviderFactory (org.keycloak.authorization.policy.provider.PolicyProviderFactory)8 StoreFactory (org.keycloak.authorization.store.StoreFactory)5 Policy (org.keycloak.authorization.model.Policy)4 EnumMap (java.util.EnumMap)3 PolicyStore (org.keycloak.authorization.store.PolicyStore)3 IOException (java.io.IOException)2 AbstractPolicyRepresentation (org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation)2 HashSet (java.util.HashSet)1 Set (java.util.Set)1 DELETE (javax.ws.rs.DELETE)1 Path (javax.ws.rs.Path)1 AuthorizationProvider (org.keycloak.authorization.AuthorizationProvider)1 ResourceServer (org.keycloak.authorization.model.ResourceServer)1 ResourceServerStore (org.keycloak.authorization.store.ResourceServerStore)1 GroupModel (org.keycloak.models.GroupModel)1 UserModel (org.keycloak.models.UserModel)1 ArtifactBindingUtils.computeArtifactBindingIdentifierString (org.keycloak.protocol.saml.util.ArtifactBindingUtils.computeArtifactBindingIdentifierString)1 ClientPolicyRepresentation (org.keycloak.representations.idm.authorization.ClientPolicyRepresentation)1 GroupPolicyRepresentation (org.keycloak.representations.idm.authorization.GroupPolicyRepresentation)1 PolicyRepresentation (org.keycloak.representations.idm.authorization.PolicyRepresentation)1