CAS Custom Authentication Handler Principal JSON Problem

43 views
Skip to first unread message

Cemal Önder

unread,
Sep 2, 2019, 5:12:51 AM9/2/19
to CAS Community
Hi everyone,

I have a JSON problem while returning custom attributes in my custom authentication handler:


return createHandlerResult( credential,
 
this.getPrincipalFactory( ).createPrincipal( credential.getId( ), attributes) );


which createPrincipal method accepts Map<String, Object>

Principal createPrincipal(String id, Map<String, Object> attributes);

When I put Map<String, List> here, it returns toString representation of the List instead of its JSON representation. Or when I add Map, it returns {"left": "key", "right": "value"}.
I have been debugging for 1 week, I see how CAS uses json-smart-2.3 library. I see that when I send Map in the Object of Map<String, Object> for the attributes, json-smart library uses its writers, for the map it uses
BeansWriter which gets fields of the class and uses as keys. So it makes sense for the HashMap, it adds left and right fields to the JSON body which I don't want.


In short I want my CAS server return Map of attributes in correct JSON way like how org.json.JSONMapper does. How can I achieve this? 
(
Also I tried directly return JSON representation in the Object section of attributes:

Map<String, Object> attributes = new HashMap<>();
String roles = "{"TestModule":["Name1"]}"; (didn't add escape quotes for simplicity)
attributes.put("roles", roles);

But CAS somehow returns my String as LinkedList to the json-smart library. So library uses its Writer for LinkedLists, which adds [] to the response which I don't want like:

I don't want this:
{"rolesPerModule":["{\"TestModuleForBouncer_LIVE\":[\"ADMIN\"]}"]}

I want like:
{"rolesPerModule":{"{\"TestModuleForBouncer_LIVE\":[\"ADMIN\"]}"}}

)

Cemal Önder

unread,
Sep 2, 2019, 5:24:42 AM9/2/19
to CAS Community
And when I look for the CAS for how it handles Principal, it merges everything as LinkedList. So for that reason whatever I add in the Object of Map<String, Object> section, it returns as array in the JSON representation like []. Which means when I add attributes.put("name", "ExampleName"), it returns as "{"name"}:["ExampleName"].

2 Eylül 2019 Pazartesi 12:12:51 UTC+3 tarihinde Cemal Önder yazdı:

Cemal Önder

unread,
Sep 2, 2019, 8:30:06 AM9/2/19
to CAS Community
Because after calling mergeAttributes of DefaultAuthenticationResultBuilder class, everything in the Principal is returned as List.


2 Eylül 2019 Pazartesi 12:12:51 UTC+3 tarihinde Cemal Önder yazdı:
Hi everyone,

Cemal Önder

unread,
Sep 4, 2019, 10:40:20 AM9/4/19
to CAS Community

Cemal Önder

unread,
Sep 6, 2019, 5:51:24 AM9/6/19
to CAS Community
Here is the solution I found: https://stackoverflow.com/a/57819588/3000280
Reply all
Reply to author
Forward
0 new messages