Refactoring to lower methods complexity
This commit is contained in:
parent
97a3e13bc6
commit
4defbc2f6a
2 changed files with 38 additions and 28 deletions
|
@ -139,41 +139,19 @@ public abstract class AbstractScimService<K extends RoleMapperModel, S extends R
|
||||||
EntityOnRemoteScimId externalId = resource.getId()
|
EntityOnRemoteScimId externalId = resource.getId()
|
||||||
.map(EntityOnRemoteScimId::new)
|
.map(EntityOnRemoteScimId::new)
|
||||||
.orElseThrow(() -> new UnexpectedScimDataException("Remote SCIM resource doesn't have an id, cannot import it in Keycloak"));
|
.orElseThrow(() -> new UnexpectedScimDataException("Remote SCIM resource doesn't have an id, cannot import it in Keycloak"));
|
||||||
Optional<ScimResourceMapping> optionalMapping = getScimResourceDao().findByExternalId(externalId, type);
|
if (validMappingAlreadyExists(externalId)) return;
|
||||||
|
|
||||||
// If an existing mapping exists, delete potential dangling references
|
|
||||||
if (optionalMapping.isPresent()) {
|
|
||||||
ScimResourceMapping mapping = optionalMapping.get();
|
|
||||||
if (entityExists(mapping.getIdAsKeycloakId())) {
|
|
||||||
LOGGER.debug("[SCIM] Valid mapping found, skipping");
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
LOGGER.info("[SCIM] Delete a dangling mapping");
|
|
||||||
getScimResourceDao().delete(mapping);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Here no keycloak user/group matching the SCIM external id exists
|
// Here no keycloak user/group matching the SCIM external id exists
|
||||||
// Try to match existing keycloak resource by properties (username, email, name)
|
// Try to match existing keycloak resource by properties (username, email, name)
|
||||||
Optional<KeycloakId> mapped = matchKeycloakMappingByScimProperties(resource);
|
Optional<KeycloakId> mapped = matchKeycloakMappingByScimProperties(resource);
|
||||||
if (mapped.isPresent()) {
|
if (mapped.isPresent()) {
|
||||||
|
// If found a mapped, update
|
||||||
LOGGER.info("[SCIM] Matched SCIM resource " + externalId + " from properties with keycloak entity " + mapped.get());
|
LOGGER.info("[SCIM] Matched SCIM resource " + externalId + " from properties with keycloak entity " + mapped.get());
|
||||||
createMapping(mapped.get(), externalId);
|
createMapping(mapped.get(), externalId);
|
||||||
syncRes.increaseUpdated();
|
syncRes.increaseUpdated();
|
||||||
} else {
|
} else {
|
||||||
switch (scimProviderConfiguration.getImportAction()) {
|
// If not, create it locally or deleting it remotely (according to the configured Import Action)
|
||||||
case CREATE_LOCAL -> {
|
createLocalOrDeleteRemote(syncRes, resource, externalId);
|
||||||
LOGGER.info("[SCIM] Create local resource for SCIM resource " + externalId);
|
|
||||||
KeycloakId id = createEntity(resource);
|
|
||||||
createMapping(id, externalId);
|
|
||||||
syncRes.increaseAdded();
|
|
||||||
}
|
|
||||||
case DELETE_REMOTE -> {
|
|
||||||
LOGGER.info("[SCIM] Delete remote resource " + externalId);
|
|
||||||
scimClient.delete(externalId);
|
|
||||||
}
|
|
||||||
case NOTHING -> LOGGER.info("[SCIM] Import action set to NOTHING");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (UnexpectedScimDataException e) {
|
} catch (UnexpectedScimDataException e) {
|
||||||
if (skipOrStopStrategy.skipInvalidDataFromScimEndpoint(getConfiguration())) {
|
if (skipOrStopStrategy.skipInvalidDataFromScimEndpoint(getConfiguration())) {
|
||||||
|
@ -198,6 +176,38 @@ public abstract class AbstractScimService<K extends RoleMapperModel, S extends R
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean validMappingAlreadyExists(EntityOnRemoteScimId externalId) {
|
||||||
|
Optional<ScimResourceMapping> optionalMapping = getScimResourceDao().findByExternalId(externalId, type);
|
||||||
|
// If an existing mapping exists, delete potential dangling references
|
||||||
|
if (optionalMapping.isPresent()) {
|
||||||
|
ScimResourceMapping mapping = optionalMapping.get();
|
||||||
|
if (entityExists(mapping.getIdAsKeycloakId())) {
|
||||||
|
LOGGER.debug("[SCIM] Valid mapping found, skipping");
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
LOGGER.info("[SCIM] Delete a dangling mapping");
|
||||||
|
getScimResourceDao().delete(mapping);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createLocalOrDeleteRemote(SynchronizationResult syncRes, S resource, EntityOnRemoteScimId externalId) throws UnexpectedScimDataException, InconsistentScimMappingException, InvalidResponseFromScimEndpointException {
|
||||||
|
switch (scimProviderConfiguration.getImportAction()) {
|
||||||
|
case CREATE_LOCAL -> {
|
||||||
|
LOGGER.info("[SCIM] Create local resource for SCIM resource " + externalId);
|
||||||
|
KeycloakId id = createEntity(resource);
|
||||||
|
createMapping(id, externalId);
|
||||||
|
syncRes.increaseAdded();
|
||||||
|
}
|
||||||
|
case DELETE_REMOTE -> {
|
||||||
|
LOGGER.info("[SCIM] Delete remote resource " + externalId);
|
||||||
|
scimClient.delete(externalId);
|
||||||
|
}
|
||||||
|
case NOTHING -> LOGGER.info("[SCIM] Import action set to NOTHING");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
protected abstract S scimRequestBodyForCreate(K roleMapperModel) throws InconsistentScimMappingException;
|
protected abstract S scimRequestBodyForCreate(K roleMapperModel) throws InconsistentScimMappingException;
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ public class ScimClient<S extends ResourceNode> implements AutoCloseable {
|
||||||
this.logAllRequests = detailedLogs;
|
this.logAllRequests = detailedLogs;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ScimClient open(ScrimEndPointConfiguration scimProviderConfiguration, ScimResourceType scimResourceType) {
|
public static <T extends ResourceNode> ScimClient<T> open(ScrimEndPointConfiguration scimProviderConfiguration, ScimResourceType scimResourceType) {
|
||||||
String scimApplicationBaseUrl = scimProviderConfiguration.getEndPoint();
|
String scimApplicationBaseUrl = scimProviderConfiguration.getEndPoint();
|
||||||
Map<String, String> httpHeaders = new HashMap<>();
|
Map<String, String> httpHeaders = new HashMap<>();
|
||||||
httpHeaders.put(HttpHeaders.AUTHORIZATION, scimProviderConfiguration.getAuthorizationHeaderValue());
|
httpHeaders.put(HttpHeaders.AUTHORIZATION, scimProviderConfiguration.getAuthorizationHeaderValue());
|
||||||
|
@ -58,7 +58,7 @@ public class ScimClient<S extends ResourceNode> implements AutoCloseable {
|
||||||
scimApplicationBaseUrl,
|
scimApplicationBaseUrl,
|
||||||
scimClientConfig
|
scimClientConfig
|
||||||
);
|
);
|
||||||
return new ScimClient(scimRequestBuilder, scimResourceType, scimProviderConfiguration.isLogAllScimRequests());
|
return new ScimClient<>(scimRequestBuilder, scimResourceType, scimProviderConfiguration.isLogAllScimRequests());
|
||||||
}
|
}
|
||||||
|
|
||||||
public EntityOnRemoteScimId create(KeycloakId id, S scimForCreation) throws InvalidResponseFromScimEndpointException {
|
public EntityOnRemoteScimId create(KeycloakId id, S scimForCreation) throws InvalidResponseFromScimEndpointException {
|
||||||
|
|
Loading…
Reference in a new issue