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