Search in sources :

Example 1 with RegexPolicyRepresentation

use of org.keycloak.representations.idm.authorization.RegexPolicyRepresentation in project keycloak by keycloak.

the class RegexPolicyProvider method evaluate.

@Override
public void evaluate(Evaluation evaluation) {
    AuthorizationProvider authorizationProvider = evaluation.getAuthorizationProvider();
    RegexPolicyRepresentation policy = representationFunction.apply(evaluation.getPolicy(), authorizationProvider);
    Attributes.Entry targetClaim = evaluation.getContext().getIdentity().getAttributes().getValue(policy.getTargetClaim());
    if (targetClaim == null) {
        return;
    }
    Pattern pattern = Pattern.compile(policy.getPattern());
    Matcher matcher = pattern.matcher(targetClaim.asString(0));
    if (matcher.matches()) {
        evaluation.grant();
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) RegexPolicyRepresentation(org.keycloak.representations.idm.authorization.RegexPolicyRepresentation) AuthorizationProvider(org.keycloak.authorization.AuthorizationProvider) Attributes(org.keycloak.authorization.attribute.Attributes)

Example 2 with RegexPolicyRepresentation

use of org.keycloak.representations.idm.authorization.RegexPolicyRepresentation in project keycloak by keycloak.

the class RegexPolicyProviderFactory method toRepresentation.

@Override
public RegexPolicyRepresentation toRepresentation(Policy policy, AuthorizationProvider authorization) {
    RegexPolicyRepresentation representation = new RegexPolicyRepresentation();
    Map<String, String> config = policy.getConfig();
    representation.setTargetClaim(config.get("targetClaim"));
    representation.setPattern(config.get("pattern"));
    return representation;
}
Also used : RegexPolicyRepresentation(org.keycloak.representations.idm.authorization.RegexPolicyRepresentation)

Example 3 with RegexPolicyRepresentation

use of org.keycloak.representations.idm.authorization.RegexPolicyRepresentation in project keycloak by keycloak.

the class RegexPolicyTest method createRegexPolicy.

private void createRegexPolicy(String name, String targetClaim, String pattern) {
    RegexPolicyRepresentation policy = new RegexPolicyRepresentation();
    policy.setName(name);
    policy.setTargetClaim(targetClaim);
    policy.setPattern(pattern);
    getClient().authorization().policies().regex().create(policy).close();
}
Also used : RegexPolicyRepresentation(org.keycloak.representations.idm.authorization.RegexPolicyRepresentation)

Aggregations

RegexPolicyRepresentation (org.keycloak.representations.idm.authorization.RegexPolicyRepresentation)3 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 AuthorizationProvider (org.keycloak.authorization.AuthorizationProvider)1 Attributes (org.keycloak.authorization.attribute.Attributes)1