progressing
This commit is contained in:
		| @@ -61,6 +61,22 @@ public class KeycloakRepository { | ||||
| 	 * @return true or false | ||||
| 	 */ | ||||
| 	public boolean register(String login, String password, String email) { | ||||
| 		UserRepresentation user = getUserRepresentation(login, password, email); | ||||
| 		UsersResource resource = getUsersResource(keycloakUrl, keycloakRealm, keycloakAdminName, keycloakAdminPassword, keycloakClientId); | ||||
| 		boolean result = register(resource, user); | ||||
| 		sendVerificationLink(login, resource); | ||||
| 		return result; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * generate a user representation | ||||
| 	 * | ||||
| 	 * @param login the login | ||||
| 	 * @param password the password | ||||
| 	 * @param email the email | ||||
| 	 * @return the user representation | ||||
| 	 */ | ||||
| 	protected UserRepresentation getUserRepresentation(String login, String password, String email) { | ||||
| 		CredentialRepresentation passwordCredentials = new CredentialRepresentation(); | ||||
| 		passwordCredentials.setTemporary(false); | ||||
| 		passwordCredentials.setType(CredentialRepresentation.PASSWORD); | ||||
| @@ -72,25 +88,33 @@ public class KeycloakRepository { | ||||
| 		user.setCredentials(Collections.singletonList(passwordCredentials)); | ||||
| 		user.setEnabled(true); | ||||
|  | ||||
| 		UsersResource instance = getInstance(); | ||||
| 		Response response = instance.create(user); | ||||
| 		boolean result = Status.OK.equals(response.getStatusInfo()); | ||||
| 		sendVerificationLink(login); | ||||
| 		return result; | ||||
| 		return user; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * register a user in keycloak | ||||
| 	 * | ||||
| 	 * @param resource the resource | ||||
| 	 * @param user the user | ||||
| 	 * @return true or false | ||||
| 	 */ | ||||
| 	protected boolean register(UsersResource resource, UserRepresentation user) { | ||||
| 		Response response = resource.create(user); | ||||
| 		return Status.OK.equals(response.getStatusInfo()); | ||||
| 	} | ||||
|  | ||||
| 	public Client getClient() { | ||||
| 		return new ResteasyClientBuilderImpl().connectionPoolSize(10).build(); | ||||
| 	} | ||||
|  | ||||
| 	public KeycloakBuilder getKeycloak() { | ||||
| 		return KeycloakBuilder.builder().serverUrl(keycloakUrl).realm(keycloakRealm).grantType(OAuth2Constants.PASSWORD) | ||||
| 				.username(keycloakAdminName).password(keycloakAdminPassword).clientId(keycloakClientId) | ||||
| 	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()); | ||||
| 	} | ||||
|  | ||||
| 	public UsersResource getInstance() { | ||||
| 		return getKeycloak().build().realm(keycloakRealm).users(); | ||||
| 	public UsersResource getUsersResource(String url, String realm, String admin, String password, String clientId) { | ||||
| 		return getKeycloak(url, realm, admin, password, clientId).build().realm(realm).users(); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| @@ -98,8 +122,7 @@ public class KeycloakRepository { | ||||
| 	 * | ||||
| 	 * @param userId the ID of the user | ||||
| 	 */ | ||||
| 	public void sendVerificationLink(String userId) { | ||||
| 		UsersResource usersResource = getInstance(); | ||||
| 	public void sendVerificationLink(String userId, UsersResource usersResource) { | ||||
| 		usersResource.get(userId).sendVerifyEmail(); | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -3,28 +3,24 @@ package de.jottyfan.camporganizer.module.registration; | ||||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||||
|  | ||||
| import org.junit.jupiter.api.Test; | ||||
| import org.junit.runner.RunWith; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.boot.test.context.SpringBootTest; | ||||
| import org.springframework.test.context.junit4.SpringRunner; | ||||
| import org.keycloak.admin.client.resource.UsersResource; | ||||
| import org.keycloak.representations.idm.UserRepresentation; | ||||
|  | ||||
| /** | ||||
|  * | ||||
|  * @author jotty | ||||
|  * | ||||
|  */ | ||||
| @RunWith(SpringRunner.class) | ||||
| @SpringBootTest | ||||
| public class TestKeycloakRepository { | ||||
|  | ||||
| 	@Autowired | ||||
| 	private KeycloakRepository repository; | ||||
|  | ||||
| 	/** | ||||
| 	 * test registration | ||||
| 	 */ | ||||
| 	@Test | ||||
| 	public void testRegister() { | ||||
| 		assertTrue(repository.register("Hans", "Dampf", "jottyfan@mail.de")); | ||||
| 		KeycloakRepository repository = new KeycloakRepository(); | ||||
| 		UserRepresentation user = repository.getUserRepresentation("Hans", "Dampf", "hans@dampf.org"); | ||||
| 		UsersResource resource = repository.getUsersResource("http://localhost:8080", "ow", "admin", "password", "biblecamp"); | ||||
| 		assertTrue(repository.register(resource, user)); | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user