Search in sources :

Example 1 with DefaultEvaluation

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

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

use of org.keycloak.authorization.policy.evaluation.DefaultEvaluation 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 4 with DefaultEvaluation

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

Example 5 with DefaultEvaluation

use of org.keycloak.authorization.policy.evaluation.DefaultEvaluation in project keycloak by keycloak.

the class PolicyEvaluationTest method testCheckUserClientRoles.

public static void testCheckUserClientRoles(KeycloakSession session) {
    session.getContext().setRealm(session.realms().getRealmByName("authz-test"));
    AuthorizationProvider authorization = session.getProvider(AuthorizationProvider.class);
    ClientModel clientModel = session.clients().getClientByClientId(session.getContext().getRealm(), "resource-server-test");
    StoreFactory storeFactory = authorization.getStoreFactory();
    ResourceServer resourceServer = storeFactory.getResourceServerStore().findByClient(clientModel);
    JSPolicyRepresentation policyRepresentation = new JSPolicyRepresentation();
    policyRepresentation.setName("testCheckUserClientRoles");
    StringBuilder builder = new StringBuilder();
    builder.append("var realm = $evaluation.getRealm();");
    builder.append("var roles = realm.getUserClientRoles('trinity', 'role-mapping-client');");
    builder.append("if (roles.size() == 1 && roles.contains('client-role-a')) { $evaluation.grant(); }");
    policyRepresentation.setCode(builder.toString());
    Policy policy = storeFactory.getPolicyStore().create(policyRepresentation, resourceServer);
    PolicyProvider provider = authorization.getProvider(policy.getType());
    DefaultEvaluation evaluation = createEvaluation(session, authorization, resourceServer, policy);
    provider.evaluate(evaluation);
    Assert.assertEquals(Effect.PERMIT, evaluation.getEffect());
}
Also used : Policy(org.keycloak.authorization.model.Policy) ClientModel(org.keycloak.models.ClientModel) JSPolicyRepresentation(org.keycloak.representations.idm.authorization.JSPolicyRepresentation) AuthorizationProvider(org.keycloak.authorization.AuthorizationProvider) PolicyProvider(org.keycloak.authorization.policy.provider.PolicyProvider) StoreFactory(org.keycloak.authorization.store.StoreFactory) ResourceServer(org.keycloak.authorization.model.ResourceServer) DefaultEvaluation(org.keycloak.authorization.policy.evaluation.DefaultEvaluation)

Aggregations

Policy (org.keycloak.authorization.model.Policy)14 DefaultEvaluation (org.keycloak.authorization.policy.evaluation.DefaultEvaluation)14 AuthorizationProvider (org.keycloak.authorization.AuthorizationProvider)12 PolicyProvider (org.keycloak.authorization.policy.provider.PolicyProvider)12 ResourceServer (org.keycloak.authorization.model.ResourceServer)10 StoreFactory (org.keycloak.authorization.store.StoreFactory)10 ClientModel (org.keycloak.models.ClientModel)10 JSPolicyRepresentation (org.keycloak.representations.idm.authorization.JSPolicyRepresentation)9 HashMap (java.util.HashMap)5 Map (java.util.Map)4 Decision (org.keycloak.authorization.Decision)4 ResourcePermission (org.keycloak.authorization.permission.ResourcePermission)4 SimpleDateFormat (java.text.SimpleDateFormat)1 Collection (java.util.Collection)1 Date (java.util.Date)1 Resource (org.keycloak.authorization.model.Resource)1 DecisionResultCollector (org.keycloak.authorization.policy.evaluation.DecisionResultCollector)1 Result (org.keycloak.authorization.policy.evaluation.Result)1 RealmModel (org.keycloak.models.RealmModel)1 UserModel (org.keycloak.models.UserModel)1