From df7e4e7f0e515c19b72810e1587dd60334664917 Mon Sep 17 00:00:00 2001 From: Jottyfan Date: Thu, 8 Dec 2022 21:26:50 +0100 Subject: [PATCH] register keycloak user (needs manage realm-user privilege) --- build.gradle | 2 +- .../registration/KeycloakRepository.java | 28 +++++++++++++++---- .../registration/TestKeycloakRepository.java | 2 +- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/build.gradle b/build.gradle index 8c46b55..3d32a0f 100644 --- a/build.gradle +++ b/build.gradle @@ -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" diff --git a/src/main/java/de/jottyfan/camporganizer/module/registration/KeycloakRepository.java b/src/main/java/de/jottyfan/camporganizer/module/registration/KeycloakRepository.java index 1646c5e..fe277fd 100644 --- a/src/main/java/de/jottyfan/camporganizer/module/registration/KeycloakRepository.java +++ b/src/main/java/de/jottyfan/camporganizer/module/registration/KeycloakRepository.java @@ -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(); } diff --git a/src/test/java/de/jottyfan/camporganizer/module/registration/TestKeycloakRepository.java b/src/test/java/de/jottyfan/camporganizer/module/registration/TestKeycloakRepository.java index 79b353f..3ff4a8b 100644 --- a/src/test/java/de/jottyfan/camporganizer/module/registration/TestKeycloakRepository.java +++ b/src/test/java/de/jottyfan/camporganizer/module/registration/TestKeycloakRepository.java @@ -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)); } }