register keycloak user (needs manage realm-user privilege)

This commit is contained in:
Jottyfan 2022-12-08 21:26:50 +01:00
parent 6aaf6dbeb4
commit df7e4e7f0e
3 changed files with 24 additions and 8 deletions

View File

@ -18,7 +18,7 @@ apply plugin: 'war'
apply plugin: 'application'
group = 'de.jottyfan.camporganizer'
version = '0.1.5'
version = '0.1.6'
sourceCompatibility = 17
mainClassName = "de.jottyfan.camporganizer.Main"

View File

@ -2,7 +2,6 @@ package de.jottyfan.camporganizer.module.registration;
import java.util.Collections;
import javax.ws.rs.client.Client;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
@ -100,19 +99,36 @@ public class KeycloakRepository {
*/
protected boolean register(UsersResource resource, UserRepresentation user) {
Response response = resource.create(user);
LOGGER.info("created new keycloak user {}", user.getUsername());
return Status.OK.equals(response.getStatusInfo());
}
public Client getClient() {
return new ResteasyClientBuilderImpl().connectionPoolSize(10).build();
}
/**
* generate a keycloak object with the special arguments
*
* @param url the url
* @param realm the realm
* @param admin the admin of the realm
* @param password the password
* @param clientId the client ID
* @return the keycloak object
*/
public KeycloakBuilder getKeycloak(String url, String realm, String admin, String password, String clientId) {
return KeycloakBuilder.builder().serverUrl(url).realm(realm).grantType(OAuth2Constants.PASSWORD)
.username(admin).password(password).clientId(clientId)
.resteasyClient(getClient());
.resteasyClient(new ResteasyClientBuilderImpl().connectionPoolSize(10).build());
}
/**
* generate a users resource object
*
* @param url the url
* @param realm the realm
* @param admin the admin of the realm
* @param password the password
* @param clientId the client ID
* @return the users resource
*/
public UsersResource getUsersResource(String url, String realm, String admin, String password, String clientId) {
return getKeycloak(url, realm, admin, password, clientId).build().realm(realm).users();
}

View File

@ -20,7 +20,7 @@ public class TestKeycloakRepository {
public void testRegister() {
KeycloakRepository repository = new KeycloakRepository();
UserRepresentation user = repository.getUserRepresentation("Hans", "Dampf", "hans@dampf.org");
UsersResource resource = repository.getUsersResource("http://localhost:8080", "ow", "admin", "password", "biblecamp");
UsersResource resource = repository.getUsersResource("http://localhost:8080", "ow", "owadmin", "password", "biblecamp");
assertTrue(repository.register(resource, user));
}
}