Search in sources :

Example 1 with ResourcePermission

use of org.keycloak.authorization.permission.ResourcePermission in project keycloak by keycloak.

the class ResourcePolicyProvider method evaluate.

@Override
public void evaluate(Evaluation evaluation) {
    DefaultEvaluation defaultEvaluation = DefaultEvaluation.class.cast(evaluation);
    Map<Policy, Map<Object, Decision.Effect>> decisionCache = defaultEvaluation.getDecisionCache();
    Policy policy = defaultEvaluation.getParentPolicy();
    Map<Object, Decision.Effect> decisions = decisionCache.computeIfAbsent(policy, p -> new HashMap<>());
    ResourcePermission permission = evaluation.getPermission();
    Decision.Effect effect = decisions.get(permission.getResource());
    if (effect != null) {
        defaultEvaluation.setEffect(effect);
        return;
    }
    super.evaluate(evaluation);
    decisions.put(permission.getResource(), defaultEvaluation.getEffect());
}
Also used : Policy(org.keycloak.authorization.model.Policy) Map(java.util.Map) HashMap(java.util.HashMap) DefaultEvaluation(org.keycloak.authorization.policy.evaluation.DefaultEvaluation) Decision(org.keycloak.authorization.Decision) ResourcePermission(org.keycloak.authorization.permission.ResourcePermission)

Example 2 with ResourcePermission

use of org.keycloak.authorization.permission.ResourcePermission in project keycloak by keycloak.

the class ScopePolicyProvider method evaluate.

@Override
public void evaluate(Evaluation evaluation) {
    DefaultEvaluation defaultEvaluation = DefaultEvaluation.class.cast(evaluation);
    Map<Policy, Map<Object, Decision.Effect>> decisionCache = defaultEvaluation.getDecisionCache();
    Policy policy = defaultEvaluation.getParentPolicy();
    Map<Object, Decision.Effect> decisions = decisionCache.computeIfAbsent(policy, p -> new HashMap<>());
    ResourcePermission permission = evaluation.getPermission();
    Decision.Effect effect = decisions.get(permission);
    if (effect != null) {
        defaultEvaluation.setEffect(effect);
        return;
    }
    Decision.Effect decision = defaultEvaluation.getEffect();
    if (decision == null) {
        super.evaluate(evaluation);
        decisions.put(permission, defaultEvaluation.getEffect());
    }
}
Also used : Policy(org.keycloak.authorization.model.Policy) Map(java.util.Map) HashMap(java.util.HashMap) DefaultEvaluation(org.keycloak.authorization.policy.evaluation.DefaultEvaluation) Decision(org.keycloak.authorization.Decision) ResourcePermission(org.keycloak.authorization.permission.ResourcePermission)

Example 3 with ResourcePermission

use of org.keycloak.authorization.permission.ResourcePermission in project keycloak by keycloak.

the class UMAPolicyProvider method evaluate.

@Override
public void evaluate(Evaluation evaluation) {
    ResourcePermission permission = evaluation.getPermission();
    Resource resource = permission.getResource();
    if (resource != null) {
        Identity identity = evaluation.getContext().getIdentity();
        // no need to evaluate UMA permissions to resource owner resources
        if (resource.getOwner().equals(identity.getId())) {
            evaluation.grant();
            return;
        }
    }
    super.evaluate(evaluation);
}
Also used : Resource(org.keycloak.authorization.model.Resource) Identity(org.keycloak.authorization.identity.Identity) ResourcePermission(org.keycloak.authorization.permission.ResourcePermission)

Example 4 with ResourcePermission

use of org.keycloak.authorization.permission.ResourcePermission in project keycloak by keycloak.

the class AggregatePolicyProvider method evaluate.

