Add ManagedRealm (#31621)

Signed-off-by: stianst <stianst@gmail.com>
This commit is contained in:
Stian Thorgersen 2024-07-26 09:09:34 +02:00 committed by GitHub
parent 6ab3e95ec5
commit bcbff87336
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 192 additions and 98 deletions

View file

@ -5,8 +5,8 @@ import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.Keycloak; import org.keycloak.admin.client.Keycloak;
import org.keycloak.common.Profile; import org.keycloak.common.Profile;
import org.keycloak.representations.info.FeatureRepresentation; import org.keycloak.representations.info.FeatureRepresentation;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.annotations.InjectAdminClient; import org.keycloak.test.framework.annotations.InjectAdminClient;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.server.KeycloakTestServerConfig; import org.keycloak.test.framework.server.KeycloakTestServerConfig;
import java.util.Optional; import java.util.Optional;

View file

@ -4,8 +4,8 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.Keycloak; import org.keycloak.admin.client.Keycloak;
import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.annotations.InjectAdminClient; import org.keycloak.test.framework.annotations.InjectAdminClient;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import java.util.List; import java.util.List;

View file

@ -4,8 +4,8 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.Keycloak; import org.keycloak.admin.client.Keycloak;
import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.annotations.InjectAdminClient; import org.keycloak.test.framework.annotations.InjectAdminClient;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import java.util.List; import java.util.List;

View file

@ -2,40 +2,40 @@ package org.keycloak.test.examples;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.annotations.InjectClient; import org.keycloak.test.framework.annotations.InjectClient;
import org.keycloak.test.framework.annotations.InjectRealm; import org.keycloak.test.framework.annotations.InjectRealm;
import org.keycloak.test.framework.annotations.InjectUser; import org.keycloak.test.framework.annotations.InjectUser;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.injection.LifeCycle; import org.keycloak.test.framework.injection.LifeCycle;
import org.keycloak.test.framework.realm.ManagedClient;
import org.keycloak.test.framework.realm.ManagedRealm;
import org.keycloak.test.framework.realm.ManagedUser;
@KeycloakIntegrationTest @KeycloakIntegrationTest
public class GlobalManagedResourcesTest { public class GlobalManagedResourcesTest {
@InjectRealm(lifecycle = LifeCycle.GLOBAL) @InjectRealm(lifecycle = LifeCycle.GLOBAL)
RealmResource realmResource; ManagedRealm realm;
@InjectClient(lifecycle = LifeCycle.GLOBAL) @InjectClient(lifecycle = LifeCycle.GLOBAL)
ClientResource clientResource; ManagedClient client;
@InjectUser(lifecycle = LifeCycle.GLOBAL) @InjectUser(lifecycle = LifeCycle.GLOBAL)
UserResource userResource; ManagedUser user;
@Test @Test
public void testCreatedRealm() { public void testCreatedRealm() {
Assertions.assertEquals("default", realmResource.toRepresentation().getRealm()); Assertions.assertEquals("default", realm.getName());
} }
@Test @Test
public void testCreatedClient() { public void testCreatedClient() {
Assertions.assertEquals("default", clientResource.toRepresentation().getClientId()); Assertions.assertEquals("default", client.getClientId());
} }
@Test @Test
public void testCreatedUser() { public void testCreatedUser() {
Assertions.assertEquals("default", userResource.toRepresentation().getUsername()); Assertions.assertEquals("default", user.getUsername());
} }
} }

View file

@ -2,13 +2,13 @@ package org.keycloak.test.examples;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.idm.ClientRepresentation; import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.annotations.InjectClient; import org.keycloak.test.framework.annotations.InjectClient;
import org.keycloak.test.framework.annotations.InjectRealm; import org.keycloak.test.framework.annotations.InjectRealm;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.injection.LifeCycle; import org.keycloak.test.framework.injection.LifeCycle;
import org.keycloak.test.framework.realm.ManagedClient;
import org.keycloak.test.framework.realm.ManagedRealm;
import java.util.List; import java.util.List;
@ -16,21 +16,23 @@ import java.util.List;
public class ManagedResources2Test { public class ManagedResources2Test {
@InjectRealm(lifecycle = LifeCycle.CLASS) @InjectRealm(lifecycle = LifeCycle.CLASS)
RealmResource realmResource; ManagedRealm realm;
@InjectClient @InjectClient
ClientResource clientResource; ManagedClient client;
@Test @Test
public void testCreatedRealm() { public void testCreatedRealm() {
Assertions.assertEquals("default", realmResource.toRepresentation().getRealm()); Assertions.assertEquals("http://localhost:8080/realms/default", realm.getBaseUrl());
Assertions.assertEquals("default", realm.getName());
Assertions.assertEquals("default", realm.admin().toRepresentation().getRealm());
} }
@Test @Test
public void testCreatedClient() { public void testCreatedClient() {
Assertions.assertEquals("default", clientResource.toRepresentation().getClientId()); Assertions.assertEquals("default", client.getClientId());
List<ClientRepresentation> clients = realmResource.clients().findByClientId("default"); List<ClientRepresentation> clients = realm.admin().clients().findByClientId("default");
Assertions.assertEquals(1, clients.size()); Assertions.assertEquals(1, clients.size());
} }

View file

@ -2,46 +2,42 @@ package org.keycloak.test.examples;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.annotations.InjectClient; import org.keycloak.test.framework.annotations.InjectClient;
import org.keycloak.test.framework.annotations.InjectRealm; import org.keycloak.test.framework.annotations.InjectRealm;
import org.keycloak.test.framework.annotations.InjectUser; import org.keycloak.test.framework.annotations.InjectUser;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.injection.LifeCycle; import org.keycloak.test.framework.injection.LifeCycle;
import org.keycloak.test.framework.realm.ManagedClient;
import java.util.List; import org.keycloak.test.framework.realm.ManagedRealm;
import org.keycloak.test.framework.realm.ManagedUser;
@KeycloakIntegrationTest @KeycloakIntegrationTest
public class ManagedResourcesTest { public class ManagedResourcesTest {
@InjectRealm(lifecycle = LifeCycle.CLASS) @InjectRealm(lifecycle = LifeCycle.CLASS)
RealmResource realmResource; ManagedRealm realm;
@InjectClient @InjectClient
ClientResource clientResource; ManagedClient client;
@InjectUser @InjectUser
UserResource userResource; ManagedUser user;
@Test @Test
public void testCreatedRealm() { public void testCreatedRealm() {
Assertions.assertEquals("default", realmResource.toRepresentation().getRealm()); Assertions.assertEquals("default", realm.getName());
} }
@Test @Test
public void testCreatedClient() { public void testCreatedClient() {
Assertions.assertEquals("default", clientResource.toRepresentation().getClientId()); Assertions.assertEquals("default", client.getClientId());
Assertions.assertEquals("default", realm.admin().clients().get(client.getId()).toRepresentation().getClientId());
List<ClientRepresentation> clients = realmResource.clients().findByClientId("default");
Assertions.assertEquals(1, clients.size());
} }
@Test @Test
public void testCreatedUser() { public void testCreatedUser() {
Assertions.assertEquals("default", userResource.toRepresentation().getUsername()); Assertions.assertEquals("default", user.getUsername());
Assertions.assertEquals("default", realm.admin().users().get(user.getId()).toRepresentation().getUsername());
} }
} }

View file

@ -2,42 +2,42 @@ package org.keycloak.test.examples;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.annotations.InjectClient; import org.keycloak.test.framework.annotations.InjectClient;
import org.keycloak.test.framework.annotations.InjectRealm; import org.keycloak.test.framework.annotations.InjectRealm;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.realm.ManagedClient;
import org.keycloak.test.framework.realm.ManagedRealm;
import org.keycloak.test.framework.realm.RealmConfig; import org.keycloak.test.framework.realm.RealmConfig;
@KeycloakIntegrationTest @KeycloakIntegrationTest
public class MultipleInstancesTest { public class MultipleInstancesTest {
@InjectRealm @InjectRealm
RealmResource realm1; ManagedRealm realm1;
@InjectRealm @InjectRealm
RealmResource realm2; ManagedRealm realm2;
@InjectRealm(ref = "another", config = CustomRealmConfig.class) @InjectRealm(ref = "another", config = CustomRealmConfig.class)
RealmResource realm3; ManagedRealm realm3;
@InjectClient(ref = "client1") @InjectClient(ref = "client1")
ClientResource client; ManagedClient client;
@InjectClient @InjectClient
ClientResource client2; ManagedClient client2;
@Test @Test
public void testMultipleInstances() { public void testMultipleInstances() {
Assertions.assertEquals("default", realm1.toRepresentation().getRealm()); Assertions.assertEquals("default", realm1.getName());
Assertions.assertEquals("default", realm2.toRepresentation().getRealm()); Assertions.assertEquals("default", realm2.getName());
Assertions.assertEquals(realm1, realm2); Assertions.assertEquals(realm1, realm2);
Assertions.assertEquals("another", realm3.toRepresentation().getRealm()); Assertions.assertEquals("another", realm3.getName());
Assertions.assertEquals("client1", client.toRepresentation().getClientId()); Assertions.assertEquals("client1", client.getClientId());
Assertions.assertEquals("default", client2.toRepresentation().getClientId()); Assertions.assertEquals("default", client2.getClientId());
} }

View file

@ -1,9 +1,9 @@
package org.keycloak.test.examples; package org.keycloak.test.examples;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.keycloak.test.framework.annotations.InjectPage;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest; import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.page.LoginPage; import org.keycloak.test.framework.page.LoginPage;
import org.keycloak.test.framework.annotations.InjectPage;
import org.keycloak.test.framework.page.WelcomePage; import org.keycloak.test.framework.page.WelcomePage;
@KeycloakIntegrationTest @KeycloakIntegrationTest

View file

@ -4,10 +4,10 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.keycloak.admin.client.Keycloak; import org.keycloak.admin.client.Keycloak;
import org.keycloak.representations.idm.UserRepresentation; import org.keycloak.representations.idm.UserRepresentation;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.annotations.InjectAdminClient; import org.keycloak.test.framework.annotations.InjectAdminClient;
import org.keycloak.test.framework.page.WelcomePage;
import org.keycloak.test.framework.annotations.InjectWebDriver; import org.keycloak.test.framework.annotations.InjectWebDriver;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.page.WelcomePage;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import java.util.List; import java.util.List;

View file

@ -0,0 +1,16 @@
package org.keycloak.test.framework.realm;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.Assertions;
public class ApiUtil {
public static String handleCreatedResponse(Response response) {
Assertions.assertEquals(201, response.getStatus());
String path = response.getLocation().getPath();
String uuid = path.substring(path.lastIndexOf('/') + 1);
response.close();
return uuid;
}
}

View file

@ -2,7 +2,6 @@ package org.keycloak.test.framework.realm;
import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.Response;
import org.keycloak.admin.client.resource.ClientResource; import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.idm.ClientRepresentation; import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.test.framework.annotations.InjectClient; import org.keycloak.test.framework.annotations.InjectClient;
import org.keycloak.test.framework.injection.InstanceContext; import org.keycloak.test.framework.injection.InstanceContext;
@ -10,9 +9,7 @@ import org.keycloak.test.framework.injection.RequestedInstance;
import org.keycloak.test.framework.injection.Supplier; import org.keycloak.test.framework.injection.Supplier;
import org.keycloak.test.framework.injection.SupplierHelpers; import org.keycloak.test.framework.injection.SupplierHelpers;
public class ClientSupplier implements Supplier<ClientResource, InjectClient> { public class ClientSupplier implements Supplier<ManagedClient, InjectClient> {
private static final String CLIENT_UUID_KEY = "clientUuid";
@Override @Override
public Class<InjectClient> getAnnotationClass() { public Class<InjectClient> getAnnotationClass() {
@ -20,13 +17,13 @@ public class ClientSupplier implements Supplier<ClientResource, InjectClient> {
} }
@Override @Override
public Class<ClientResource> getValueType() { public Class<ManagedClient> getValueType() {
return ClientResource.class; return ManagedClient.class;
} }
@Override @Override
public ClientResource getValue(InstanceContext<ClientResource, InjectClient> instanceContext) { public ManagedClient getValue(InstanceContext<ManagedClient, InjectClient> instanceContext) {
RealmResource realm = instanceContext.getDependency(RealmResource.class); ManagedRealm realm = instanceContext.getDependency(ManagedRealm.class);
ClientConfig config = SupplierHelpers.getInstance(instanceContext.getAnnotation().config()); ClientConfig config = SupplierHelpers.getInstance(instanceContext.getAnnotation().config());
ClientRepresentation clientRepresentation = config.getRepresentation(); ClientRepresentation clientRepresentation = config.getRepresentation();
@ -36,26 +33,22 @@ public class ClientSupplier implements Supplier<ClientResource, InjectClient> {
clientRepresentation.setClientId(clientId); clientRepresentation.setClientId(clientId);
} }
Response response = realm.clients().create(clientRepresentation); Response response = realm.admin().clients().create(clientRepresentation);
String uuid = ApiUtil.handleCreatedResponse(response);
clientRepresentation.setId(uuid);
String path = response.getLocation().getPath(); ClientResource clientResource = realm.admin().clients().get(uuid);
String clientId = path.substring(path.lastIndexOf('/') + 1); return new ManagedClient(clientRepresentation, clientResource);
response.close();
instanceContext.addNote(CLIENT_UUID_KEY, clientId);
return realm.clients().get(clientId);
} }
@Override @Override
public boolean compatible(InstanceContext<ClientResource, InjectClient> a, RequestedInstance<ClientResource, InjectClient> b) { public boolean compatible(InstanceContext<ManagedClient, InjectClient> a, RequestedInstance<ManagedClient, InjectClient> b) {
return a.getAnnotation().config().equals(b.getAnnotation().config()); return a.getAnnotation().config().equals(b.getAnnotation().config());
} }
@Override @Override
public void close(InstanceContext<ClientResource, InjectClient> instanceContext) { public void close(InstanceContext<ManagedClient, InjectClient> instanceContext) {
instanceContext.getValue().remove(); instanceContext.getValue().admin().remove();
} }
} }

View file

@ -0,0 +1,28 @@
package org.keycloak.test.framework.realm;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.representations.idm.ClientRepresentation;
public class ManagedClient {
private final ClientRepresentation createdRepresentation;
private final ClientResource clientResource;
public ManagedClient(ClientRepresentation createdRepresentation, ClientResource clientResource) {
this.createdRepresentation = createdRepresentation;
this.clientResource = clientResource;
}
public String getId() {
return createdRepresentation.getId();
}
public String getClientId() {
return createdRepresentation.getClientId();
}
public ClientResource admin() {
return clientResource;
}
}

View file

@ -0,0 +1,30 @@
package org.keycloak.test.framework.realm;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.idm.RealmRepresentation;
public class ManagedRealm {
private final String baseUrl;
private final RealmRepresentation createdRepresentation;
private final RealmResource realmResource;
public ManagedRealm(String baseUrl, RealmRepresentation createdRepresentation, RealmResource realmResource) {
this.baseUrl = baseUrl;
this.createdRepresentation = createdRepresentation;
this.realmResource = realmResource;
}
public String getBaseUrl() {
return baseUrl;
}
public String getName() {
return createdRepresentation.getRealm();
}
public RealmResource admin() {
return realmResource;
}
}

View file

@ -0,0 +1,29 @@
package org.keycloak.test.framework.realm;
import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.representations.idm.UserRepresentation;
public class ManagedUser {
private final UserRepresentation createdRepresentation;
private final UserResource userResource;
public ManagedUser(UserRepresentation createdRepresentation, UserResource userResource) {
this.createdRepresentation = createdRepresentation;
this.userResource = userResource;
}
public String getId() {
return createdRepresentation.getId();
}
public String getUsername() {
return createdRepresentation.getUsername();
}
public UserResource admin() {
return userResource;
}
}

View file

@ -8,8 +8,9 @@ import org.keycloak.test.framework.injection.InstanceContext;
import org.keycloak.test.framework.injection.RequestedInstance; import org.keycloak.test.framework.injection.RequestedInstance;
import org.keycloak.test.framework.injection.Supplier; import org.keycloak.test.framework.injection.Supplier;
import org.keycloak.test.framework.injection.SupplierHelpers; import org.keycloak.test.framework.injection.SupplierHelpers;
import org.keycloak.test.framework.server.KeycloakTestServer;
public class RealmSupplier implements Supplier<RealmResource, InjectRealm> { public class RealmSupplier implements Supplier<ManagedRealm, InjectRealm> {
private static final String REALM_NAME_KEY = "realmName"; private static final String REALM_NAME_KEY = "realmName";
@ -19,12 +20,13 @@ public class RealmSupplier implements Supplier<RealmResource, InjectRealm> {
} }
@Override @Override
public Class<RealmResource> getValueType() { public Class<ManagedRealm> getValueType() {
return RealmResource.class; return ManagedRealm.class;
} }
@Override @Override
public RealmResource getValue(InstanceContext<RealmResource, InjectRealm> instanceContext) { public ManagedRealm getValue(InstanceContext<ManagedRealm, InjectRealm> instanceContext) {
KeycloakTestServer server = instanceContext.getDependency(KeycloakTestServer.class);
Keycloak adminClient = instanceContext.getDependency(Keycloak.class); Keycloak adminClient = instanceContext.getDependency(Keycloak.class);
RealmConfig config = SupplierHelpers.getInstance(instanceContext.getAnnotation().config()); RealmConfig config = SupplierHelpers.getInstance(instanceContext.getAnnotation().config());
@ -43,17 +45,18 @@ public class RealmSupplier implements Supplier<RealmResource, InjectRealm> {
// TODO Token needs to be invalidated after creating realm to have roles for new realm in the token. Maybe lightweight access tokens could help. // TODO Token needs to be invalidated after creating realm to have roles for new realm in the token. Maybe lightweight access tokens could help.
adminClient.tokenManager().invalidate(adminClient.tokenManager().getAccessTokenString()); adminClient.tokenManager().invalidate(adminClient.tokenManager().getAccessTokenString());
return adminClient.realm(realmRepresentation.getRealm()); RealmResource realmResource = adminClient.realm(realmRepresentation.getRealm());
return new ManagedRealm(server.getBaseUrl() + "/realms/" + realmName, realmRepresentation, realmResource);
} }
@Override @Override
public boolean compatible(InstanceContext<RealmResource, InjectRealm> a, RequestedInstance<RealmResource, InjectRealm> b) { public boolean compatible(InstanceContext<ManagedRealm, InjectRealm> a, RequestedInstance<ManagedRealm, InjectRealm> b) {
return a.getAnnotation().config().equals(b.getAnnotation().config()); return a.getAnnotation().config().equals(b.getAnnotation().config());
} }
@Override @Override
public void close(InstanceContext<RealmResource, InjectRealm> instanceContext) { public void close(InstanceContext<ManagedRealm, InjectRealm> instanceContext) {
instanceContext.getValue().remove(); instanceContext.getValue().admin().remove();
} }
} }

View file

@ -1,7 +1,6 @@
package org.keycloak.test.framework.realm; package org.keycloak.test.framework.realm;
import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.Response;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.admin.client.resource.UserResource; import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.representations.idm.UserRepresentation; import org.keycloak.representations.idm.UserRepresentation;
import org.keycloak.test.framework.annotations.InjectUser; import org.keycloak.test.framework.annotations.InjectUser;
@ -10,7 +9,7 @@ import org.keycloak.test.framework.injection.RequestedInstance;
import org.keycloak.test.framework.injection.Supplier; import org.keycloak.test.framework.injection.Supplier;
import org.keycloak.test.framework.injection.SupplierHelpers; import org.keycloak.test.framework.injection.SupplierHelpers;
public class UserSupplier implements Supplier<UserResource, InjectUser> { public class UserSupplier implements Supplier<ManagedUser, InjectUser> {
private static final String USER_UUID_KEY = "userUuid"; private static final String USER_UUID_KEY = "userUuid";
@ -20,13 +19,13 @@ public class UserSupplier implements Supplier<UserResource, InjectUser> {
} }
@Override @Override
public Class<UserResource> getValueType() { public Class<ManagedUser> getValueType() {
return UserResource.class; return ManagedUser.class;
} }
@Override @Override
public UserResource getValue(InstanceContext<UserResource, InjectUser> instanceContext) { public ManagedUser getValue(InstanceContext<ManagedUser, InjectUser> instanceContext) {
RealmResource realm = instanceContext.getDependency(RealmResource.class); ManagedRealm realm = instanceContext.getDependency(ManagedRealm.class);
UserConfig config = SupplierHelpers.getInstance(instanceContext.getAnnotation().config()); UserConfig config = SupplierHelpers.getInstance(instanceContext.getAnnotation().config());
UserRepresentation userRepresentation = config.getRepresentation(); UserRepresentation userRepresentation = config.getRepresentation();
@ -36,26 +35,24 @@ public class UserSupplier implements Supplier<UserResource, InjectUser> {
userRepresentation.setUsername(username); userRepresentation.setUsername(username);
} }
Response response = realm.users().create(userRepresentation); Response response = realm.admin().users().create(userRepresentation);
String uuid = ApiUtil.handleCreatedResponse(response);
String path = response.getLocation().getPath(); instanceContext.addNote(USER_UUID_KEY, uuid);
String userId = path.substring(path.lastIndexOf('/') + 1);
response.close(); UserResource userResource = realm.admin().users().get(uuid);
userRepresentation.setId(uuid);
instanceContext.addNote(USER_UUID_KEY, userId); return new ManagedUser(userRepresentation, userResource);
return realm.users().get(userId);
} }
@Override @Override
public boolean compatible(InstanceContext<UserResource, InjectUser> a, RequestedInstance<UserResource, InjectUser> b) { public boolean compatible(InstanceContext<ManagedUser, InjectUser> a, RequestedInstance<ManagedUser, InjectUser> b) {
return a.getAnnotation().config().equals(b.getAnnotation().config()); return a.getAnnotation().config().equals(b.getAnnotation().config());
} }
@Override @Override
public void close(InstanceContext<UserResource, InjectUser> instanceContext) { public void close(InstanceContext<ManagedUser, InjectUser> instanceContext) {
instanceContext.getValue().remove(); instanceContext.getValue().admin().remove();
} }
} }