package at.asitplus.eidas.specific.modules.auth.eidas.v2.handler; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import at.asitplus.eidas.specific.core.config.IEidasSpConfiguration; import at.asitplus.eidas.specific.modules.auth.eidas.v2.exception.EidPostProcessingException; import at.asitplus.eidas.specific.modules.auth.eidas.v2.exception.EidasAttributeException; import at.gv.egiz.eaaf.core.api.data.EaafConstants; import at.gv.egiz.eaaf.core.api.idp.IConfiguration; import at.gv.egiz.eaaf.core.api.idp.ISpConfiguration; import eu.eidas.auth.commons.light.impl.LightRequest.Builder; import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; /** * Ulraine specific eIDAS AuthnRequest generation. * * @author tlenz * */ @Slf4j public class UaEidProcessor extends AbstractEidProcessor { private static final String CONFIG_PROP_UA_SPECIFIC_LOA = "auth.eIDAS.node_v2.loa.ua.requested"; private static final String CONFIG_PROP_UA_WORKAROUND_DATEOFBIRTH = "auth.eIDAS.node_v2.workaround.ua.dateofbirth"; private static final String STATIC_DATE_OF_BIRTH = "2000-05-29"; private static final String canHandleCC = "UA"; @Autowired IConfiguration config; @Getter @Setter private int priority = 1; @Override public String getName() { return "UA-PostProcessor"; } @Override public boolean canHandle(String countryCode) { return countryCode != null && countryCode.equalsIgnoreCase(canHandleCC); } @Override protected Map getCountrySpecificRequestedAttributes() { return new HashMap<>(); } @Override protected void buildLevelOfAssurance(ISpConfiguration spConfig, Builder authnRequestBuilder) { // allow override of LoA, because UA maybe only support not-notified LoA levels final String uaSpecificLoA = config.getBasicConfiguration(CONFIG_PROP_UA_SPECIFIC_LOA); if (StringUtils.isNotEmpty(uaSpecificLoA)) { authnRequestBuilder.levelsOfAssuranceValues(Arrays.asList(uaSpecificLoA)); // set non-notified LoA as allowed LoA final List allowedLoa = new ArrayList<>(); allowedLoa.addAll(spConfig.getRequiredLoA()); allowedLoa.add(uaSpecificLoA); ((IEidasSpConfiguration) spConfig).setRequiredLoA(allowedLoa); ((IEidasSpConfiguration) spConfig).setLoAMachtingMode(EaafConstants.EIDAS_LOA_MATCHING_EXACT); log.info("Set UA specific LoA level to: {} with matching-mode: {}", StringUtils.join(allowedLoa, "|"), EaafConstants.EIDAS_LOA_MATCHING_EXACT); } else { super.buildLevelOfAssurance(spConfig, authnRequestBuilder); } } @Override protected String processDateOfBirth(Object dateOfBirthObj) throws EidPostProcessingException, EidasAttributeException { final String dateOfBirth = super.processDateOfBirth(dateOfBirthObj); try { final Date dateElement = new SimpleDateFormat("yyyy-MM-dd").parse(dateOfBirth); if (basicConfig.getBasicConfigurationBoolean(CONFIG_PROP_UA_WORKAROUND_DATEOFBIRTH, false) && dateElement.after(new Date())) { log.warn("DateOfBirth: {} is in the future. Use static DateOfBirth as backup", dateOfBirth); return STATIC_DATE_OF_BIRTH; } else { return dateOfBirth; } } catch (final ParseException e) { log.warn("Can not parse dateOfBirth", e); return dateOfBirth; } } }