All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
pl.edu.icm.unity.engine.userimport.UserImportServiceImpl Maven / Gradle / Ivy
/*
* Copyright (c) 2016 ICM Uniwersytet Warszawski All rights reserved.
* See LICENCE.txt file for licensing information.
*/
package pl.edu.icm.unity.engine.userimport;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.stream.Collectors;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import eu.unicore.util.configuration.ConfigurationException;
import pl.edu.icm.unity.base.identity.IdentityTaV;
import pl.edu.icm.unity.base.utils.Log;
import pl.edu.icm.unity.engine.api.authn.AuthenticationResult;
import pl.edu.icm.unity.engine.api.authn.AuthenticationResult.Status;
import pl.edu.icm.unity.engine.api.authn.RemoteAuthenticationException;
import pl.edu.icm.unity.engine.api.authn.RemoteAuthenticationResult;
import pl.edu.icm.unity.engine.api.authn.remote.RemoteAuthnResultTranslator;
import pl.edu.icm.unity.engine.api.config.ConfigurationLoader;
import pl.edu.icm.unity.engine.api.config.UnityServerConfiguration;
import pl.edu.icm.unity.engine.api.userimport.UserImportSPI;
import pl.edu.icm.unity.engine.api.userimport.UserImportSPIFactory;
import pl.edu.icm.unity.engine.api.userimport.UserImportSerivce;
import pl.edu.icm.unity.engine.api.userimport.UserImportSpec;
/**
* Implementation of user import service. Loads configured importers, configures them and run when requested.
* Maintains timers to skip too frequent imports.
*/
@Component
public class UserImportServiceImpl implements UserImportSerivce
{
private static final Logger log = Log.getLogger(Log.U_SERVER_USER_IMPORT, UserImportServiceImpl.class);
private Map handlersByKey;
@Autowired
public UserImportServiceImpl(UnityServerConfiguration mainCfg, Optional> importersF,
RemoteAuthnResultTranslator remoteAuthnResultProcessor)
{
this(mainCfg, importersF.orElse(new ArrayList<>()),
remoteAuthnResultProcessor, new ConfigurationLoader());
}
public UserImportServiceImpl(UnityServerConfiguration mainCfg, List importersF,
RemoteAuthnResultTranslator verificatorUtil,
ConfigurationLoader configLoader)
{
Map importersFM = new HashMap<>();
importersF.forEach(spiF -> importersFM.put(spiF.getName(), spiF));
List definedImporters = mainCfg.getSortedListKeys(
UnityServerConfiguration.IMPORT_PFX);
handlersByKey = new HashMap<>();
for (String key: definedImporters)
{
String importerCfg = mainCfg.getValue(UnityServerConfiguration.IMPORT_PFX + key);
handlersByKey.put(key, loadHandler(importerCfg, importersFM,
verificatorUtil, configLoader));
}
}
private SingleUserImportHandler loadHandler(String importerCfg, Map importersFM,
RemoteAuthnResultTranslator verificatorUtil,
ConfigurationLoader cfgLoader)
{
Properties properties = cfgLoader.getProperties(importerCfg);
UserImportProperties cfg = new UserImportProperties(properties);
String type = cfg.getValue(UserImportProperties.TYPE);
UserImportSPIFactory userImportSPIFactory = importersFM.get(type);
if (userImportSPIFactory == null)
throw new ConfigurationException("The type '" + type +
"' of user import is not known in " + importerCfg +
". Known types are: " + importersFM.keySet());
String remoteIdp = cfg.getValue(UserImportProperties.REMOTE_IDP_NAME);
UserImportSPI instance = userImportSPIFactory.getInstance(properties, remoteIdp);
return new SingleUserImportHandler(verificatorUtil, instance, cfg);
}
@Override
public List importToExistingUser(List imports,
IdentityTaV existingUser)
{
return importUser(imports, Optional.of(existingUser));
}
@Override
public List importUser(List imports)
{
return importUser(imports, Optional.empty());
}
private List importUser(List imports,
Optional existingIdentity)
{
if (imports.size() == 1 && imports.get(0).isUseAllImporters())
imports = getAllImportersFor(imports.get(0).identityValue,
imports.get(0).identityType);
List ret = new ArrayList<>();
for (UserImportSpec userImport: imports)
{
log.debug("Trying to import user {} from {}", userImport.identityValue,
userImport.importerKey);
if (userImport.identityValue == null)
{
log.warn("There is no identity to import");
continue;
}
SingleUserImportHandler handler = handlersByKey.get(userImport.importerKey);
if (handler == null)
{
log.warn("There is no importer configured with key {}, skipping it",
userImport.importerKey);
continue;
}
AuthenticationResult result;
try
{
result = handler.importUser(userImport.identityValue,
userImport.identityType, existingIdentity);
} catch (RemoteAuthenticationException e)
{
log.debug("User import has thrown an authentication exception, skipping it", e);
ret.add(new ImportResult(userImport.importerKey, RemoteAuthenticationResult.notApplicable()));
continue;
} catch (Exception e)
{
log.debug("User import has thrown an exception, skipping it", e);
ret.add(new ImportResult(userImport.importerKey, RemoteAuthenticationResult.notApplicable()));
continue;
}
ret.add(new ImportResult(userImport.importerKey, result != null ?
result : RemoteAuthenticationResult.notApplicable()));
if (result != null && result.getStatus() != Status.notApplicable)
{
log.info("Import handler {} has imported the user {}",
userImport.importerKey, userImport.identityValue);
} else
{
log.info("Import handler {} returned nothing or notApplicable status",
userImport.importerKey);
}
}
return ret;
}
private List getAllImportersFor(String identityValue, String identityType)
{
return handlersByKey.keySet().stream()
.map(key -> new UserImportSpec(key, identityValue, identityType))
.collect(Collectors.toList());
}
}