Search in sources :

Example 1 with PolicyProvider

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

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

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

Example 4 with PolicyProvider

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

the class PolicyEvaluationTest method testCheckDateAndTime.

public static void testCheckDateAndTime(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);
    TimePolicyRepresentation policyRepresentation = new TimePolicyRepresentation();
    policyRepresentation.setName("testCheckDateAndTime");
    // set the notOnOrAfter for 1 hour from now
    long notOnOrAfter = System.currentTimeMillis() + 3600000;
    Date notOnOrAfterDate = new Date(notOnOrAfter);
    policyRepresentation.setNotOnOrAfter(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(notOnOrAfterDate));
    // evaluation should succeed with the default context as it uses the current time as the date to be compared.
    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());
    // lets now override the context to use a time that exceeds the time that was set in the policy.
    long contextTime = System.currentTimeMillis() + 5400000;
    Map<String, Collection<String>> attributes = new HashMap<>();
    attributes.put("kc.time.date_time", Arrays.asList(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(contextTime))));
    evaluation = createEvaluation(session, authorization, null, resourceServer, policy, attributes);
    provider.evaluate(evaluation);
    Assert.assertEquals(Effect.DENY, evaluation.getEffect());
}
Also used : Policy(org.keycloak.authorization.model.Policy) HashMap(java.util.HashMap) AuthorizationProvider(org.keycloak.authorization.AuthorizationProvider) StoreFactory(org.keycloak.authorization.store.StoreFactory) Date(java.util.Date) DefaultEvaluation(org.keycloak.authorization.policy.evaluation.DefaultEvaluation) ClientModel(org.keycloak.models.ClientModel) TimePolicyRepresentation(org.keycloak.representations.idm.authorization.TimePolicyRepresentation) PolicyProvider(org.keycloak.authorization.policy.provider.PolicyProvider) Collection(java.util.Collection) ResourceServer(org.keycloak.authorization.model.ResourceServer) SimpleDateFormat(java.text.SimpleDateFormat)

Example 5 with PolicyProvider

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

the class PolicyEvaluationTest method testCheckUserInGroup.

public static void testCheckUserInGroup(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("testCheckUserInGroup");
    StringBuilder builder = new StringBuilder();
    builder.append("var realm = $evaluation.getRealm();");
    builder.append("if (realm.isUserInGroup('marta', 'Group C')) { $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.assertNull(evaluation.getEffect());
    builder = new StringBuilder();
    builder.append("var realm = $evaluation.getRealm();");
    builder.append("if (realm.isUserInGroup('marta', 'Group A')) { $evaluation.grant(); }");
    policyRepresentation.setCode(builder.toString());
    policyRepresentation.setId(policy.getId());
    policy = RepresentationToModel.toModel(policyRepresentation, authorization, policy);
    evaluation = createEvaluation(session, authorization, resourceServer, policy);
    provider.evaluate(evaluation);
    Assert.assertEquals(Effect.PERMIT, evaluation.getEffect());
    builder = new StringBuilder();
    builder.append("var realm = $evaluation.getRealm();");
    builder.append("if (realm.isUserInGroup('marta', '/Group A')) { $evaluation.grant(); }");
    policyRepresentation.setCode(builder.toString());
    policyRepresentation.setId(policy.getId());
    policy = RepresentationToModel.toModel(policyRepresentation, authorization, policy);
    evaluation = createEvaluation(session, authorization, resourceServer, policy);
    provider.evaluate(evaluation);
    Assert.assertEquals(Effect.PERMIT, evaluation.getEffect());
    builder = new StringBuilder();
    builder.append("var realm = $evaluation.getRealm();");
    builder.append("if (realm.isUserInGroup('marta', '/Group A/Group B')) { $evaluation.grant(); }");
    policyRepresentation.setCode(builder.toString());
    policyRepresentation.setId(policy.getId());
    policy = RepresentationToModel.toModel(policyRepresentation, authorization, policy);
    evaluation = createEvaluation(session, authorization, resourceServer, policy);
    provider.evaluate(evaluation);
    Assert.assertNull(evaluation.getEffect());
    builder = new StringBuilder();
    builder.append("var realm = $evaluation.getRealm();");
    builder.append("if (realm.isUserInGroup('alice', '/Group A/Group B/Group E')) { $evaluation.grant(); }");
    policyRepresentation.setCode(builder.toString());
    policyRepresentation.setId(policy.getId());
    policy = RepresentationToModel.toModel(policyRepresentation, authorization, policy);
    evaluation = createEvaluation(session, authorization, resourceServer, policy);
    provider.evaluate(evaluation);
    Assert.assertEquals(Effect.PERMIT, evaluation.getEffect());
    builder = new StringBuilder();
    builder.append("var realm = $evaluation.getRealm();");
    builder.append("if (realm.isUserInGroup('alice', '/Group A')) { $evaluation.grant(); }");
    policyRepresentation.setCode(builder.toString());
    policyRepresentation.setId(policy.getId());
    policy = RepresentationToModel.toModel(policyRepresentation, authorization, policy);
    evaluation = createEvaluation(session, authorization, resourceServer, policy);
    provider.evaluate(evaluation);
    Assert.assertEquals(Effect.PERMIT, evaluation.getEffect());
    builder = new StringBuilder();
    builder.append("var realm = $evaluation.getRealm();");
    builder.append("if (!realm.isUserInGroup('alice', '/Group A', false)) { $evaluation.grant(); }");
    policyRepresentation.setCode(builder.toString());
    policyRepresentation.setId(policy.getId());
    policy = RepresentationToModel.toModel(policyRepresentation, authorization, policy);
    evaluation = createEvaluation(session, authorization, resourceServer, policy);
    provider.evaluate(evaluation);
    Assert.assertNull(evaluation.getEffect());
    builder = new StringBuilder();
    builder.append("var realm = $evaluation.getRealm();");
    builder.append("if (realm.isUserInGroup('alice', '/Group E')) { $evaluation.grant(); }");
    policyRepresentation.setCode(builder.toString());
    policyRepresentation.setId(policy.getId());
    policy = RepresentationToModel.toModel(policyRepresentation, authorization, policy);
    evaluation = createEvaluation(session, authorization, resourceServer, policy);
    provider.evaluate(evaluation);
    Assert.assertNull(evaluation.getEffect());
    builder = new StringBuilder();
    builder.append("var realm = $evaluation.getRealm();");
    builder.append("if (realm.isUserInGroup('alice', 'Group E')) { $evaluation.grant(); }");
    policyRepresentation.setCode(builder.toString());
    policyRepresentation.setId(policy.getId());
    policy = RepresentationToModel.toModel(policyRepresentation, authorization, policy);
    evaluation = createEvaluation(session, authorization, resourceServer, policy);
    provider.evaluate(evaluation);
    Assert.assertNull(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

AuthorizationProvider (org.keycloak.authorization.AuthorizationProvider)12 Policy (org.keycloak.authorization.model.Policy)12 DefaultEvaluation (org.keycloak.authorization.policy.evaluation.DefaultEvaluation)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)3 Map (java.util.Map)2 Decision (org.keycloak.authorization.Decision)2 ResourcePermission (org.keycloak.authorization.permission.ResourcePermission)2 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