Keycloak UserRegistrationProvider REGISTER_ERROR

Viewed 282

I am trying to write keycloak user id to external database on registration, but keycloak crashes even without database call.

addUser code is already simplified to bare minimum:

    @Override
    public UserModel addUser(RealmModel realm, String username) {
        String uuid = UUID.randomUUID().toString();
        
        UserAdapter adapter = new UserAdapter(
                this.keycloakSession,
                realm,
                this.componentModel,
                uuid
        );

        adapter.setUsername(username);

        return adapter;
    }

I just create UserAdapter, set my own generated UUID and return it like it's done in many guides/code examples.

UserAdapter is:

public class UserAdapter extends AbstractUserAdapterFederatedStorage {
    protected String keycloakId;
    protected String username;

    public UserAdapter(KeycloakSession session, RealmModel realm, ComponentModel storageProviderModel, String uid) {
        super(session, realm, storageProviderModel);
        keycloakId = StorageId.keycloakId(storageProviderModel, uid);
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String getId() {
        return keycloakId;
    }
}

But on registration i get those errors from keycloak:

  1. WARN [org.keycloak.services] (default task-2) KC-SERVICES0013: Failed authentication: java.lang.RuntimeException: No user model provided for persisting changes
  2. WARN [org.keycloak.events] (default task-2) type=LOGIN_ERROR, realmId=product, clientId=spa, userId=null, ipAddress=192.168.208.1, error=invalid_user_credentials, auth_method=openid-connect, redirect_uri=http://product.test/, authSessionParentId=3ccd3825-8ba1-4d26-8a00-bdfb968f442c, authSessionTabId=CchD1Kp-5Lo

As i understand my UserAdapter is faulty, and sure login fails after user store failed, but i can't understand what's wrong, i've already tried to set a break points with logs and till return from addUser everything looks kind of good?

What am i missing?

1 Answers

I faced the same problem because I had overridden the getUserById() method from UserLookupProvider interface.

When registering a user, you go through several classes - depending on your Keycloak registration configuration :

  • RegistrationUserCreation (where the addUser method is called)
  • RegistrationPassword (where the updateCredential method is called)
  • RegistrationProfile

In both RegistrationPassword and RegistrationProfile classes, the user is retrieve using the overridden getUserById() method. Depending on how you've implemented it, it might return null. In my case it was calling an external API, but the user was not saved, yet.

Your error message might be thrown from the RegistrationProfile class

Related