allow content type change

This commit is contained in:
Hugo Renard 2022-02-11 14:10:53 +01:00
parent bdc4846a53
commit 7f127691b2
Signed by: hougo
GPG key ID: 3A285FD470209C59
3 changed files with 23 additions and 6 deletions

View file

@ -32,9 +32,11 @@ public class ScimClient {
final private RetryRegistry registry; final private RetryRegistry registry;
final private String name; final private String name;
final private KeycloakSession session; final private KeycloakSession session;
final private String contentType;
public ScimClient(String name, String url, KeycloakSession session) { public ScimClient(String name, String url, String contentType, KeycloakSession session) {
this.name = name; this.name = name;
this.contentType = contentType;
this.session = session; this.session = session;
var target = client.target(url); var target = client.target(url);
@ -61,12 +63,13 @@ public class ScimClient {
var retry = registry.retry("create-" + kcUser.getId()); var retry = registry.retry("create-" + kcUser.getId());
var spUser = retry.executeSupplier(() -> { var spUser = retry.executeSupplier(() -> {
try { try {
return scimService.create("Users", user); return scimService.createRequest("Users", user).contentType(contentType).invoke();
} catch (ScimException e) { } catch (ScimException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
}); });
var scimUser = toScimUser(spUser); var scimUser = toScimUser(spUser);
scimUser.setLocalId(kcUser.getId());
getEM().persist(scimUser); getEM().persist(scimUser);
} }
@ -83,7 +86,7 @@ public class ScimClient {
var retry = registry.retry("replace-" + kcUser.getId()); var retry = registry.retry("replace-" + kcUser.getId());
retry.executeSupplier(() -> { retry.executeSupplier(() -> {
try { try {
return scimService.replace(user); return scimService.replaceRequest(user).contentType(contentType).invoke();
} catch (ScimException e) { } catch (ScimException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -102,7 +105,7 @@ public class ScimClient {
var retry = registry.retry("delete-" + userId); var retry = registry.retry("delete-" + userId);
retry.executeSupplier(() -> { retry.executeSupplier(() -> {
try { try {
scimService.delete("Users", resource.getRemoteId()); scimService.deleteRequest("Users", resource.getRemoteId()).contentType(contentType).invoke();
} catch (ScimException e) { } catch (ScimException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -149,6 +152,7 @@ public class ScimClient {
name.setGivenName(kcUser.getFirstName()); name.setGivenName(kcUser.getFirstName());
name.setFamilyName(kcUser.getLastName()); name.setFamilyName(kcUser.getLastName());
user.setName(name); user.setName(name);
user.setDisplayName(kcUser.getFirstName() + " " + kcUser.getLastName());
var emails = new ArrayList<Email>(); var emails = new ArrayList<Email>();
if (kcUser.getEmail() != "") { if (kcUser.getEmail() != "") {
@ -156,6 +160,7 @@ public class ScimClient {
emails.add(email); emails.add(email);
} }
user.setEmails(emails); user.setEmails(emails);
user.setActive(kcUser.isEnabled());
return user; return user;
} }

View file

@ -17,11 +17,11 @@ public class ScimDispatcher {
public void run(Consumer<ScimClient> f) { public void run(Consumer<ScimClient> f) {
session.getContext().getRealm().getComponentsStream() session.getContext().getRealm().getComponentsStream()
.filter((m) -> { .filter((m) -> {
return ScimStorageProviderFactory.ID.equals(m.getProviderId()); return ScimStorageProviderFactory.ID.equals(m.getProviderId()) && m.get("enabled").equals("true");
}) })
.forEach(m -> { .forEach(m -> {
LOGGER.infof("%s %s %s %s", m.getId(), m.getName(), m.getProviderId(), m.getProviderType()); LOGGER.infof("%s %s %s %s", m.getId(), m.getName(), m.getProviderId(), m.getProviderType());
var client = new ScimClient(m.getName(), m.get("endpoint"), session); var client = new ScimClient(m.getName(), m.get("endpoint"), m.get("content-type"), session);
try { try {
f.accept(client); f.accept(client);
} finally { } finally {

View file

@ -2,6 +2,10 @@ package sh.libre.scim.storage;
import java.util.List; import java.util.List;
import javax.ws.rs.core.MediaType;
import com.unboundid.scim2.client.ScimService;
import org.keycloak.component.ComponentModel; import org.keycloak.component.ComponentModel;
import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSession;
import org.keycloak.provider.ProviderConfigProperty; import org.keycloak.provider.ProviderConfigProperty;
@ -20,6 +24,14 @@ public class ScimStorageProviderFactory implements UserStorageProviderFactory<Sc
.helpText("External SCIM 2.0 base " + .helpText("External SCIM 2.0 base " +
"URL (/ServiceProviderConfig /Schemas and /ResourcesTypes should be accessible)") "URL (/ServiceProviderConfig /Schemas and /ResourcesTypes should be accessible)")
.add() .add()
.property()
.name("content-type")
.type(ProviderConfigProperty.LIST_TYPE)
.label("Endpoint content type")
.helpText("Only used when endpoint doesn't support application/scim+json")
.options(MediaType.APPLICATION_JSON.toString(), ScimService.MEDIA_TYPE_SCIM_TYPE.toString())
.defaultValue(ScimService.MEDIA_TYPE_SCIM_TYPE.toString())
.add()
.build(); .build();
} }