Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we create users in Keycloak by sending a json array containing more than 2 user info?

I was able to create user in Keycloak by posting a json containing only 1 user using postman.

http://localhost:8080/auth/admin/realms/master/users

But when i tried to create more than 1 user by passing a json array of more than 1 record i am getting a 500 Internal server error

[
  {
    "username": "user1",
    "firstName": "John",
    "attributes": {
      "pl_uid": null
    },
    "credentials": [
      {
        "temporary": true,
        "type": "password",
        "value": "ares2012"
      }
    ]
  },
  {
    "username": "1000195",
    "firstName": "Matt",
    "attributes": {
      "pl_uid": null
    },
    "credentials": [
      {
        "temporary": true,
        "type": "password",
        "value": "rx3o0t9f"
      }
    ]
  }
]

Is there any way by which we can send a json array to keycloak and create users there?

like image 478
Midhun Gopinath Avatar asked Feb 04 '19 08:02

Midhun Gopinath


People also ask

How do I add multiple users to a Keycloak?

And then you can create a user like this: Keycloak keycloak = Keycloak. getInstance("http://localhost:8080/auth", "master", "admin", "admin", "admin-cli", "password"); UserRepresentation user = new UserRepresentation(); user. setUsername("username1"); user.

How many users can Keycloak handle?

Get users. Default number of users is 100. Parameters max and first allow to paginate and retrieve more than 100 users.

What is user federation in Keycloak?

user federation provider. Keycloak can store and manage users. Often, companies already have LDAP or Active Directory services that store user and credential information. You can point Keycloak to validate credentials from those external stores and pull in identity information. identity provider.

How do I list users in a Keycloak?

You need first to obtain an access token from Master realm and then using this access token submit a request to realm you want to get users from. Make sure to use "client_id=admin-cli" parameter when requesting the access token from Master realm.


2 Answers

Please have a look on the discussion added in Keycloak mailing list

Just double checked the approach I suggested. I thought we had made it possible to import users into an existing realm, but that's not the case. You have to create the whole realm. It's still possible to do it this way, first create the realm and add an example user. Stop the server and run it again with:

 bin/standalone.sh -Dkeycloak.migration.action=export -Dkeycloak.migration.provider=dir -Dkeycloak.migration.realmName=<realm name> -Dkeycloak.migration.dir=<dir name>
  • Replace realm name and dir name

In dir name you should then get a few json files. You can then update realm name-users-0.json to add the users you want to import.

As Bill points out the admin client could be a good alternative approach. We also have a Java client that makes it simpler to use. Have a look at the admin-client example.

So this URL can help. Have a look at this link

Another option is using partialImport API for importing users (use admin user token):

access_token=`curl --data "grant_type=password&username=admin&password=admin&client_secret=secret&client_id=admin-cli" http://localhost:8180/auth/realms/master/protocol/openid-connect/token| jq -r .access_token`

curl -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' -H "Authorization: Bearer $access_token"  --data "@$PWD/myrealm-users-0.json"  http://localhost:8180/auth/admin/realms/myrealm/partialImport
like image 155
Subodh Joshi Avatar answered Sep 18 '22 13:09

Subodh Joshi


After checking Keycloak's REST API doesn't look like bulk/batch requests for creating users are accepted. The only solution would be to send the POST request for every user.

This is not surprising, HTTP is not prepared for this kind of requests:

As HTTP does not provide proper guidance for handling batch/bulk requests and responses.

from this RESTful guide. Have a look to it, it's really usefull in REST devlopments.

like image 35
Victor Calatramas Avatar answered Sep 20 '22 13:09

Victor Calatramas