I have an app that takes in login credentials: username and password for a user. I have a rest api that internally calls the keycloak REST API: /auth/realms/realmname/protocol/openid-connect/token and gets the access token for this user.
Now I am building another REST API to access a resource where I want to do the following: doSomething(accesstoken, data){a) call keycloak API to validate access token and get roles. b) if role == manager, process(data)c) else: return error msg.}
Now, how do I do (a): validating the access token and getting the roles associated with it. I know we can do:
As seen, it doesnt give the roles at all. How do I then tell what roles this access token has? Interestingly, when I search for this, many resources are suggesting the above userinfo endpoint. But this merely tells me taht the access token I provided is valid. Does not give roles for that. In other words - it authenticates but does not authorize.
Usually Keycloak OIDC client has assigned default roles scope, where all roles related mappers (e.g. client/realm role mappers) are configured. So you can modify those mappers in that scope to “publish” data also to userinfo output. Or you can configure those mappers on the client level as well. Of course you need to be sure that user has assigned some roles.
This is an output example: "jti":"c98d59b9-c6c5-455b-a0d6- b7bcca50b59c","exp":1545886929,"nbf":0,"iat":1545886629,"iss":"http://localhost:8180/auth/realms/demo","aud":"crn-user-ldap","sub":"9a4ce56b-028a-4fb8-8f4c-db1ef5c800b9","typ":"Bearer","azp":"crn-user-ldap","auth_time":0,"session_state":"d336cacd-2ba6-49e4-bd89-836de43b2275","name":"demo user","given_name":"demo","family_name":"user","preferred_username":"demouser1","email":"demouser@haventec.com","acr":"1","allowed-origins":[],"realm_access":{"roles":["uma_authorization"]},"resource_access":{"account":{"roles":["manage-account","manage-account-links","view-profile"]}},"client_id":"crn-user-ldap","username":"demouser1","active":true}
I’m using the easy way to fold the roles into an id token. All you need is the keycloak admin console. Come into your realm. Next → Client sopes → roles and you get to the Settings tab. There enable “Include in token scope”. Next, go to the Mappers tab on the same page. There you need “realm roles” - go in there and include all “ADD” as well, do the same with “client roles” in the same Mappers tab After that all roles will be stacked as claims in your id token and on the backend you can read them from claims of Principal class or same classes which is also a lot to read token in Spring Boot Security.