'Keycloak Userstorage SPI: why both the getUserById() and getUserByUsername() called when I request for tokens?

I implemented a Userstorage SPI for Keycloak and I see that when I try to have my tokens, it uses both the getUserByUsername() and getUserById() method (https://www.keycloak.org/docs-api/13.0/javadocs/org/keycloak/storage/user/UserLookupProvider.html):

  1. I try to have my tokens like this:

enter image description here

I implemented the methods here (I know the logging message is not the best, this is just a tutorial project):

public class UserStorageProviderImpl implements UserStorageProvider, UserLookupProvider, CredentialInputValidator, UserQueryProvider {
   
   ...

    @Override
    public UserModel getUserByUsername(String username, RealmModel realm) {
        log.info("------------------- username = " + username);
        User user = UserRepository.findByName(username, realm, model, session);

        AbstractUserAdapterImpl um = Mapper.toUserModel(session, realm, model, user);

        return um;
    }


    private static final Logger log = LoggerFactory.getLogger(UserStorageProviderImpl.class);

    @Override
    public UserModel getUserById(String id, RealmModel realm) {
        log.info("------------------- id = " + id);
        StorageId sid = new StorageId(id);
        String name = sid.getExternalId();
        User user = UserRepository.findByName(name, realm, model, session);
        return Mapper.toUserModel(session, realm, model, user);
    }
   
}

As you can see, both log messages can be seen (------------------ username = and -------------------- id = )

enter image description here

My question is: why? Sadly I did not find any information in the documents about the flow it works. How each methods following eachother.



Sources

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

Source: Stack Overflow

Solution Source