@Override
public void evaluate(Evaluation evaluation) {
    DecisionResultCollector decision = new DecisionResultCollector() {

        @Override
        protected void onComplete(Result result) {
            if (isGranted(result.getResults().iterator().next())) {
                evaluation.grant();
            } else {
                evaluation.deny();
            }
        }
    };
    AuthorizationProvider authorization = evaluation.getAuthorizationProvider();
    Policy policy = evaluation.getPolicy();
    DefaultEvaluation defaultEvaluation = DefaultEvaluation.class.cast(evaluation);
    Map<Policy, Map<Object, Decision.Effect>> decisionCache = defaultEvaluation.getDecisionCache();
    ResourcePermission permission = evaluation.getPermission();
    for (Policy associatedPolicy : policy.getAssociatedPolicies()) {
        Map<Object, Decision.Effect> decisions = decisionCache.computeIfAbsent(associatedPolicy, p -> new HashMap<>());
        Decision.Effect effect = decisions.get(permission);
        DefaultEvaluation eval = new DefaultEvaluation(evaluation.getPermission(), evaluation.getContext(), policy, associatedPolicy, decision, authorization, decisionCache);
        if (effect == null) {
            PolicyProvider policyProvider = authorization.getProvider(associatedPolicy.getType());
            policyProvider.evaluate(eval);
            eval.denyIfNoEffect();
            decisions.put(permission, eval.getEffect());
        } else {
            eval.setEffect(effect);
        }
    }
    decision.onComplete(permission);
}
Also used : Policy(org.keycloak.authorization.model.Policy) AuthorizationProvider(org.keycloak.authorization.AuthorizationProvider) DecisionResultCollector(org.keycloak.authorization.policy.evaluation.DecisionResultCollector) DefaultEvaluation(org.keycloak.authorization.policy.evaluation.DefaultEvaluation) Decision(org.keycloak.authorization.Decision) Result(org.keycloak.authorization.policy.evaluation.Result) PolicyProvider(org.keycloak.authorization.policy.provider.PolicyProvider) HashMap(java.util.HashMap) Map(java.util.Map) ResourcePermission(org.keycloak.authorization.permission.ResourcePermission)

Example 5 with ResourcePermission

use of org.keycloak.authorization.permission.ResourcePermission in project keycloak by keycloak.

the class AbstractPermissionProvider method evaluate.

@Override
public void evaluate(Evaluation evaluation) {
    AuthorizationProvider authorization = evaluation.getAuthorizationProvider();
    DefaultEvaluation defaultEvaluation = DefaultEvaluation.class.cast(evaluation);
    Map<Policy, Map<Object, Decision.Effect>> decisionCache = defaultEvaluation.getDecisionCache();
    Policy policy = evaluation.getPolicy();
    ResourcePermission permission = evaluation.getPermission();
    for (Policy associatedPolicy : policy.getAssociatedPolicies()) {
        Map<Object, Decision.Effect> decisions = decisionCache.computeIfAbsent(associatedPolicy, p -> new HashMap<>());
        Decision.Effect effect = decisions.get(permission);
        defaultEvaluation.setPolicy(associatedPolicy);
        if (effect == null) {
            PolicyProvider policyProvider = authorization.getProvider(associatedPolicy.getType());
            if (policyProvider == null) {
                throw new RuntimeException("No policy provider found for policy [" + associatedPolicy.getType() + "]");
            }
            policyProvider.evaluate(defaultEvaluation);
            evaluation.denyIfNoEffect();
            decisions.put(permission, defaultEvaluation.getEffect());
        } else {
            defaultEvaluation.setEffect(effect);
        }
    }
}
Also used : Policy(org.keycloak.authorization.model.Policy) AuthorizationProvider(org.keycloak.authorization.AuthorizationProvider) DefaultEvaluation(org.keycloak.authorization.policy.evaluation.DefaultEvaluation) Decision(org.keycloak.authorization.Decision) PolicyProvider(org.keycloak.authorization.policy.provider.PolicyProvider) Map(java.util.Map) HashMap(java.util.HashMap) ResourcePermission(org.keycloak.authorization.permission.ResourcePermission)

Aggregations

ResourcePermission (org.keycloak.authorization.permission.ResourcePermission)19 Map (java.util.Map)9 Policy (org.keycloak.authorization.model.Policy)9 Resource (org.keycloak.authorization.model.Resource)9 Scope (org.keycloak.authorization.model.Scope)9 Permission (org.keycloak.representations.idm.authorization.Permission)8 HashMap (java.util.HashMap)7 AuthorizationProvider (org.keycloak.authorization.AuthorizationProvider)7 ResourceServer (org.keycloak.authorization.model.ResourceServer)7 ArrayList (java.util.ArrayList)6 Decision (org.keycloak.authorization.Decision)6 StoreFactory (org.keycloak.authorization.store.StoreFactory)6 HashSet (java.util.HashSet)5 Set (java.util.Set)5 Collection (java.util.Collection)4 DefaultEvaluation (org.keycloak.authorization.policy.evaluation.DefaultEvaluation)4 ClientModel (org.keycloak.models.ClientModel)4 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3