Search code examples
arrayskeycloakroleskeycloak-rest-api

Keycloak client role attribute array


Situation

I have a keycloak server (v12.0.2) running with a client that has some roles. I can add custom attributes to that roles and retrieve them. No problem. But the roles always return an array.

entering key "foo" and value "bar" gives me

 "attributes": {
    "foo": [
      "bar"
    ]
  }

What I would like to have

I would like to have multiple entries in the array. To stay in the previous example, I would like to have "bar" and "baz".

 "attributes": {
    "foo": [
      "bar",
      "baz"
    ]
  }

What I have tried

  • Simply adding 2 entries with the same key - that just leads to overwriting the first entry with the second. So I get
 "attributes": {
    "foo": [
      "baz"
    ]
  }
  • Supplying an array index in the key ("foo[0]" = "bar" and "foo[1]" = "baz" is just two different keys and giving me
  "attributes": {
    "foo[0]": [
      "bar"
    ],
    "foo[1]": [
      "baz"
    ]
  }
  • Separating the values with semicolon, space or comma returns
  "attributes": {
    "foo": [
      "bar,baz"
    ]
  }

(and the same with ";" or " " respectively)

Is there any way to do that or do I have to go with custom defined separators and split the string in my application (which is not a problem, but I think getting the values as an array would be better)


Solution

  • You can use ## as a delimiter, example: bar##baz . But with this delimiter the order of the array is not guaranteed , though you save the data as bar##baz the array may return as foo[0]= "baz" and foo[1] as="bar".

    If the order is not important you can try this delimiter.