/* * Copyright 2014 Federal Chancellery Austria * MOA-ID has been developed in a cooperation between BRZ, the Federal * Chancellery Austria - ICT staff unit, and Graz University of Technology. * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. */ package at.gv.egovernment.moa.id.config.webgui.validation.task.impl; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.regex.Pattern; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.gv.egiz.components.configuration.api.Configuration; import at.gv.egiz.components.configuration.api.ConfigurationException; import at.gv.egiz.eaaf.core.impl.utils.KeyValueUtils; import at.gv.egovernment.moa.id.commons.config.MOAIDConfigurationConstants; import at.gv.egovernment.moa.id.commons.validation.ValidationHelper; import at.gv.egovernment.moa.id.config.webgui.exception.ConfigurationTaskValidationException; import at.gv.egovernment.moa.id.config.webgui.exception.ValidationObjectIdentifier; import at.gv.egovernment.moa.id.config.webgui.helper.LanguageHelper; import at.gv.egovernment.moa.id.config.webgui.validation.task.AbstractTaskValidator; import at.gv.egovernment.moa.id.config.webgui.validation.task.IDynamicLoadableTaskValidator; import at.gv.egovernment.moa.util.MiscUtil; /** * @author tlenz * */ public class ServicesProtocolSTORKTask extends AbstractTaskValidator implements IDynamicLoadableTaskValidator { private static final Logger log = LoggerFactory.getLogger(ServicesProtocolSTORKTask.class); private static final List KEYWHITELIST; static { ArrayList temp = new ArrayList(); KEYWHITELIST = Collections.unmodifiableList(temp); } /* (non-Javadoc) * @see at.gv.egovernment.moa.id.config.webgui.validation.task.ITaskValidator#getKeyPrefix() */ @Override public String getKeyPrefix() { return ""; } /* (non-Javadoc) * @see at.gv.egovernment.moa.id.config.webgui.validation.task.ITaskValidator#getName() */ @Override public String getName() { return "Service - General Configuration Task"; } /* (non-Javadoc) * @see at.gv.egovernment.moa.id.config.webgui.validation.task.ITaskValidator#postProcessing(java.util.Map, java.util.List, at.gv.egiz.components.configuration.api.Configuration) */ @Override public Map postProcessing(Map input, List keysToDelete, Configuration dbconfig) { Map newConfigValues = new HashMap(); try { //search actually stored service configurations List storedServices = new ArrayList(); for (String prefix : getModulValidatorPrefix()) { String[] storedService = dbconfig.findConfigurationId(prefix + ".%." + MOAIDConfigurationConstants.SERVICE_UNIQUEIDENTIFIER); if (storedService != null && storedService.length > 0) storedServices.addAll(Arrays.asList(storedService)); } String GUIServiceUniqueID = input.get(MOAIDConfigurationConstants.SERVICE_UNIQUEIDENTIFIER); String selectedServiceKey = null; for (String serviceKey : storedServices) { String storedUniqueId = dbconfig.getStringValue(serviceKey); if (storedUniqueId.equals(GUIServiceUniqueID)) { selectedServiceKey = KeyValueUtils.getPrefixFromKey(serviceKey, MOAIDConfigurationConstants.SERVICE_UNIQUEIDENTIFIER); log.debug("Find service with key: " + selectedServiceKey + " --> Start STORK attribute provider postProcessing."); break; } } //load actually stored attribute provider names for service Map storedAttributeProviders = new HashMap(); if (MiscUtil.isNotEmpty(selectedServiceKey)) { String[] storedAttribteProviderNames = dbconfig.findConfigurationId( selectedServiceKey + "." + MOAIDConfigurationConstants.SERVICE_AUTH_STORK_ATTRIBUTPROVIDER_LIST + ".%." + MOAIDConfigurationConstants.SERVICE_AUTH_STORK_ATTRIBUTPROVIDER_LIST_NAME); if (storedAttribteProviderNames != null) { for (String el : storedAttribteProviderNames) { String attrProviderName = dbconfig.getStringValue(el); storedAttributeProviders.put(attrProviderName, el); } } } Map storkAttrProviders = KeyValueUtils.getSubSetWithPrefix(input, MOAIDConfigurationConstants.SERVICE_AUTH_STORK_ATTRIBUTPROVIDER_LIST); Iterator> interator = storkAttrProviders.entrySet().iterator(); while (interator.hasNext()) { Entry current = interator.next(); if (current.getKey().endsWith(MOAIDConfigurationConstants.SERVICE_AUTH_STORK_ATTRIBUTPROVIDER_LIST_NAME)) { String guiAttrProviderName = current.getValue(); if (storedAttributeProviders.containsKey(guiAttrProviderName)) { log.trace("STORK attribute provider: " + guiAttrProviderName + " is already stored"); storedAttributeProviders.remove(guiAttrProviderName); } else { log.trace("Add new STORK attribute provider: " + guiAttrProviderName + " to service"); } } } if (!storedAttributeProviders.isEmpty()) { log.trace("Remove STORK attribute providers from configuration."); for (String el : storedAttributeProviders.values()) { String removeString = KeyValueUtils.getPrefixFromKey(el, MOAIDConfigurationConstants.SERVICE_AUTH_STORK_ATTRIBUTPROVIDER_LIST_NAME); keysToDelete.add(removeString + "*"); log.debug("Remove STORK attribute provider with key:" + removeString + "*"); } } } catch (ConfigurationException e) { log.error("Configuration access FAILED.", e); } if (newConfigValues.isEmpty()) return null; else return newConfigValues; } /* (non-Javadoc) * @see at.gv.egovernment.moa.id.config.webgui.validation.task.AbstractTaskValidator#tastValidate(java.util.Map) */ @Override protected void taskValidate(Map input) throws ConfigurationTaskValidationException { List errors = new ArrayList(); //TODO: add in further version //STORK MIS attribute provider requires PublicService Target and therefore VIDP config is actually used. // //check V-IDP specific Target configurations // String isBusinessService = input.get(MOAIDConfigurationConstants.SERVICE_BUSINESSSERVICE); // if (!Boolean.parseBoolean(isBusinessService)) { // log.info("STORK V-IDP only allowed as business Service."); // errors.add(new ValidationObjectIdentifier( // MOAIDConfigurationConstants.SERVICE_BUSINESSSERVICE, // "BusinessService", // "STORK V-IDP only allowed as business Service.")); // } String identificationType = input.get(MOAIDConfigurationConstants.SERVICE_AUTH_TARGET_BUSINESS_TYPE); //TODO: add in further version //STORK MIS attribute provider requires PublicService Target and therefore VIDP config is actually used. // if (MiscUtil.isEmpty(identificationType) || // !MOAIDConfigurationConstants.IDENIFICATIONTYPE_STORK.equals(identificationType)) { if (MiscUtil.isNotEmpty(identificationType) && !(MOAIDConfigurationConstants.IDENIFICATIONTYPE_STORK.equals(identificationType) || MOAIDConfigurationConstants.IDENIFICATIONTYPE_EIDAS.equals(identificationType))) { log.info("STORK V-IDP only allowes identification numbers with STORK prefix."); errors.add(new ValidationObjectIdentifier( MOAIDConfigurationConstants.SERVICE_AUTH_TARGET_BUSINESS_TYPE, "BusinessService - IdentificationType", "STORK V-IDP only allowes identification numbers with STORK prefix")); } Map storkAttrProviders = KeyValueUtils.getSubSetWithPrefix(input, MOAIDConfigurationConstants.SERVICE_AUTH_STORK_ATTRIBUTPROVIDER_LIST); Iterator> interator = storkAttrProviders.entrySet().iterator(); while (interator.hasNext()) { Entry current = interator.next(); if (current.getKey().endsWith(MOAIDConfigurationConstants.SERVICE_AUTH_STORK_ATTRIBUTPROVIDER_LIST_NAME)) { log.trace("Validate STORK attribute provider with key: " + current.getKey() + " value: " + current.getValue()); String index = KeyValueUtils.getParentKey(current.getKey()); //validate attribute provider name String attrProviderName = current.getValue(); if (MiscUtil.isEmpty(attrProviderName)) { log.info("AttributeProviderPlugin Name is empty."); errors.add(new ValidationObjectIdentifier( MOAIDConfigurationConstants.SERVICE_AUTH_STORK_ATTRIBUTPROVIDER_LIST + "." + index + "." + MOAIDConfigurationConstants.SERVICE_AUTH_STORK_ATTRIBUTPROVIDER_LIST_NAME, "STORK - Attribute Provider", LanguageHelper.getErrorString("validation.stork.ap.name.empty"))); } else { if (!MOAIDConfigurationConstants.ALLOWED_STORKATTRIBUTEPROVIDERS.contains(attrProviderName)) { log.info("AttributeProviderPlugin Name is not supported."); errors.add(new ValidationObjectIdentifier( MOAIDConfigurationConstants.SERVICE_AUTH_STORK_ATTRIBUTPROVIDER_LIST + "." + index + "." + MOAIDConfigurationConstants.SERVICE_AUTH_STORK_ATTRIBUTPROVIDER_LIST_NAME, "STORK - Attribute Provider", LanguageHelper.getErrorString("validation.stork.ap.name.valid"))); } } String attrProviderURL = storkAttrProviders.get(index + "." + MOAIDConfigurationConstants.SERVICE_AUTH_STORK_ATTRIBUTPROVIDER_LIST_URL); String attrProviderAttr = storkAttrProviders.get(index + "." + MOAIDConfigurationConstants.SERVICE_AUTH_STORK_ATTRIBUTPROVIDER_LIST_ATTRIBUTES); if (MiscUtil.isEmpty(attrProviderURL) || !ValidationHelper.validateURL(attrProviderURL)) { log.info("AttributeProviderPlugin URL has no valid form."); errors.add(new ValidationObjectIdentifier( MOAIDConfigurationConstants.SERVICE_AUTH_STORK_ATTRIBUTPROVIDER_LIST + "." + index + "." + MOAIDConfigurationConstants.SERVICE_AUTH_STORK_ATTRIBUTPROVIDER_LIST_URL, "STORK - Attribute Provider", LanguageHelper.getErrorString("validation.stork.ap.url.valid"))); } if (MiscUtil.isEmpty(attrProviderAttr) || !attrProviderAttr.matches("[a-zA-Z]+(, ?[a-zA-Z]+)*")) { log.info("AttributeProviderPlugin attributes are empty or do not match csv format."); errors.add(new ValidationObjectIdentifier( MOAIDConfigurationConstants.SERVICE_AUTH_STORK_ATTRIBUTPROVIDER_LIST + "." + index + "." + MOAIDConfigurationConstants.SERVICE_AUTH_STORK_ATTRIBUTPROVIDER_LIST_ATTRIBUTES, "STORK - Attribute Provider", LanguageHelper.getErrorString("validation.stork.ap.attributes.valid"))); } } } if (!errors.isEmpty()) throw new ConfigurationTaskValidationException(errors); } /* (non-Javadoc) * @see at.gv.egovernment.moa.id.config.webgui.validation.task.AbstractTaskValidator#getAllAllowedKeys() */ @Override public List getAllAllowedPatterns() { return generatePatternsFromKeys(KEYWHITELIST); } /* (non-Javadoc) * @see at.gv.egovernment.moa.id.config.webgui.validation.task.IDynamicLoadableTaskValidator#getModulValidatorPrefix() */ @Override public List getModulValidatorPrefix() { return Arrays.asList( MOAIDConfigurationConstants.PREFIX_MOAID_SERVICES_VIDP ); } }