@Test public void testComposeResponse() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, KeyException, TransformerException, MarshalException, XMLSignatureException, SAXException, IOException, ParserConfigurationException { Mockito.when(sigHelper.signSamlAssertion(Mockito.any(Document.class))).thenAnswer(new Answer<Document>() { @Override public Document answer(InvocationOnMock invocation) throws Throwable { return (Document) invocation.getArguments()[0]; } }); Map<String, String> attributes = new HashMap<String, String>(); attributes.put("userName", "User Name"); attributes.put("tenant", "tenantId"); attributes.put("edorg", "NC-edorg"); attributes.put("adminRealm", "myrealm"); attributes.put("userType", "staff"); String encodedXml = composer.componseResponse("dest", "issuer", "requestId", "userId", attributes, Arrays.asList("role1", "role2")); String xml = new String(Base64.decodeBase64(encodedXml), "UTF8"); Mockito.verify(sigHelper).signSamlAssertion(Mockito.any(Document.class)); DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance(); fact.setNamespaceAware(true); Document doc = fact.newDocumentBuilder().parse(new InputSource(new StringReader(xml))); assertEquals("dest", doc.getDocumentElement().getAttribute("Destination")); assertEquals("requestId", doc.getDocumentElement().getAttribute("InResponseTo")); assertTrue(hasAttributeValue(doc, "userName", "User Name")); assertTrue(hasAttributeValue(doc, "userId", "userId")); assertTrue(hasAttributeValue(doc, "userType", "staff")); assertTrue(hasAttributeValue(doc, "roles", "role1")); assertTrue(hasAttributeValue(doc, "roles", "role2")); assertTrue(hasAttributeValue(doc, "tenant", "tenantId")); assertTrue(hasAttributeValue(doc, "edorg", "NC-edorg")); assertTrue(hasAttributeValue(doc, "adminRealm", "myrealm")); assertEquals("issuer", xpathOne(doc, "/samlp:Response/saml:Issuer").getTextContent()); assertTrue(xpathOne(doc, "/samlp:Response/@IssueInstant").getTextContent().matches( "\\d\\d\\d\\d-\\d\\d-\\d\\dT\\d\\d:\\d\\d:\\d\\dZ")); assertTrue(xpathOne(doc, "/samlp:Response/saml:Assertion/@IssueInstant").getTextContent().matches( "\\d\\d\\d\\d-\\d\\d-\\d\\dT\\d\\d:\\d\\d:\\d\\dZ")); assertTrue(!xpathOne(doc, "/samlp:Response/@ID").getTextContent().equals("__RESPONSE_ID__")); assertTrue(!xpathOne(doc, "/samlp:Response/saml:Assertion/@ID").getTextContent().equals("__ASSERTION_ID__")); assertTrue(!xpathOne(doc, "/samlp:Response/@Destination").getTextContent().equals("__DESTINATION__")); assertEquals("requestId", xpathOne(doc, "/samlp:Response/saml:Assertion/saml:Subject/saml:SubjectConfirmation/saml:SubjectConfirmationData/@InResponseTo").getTextContent()); assertEquals("dest", xpathOne(doc, "/samlp:Response/saml:Assertion/saml:Subject/saml:SubjectConfirmation/saml:SubjectConfirmationData/@Recipient").getTextContent()); String expDate = xpathOne(doc, "/samlp:Response/saml:Assertion/saml:Subject/saml:SubjectConfirmation/saml:SubjectConfirmationData/@NotOnOrAfter").getTextContent(); DateTime dt = DateTime.parse(expDate); assertTrue(dt.minusMinutes(9).isAfterNow()); assertTrue(dt.minusMinutes(11).isBeforeNow()); }
public String componseResponse(String destination, String issuer, String requestId, String userId, Map<String, String> attributes, List<String> roles) { String unsignedResponse = createUnsignedResponse(destination, issuer, requestId, userId, attributes, roles); byte[] signedResponse = signResponse(unsignedResponse); return Base64.encodeBase64String(signedResponse); }
SamlResponseComposer { public String componseResponse(String destination, String issuer, String requestId, String userId, Map<String, String> attributes, List<String> roles) { String unsignedResponse = createUnsignedResponse(destination, issuer, requestId, userId, attributes, roles); byte[] signedResponse = signResponse(unsignedResponse); return Base64.encodeBase64String(signedResponse); } }
SamlResponseComposer { public String componseResponse(String destination, String issuer, String requestId, String userId, Map<String, String> attributes, List<String> roles) { String unsignedResponse = createUnsignedResponse(destination, issuer, requestId, userId, attributes, roles); byte[] signedResponse = signResponse(unsignedResponse); return Base64.encodeBase64String(signedResponse); }  }
SamlResponseComposer { public String componseResponse(String destination, String issuer, String requestId, String userId, Map<String, String> attributes, List<String> roles) { String unsignedResponse = createUnsignedResponse(destination, issuer, requestId, userId, attributes, roles); byte[] signedResponse = signResponse(unsignedResponse); return Base64.encodeBase64String(signedResponse); }  String componseResponse(String destination, String issuer, String requestId, String userId,
            Map<String, String> attributes, List<String> roles); }
SamlResponseComposer { public String componseResponse(String destination, String issuer, String requestId, String userId, Map<String, String> attributes, List<String> roles) { String unsignedResponse = createUnsignedResponse(destination, issuer, requestId, userId, attributes, roles); byte[] signedResponse = signResponse(unsignedResponse); return Base64.encodeBase64String(signedResponse); }  String componseResponse(String destination, String issuer, String requestId, String userId,
            Map<String, String> attributes, List<String> roles);  }