Behrooz TahanZadeh
Behrooz TahanZadeh

Reputation: 71

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.

Upvotes: 3

Views: 1569

Answers (1)

Behrooz TahanZadeh
Behrooz TahanZadeh

Reputation: 71

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.

Upvotes: 1

Related Questions