prepared, but not tested
This commit is contained in:
		| @@ -58,8 +58,8 @@ public class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter { | ||||
| 		http.authorizeRequests() | ||||
| 		// @formatter:off | ||||
| 		  .antMatchers("/dashboard/**", "/business/**", "/confirmation/**").authenticated() | ||||
|   		.anyRequest().permitAll(); | ||||
|   	// @formatter:on | ||||
| 		  .anyRequest().permitAll(); | ||||
| 		// @formatter:on | ||||
| //		http.anonymous().disable(); | ||||
| 		http.csrf().disable(); | ||||
| 	} | ||||
|   | ||||
| @@ -1,10 +1,23 @@ | ||||
| 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; | ||||
|  | ||||
| import org.apache.logging.log4j.LogManager; | ||||
| import org.apache.logging.log4j.Logger; | ||||
| import org.jboss.resteasy.client.jaxrs.internal.ResteasyClientBuilderImpl; | ||||
| import org.keycloak.OAuth2Constants; | ||||
| import org.keycloak.admin.client.KeycloakBuilder; | ||||
| import org.keycloak.admin.client.resource.UsersResource; | ||||
| import org.keycloak.representations.idm.CredentialRepresentation; | ||||
| import org.keycloak.representations.idm.UserRepresentation; | ||||
| import org.springframework.beans.factory.annotation.Value; | ||||
| import org.springframework.stereotype.Repository; | ||||
|  | ||||
|  | ||||
| /** | ||||
|  * | ||||
|  * @author henkej | ||||
| @@ -23,6 +36,12 @@ public class KeycloakRepository { | ||||
| 	@Value("${keycloak.realm:ow}") | ||||
| 	private String keycloakRealm; | ||||
|  | ||||
| 	@Value("${keycloak.admin.name:admin") | ||||
| 	private String keycloakAdminName; | ||||
|  | ||||
| 	@Value("${keycloak.admin.password:password") | ||||
| 	private String keycloakAdminPassword; | ||||
|  | ||||
| 	/** | ||||
| 	 * get the url of the user client | ||||
| 	 * | ||||
| @@ -37,17 +56,59 @@ public class KeycloakRepository { | ||||
| 	/** | ||||
| 	 * register the login in keycloak | ||||
| 	 * | ||||
| 	 * @param login | ||||
| 	 * @param password | ||||
| 	 * @param login the username | ||||
| 	 * @param password the password | ||||
| 	 * @param email the email | ||||
| 	 * @return true or false | ||||
| 	 */ | ||||
| 	public void register(String login, String password) { | ||||
| 		// TODO: check for trailing and leading / | ||||
| 		String registrationUrl = String.format("%s/admin/realms/%s/users", keycloakUrl, keycloakRealm); | ||||
| 		// see | ||||
| 		// https://www.keycloak.org/docs-api/17.0/rest-api/index.html#_users_resource | ||||
| 		// https://canada1.discourse-cdn.com/free1/uploads/keycloak/original/2X/3/379bbfe8857de117771149174a96e4216ebe9c76.png | ||||
| 		// TODO Auto-generated method stub | ||||
| 		LOGGER.error("not yet implemented registration of user {} in keycloak", login); | ||||
| 	public boolean register(String login, String password, String email) { | ||||
| 		CredentialRepresentation passwordCredentials = new CredentialRepresentation(); | ||||
|         passwordCredentials.setTemporary(false); | ||||
|         passwordCredentials.setType(CredentialRepresentation.PASSWORD); | ||||
|         passwordCredentials.setValue(password); | ||||
|  | ||||
|         UserRepresentation user = new UserRepresentation(); | ||||
| 	    user.setUsername(login); | ||||
| 	    user.setEmail(email); | ||||
| 	    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; | ||||
| 	} | ||||
|  | ||||
| 	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) | ||||
|                 .resteasyClient(getClient()); | ||||
| 	} | ||||
|  | ||||
| 	public UsersResource getInstance() { | ||||
|         return getKeycloak().build().realm(keycloakRealm).users(); | ||||
|     } | ||||
|  | ||||
| 	/** | ||||
| 	 * send a verification link for newly registered users | ||||
| 	 * | ||||
| 	 * @param userId the ID of the user | ||||
| 	 */ | ||||
| 	public void sendVerificationLink(String userId) { | ||||
| 		UsersResource usersResource = getInstance(); | ||||
| 		usersResource.get(userId).sendVerifyEmail(); | ||||
| 	} | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -0,0 +1,28 @@ | ||||
| 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; | ||||
|  | ||||
| /** | ||||
|  * | ||||
|  * @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")); | ||||
| 	} | ||||
| } | ||||
		Reference in New Issue
	
	Block a user