package at.asitplus.eidas.specific.modules.authmodule_eIDASv2.service; import java.io.File; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.PostConstruct; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import at.asitplus.eidas.specific.modules.authmodule_eIDASv2.Constants; import at.gv.egiz.eaaf.core.api.idp.IConfiguration; import at.gv.egiz.eaaf.core.exceptions.EAAFConfigurationException; import at.gv.egiz.eaaf.core.impl.utils.KeyValueUtils; import eu.eidas.auth.commons.attribute.AttributeRegistries; import eu.eidas.auth.commons.attribute.AttributeRegistry; @Service("attributeRegistry") public class eIDASAttributeRegistry { private static final Logger log = LoggerFactory.getLogger(eIDASAttributeRegistry.class); @Autowired private IConfiguration basicConfig; private AttributeRegistry coreAttributeRegistry; private String eidasAttributesFile; private String additionalAttributesFile; @PostConstruct private void initialize() throws RuntimeException { try { if (eidasAttributesFile.isEmpty()) { log.error("Basic eIDAS addribute definition NOT defined"); throw new EAAFConfigurationException("config.30", new Object[] {"eidas-attributes.xml"}); } boolean additionalAttrAvailabe = false; if (!additionalAttributesFile.isEmpty()) { File file = new File(additionalAttributesFile); if (file.exists()) additionalAttrAvailabe = true; } if (!additionalAttrAvailabe) { log.info("Start eIDAS ref. impl. Core without additional eIDAS attribute definitions ... "); coreAttributeRegistry = AttributeRegistries.fromFiles(eidasAttributesFile, null); } else { //load attribute definitions log.info("Start eIDAS ref. impl. Core with additional eIDAS attribute definitions ... "); coreAttributeRegistry = AttributeRegistries.fromFiles(eidasAttributesFile, null, additionalAttributesFile); } } catch (Throwable e) { log.error("Can NOT initialize eIDAS attribute definition." , e); new RuntimeException("Can NOT initialize eIDAS attribute definition.", e); } } public AttributeRegistry getCoreAttributeRegistry() { return coreAttributeRegistry; } public Map getAttributeSetFromConfiguration() { Map result = new HashMap(); /*TODO: select set for representation if mandates should be used. * It's an open task in respect to requested eIDAS attributes and isRequired flag, * because there can be a decision problem in case of natural or legal person representation! * From an Austrian use-case point of view, an Austrian service provider can support mandates for * natural and legal persons at the same time. However, we CAN NOT request attributes for natural AND * legal persons on the same time, because it's not possible to represent both simultaneously. */ Map configAttributes = basicConfig.getBasicMOAIDConfigurationWithPrefix( Constants.CONIG_PROPS_EIDAS_NODE_ATTRIBUTES_REQUESTED_ONLYNATURAL); for (String el: configAttributes.values()) { if (StringUtils.isNotEmpty(el.trim())) { List attrDef = KeyValueUtils.getListOfCSVValues(el.trim()); boolean isRequired = false; if (attrDef.size() == 2) isRequired = Boolean.parseBoolean(attrDef.get(1)); result.put(attrDef.get(0), isRequired); } } log.trace("Load #" + result.size() + " requested attributes from configuration"); return result; } public void setEidasAttributesFile(String eidasAttributesFile) { this.eidasAttributesFile = eidasAttributesFile; } public void setAdditionalAttributesFile(String additionalAttributesFile) { this.additionalAttributesFile = additionalAttributesFile; } }