prepared, but not tested
This commit is contained in:
parent
abeba4935c
commit
75a625b969
@ -6,25 +6,24 @@
|
||||
<attribute name="gradle_used_by_scope" value="main,test"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src/test/java"/>
|
||||
<classpathentry kind="src" output="bin/main" path="src/main/resources">
|
||||
<attributes>
|
||||
<attribute name="gradle_scope" value="main"/>
|
||||
<attribute name="gradle_used_by_scope" value="main,test"/>
|
||||
</attributes>
|
||||
</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>
|
||||
<attribute name="gradle_scope" value="test"/>
|
||||
<attribute name="gradle_used_by_scope" value="test"/>
|
||||
<attribute name="test" value="true"/>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</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.buildship.core.gradleclasspathcontainer">
|
||||
<attributes>
|
||||
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
|
||||
<classpathentry kind="output" path="bin/default"/>
|
||||
</classpath>
|
||||
|
@ -1,2 +1,13 @@
|
||||
arguments=
|
||||
auto.sync=false
|
||||
build.scans.enabled=false
|
||||
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
|
||||
connection.project.dir=
|
||||
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
|
||||
|
@ -1,8 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project-modules id="moduleCoreId" project-version="1.5.0">
|
||||
<wb-module deploy-name="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/java"/>
|
||||
</wb-module>
|
||||
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
|
||||
|
||||
<wb-module deploy-name="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/java"/>
|
||||
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/java"/>
|
||||
|
||||
</wb-module>
|
||||
|
||||
</project-modules>
|
||||
|
@ -18,7 +18,7 @@ apply plugin: 'war'
|
||||
apply plugin: 'application'
|
||||
|
||||
group = 'de.jottyfan.camporganizer'
|
||||
version = '0.1.4'
|
||||
version = '0.1.5'
|
||||
sourceCompatibility = 17
|
||||
mainClassName = "de.jottyfan.camporganizer.Main"
|
||||
|
||||
@ -28,8 +28,9 @@ repositories {
|
||||
url "https://www.jottyfan.de/libs/"
|
||||
}
|
||||
}
|
||||
|
||||
ext {
|
||||
set('keycloakVersion', '19.0.2')
|
||||
set('keycloakVersion', '20.0.1')
|
||||
}
|
||||
|
||||
war {
|
||||
@ -55,6 +56,10 @@ dependencies {
|
||||
|
||||
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
|
||||
implementation 'org.jasypt:jasypt:1.9.3'
|
||||
|
||||
|
@ -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"));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user