Ask Your Question
3

How can claims be pushed to policies based on regular expressions in Keycloak?

asked 2022-06-07 11:00:00 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-09-18 05:00:00 +0000

woof gravatar image

Keycloak provides a feature called "Policy Script" where claims can be pushed to policies based on regular expressions using JavaScript. The Policy Script allows for custom validation of a policy request before it is granted or denied.

To use regular expressions in Policy Script:

  1. Go to the Keycloak admin console and select the realm you want to configure.
  2. Click on "Clients" in the left-hand menu and select the client that you want to configure.
  3. Click on "Authorization" in the client settings menu.
  4. Under "Policy" click "Add policy".
  5. In the "Add policy" screen, select "Script" as the policy type.
  6. In the "Script" field, write the JavaScript code that will check for the regular expression pattern that matches the claim you want to push to the policy.
  7. Use the following code snippet to evaluate a regular expression:
var regex = new RegExp('your-regular-expression');
if (regex.test(claim-value)) {
  // Add the claim to the policy
  policy.addClaim('your-claim-name', 'your-claim-value');
}
  1. In the "Apply Policy to" field, select the resources, scopes, and/or roles to which the policy should be applied.
  2. Click "Save" to create the policy.

Now, when a user tries to access a resource that requires the policy, Keycloak will execute the Policy Script and add the claim to the policy if the regular expression matches the claim value.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-06-07 11:00:00 +0000

Seen: 12 times

Last updated: Sep 18 '21