// TODO - Using a library for managing collections
public static Map<String, List<Object>> parseAttributes(String samlResponse,
      Set<String> attributes) throws ServletException {
    Map<String, List<Object>> parsedResult = new HashMap<>();
    // TODO - Using a library for managing collections
    if (attributes == null || attributes.isEmpty()) {
      return parsedResult;
    }
    byte[] samlAssertionDecoded = new byte[0];
    try {
      samlAssertionDecoded = Base64.decodeBase64(samlResponse.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException("Unsupported encoding to decode Saml Response");
    }
    ByteArrayInputStream is = new ByteArrayInputStream(samlAssertionDecoded);
    Assertion assertion;
    try {
      DocumentBuilder builder = factory.newDocumentBuilder();
      Document document = builder.parse(is);
      Element element = document.getDocumentElement();
      Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(element);
      if (unmarshaller == null) {
        throw new RuntimeException("Could not find an unmarshaller for XML parsing");
      }
      XMLObject responseXmlObj = unmarshaller.unmarshall(element);
      Response response = (Response) responseXmlObj;
      if (response.getAssertions() == null || response.getAssertions().isEmpty()) {
        throw new ServletException("No assertions found in SAML response");
      }
      assertion = response.getAssertions().get(0);
    } catch (UnmarshallingException | ParserConfigurationException | SAXException | IOException e) {
      throw new ServletException("Could not parse the SAML assertion", e);
    }
    for (AttributeStatement attributeStatement : assertion.getAttributeStatements()) {
      for (Attribute attribute : attributeStatement.getAttributes()) {
        if (attributes.contains(attribute.getName())) {
          List<Object> values = new ArrayList<>(attribute.getAttributeValues());
          parsedResult.put(attribute.getName(), values);
        }
      }
    }
    return parsedResult;
  }
Set<String> attributes) throws ServletException {
Map<String, List<Object>> parsedResult = new HashMap<>();
// TODO - Using a library for managing collections
if (attributes == null || attributes.isEmpty()) {
return parsedResult;
public static Map<String, List<Object>> parseAttributes(String samlResponse,
Set<String> attributes) throws ServletException {
Map<String, List<Object>> parsedResult = new HashMap<>();
// TODO - Using a library for managing collections
if (attributes == null || attributes.isEmpty()) {
return parsedResult;
}
byte[] samlAssertionDecoded = new byte[0];
try {
samlAssertionDecoded = Base64.decodeBase64(samlResponse.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Unsupported encoding to decode Saml Response");
}
ByteArrayInputStream is = new ByteArrayInputStream(samlAssertionDecoded);
public static Map<String, List<Object>> parseAttributes(String samlResponse,
Set<String> attributes) throws ServletException {
Map<String, List<Object>> parsedResult = new HashMap<>();
// TODO - Using a library for managing collections
if (attributes == null || attributes.isEmpty()) {
return parsedResult;
}
byte[] samlAssertionDecoded = new byte[0];
try {
samlAssertionDecoded = Base64.decodeBase64(samlResponse.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Unsupported encoding to decode Saml Response");
}
ByteArrayInputStream is = new ByteArrayInputStream(samlAssertionDecoded);
Assertion assertion;
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(is);
Element element = document.getDocumentElement();
Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(element);
if (unmarshaller == null) {
throw new RuntimeException("Could not find an unmarshaller for XML parsing");
}
XMLObject responseXmlObj = unmarshaller.unmarshall(element);