question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Plugin migrating users without password validation

See original GitHub issue

I am running the current commit of this plugin with the standard Dockerfile of keycloak 15.0.0 in it. I have run all previous commits for test as well.

Initially it all worked fine, and my users have been migrated only after my POST REST legacy endpoint returned 200 for a valid password.

Now, after no changes made, to the best of my knowledge, users are being migrated (although not logged in) without their passwords in case they submit a wrong password (GET returns 200, POST returns 401 for wrong password). Previously user was not migrated in this situation. Now it is, according to this log:

2021-08-23T22:49:49.521910840Z22:49:49,521 INFO [com.danielfrak.code.keycloak.providers.rest.remote.UserModelFactory] (default task-4) Creating user model for: matheusmansour

We can see my REST microservice has been called with POST status code different than 200, nonetheless:

2021-08-23T22:49:49.515833Z **GET200** 964 B39 msApache-HttpClient/4.5.13 (Java/11.0.12) https://facily-wp-user-migration-ch4ssh6qga-uc.a.run.app/auth/matheusmansour
Aviso
2021-08-23T22:49:49.579379Z **POST401** 719 B21 msApache-HttpClient/4.5.13 (Java/11.0.12) https://facily-wp-user-migration-ch4ssh6qga-uc.a.run.app/auth/matheusmansour

Any clue why this might be happening? Been stuck with it for 4 days with code breaking in production and had to turn the plugin off. Thanks very much for any help!!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
chucksellickcommented, Sep 8, 2021

After some further digging I found a fix that appears to work, although I am not sure of the further implications. Based on the LDAP provider built into Keycloak, I found there was some code checking if the user already exists in Keycloak before attempting to create it, whereas the legacy plugin just attempts to create the user every time we find by username or find by email; obviously the reset password flow is searching for the user twice and therefore trying to duplicate the record.

I updated the getUserModel in LegacyProvider.java as follows:

    private UserModel getUserModel(RealmModel realm, String username, Supplier<Optional<LegacyUser>> user) {
      // NOTE: This should be an additional delegate passed in, in my case i am only ever using email
      UserModel userModel = session.userLocalStorage().getUserByEmail(username, realm);
      if (userModel != null) {
          LOG.debugf("Authenticated user [%s] found in Keycloak storage", username);
          return userModel;
      }

      return user.get()
              .map(u -> userModelFactory.create(u, realm))
              .orElseGet(() -> {
                  LOG.warnf("User not found in external repository: %s", username);
                  return null;
              });
    }

I am not submitting this as a PR right now because this breaks a number of tests and I am not the most familiar with Java testing frameworks and frankly, having been battling with this whole setup for several weeks now, I am a bit burnt out on this. Hopefully someone else can take this and make this into a workable PR.

1reaction
chucksellickcommented, Sep 7, 2021

I am getting the same problem with forgotten password behaviour, it seems like UserModelFactory::create ends up being called twice and this throws a duplicate record error. This issue is a showstopper for us, we can’t migrate users without the ability for users to recover a lost password, as is always the case many users will not have their correct password. Would really love some further input on this, but right now it seems like maybe it’s a bug in Keycloak’s user federation and how that interacts with plugins?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I migrate (export and import) wordpress users ...
Migrating WordPress users with their password is easily possible with this WordPress user import export plugin.
Read more >
User Migration Your Way
Automatic Migration gives you an opportunity to validate the password before it is hashed, so it is the best option when your hash...
Read more >
Technical Strategies for Migrating Users to Auth0
Auth0 offers multiple strategies for migrating users from an existing application. Bulk Importing users, either via the Management API or the ...
Read more >
6.4.1.3 Migrating Away from Pre-4.1 Password Hashing and ...
The result is that no account has an empty plugin value and no account uses pre-4.1 password hashing or the mysql_old_password plugin.
Read more >
user-migration/README.md at master · azure-ad-b2c ... - GitHub
The password reset journey must be modified such that when a user resets a password on an account that is due migration, then...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found