I've seen those two post that give a solution to this question but they do not provide detailed enough informations about how to do it for non Java developer like me:
Keycloak add extra claims from database / external source
How to register a custom ProtocolMapper in Keycloak?
Here is a recap of their solutions that could help others if filled with more details.
Process expected from 1st link
- User logs in
- My custom protocol mapper gets called, where I overwrite the transformAccessToken method
- Here I log in the client where the protocol mapper is in into keycloak, as a service. Here don't forget to use another client ID instead the one you're building the protocol mapper for, you'll enter an endless recursion otherwise.
- I get the access token into the protocol mapper and I call the rest endpoint of my application to grab the extra claims, which is secured.
- Get the info returned by the endpoint and add it as extra claims
Steps to achieve it from 2nd link
Implement the ProtocolMapper interface and add the file "META-INF/services/org.keycloak.protocol.ProtocolMapper" containing the reference to the class.
At this point Keycloak recognizes the new implementation. And you should be able to configure it via the admin console.
To add some data to the token add the following interfaces
org.keycloak.protocol.oidc.mappers.OIDCAccessTokenMapper
and implement the methods according to the interface
Then add the file "META-INF/jboss-deployment-structure.xml" with the following content
<?xml version="1.0" encoding="UTF-8"?> <jboss-deployment-structure> <deployment> <dependencies> <module name="org.keycloak.keycloak-services"/> </dependencies> </deployment> </jboss-deployment-structure>And after doing all this the custom transformAccessToken() method is called on every request to URL http://:/auth/realms/testrealm/protocol/openid-connect/token
After reading this I have a few questions :
- How do you ”Implement the ProtocolMapper”
- Where do you add the files mentionned earlier ? ( can't see any META-INF/ directory in my Keycloak installation folder )
- How and where do you ”add the following interfaces”
- What does the custom transformAccessToken() looks like
Thank you all for your time. Let me know if I miss summarise their answers.
Edit :
I'm starting a bounty with the hope that someone will be able to give me detailled steps on how to add extra claims from database in Keycloak 3.4.3 ( Detailed enough for a non Java dev )
Edit 2 A method descibed here could do the trick but lack details. Keycloak create a custom identity provider mapper