Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Has anyone made import functionality to Keycloak?

Tags:

keycloak

We need to import hundreds of users to Keycloak from csv-file. I haven't found any ready-made import functionality to do this.

Has anyone made any import routine or at least some skeleton to build on? REST API is probably the only way to do it - or is there another way?

like image 667
Borre Avatar asked Jan 08 '23 16:01

Borre


1 Answers

I've recently build something like that in Java. You can use the REST Api, but there is also the admin-client. See Programmatically adding users in Keycloak for some pointers. It should be trivial to add CSV support for that using e.g. Apache Commons CSV.

Basically you can add a Maven dependency:

<dependency>
  <groupId>org.keycloak</groupId>
  <artifactId>keycloak-admin-client</artifactId>
  <version>1.4.0.Final</version>
</dependency>

And use it like this:

Keycloak kc = Keycloak.getInstance(
  "http://localhost:8080/auth", 
  "master", // the realm to log in to
  "admin", "password",  // the user
  "security-admin-console");

CredentialRepresentation credential = new CredentialRepresentation();
credential.setType(CredentialRepresentation.PASSWORD);
credential.setValue("test123");
UserRepresentation user = new UserRepresentation();
user.setUsername("testuser");
user.setFirstName("Test");
user.setLastName("User");
user.setCredentials(Arrays.asList(credential));
kc.realm("master").users().create(user);
like image 184
Arjan Lamers Avatar answered Feb 12 '23 20:02

Arjan Lamers