package at.asitplus.eidas.specific.connector.config; import java.net.URL; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import org.apache.commons.lang3.StringUtils; import at.asitplus.eidas.specific.connector.MsEidasNodeConstants; import at.gv.egiz.eaaf.core.api.idp.ISpConfiguration; import at.gv.egiz.eaaf.core.exceptions.EaafConfigurationException; import at.gv.egiz.eaaf.core.exceptions.EaafException; import at.gv.egiz.eaaf.core.impl.idp.conf.AbstractSpringBootConfigurationImpl; import at.gv.egiz.eaaf.core.impl.utils.KeyValueUtils; import lombok.extern.slf4j.Slf4j; @Slf4j public class SpringBootBasicConfigurationProvider extends AbstractSpringBootConfigurationImpl { private final Map spConfigCache = new HashMap<>(); @Override public ISpConfiguration getServiceProviderConfiguration(String entityId) throws EaafConfigurationException { if (!spConfigCache.containsKey(entityId)) { log.debug("SP: " + entityId + " is NOT cached. Starting load operation ... "); final Map allSPs = getBasicConfigurationWithPrefix( MsEidasNodeConstants.PROP_CONFIG_SP_LIST_PREFIX); for (Entry entry : allSPs.entrySet()) { if (entry.getKey().endsWith(MsEidasNodeConstants.PROP_CONFIG_SP_UNIQUEIDENTIFIER) && entry.getValue().equals(entityId)) { final String listId = KeyValueUtils.getParentKey(entry.getKey()); log.trace("Find SP configuration with list-Id: " + listId + ". Extracting configuration elements ... "); final Map spConfig = KeyValueUtils.getSubSetWithPrefix(allSPs, listId + KeyValueUtils.KEY_DELIMITER); spConfigCache.put(entityId, new ServiceProviderConfiguration(spConfig, this)); break; } } if (spConfigCache.containsKey(entityId)) { log.info("SP: " + entityId + " is loaded. Continuing auth. process ... "); } else { log.warn("SP: " + entityId + " is NOT found in configuration. Stopping auth. process ... "); return null; } } else { log.trace("SP: " + entityId + " is already cached. Use configuration from there ... "); } return spConfigCache.get(entityId); } @Override public T getServiceProviderConfiguration(String entityId, Class decorator) throws EaafConfigurationException { final ISpConfiguration spConfig = getServiceProviderConfiguration(entityId); if (spConfig != null && decorator != null) { if (decorator.isInstance(spConfig)) { return (T) spConfig; } else { log.error("SPConfig: " + spConfig.getClass().getName() + " is NOT instance of: " + decorator .getName()); } } return null; } @Override public String validateIdpUrl(URL url) throws EaafException { log.trace("Validate requested URL: " + url); String urlPrefixFromConfig = getBasicConfiguration( MsEidasNodeConstants.PROP_CONFIG_APPLICATION_PUBLIC_URL_PREFIX); if (StringUtils.isEmpty(urlPrefixFromConfig)) { log.warn("Application config containts NO URL prefix"); throw new EaafConfigurationException("config.27", new Object[] { "Application config containts NO " + getApplicationSpecificKeyPrefix() + MsEidasNodeConstants.PROP_CONFIG_APPLICATION_PUBLIC_URL_PREFIX }); } // remove last slash if (urlPrefixFromConfig.endsWith("/")) { urlPrefixFromConfig = urlPrefixFromConfig.substring(0, urlPrefixFromConfig.length() - 1); } if (getBasicConfigurationBoolean( MsEidasNodeConstants.PROP_CONFIG_APPLICATION_PUBLIC_URL_REQUEST_VALIDATION, false)) { if (url != null && url.toExternalForm().startsWith(urlPrefixFromConfig)) { return urlPrefixFromConfig; } log.info("URL: " + url + " does NOT match to allowed application prefix: " + urlPrefixFromConfig); return null; } else { return urlPrefixFromConfig; } } @Override public String getApplicationSpecificKeyPrefix() { return MsEidasNodeConstants.PROP_CONFIG_APPLICATION_PREFIX; } @Override protected String getBackupConfigPath() { return null; } }