Issue with a javascript mapper

161 views
Skip to first unread message

Rémy Dernat

unread,
Aug 29, 2023, 5:06:59 AM8/29/23
to Keycloak User
Hi,

I am trying to setup a javascript mapper to transform a user custom attribute (cn) to another string for saml. It does not seem to work, as the value is empty.

I validated my code through java 8 `jjs` interpreter.

So, my guess is that the user.getAttribute() method does not work.

Here is the code :

```js

var cn = user.getAttribute("cn").toString();

var StringArray = Java.type("java.lang.String[]");
var ArrayList = Java.type('java.util.ArrayList');

var CNs = cn.split(new RegExp("\\s+"));

function printNOn(arg) { java.lang.System.out.print(arg); }

var index = 0;
for each (var word in CNs) {
    if ( index === 0 ) {
        sep = "";
       
    } else { sep = " "; }
    first_char = word[0].toUpperCase();
    other_chars = word.substring(1);
    printNOn(sep+first_char+other_chars);
    var index = index+1;
}

```

This script is very basic. It replaces every first character with an uppercase character.

Otherwise, I know that nashorn will be deprecated soon, but I don't know how to it with another method. If I have to do so, please give me an example with a saml mapper and the associated script... I am using a docker compose to start keycloak.

Thanks,

Best regards,
Rémy
Reply all
Reply to author
Forward
0 new messages