Keycloak: get all users without password

Viewed 90

I am customizing Keycloak. Now I want to get a list with all users, that have not set a password yet. In the web interface I can check if a user has a password, like this (no password set):

enter image description here

What I want is something like this:

session.users()
        .getUsersStream(session.getContext().getRealm())
        .filter(userModel -> user.isPasswordSet() == false)
        ...

Is there a functionality like isPasswordSet(), so that I can retrieve a list of all users without a password? Thanks.

2 Answers

This API can check user has a password or not http://localhost:8080/auth/admin/realms/test/users/{user-id}/credentials

I demo two user, one user(set-password-user) already set the password. other user(no-password-user) not yet set the password.

  1. "set-password-user" assign the role as "manage-users" enter image description here

  2. get an access-token enter image description here

  3. get the user list enter image description here

  4. get the credential of "set-password-user" enter image description here

  5. get the credential of "no-password-user" enter image description here

You can see difference between 4. and 5. So you can make user.isPasswordSet() function with #4/5's results

I am not sure but programmatically I think you can do something similar to:

Use the UserCredentialStoreManager class

UserCredentialStoreManager user_credentials_store_manager =  UserCredentialStoreManager(session)

and use the method isConfiguredFor

boolean isConfiguredFor(RealmModel realm, UserModel user, String type)

Checks to see if user has credential type configured. Looks in UserStorageProvider or UserFederationProvider first, then loops through each CredentialProvider.

In your case you want to check for the type PasswordCredentialModel.TYPE

Related