prepared, but not tested

This commit is contained in:
Jörg Henke 2022-11-26 22:43:23 +01:00
parent abeba4935c
commit 75a625b969
7 changed files with 136 additions and 26 deletions

View File

@ -6,25 +6,24 @@
<attribute name="gradle_used_by_scope" value="main,test"/> <attribute name="gradle_used_by_scope" value="main,test"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="src" path="src/test/java"/>
<classpathentry kind="src" output="bin/main" path="src/main/resources"> <classpathentry kind="src" output="bin/main" path="src/main/resources">
<attributes> <attributes>
<attribute name="gradle_scope" value="main"/> <attribute name="gradle_scope" value="main"/>
<attribute name="gradle_used_by_scope" value="main,test"/> <attribute name="gradle_used_by_scope" value="main,test"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="src" output="bin/test" path="src/test/java"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17/">
<attributes> <attributes>
<attribute name="gradle_scope" value="test"/> <attribute name="module" value="true"/>
<attribute name="gradle_used_by_scope" value="test"/>
<attribute name="test" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17/"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/> <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"> <classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer">
<attributes> <attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/> <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="output" path="bin/default"/> <classpathentry kind="output" path="bin/default"/>
</classpath> </classpath>

View File

@ -1,2 +1,13 @@
arguments=
auto.sync=false
build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
connection.project.dir= connection.project.dir=
eclipse.preferences.version=1 eclipse.preferences.version=1
gradle.user.home=
java.home=
jvm.arguments=
offline.mode=false
override.workspace.settings=false
show.console.view=false
show.executions.view=false

View File

@ -1,8 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="CampOrganizer2"> <wb-module deploy-name="camporganizer2">
<property name="context-root" value="CampOrganizer2"/>
<property name="context-root" value="camporganizer2"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="src/main/resources"/> <wb-resource deploy-path="/WEB-INF/classes" source-path="src/main/resources"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="src/main/java"/> <wb-resource deploy-path="/WEB-INF/classes" source-path="src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/java"/>
</wb-module> </wb-module>
</project-modules> </project-modules>

View File

@ -18,7 +18,7 @@ apply plugin: 'war'
apply plugin: 'application' apply plugin: 'application'
group = 'de.jottyfan.camporganizer' group = 'de.jottyfan.camporganizer'
version = '0.1.4' version = '0.1.5'
sourceCompatibility = 17 sourceCompatibility = 17
mainClassName = "de.jottyfan.camporganizer.Main" mainClassName = "de.jottyfan.camporganizer.Main"
@ -28,8 +28,9 @@ repositories {
url "https://www.jottyfan.de/libs/" url "https://www.jottyfan.de/libs/"
} }
} }
ext { ext {
set('keycloakVersion', '19.0.2') set('keycloakVersion', '20.0.1')
} }
war { war {
@ -55,6 +56,10 @@ dependencies {
implementation 'org.keycloak:keycloak-spring-boot-starter' implementation 'org.keycloak:keycloak-spring-boot-starter'
// for using the keycloak rest interface
implementation 'org.keycloak:keycloak-admin-client:20.0.1'
implementation 'org.jboss.resteasy:resteasy-client:5.0.0.Final'
// backward compatibility until the complete registration is converted to keycloak // backward compatibility until the complete registration is converted to keycloak
implementation 'org.jasypt:jasypt:1.9.3' implementation 'org.jasypt:jasypt:1.9.3'

View File

@ -1,10 +1,23 @@
package de.jottyfan.camporganizer.module.registration; 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.LogManager;
import org.apache.logging.log4j.Logger; 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.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
/** /**
* *
* @author henkej * @author henkej
@ -23,6 +36,12 @@ public class KeycloakRepository {
@Value("${keycloak.realm:ow}") @Value("${keycloak.realm:ow}")
private String keycloakRealm; 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 * get the url of the user client
* *
@ -37,17 +56,59 @@ public class KeycloakRepository {
/** /**
* register the login in keycloak * register the login in keycloak
* *
* @param login * @param login the username
* @param password * @param password the password
* @param email the email
* @return true or false
*/ */
public void register(String login, String password) { public boolean register(String login, String password, String email) {
// TODO: check for trailing and leading / CredentialRepresentation passwordCredentials = new CredentialRepresentation();
String registrationUrl = String.format("%s/admin/realms/%s/users", keycloakUrl, keycloakRealm); passwordCredentials.setTemporary(false);
// see passwordCredentials.setType(CredentialRepresentation.PASSWORD);
// https://www.keycloak.org/docs-api/17.0/rest-api/index.html#_users_resource passwordCredentials.setValue(password);
// https://canada1.discourse-cdn.com/free1/uploads/keycloak/original/2X/3/379bbfe8857de117771149174a96e4216ebe9c76.png
// TODO Auto-generated method stub UserRepresentation user = new UserRepresentation();
LOGGER.error("not yet implemented registration of user {} in keycloak", login); 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();
} }
} }

View File

@ -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"));
}
}