Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploy a Javascript Policy in Keycloak 10

I've followed the Create a JAR with the scripts to deploy instruction to create a custom Javascript policy for checking user attributes and it seems to be deployed successfully, however, I can't see the policy name in the create policy dropdown. Is there any setting I need to enable for this feature?

After uploading the .jar file to /keycloak/standalone/deployments, it says WFLYSRV0010: Deployed "script.jar" (runtime-name : "script.jar") without any error. I can also see the {filename}.jar.deployed next to my script file.

Setup: I use docker-compose to launch a jboss/keycloak version 10.0.1 container. Here's my docker-compose file:

version: "3.1"
services:
    keycloak:
        image: jboss/keycloak
        restart: always
        ports:
            - 8080:8080
        environment:
            KEYCLOAK_PASSWORD: admin
            KEYCLOAK_USER: admin
        volumes:
            - ./deployments:/opt/jboss/keycloak/standalone/deployments

keycloak-scripts.json:

{
    "policies": [
        {
            "name": "my-policy",
            "fileName": "my-script-policy.js",
            "description": "My Policy from a JS file"
        }
    ]
}

my-script-policy.js:

var context = $evaluation.getContext();
var contextAttributes = context.getAttributes();

if (contextAttributes.containsValue('kc.client.network.ip_address', '127.0.0.1')) {
    $evaluation.grant();
}

Client policy setting

Thank you in advance for any suggestions.

like image 311
Behrooz TahanZadeh Avatar asked Aug 26 '26 12:08

Behrooz TahanZadeh


1 Answers

It has been a very long time, and I did not use this policy! Yet I managed to create a javascript policy at the time. I believe the issue was the script file name: my keycloak-provider repo

I will try to improve the README file and test it with the latest version of Keycloak to see if it still works.

like image 157
Behrooz TahanZadeh Avatar answered Aug 28 '26 02:08

Behrooz TahanZadeh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!