Search in sources :

Example 1 with DecisionResultCollector

use of org.keycloak.authorization.policy.evaluation.DecisionResultCollector 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)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 AuthorizationProvider (org.keycloak.authorization.AuthorizationProvider)1 Decision (org.keycloak.authorization.Decision)1 Policy (org.keycloak.authorization.model.Policy)1 ResourcePermission (org.keycloak.authorization.permission.ResourcePermission)1 DecisionResultCollector (org.keycloak.authorization.policy.evaluation.DecisionResultCollector)1 DefaultEvaluation (org.keycloak.authorization.policy.evaluation.DefaultEvaluation)1 Result (org.keycloak.authorization.policy.evaluation.Result)1 PolicyProvider (org.keycloak.authorization.policy.provider.PolicyProvider)1