[keycloak-user] Groups as array of objects (Script Mapper ? )

Daniel Teixeira ddtxra at gmail.com
Fri Jul 20 08:26:13 EDT 2018


Hello,
I am trying to configure my userinfo token to get the groups, as an array
of objects.
Currently if I add the "Group Membership" mapper in my client, an array of
Strings with the groups is returns.

{
  "name": "Dummy User",
  "groups": ["group1", "group2", "group3"]
   ...
}

But what I need for a SSO Confluence plugin to work is the following format:
(The name of the attributes don't matter, but I need an array of objects
for the groups)

{
  "name": "Dummy User",
  "groups": [ {"group_name": "group1"},
                   {"group_name": "group2"},
                   {"group_name": "group3"}  ]
 ...
}

So I have tried to created Script Mapper as follows:

*var groups = [];*
*user.getGroups().forEach(function(groupModel) {*
*    var groupName = groupModel.getName();*
*    groups.push({"group_name": groupName});*
*})*

*token.setOtherClaims("groups", groups);*


But this script produces a token as following:

{
  "name": "Dummy User",
  "groups": {
    "0": {
      "group_name": "group1"
    },
    "1": {
      "group_name": "group2"
    },
    "2": {
      "group_name": "group3"
    }
  },
  ....
}

Which is not an array of object, but a map of objects.
I have tried to toggle the option multivalued but it didn't change anything.

Is there a way to have an array?
Could someone help me with that?
Thanks in advance!

Cheers,
Daniel Teixeira


More information about the keycloak-user mailing list