package eu.stork.peps.test.simple; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.junit.Test; import org.opensaml.xml.parse.BasicParserPool; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import eu.stork.peps.auth.commons.IPersonalAttributeList; import eu.stork.peps.auth.commons.PersonalAttribute; import eu.stork.peps.auth.commons.PersonalAttributeList; import eu.stork.peps.auth.commons.STORKAttrQueryRequest; import eu.stork.peps.auth.commons.STORKAttrQueryResponse; import eu.stork.peps.auth.commons.STORKAuthnRequest; import eu.stork.peps.auth.commons.STORKAuthnResponse; import eu.stork.peps.auth.commons.STORKStatusCode; import eu.stork.peps.auth.engine.STORKSAMLEngine; import eu.stork.peps.exceptions.STORKSAMLEngineException; public class StorkNewResponseTest { /** The engine. */ private static STORKSAMLEngine engine = STORKSAMLEngine.getInstance("CONF1"); private static STORKSAMLEngine engine0 = STORKSAMLEngine.getInstance("CONF0"); /** * Gets the engine. * * @return the engine */ public static STORKSAMLEngine getEngine() { return engine; } public static STORKSAMLEngine getEngine2() { return engine0; } /** * Sets the engine. * * @param newEngine the new engine */ public static void setEngine(final STORKSAMLEngine newEngine) { StorkNewResponseTest.engine = newEngine; } /** The destination. */ private static String destination; /** The service provider name. */ private static String spName; /** The service provider sector. */ private static String spSector; /** The service provider institution. */ private static String spInstitution; /** The service provider application. */ private static String spApplication; /** The service provider country. */ private static String spCountry; /** The service provider id. */ private static String spId; /** The quality authentication assurance level. */ private static final int QAAL = 3; /** The state. */ private static String state = "ES"; /** The town. */ private static String town = "Madrid"; /** The municipality code. */ private static String municipalityCode = "MA001"; /** The postal code. */ private static String postalCode = "28038"; /** The street name. */ private static String streetName = "Marchamalo"; /** The street number. */ private static String streetNumber = "3"; /** The apartament number. */ private static String apartamentNumber = "5ยบ E"; /** The List of Personal Attributes. */ private static IPersonalAttributeList pal; /** The List of Personal Attributes. */ private static IPersonalAttributeList pal2; /** The assertion consumer URL. */ private static String assertConsumerUrl; /** The authentication request. */ private static byte[] authRequest; /** The authentication response. */ private static byte[] authResponse; /** The authentication request. */ private static STORKAuthnRequest authenRequest; /** The authentication response. */ private static STORKAuthnResponse authnResponse; /** The Constant LOG. */ private static final Logger LOG = LoggerFactory .getLogger(StorkResponseTest.class.getName()); /** * Instantiates a new stork response test. */ public StorkNewResponseTest() { super(); } /** The IP address. */ private static String ipAddress; /** The is hashing. */ private final boolean isHashing = Boolean.TRUE; /** The is not hashing. */ private final boolean isNotHashing = Boolean.FALSE; /** The ERROR text. */ private static final String ERROR_TXT = "generateAuthnResponse(...) should've thrown an STORKSAMLEngineException!"; /** Parser manager used to parse XML. */ private static BasicParserPool parser; /** The attribute query request. */ private static STORKAttrQueryRequest attrQueryenRequest; /** The attribute query response. */ private static STORKAttrQueryResponse attrQeuryenResponse; /** The attribute query request. */ private static byte[] attrQueryRequest; /** The attribute query response. */ private static byte[] attrQueryResponse; static { parser = new BasicParserPool(); parser.setNamespaceAware(true); pal = new PersonalAttributeList(); pal2 = new PersonalAttributeList(); PersonalAttribute isAgeOver = new PersonalAttribute(); isAgeOver.setName("isAgeOver"); isAgeOver.setIsRequired(false); ArrayList ages = new ArrayList(); ages.add("16"); ages.add("18"); isAgeOver.setValue(ages); pal.add(isAgeOver); PersonalAttribute dateOfBirth = new PersonalAttribute(); dateOfBirth.setName("dateOfBirth"); dateOfBirth.setIsRequired(false); pal.add(dateOfBirth); PersonalAttribute eIDNumber = new PersonalAttribute(); eIDNumber.setName("eIdentifier"); eIDNumber.setIsRequired(true); pal.add(eIDNumber); final PersonalAttribute givenName = new PersonalAttribute(); givenName.setName("givenName"); givenName.setIsRequired(true); pal.add(givenName); PersonalAttribute canRessAddress = new PersonalAttribute(); canRessAddress.setName("canonicalResidenceAddress"); canRessAddress.setIsRequired(true); pal.add(canRessAddress); PersonalAttribute newAttribute = new PersonalAttribute(); newAttribute.setName("newAttribute2"); newAttribute.setIsRequired(true); pal.add(newAttribute); PersonalAttribute hasDegree = new PersonalAttribute(); hasDegree.setName("hasDegree"); List vals = new ArrayList(); vals.add("Engineering"); vals.add("Computer Science"); hasDegree.setValue(vals); pal2.add(hasDegree); PersonalAttribute mandate = new PersonalAttribute(); mandate.setName("mandateContent"); List manvalues = new ArrayList(); manvalues.add("Powers"); mandate.setValue(manvalues); pal2.add(mandate); destination = "http://C-PEPS.gov.xx/PEPS/ColleagueRequest"; assertConsumerUrl = "http://S-PEPS.gov.xx/PEPS/ColleagueResponse"; spName = "University Oxford"; spName = "University of Oxford"; spSector = "EDU001"; spInstitution = "OXF001"; spApplication = "APP001"; spCountry = "EN"; spId = "EDU001-APP001-APP001"; final STORKAuthnRequest request = new STORKAuthnRequest(); request.setDestination(destination); request.setProviderName(spName); request.setQaa(QAAL); request.setPersonalAttributeList(pal); request.setAssertionConsumerServiceURL(assertConsumerUrl); // news parameters request.setSpSector(spSector); request.setSpInstitution(spInstitution); request.setSpApplication(spApplication); request.setSpCountry(spCountry); request.setSPID(spId); request.setCitizenCountryCode("ES"); try { authRequest = getEngine().generateSTORKAuthnRequest(request) .getTokenSaml(); authenRequest = getEngine().validateSTORKAuthnRequest(authRequest); } catch (STORKSAMLEngineException e) { fail("Error create STORKAuthnRequest"); } ipAddress = "111.222.333.444"; pal = new PersonalAttributeList(); isAgeOver = new PersonalAttribute(); isAgeOver.setName("isAgeOver"); isAgeOver.setIsRequired(true); ages = new ArrayList(); ages.add("16"); ages.add("18"); isAgeOver.setValue(ages); isAgeOver.setStatus(STORKStatusCode.STATUS_AVAILABLE.toString()); pal.add(isAgeOver); dateOfBirth = new PersonalAttribute(); dateOfBirth.setName("dateOfBirth"); dateOfBirth.setIsRequired(false); final ArrayList date = new ArrayList(); date.add("16/12/2008"); dateOfBirth.setValue(date); dateOfBirth.setStatus(STORKStatusCode.STATUS_AVAILABLE.toString()); pal.add(dateOfBirth); eIDNumber = new PersonalAttribute(); eIDNumber.setName("eIdentifier"); eIDNumber.setIsRequired(true); final ArrayList idNumber = new ArrayList(); idNumber.add("123456789PA"); eIDNumber.setValue(idNumber); eIDNumber.setStatus(STORKStatusCode.STATUS_AVAILABLE.toString()); pal.add(eIDNumber); canRessAddress = new PersonalAttribute(); canRessAddress.setName("canonicalResidenceAddress"); canRessAddress.setIsRequired(true); canRessAddress.setStatus(STORKStatusCode.STATUS_AVAILABLE.toString()); final HashMap address = new HashMap(); address.put("state", state); address.put("municipalityCode", municipalityCode); address.put("town", town); address.put("postalCode", postalCode); address.put("streetName", streetName); address.put("streetNumber", streetNumber); address.put("apartamentNumber", apartamentNumber); canRessAddress.setComplexValue(address); pal.add(canRessAddress); newAttribute = new PersonalAttribute(); newAttribute.setName("newAttribute2"); newAttribute.setIsRequired(true); newAttribute.setStatus(STORKStatusCode.STATUS_AVAILABLE.toString()); final HashMap values = new HashMap(); values.put("value1", "value1"); values.put("value2", "value2"); values.put("value3", "value3"); values.put("value4", "value4"); newAttribute.setComplexValue(values); pal.add(newAttribute); final STORKAttrQueryRequest arequest = new STORKAttrQueryRequest(); arequest.setDestination(destination); //request.setProviderName(spName); arequest.setQaa(QAAL); arequest.setPersonalAttributeList(pal2); request.setAssertionConsumerServiceURL(assertConsumerUrl); // news parameters arequest.setSpSector(spSector); arequest.setSpInstitution(spInstitution); arequest.setSpApplication(spApplication); arequest.setSpCountry(spCountry); arequest.setSPID(spId); arequest.setCitizenCountryCode("IS"); arequest.setAssertionConsumerServiceURL(assertConsumerUrl); try { attrQueryRequest = getEngine().generateSTORKAttrQueryRequest(arequest) .getTokenSaml(); attrQueryenRequest = getEngine().validateSTORKAttrQueryRequest(attrQueryRequest); } catch (STORKSAMLEngineException e) { fail("Error create STORKAuthnRequest"); } } /** * Test generate authentication request without errors. * * @throws STORKSAMLEngineException the STORKSAML engine exception */ @Test public final void testGenerateAuthnResponseWithSimpleRes() throws STORKSAMLEngineException { //Create the response holding pal final STORKAuthnResponse response = new STORKAuthnResponse(); response.setPersonalAttributeList(pal); //List of query responses List resList = new ArrayList(); final STORKAttrQueryResponse aResponse1 = new STORKAttrQueryResponse(); aResponse1.setPersonalAttributeList(pal2); final STORKAttrQueryResponse aStorkResponse1 = getEngine2() .generateSTORKAttrQueryResponse(attrQueryenRequest, aResponse1, ipAddress, destination, isNotHashing); attrQueryResponse = aStorkResponse1.getTokenSaml(); resList.add(getEngine2().validateSTORKAttrQueryResponse(attrQueryResponse, ipAddress)); final STORKAttrQueryResponse aResponse2 = new STORKAttrQueryResponse(); aResponse2.setPersonalAttributeList(pal2); final STORKAttrQueryResponse aStorkResponse2 = getEngine() .generateSTORKAttrQueryResponse(attrQueryenRequest, aResponse2, ipAddress, destination, isNotHashing); attrQueryResponse = aStorkResponse2.getTokenSaml(); resList.add(getEngine().validateSTORKAttrQueryResponse(attrQueryResponse, ipAddress)); //Create the final response containing query responses final STORKAuthnResponse storkResponse = getEngine() .generateSTORKAuthnResponseAfterQuery(authenRequest, response, ipAddress, isNotHashing, resList); authResponse = storkResponse.getTokenSaml(); //Validate, write out and print out the attribute lists a STORKAuthnResponse finalResponse = getEngine().validateSTORKAuthnResponseWithQuery(authResponse, ipAddress); System.out.println("The original pal: " + finalResponse.getPersonalAttributeList().toString()); for (int i = 0; i < finalResponse.getPersonalAttributeLists().size(); i++) { System.out.println("Pal "+ Integer.toString(i) + ": " + finalResponse.getPersonalAttributeLists().get(i).toString()); } System.out.println("The total pal: " + finalResponse.getTotalPersonalAttributeList().toString()); FileOutputStream output = null; try { output = new FileOutputStream(new File(System.getProperty("user.dir") + "/src/test/resources/data/eu/stork/STORKSAMLEngine/AuthAttrQResponse.xml")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { output.write(authResponse); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * Test generate authentication request without errors. * * @throws STORKSAMLEngineException the STORKSAML engine exception */ @Test public final void testGenerateAuthnResponseWithComplexRes() throws STORKSAMLEngineException { //Create the response holding pal final STORKAuthnResponse response = new STORKAuthnResponse(); response.setPersonalAttributeList(pal); //Create a list for the attribute query responses we have List resList = new ArrayList(); //Create the first attribute query response final STORKAttrQueryResponse aResponse1 = new STORKAttrQueryResponse(); aResponse1.setPersonalAttributeList(pal2); //Generate the first response final STORKAttrQueryResponse aStorkResponse1 = getEngine2() .generateSTORKAttrQueryResponse(attrQueryenRequest, aResponse1, ipAddress, destination, isNotHashing); attrQueryResponse = aStorkResponse1.getTokenSaml(); //Validate it and add to the response list resList.add(getEngine2().validateSTORKAttrQueryResponse(attrQueryResponse, ipAddress)); //Create a second response like the first final STORKAttrQueryResponse aResponse2 = new STORKAttrQueryResponse(); aResponse2.setPersonalAttributeList(pal2); final STORKAttrQueryResponse aStorkResponse2 = getEngine() .generateSTORKAttrQueryResponse(attrQueryenRequest, aResponse2, ipAddress, destination, isNotHashing); attrQueryResponse = aStorkResponse2.getTokenSaml(); resList.add(getEngine().validateSTORKAttrQueryResponse(attrQueryResponse, ipAddress)); //Create a attribute query response which contains multiple assertions from an AP final STORKAttrQueryResponse aResponseMulti = new STORKAttrQueryResponse(); aResponseMulti.setPersonalAttributeList(pal2); //Create a list for the responses which the multiple assertion response will hold List multiResponses = new ArrayList(); //Create two more simple responses which the multi assertion response will hold final STORKAttrQueryResponse aResponse3 = new STORKAttrQueryResponse(); aResponse3.setPersonalAttributeList(pal2); final STORKAttrQueryResponse aStorkResponse3 = getEngine2() .generateSTORKAttrQueryResponse(attrQueryenRequest, aResponse3, ipAddress, destination, isNotHashing); attrQueryResponse = aStorkResponse3.getTokenSaml(); //Validate and add to the multi response multiResponses.add(getEngine2().validateSTORKAttrQueryResponse(attrQueryResponse, ipAddress)); final STORKAttrQueryResponse aResponse4 = new STORKAttrQueryResponse(); aResponse4.setPersonalAttributeList(pal2); final STORKAttrQueryResponse aStorkResponse4 = getEngine() .generateSTORKAttrQueryResponse(attrQueryenRequest, aResponse4, ipAddress, destination, isNotHashing); attrQueryResponse = aStorkResponse4.getTokenSaml(); multiResponses.add(getEngine2().validateSTORKAttrQueryResponse(attrQueryResponse, ipAddress)); //Generate the multi assertion query response final STORKAttrQueryResponse aStorkResponseMulti = getEngine() .generateSTORKAttrQueryResponseWithAssertions(attrQueryenRequest, aResponseMulti, multiResponses, ipAddress, destination, isNotHashing); attrQueryResponse = aStorkResponseMulti.getTokenSaml(); //Add to the list of query responses resList.add(getEngine().validateSTORKAttrQueryResponse(attrQueryResponse, ipAddress)); //Generate the stork response with all the query responses final STORKAuthnResponse storkResponse = getEngine() .generateSTORKAuthnResponseAfterQuery(authenRequest, response, ipAddress, isNotHashing, resList); authResponse = storkResponse.getTokenSaml(); //Validate it, write out and print out the personal attribute lists STORKAuthnResponse finalResponse = getEngine().validateSTORKAuthnResponseWithQuery(authResponse, ipAddress); System.out.println("The original pal: " + finalResponse.getPersonalAttributeList().toString()); for (int i = 0; i < finalResponse.getPersonalAttributeLists().size(); i++) { System.out.println("Pal "+ Integer.toString(i) + ": " + finalResponse.getPersonalAttributeLists().get(i).toString()); } System.out.println("The total pal: " + finalResponse.getTotalPersonalAttributeList().toString()); FileOutputStream output = null; try { output = new FileOutputStream(new File(System.getProperty("user.dir") + "/src/test/resources/data/eu/stork/STORKSAMLEngine/AuthAttrQMultiAssertResponse.xml")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { output.write(authResponse); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } assertNotNull(finalResponse); } }