package at.asitplus.eidas.specific.modules.msproxyservice.test.protocol; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThrows; import java.io.IOException; import java.net.URISyntaxException; import java.util.Arrays; import java.util.List; import java.util.UUID; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.StringUtils; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import at.asitplus.eidas.specific.core.config.ServiceProviderConfiguration; import at.asitplus.eidas.specific.core.test.config.dummy.MsConnectorDummyConfigMap; import at.asitplus.eidas.specific.modules.core.eidas.EidasConstants; import at.asitplus.eidas.specific.modules.core.eidas.service.EidasAttributeRegistry; import at.asitplus.eidas.specific.modules.core.eidas.test.dummy.DummySpecificCommunicationService; import at.asitplus.eidas.specific.modules.msproxyservice.MsProxyServiceConstants; import at.asitplus.eidas.specific.modules.msproxyservice.exception.EidasProxyServiceException; import at.asitplus.eidas.specific.modules.msproxyservice.protocol.EidasProxyServiceController; import at.gv.egiz.eaaf.core.api.data.EaafConstants; import at.gv.egiz.eaaf.core.api.data.ExtendedPvpAttributeDefinitions.SpMandateModes; import at.gv.egiz.eaaf.core.exceptions.EaafException; import at.gv.egiz.eaaf.core.exceptions.EaafStorageException; import at.gv.egiz.eaaf.core.impl.idp.module.test.DummyProtocolAuthService; import eu.eidas.auth.commons.EidasParameterKeys; import eu.eidas.auth.commons.attribute.ImmutableAttributeMap; import eu.eidas.auth.commons.light.impl.LightRequest; import eu.eidas.specificcommunication.protocol.SpecificCommunicationService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/spring/SpringTest-context_basic_test.xml", "/spring/SpringTest-context_basic_mapConfig2.xml", }) @EnableWebMvc public class EidasProxyServiceControllerBorisTest { @Autowired private EidasProxyServiceController controller; @Autowired private DummySpecificCommunicationService proxyService; @Autowired private DummyProtocolAuthService authService; @Autowired private EidasAttributeRegistry attrRegistry; @Autowired private ApplicationContext context; @Autowired MsConnectorDummyConfigMap config; private MockHttpServletRequest httpReq; private MockHttpServletResponse httpResp; private SpecificCommunicationService springManagedSpecificConnectorCommunicationService; /** * jUnit test set-up. */ @Before public void setUp() throws EaafStorageException, URISyntaxException { httpReq = new MockHttpServletRequest("POST", "http://localhost/ms_connector/eidas/light/idp/redirect"); httpResp = new MockHttpServletResponse(); RequestContextHolder.resetRequestAttributes(); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(httpReq, httpResp)); proxyService.setiLightRequest(null); proxyService.setError(null); config.putConfigValue("eidas.ms.auth.eIDAS.node_v2.proxy.forward.endpoint", "http://eidas.proxy/endpoint"); springManagedSpecificConnectorCommunicationService = (SpecificCommunicationService) context.getBean( EidasConstants.SPECIFIC_PROXYSERVICE_COMMUNICATION_SERVICE); } @Test public void bothBorisAttributesInSingleRequest() throws IOException, EaafException { //initialize state httpReq.addParameter(EidasParameterKeys.TOKEN.toString(), RandomStringUtils.randomAlphanumeric(10)); String spCountryCode = RandomStringUtils.randomAlphabetic(2).toUpperCase(); String issuer = RandomStringUtils.randomAlphabetic(10); LightRequest.Builder authnReqBuilder = LightRequest.builder() .id(UUID.randomUUID().toString()) .issuer(issuer) .citizenCountryCode(RandomStringUtils.randomAlphabetic(2).toUpperCase()) .levelOfAssurance(EaafConstants.EIDAS_LOA_HIGH) .spCountryCode(spCountryCode) .spType("public") .requestedAttributes(ImmutableAttributeMap.builder() .put(attrRegistry.getCoreAttributeRegistry().getByFriendlyName( EidasConstants.eIDAS_ATTR_LEGALNAME).first()) .put(attrRegistry.getCoreAttributeRegistry().getByFriendlyName( EidasConstants.eIDAS_ATTR_LEGALPERSONIDENTIFIER).first()) .put(attrRegistry.getCoreAttributeRegistry().getByFriendlyName( "eJusticeLegalPersonRole").first()) .put(attrRegistry.getCoreAttributeRegistry().getByFriendlyName( "eJusticeNaturalPersonRole").first()) .build()); proxyService.setiLightRequest(authnReqBuilder.build()); // set default mandate configuration List mandateProfilesNat = Arrays.asList(RandomStringUtils.randomAlphabetic(5), RandomStringUtils.randomAlphabetic(5)); List mandateProfilesJur = Arrays.asList(RandomStringUtils.randomAlphabetic(5), RandomStringUtils.randomAlphabetic(5)); config.putConfigValue(MsProxyServiceConstants.CONIG_PROPS_EIDAS_PROXY_MANDATES_ENABLED, "true"); config.putConfigValue(MsProxyServiceConstants.CONIG_PROPS_EIDAS_PROXY_MANDATES_PROFILE_DEFAULT_NATURAL, StringUtils.join(mandateProfilesNat, ",")); config.putConfigValue(MsProxyServiceConstants.CONIG_PROPS_EIDAS_PROXY_MANDATES_PROFILE_DEFAULT_LEGAL, StringUtils.join(mandateProfilesJur, ",")); //execute EidasProxyServiceException error = assertThrows("wrong exception", EidasProxyServiceException.class, () -> controller.receiveEidasAuthnRequest(httpReq, httpResp)); assertEquals("wrong errorCode", "eidas.proxyservice.ejustice.01", error.getErrorId()); } @Test public void validAuthnRequestWithBorisAttributeLegal() throws IOException, EaafException { //initialize state httpReq.addParameter(EidasParameterKeys.TOKEN.toString(), RandomStringUtils.randomAlphanumeric(10)); String spCountryCode = RandomStringUtils.randomAlphabetic(2).toUpperCase(); String issuer = RandomStringUtils.randomAlphabetic(10); LightRequest.Builder authnReqBuilder = LightRequest.builder() .id(UUID.randomUUID().toString()) .issuer(issuer) .citizenCountryCode(RandomStringUtils.randomAlphabetic(2).toUpperCase()) .levelOfAssurance(EaafConstants.EIDAS_LOA_HIGH) .spCountryCode(spCountryCode) .spType("public") .requestedAttributes(ImmutableAttributeMap.builder() .put(attrRegistry.getCoreAttributeRegistry().getByFriendlyName( EidasConstants.eIDAS_ATTR_LEGALNAME).first()) .put(attrRegistry.getCoreAttributeRegistry().getByFriendlyName( EidasConstants.eIDAS_ATTR_LEGALPERSONIDENTIFIER).first()) .put(attrRegistry.getCoreAttributeRegistry().getByFriendlyName( "eJusticeLegalPersonRole").first()) .build()); proxyService.setiLightRequest(authnReqBuilder.build()); // set default mandate configuration List mandateProfilesNat = Arrays.asList(RandomStringUtils.randomAlphabetic(5), RandomStringUtils.randomAlphabetic(5)); List mandateProfilesJur = Arrays.asList(RandomStringUtils.randomAlphabetic(5), RandomStringUtils.randomAlphabetic(5)); config.putConfigValue(MsProxyServiceConstants.CONIG_PROPS_EIDAS_PROXY_MANDATES_ENABLED, "true"); config.putConfigValue(MsProxyServiceConstants.CONIG_PROPS_EIDAS_PROXY_MANDATES_PROFILE_DEFAULT_NATURAL, StringUtils.join(mandateProfilesNat, ",")); config.putConfigValue(MsProxyServiceConstants.CONIG_PROPS_EIDAS_PROXY_MANDATES_PROFILE_DEFAULT_LEGAL, StringUtils.join(mandateProfilesJur, ",")); //execute controller.receiveEidasAuthnRequest(httpReq, httpResp); //validate state ServiceProviderConfiguration spConfig = authService.getPendingReq().getServiceProviderConfiguration(ServiceProviderConfiguration.class); assertNotNull("mandateprofiles", spConfig.getMandateProfiles()); assertFalse("mandateprofiles not empty", spConfig.getMandateProfiles().isEmpty()); assertEquals("mandateprofile size", 1, spConfig.getMandateProfiles().size()); assertEquals("mandateprofile", "MUST_BE_UPDATED", spConfig.getMandateProfiles().get(0)); assertEquals("MandateMode", SpMandateModes.LEGAL, spConfig.getMandateMode()); assertEquals("requested IDA attributes", 10, spConfig.getRequestedAttributes().size()); } @Test public void validAuthnRequestWithBorisAttributeNat() throws IOException, EaafException { //initialize state httpReq.addParameter(EidasParameterKeys.TOKEN.toString(), RandomStringUtils.randomAlphanumeric(10)); String spCountryCode = RandomStringUtils.randomAlphabetic(2).toUpperCase(); String issuer = RandomStringUtils.randomAlphabetic(10); LightRequest.Builder authnReqBuilder = LightRequest.builder() .id(UUID.randomUUID().toString()) .issuer(issuer) .citizenCountryCode(RandomStringUtils.randomAlphabetic(2).toUpperCase()) .levelOfAssurance(EaafConstants.EIDAS_LOA_HIGH) .spCountryCode(spCountryCode) .spType("public") .requestedAttributes(ImmutableAttributeMap.builder() .put(attrRegistry.getCoreAttributeRegistry().getByFriendlyName( EidasConstants.eIDAS_ATTR_PERSONALIDENTIFIER).first()) .put(attrRegistry.getCoreAttributeRegistry().getByFriendlyName( "eJusticeNaturalPersonRole").first()) .build()); proxyService.setiLightRequest(authnReqBuilder.build()); // set default mandate configuration List mandateProfilesNat = Arrays.asList(RandomStringUtils.randomAlphabetic(5), RandomStringUtils.randomAlphabetic(5)); List mandateProfilesJur = Arrays.asList(RandomStringUtils.randomAlphabetic(5), RandomStringUtils.randomAlphabetic(5)); config.putConfigValue(MsProxyServiceConstants.CONIG_PROPS_EIDAS_PROXY_MANDATES_ENABLED, "true"); config.putConfigValue(MsProxyServiceConstants.CONIG_PROPS_EIDAS_PROXY_MANDATES_PROFILE_DEFAULT_NATURAL, StringUtils.join(mandateProfilesNat, ",")); config.putConfigValue(MsProxyServiceConstants.CONIG_PROPS_EIDAS_PROXY_MANDATES_PROFILE_DEFAULT_LEGAL, StringUtils.join(mandateProfilesJur, ",")); //execute controller.receiveEidasAuthnRequest(httpReq, httpResp); //validate state ServiceProviderConfiguration spConfig = authService.getPendingReq().getServiceProviderConfiguration(ServiceProviderConfiguration.class); assertNotNull("mandateprofiles", spConfig.getMandateProfiles()); assertFalse("mandateprofiles not empty", spConfig.getMandateProfiles().isEmpty()); assertEquals("mandateprofile size", 1, spConfig.getMandateProfiles().size()); assertEquals("mandateprofile", "MUST_BE_UPDATED", spConfig.getMandateProfiles().get(0)); assertEquals("MandateMode", SpMandateModes.LEGAL, spConfig.getMandateMode()); assertEquals("requested IDA attributes", 6, spConfig.getRequestedAttributes().size()); } }