0

I am trying to create user with keycloak's /users endpoint in a spring boot project . These are the steps I have followed First created an admin in master realm and admin-cli client. Used that to get instance of keycloak for further operations.

return Keycloak.getInstance(
            keycloakProperties.getAuthServerUrl(),
            "master",
            adminConfig.getUserName(),
            adminConfig.getPassword(),
            "admin-cli");

I am able to get the user created if I don't add the client representation in user. If I add the CredentialRepresentation inside the userRepresentation object ,I am getting the BadRequest error (400 code) without any details . I looked up the keycloak server logs that too wasnt helpful. As a try,I did the user creation first and reset password later,then also user created,but unable to set password with reset-password endpoint (same error) .As mentioned in the documents,I have set the enabled property of userRepresentation to true. Keycloak version : 12.0.4 ,Spring boot version : 2.1.6.RELEASE I tried to read the entity from the response received, but failed in that too. Any help to figure out the issue will e appreciated. // Code for usercreation

UserRepresentation userRepresentation = new UserRepresentation();
    userRepresentation.setUsername(userModel.getEmail());
    userRepresentation.setEmail(userModel.getEmail());
    userRepresentation.setCredentials(Arrays.asList(createPasswordCredentials(userModel.getPassword())));
    userRepresentation.setEnabled(true);
    Keycloak keycloak = getKeycloakInstance(keycloakProperties);
    Response response = keycloak.realm(keycloakProperties.getRealm()).users().create(userRepresentation);
    log.info("Response Code {}", response.getStatus());

Code for CredentialRepresentation :

private static CredentialRepresentation  createPasswordCredentials(String password) {
    CredentialRepresentation passwordCredentials = new CredentialRepresentation();
    passwordCredentials.setTemporary(false);
    passwordCredentials.setType(CredentialRepresentation.PASSWORD);
    passwordCredentials.setValue(password);
    return passwordCredentials;
  }
Mittal
  • 105
  • 3
  • 12
  • can you maybe add the relevant code which raise the error to you question? the part where you create the userRepresenation – Evil_skunk Apr 05 '21 at 19:38
  • Response response = keycloak.realm(keycloakProperties.getRealm()).users().create(userRepresentation); log.info("Response Code {}", response.getStatus()); // returns 400 if I set userRepresentation.setCredentials(Arrays.asList(createPasswordCredentials(userModel.getPassword()))); – Mittal Apr 05 '21 at 19:47
  • 1
    The error is probably in how you fill the `userRepresentation`. Can you maybe edit your question and add the full code of how this variable is created and filled (and also the code for your `CredentialRepresentation`, Otherwise it's really hard to help – Evil_skunk Apr 05 '21 at 19:52
  • Edited the question. – Mittal Apr 05 '21 at 20:10
  • 1
    :-( i tried exactly your code (plain java, not in spring) and received a 201 response (new user with password set), also with KC 12.0.4 – Evil_skunk Apr 05 '21 at 20:18
  • @Evil_skunk for the Keycloak.getInstance which import did you have to use? – dreamcrash Apr 05 '21 at 20:20
  • 1
    i used `keycloak-admin-client`:12.0.4 from org.keycloak, and imported `org.keycloak.admin.client.Keycloak` – Evil_skunk Apr 05 '21 at 20:22
  • Can you pls share the code , how you get keyclock instance?I have been gone through the documentation multiple times, but couldnt figure out the issue, my user has admin role to manage users – Mittal Apr 05 '21 at 20:22
  • @Mittal which documentation are you following can you provide a link to it pls – dreamcrash Apr 05 '21 at 20:26
  • @Mittal added my (working) testing code here https://pastebin.com/HhDFrXb5 – Evil_skunk Apr 05 '21 at 20:29
  • That looks no difference , I spent a lot of errors to figure this out. @dreamcrash I just followed the official documentation and certain articles https://www.keycloak.org/docs/latest/securing_apps/index.html https://ioansurariu.ro/2020/10/19/users-and-roles-management-using-keycloak-java-apis/ – Mittal Apr 05 '21 at 21:33

1 Answers1

0

I just want to update the real cause behind this issue,The server I used was of old version ,and the issue got fixed when tested against latest keycloak server. Thank you for the support guys :)

Mittal
  • 105
  • 3
  • 12