'How to getGroups() in keycloak using UserFederatedStorageProvider

I have created a User storage provider that connects with keycloak with an external DB and gets the Users & other related details successfully. In the same way, I am trying to get the groups associated with a particular user from external storage to populate them in the user groups page and mainly while token generation.

I have been trying to do this using the getGroups() method in UserFederatedStorageProvider but it doesn't get called anywhere from keycloak or even while token generation. I have deployed the keycloak in my local and have tested it. Attaching my code snippets for reference.

    @Override
    public Set<GroupModel> getGroups(RealmModel realm, String userId) {
        // TODO Auto-generated method stub
        HashSet<GroupModel> testGroup = new HashSet<GroupModel>();
        logger.info("IN GET GROUPS ----------" + userId);
        GroupModel group1 = new Groups();
        group1.setName("testGroup1");
        testGroup.add(group1);
        return testGroup;
    }

I am using a UserStorageProviderFactory and then in my UserStorageProvider class I am implementing these for CRUD on Users and Credential handling. The one being implemented for groups is UserFederatedStorageProvider.

public class StorageProvider implements UserStorageProvider, 
    UserLookupProvider, 
    UserRegistrationProvider,
    UserQueryProvider,
    UserFederatedStorageProvider,
    CredentialInputUpdater, 
    CredentialInputValidator {


Solution 1:[1]

Although one year later, I had the exact same problem using keycloak v18. The solution: your overridden functions need to be in your UserAdapter class and not in the UserFederatedStorageProvider.

Of course your user adapter should extend the related class, eg:

public class UserAdapter extends AbstractUserAdapterFederatedStorage

This solved my problem!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kostisalex