package at.gv.egovernment.moa.id.auth.parser; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.w3c.dom.Element; import org.w3c.dom.traversal.NodeIterator; import at.gv.egovernment.moa.id.*; import at.gv.egovernment.moa.id.auth.data.CreateXMLSignatureResponse; import at.gv.egovernment.moa.id.auth.data.SAMLAttribute; import at.gv.egovernment.moa.util.Constants; import at.gv.egovernment.moa.util.DOMUtils; import at.gv.egovernment.moa.util.XPathUtils; /** * Parses an <InfoboxReadResponse> returned from * the security layer * * @author Stefan Knirsch * @version $Id$ */ public class CreateXMLSignatureResponseParser { // // XPath namespace prefix shortcuts // /** Xpath prefix for reaching SecurityLayer 1.0 Namespaces */ private static final String SL10 = Constants.SL10_PREFIX + ":"; /** Xpath prefix for reaching SecurityLayer 1.1 Namespaces */ private static final String SL11 = Constants.SL11_PREFIX + ":"; /** Xpath prefix for reaching SAML Namespaces */ private static final String SAML = Constants.SAML_PREFIX + ":"; /** Xpath prefix for reaching XML-DSIG Namespaces */ private static final String DSIG = Constants.DSIG_PREFIX + ":"; /** Xpath expression to the root element */ private static final String ROOT = "/" + SL11 + "CreateXMLSignatureResponse/"; /** Xpath expression to the SAML:Assertion element */ private static final String SAML_ASSERTION_XPATH = ROOT + SAML + "Assertion"; /** Xpath expression to the SAML:NameIdentifier element */ private static final String SAML_SUBJECT_NAME_IDENTIFIER_XPATH = SAML_ASSERTION_XPATH + "/" + SAML + "AttributeStatement/" + SAML + "Subject/" + SAML + "NameIdentifier"; /** Xpath expression to the AttributeStatement element */ private static final String SAML_ATTRIBUTE_XPATH = SAML_ASSERTION_XPATH + "/" + SAML + "AttributeStatement/" + SAML + "Attribute"; /** Xpath expression to the AttributeValue element */ private static final String SAML_ATTRIBUTE_VALUE_XPATH = SAML + "AttributeValue"; /** This is the root element of the XML-Document provided by the Security Layer Card */ private Element sigResponse; /** * Constructor for CreateXMLSignatureResponseParser. * A DOM-representation of the incoming String will be created * @param xmlResponse <InfoboxReadResponse> as String * @throws AuthenticationException if any authentication error occurs * @throws ParseException if an element cannot be parsed */ public CreateXMLSignatureResponseParser(String xmlResponse) throws AuthenticationException, ParseException { ErrorResponseParser erp = new ErrorResponseParser(xmlResponse); if (erp.getErrorCode() != null) { throw new AuthenticationException("auth.08", new Object[] { erp.getErrorCode(), erp.getErrorInfo()}); } try { InputStream s = new ByteArrayInputStream(xmlResponse.getBytes("UTF-8")); sigResponse = DOMUtils.parseXmlValidating(s); } catch (Throwable t) { throw new ParseException("parser.01", new Object[] { t.toString()}, t); } } /** * Constructor for CreateXMLSignatureResponseParser. * A DOM-representation of the incoming Inputstream will be created * @param xmlResponse <InfoboxReadResponse> as InputStream * @throws AuthenticationException if any Authentication error occurs * @throws ParseException if an element cannot be parsed */ public CreateXMLSignatureResponseParser(InputStream is) throws AuthenticationException, ParseException { ErrorResponseParser erp = new ErrorResponseParser(is); if (erp.getErrorCode() != null) { throw new AuthenticationException("auth.08", new Object[] { erp.getErrorCode(), erp.getErrorInfo()}); } try { sigResponse = DOMUtils.parseXmlValidating(is); } catch (Throwable t) { throw new ParseException("parser.01", new Object[] { t.toString()}, t); } } /** * Constructor for CreateXMLSignatureResponseParser. * The incoming Element will be used for further operations * @param xmlResponse <InfoboxReadResponse> as InputStream */ public CreateXMLSignatureResponseParser(Element xmlResponse) { sigResponse = xmlResponse; } /** * Parses the identity link from <InfoboxReadResponse> * @return Identity link * @throws ParseException */ public CreateXMLSignatureResponse parseResponse() throws ParseException { CreateXMLSignatureResponse cResp; try { cResp = new CreateXMLSignatureResponse(); cResp.setSamlNameIdentifier(XPathUtils.getElementValue(sigResponse, SAML_SUBJECT_NAME_IDENTIFIER_XPATH, null)); cResp.setSamlAssertion((Element) XPathUtils.selectSingleNode(sigResponse, SAML_ASSERTION_XPATH)); NodeIterator attrIter = XPathUtils.selectNodeIterator(sigResponse, SAML_ATTRIBUTE_XPATH); Element samlAttr; List samlAttributes = new ArrayList(); while ((samlAttr = (Element) attrIter.nextNode()) != null) { String attrName = XPathUtils.getAttributeValue(samlAttr, "@AttributeName", ""); String attrNamespace = XPathUtils.getAttributeValue(samlAttr, "@AttributeNamespace", ""); String attrValue = XPathUtils.getElementValue(samlAttr, SAML_ATTRIBUTE_VALUE_XPATH, ""); samlAttributes.add(new SAMLAttribute(attrName, attrNamespace, attrValue)); } SAMLAttribute[] result = new SAMLAttribute[samlAttributes.size()]; samlAttributes.toArray(result); cResp.setSamlAttributes(result); } catch (Throwable t) { throw new ParseException("parser.01", new Object[] { t.toString()}, t); } return cResp; } }