I have a SAML IdP set up in Keycloak and it is working fine. We get an attribute from SAML called “Groups” which is a list of group IDs. So I set up some mappers of type “Advanced Attribute to Role”. I mapped GroupA to RoleA, GroupB to RoleB, etc. This works fine at first. If a user’s "Groups" attribute contains "GroupA", then that user is correctly assigned RoleA.
The problem is when a user is re-assigned from GroupA to GroupB. Now when that user logs on, he is correctly assigned RoleB, but he retains RoleA from before. If a user simply loses GroupA, that user retains RoleA.
I want a user to have those roles and ONLY those roles that are mapped in based on his current Groups attribute. And if a user loses a group attribute value, then he should lose the corresponding role.
How can I fix this situation? Can this be done using SAML attribute mappers? What’s the proper way to do this?


