From 235241ec5f18ad26c013b091887ce5f651462553 Mon Sep 17 00:00:00 2001 From: "harald.bratko" Date: Fri, 2 Sep 2005 16:01:29 +0000 Subject: Redesigned because response has been parsed twice. git-svn-id: https://joinup.ec.europa.eu/svn/moa-idspss/trunk@490 d688527b-c9ab-4aba-bd8d-4036d912da1d --- .../parser/CreateXMLSignatureResponseParser.java | 88 ++++++++------- .../moa/id/auth/parser/ErrorResponseParser.java | 92 +++++++--------- .../auth/parser/IdentityLinkAssertionParser.java | 13 ++- .../id/auth/parser/InfoboxReadResponseParser.java | 122 ++++++++++++++------- 4 files changed, 178 insertions(+), 137 deletions(-) (limited to 'id.server/src/at/gv/egovernment') diff --git a/id.server/src/at/gv/egovernment/moa/id/auth/parser/CreateXMLSignatureResponseParser.java b/id.server/src/at/gv/egovernment/moa/id/auth/parser/CreateXMLSignatureResponseParser.java index cae470cc4..4264ca2cb 100644 --- a/id.server/src/at/gv/egovernment/moa/id/auth/parser/CreateXMLSignatureResponseParser.java +++ b/id.server/src/at/gv/egovernment/moa/id/auth/parser/CreateXMLSignatureResponseParser.java @@ -45,26 +45,22 @@ public class CreateXMLSignatureResponseParser { 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; + /** This is the root element of the CreateXMLsignatureResponse */ + private Element sigResponse_; /** - * Constructor for CreateXMLSignatureResponseParser. - * A DOM-representation of the incoming String will be created - * @param xmlResponse <InfoboxReadResponse> as String + * Parses and validates the document given as string and extracts the + * root element. + * + * @param xmlResponse <CreateXMLSignatureResponse> 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); + init(s); } catch (Throwable t) { throw new ParseException("parser.01", new Object[] { t.toString()}, t); @@ -72,27 +68,16 @@ public class CreateXMLSignatureResponseParser { } /** - * 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 + * Parses and validates the document given as stream and extracts the + * root element. + * + * @param xmlResponse <CreateXMLSignatureResponse> as String + * + * @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); - } + init(is); } /** @@ -101,13 +86,40 @@ public class CreateXMLSignatureResponseParser { * @param xmlResponse <InfoboxReadResponse> as InputStream */ public CreateXMLSignatureResponseParser(Element xmlResponse) { - sigResponse = xmlResponse; - + sigResponse_ = xmlResponse; + } + + /** + * Initializes the parser. + * Parses and validates the document given as stream and extracts the + * root element. + * + * @param is The CreateXMLSignatureResponse as stream. + * @throws AuthenticationException if an authentication error occurs. + * @throws ParseException If an error occurs on parsing the the document. + */ + private void init(InputStream is) throws AuthenticationException, ParseException { + try { + + Element responseElem = DOMUtils.parseXmlValidating(is); + + if ("CreateXMLSignatureResponse".equals(responseElem.getLocalName())) { + sigResponse_ = responseElem; + } else { + ErrorResponseParser erp = new ErrorResponseParser(responseElem); + throw new AuthenticationException("auth.08", new Object[] { erp.getErrorCode(), erp.getErrorInfo()}); + } + + } catch (Throwable t) { + throw new ParseException("parser.01", new Object[] { t.toString()}, t); + } } /** - * Parses the identity link from <InfoboxReadResponse> - * @return Identity link + * Unmarshalls the <@link sigResponse> to an + * <CreateXMLSignatureResponse> object. + * + * @return a <CreateXMLSignatureResponse> object * @throws ParseException */ @@ -115,10 +127,10 @@ public class CreateXMLSignatureResponseParser { CreateXMLSignatureResponse cResp; try { cResp = new CreateXMLSignatureResponse(); - String slPrefix = XPathUtils.getSlPrefix(sigResponse); - cResp.setSamlNameIdentifier(XPathUtils.getElementValue(sigResponse, "/" + slPrefix + SAML_SUBJECT_NAME_IDENTIFIER_XPATH, null)); - cResp.setSamlAssertion((Element) XPathUtils.selectSingleNode(sigResponse, "/" + slPrefix + SAML_ASSERTION_XPATH)); - NodeIterator attrIter = XPathUtils.selectNodeIterator(sigResponse, "/" + slPrefix + SAML_ATTRIBUTE_XPATH); + String slPrefix = XPathUtils.getSlPrefix(sigResponse_); + cResp.setSamlNameIdentifier(XPathUtils.getElementValue(sigResponse_, "/" + slPrefix + SAML_SUBJECT_NAME_IDENTIFIER_XPATH, null)); + cResp.setSamlAssertion((Element) XPathUtils.selectSingleNode(sigResponse_, "/" + slPrefix + SAML_ASSERTION_XPATH)); + NodeIterator attrIter = XPathUtils.selectNodeIterator(sigResponse_, "/" + slPrefix + SAML_ATTRIBUTE_XPATH); Element samlAttr; List samlAttributes = new ArrayList(); while ((samlAttr = (Element) attrIter.nextNode()) != null) { diff --git a/id.server/src/at/gv/egovernment/moa/id/auth/parser/ErrorResponseParser.java b/id.server/src/at/gv/egovernment/moa/id/auth/parser/ErrorResponseParser.java index 7084faf1f..8edeec8ae 100644 --- a/id.server/src/at/gv/egovernment/moa/id/auth/parser/ErrorResponseParser.java +++ b/id.server/src/at/gv/egovernment/moa/id/auth/parser/ErrorResponseParser.java @@ -1,89 +1,71 @@ package at.gv.egovernment.moa.id.auth.parser; -import java.io.ByteArrayInputStream; -import java.io.InputStream; +import java.util.List; import org.w3c.dom.Element; +import org.w3c.dom.NodeList; import at.gv.egovernment.moa.id.ParseException; import at.gv.egovernment.moa.util.DOMUtils; -import at.gv.egovernment.moa.util.XPathUtils; /** - * Parses an <InfoboxReadResponse>. + * Parses an <ErrorResponse>. * * @author Stefan Knirsch * @version $Id$ */ public class ErrorResponseParser { - - /** This is the root element of the XML-Document provided by the Security Layer Card */ - private Element errorElement; - + /** - * Constructor for InfoboxReadResponseParser. - * A DOM-representation of the incoming String will be created - * @param xmlResponse <InfoboxReadResponse> as String - * @throws ParseException on any error + * The error code included in this error response. + * 1000 is used as default value, if some problems occur on + * evaluating the error response. */ - public ErrorResponseParser(String xmlResponse) throws ParseException { - try { - InputStream s = new ByteArrayInputStream(xmlResponse.getBytes("UTF-8")); - errorElement = DOMUtils.parseXmlValidating(s); - } - catch (Throwable t) { - throw new ParseException("parser.01", new Object[] { t.toString()}, t); - } - } + private String errorCode_ = "1000"; + + /** + * The error info included in this error response. + * <Unklassifizierter Fehler.> is used as default value, + * if some problems occur on evaluating the error response. + */ + private String errorInfo_ = "Unklassifizierter Fehler."; + /** - * Constructor for InfoboxReadResponseParser. - * A DOM-representation of the incoming Inputstream will be created - * @param xmlResponse <InfoboxReadResponse> as InputStream - * @throws ParseException on any error + * This Constructor extracts the error code and error info included in this + * error response. + * + * @param errorElement The error element. This is the root element of + * the error response. */ - public ErrorResponseParser(InputStream xmlResponse) throws ParseException { - try { - errorElement = DOMUtils.parseXmlValidating(xmlResponse); - } - catch (Throwable t) { - throw new ParseException("parser.01", new Object[] { t.toString() }, t); + public ErrorResponseParser(Element errorElement) throws ParseException { + if (errorElement != null) { + String namespace = errorElement.getNamespaceURI(); + NodeList nl = errorElement.getElementsByTagNameNS(namespace, "ErrorCode"); + if (nl.getLength() == 1) { + errorCode_ = ((Element)nl.item(0)).getFirstChild().getNodeValue(); + } + nl = errorElement.getElementsByTagNameNS(namespace, "Info"); + if (nl.getLength() == 1) { + errorInfo_ = ((Element)nl.item(0)).getFirstChild().getNodeValue(); + } } } /** - * Method getErrorCode. returns the error code - * @return String + * Returns the error code included in this error response. */ public String getErrorCode() { - - String slPrefix = XPathUtils.getSlPrefix(errorElement); - StringBuffer sb = new StringBuffer("/"); - sb.append(slPrefix); - sb.append(":ErrorResponse/"); - sb.append(slPrefix); - sb.append(":ErrorCode"); - String errorCodeXPath = sb.toString(); - return XPathUtils.getElementValue(errorElement,errorCodeXPath,null); - + return errorCode_ ; } /** - * Method getErrorInfo: returns the information about the error - * @return String + * Returns the information included in this error response. + * @return */ public String getErrorInfo() { - - String slPrefix = XPathUtils.getSlPrefix(errorElement); - StringBuffer sb = new StringBuffer("/"); - sb.append(slPrefix); - sb.append(":ErrorResponse/"); - sb.append(slPrefix); - sb.append(":Info"); - String errorInfoXPath = sb.toString(); - return XPathUtils.getElementValue(errorElement,errorInfoXPath,null); - + return errorInfo_ ; } diff --git a/id.server/src/at/gv/egovernment/moa/id/auth/parser/IdentityLinkAssertionParser.java b/id.server/src/at/gv/egovernment/moa/id/auth/parser/IdentityLinkAssertionParser.java index 2df33725a..9ed6909b3 100644 --- a/id.server/src/at/gv/egovernment/moa/id/auth/parser/IdentityLinkAssertionParser.java +++ b/id.server/src/at/gv/egovernment/moa/id/auth/parser/IdentityLinkAssertionParser.java @@ -34,8 +34,6 @@ public class IdentityLinkAssertionParser { /** Xpath prefix for reaching PersonData Namespaces */ private static final String PDATA = Constants.PD_PREFIX + ":"; - /** Xpath prefix for reaching SecurityLayer 1.0 Namespaces */ - private static final String SL10 = Constants.SL10_PREFIX + ":"; /** Xpath prefix for reaching SAML Namespaces */ private static final String SAML = Constants.SAML_PREFIX + ":"; /** Xpath prefix for reaching XML-DSIG Namespaces */ @@ -43,7 +41,7 @@ public class IdentityLinkAssertionParser { /** Xpath prefix for reaching ECDS Namespaces */ private static final String ECDSA = Constants.ECDSA_PREFIX + ":"; /** Xpath expression to the root element */ - private static final String ROOT = "/" + SAML + "Assertion/"; + private static final String ROOT = ""; /** Xpath expression to the SAMLSubjectConfirmationData element */ private static final String SAML_SUBJECT_CONFIRMATION_DATA_XPATH = ROOT @@ -178,6 +176,15 @@ public class IdentityLinkAssertionParser { throw new ParseException("parser.01", new Object[] { t.toString()}, t); } } + + /** + * Sets the <@link assertionElem>. + * @param xmlAssertion the assertion element + * @throws ParseException on any parsing error + */ + public IdentityLinkAssertionParser(Element xmlAssertion) throws ParseException { + assertionElem = xmlAssertion; + } /** * Constructor for IdentityLinkAssertionParser. diff --git a/id.server/src/at/gv/egovernment/moa/id/auth/parser/InfoboxReadResponseParser.java b/id.server/src/at/gv/egovernment/moa/id/auth/parser/InfoboxReadResponseParser.java index ffb80aadd..0cedda28d 100644 --- a/id.server/src/at/gv/egovernment/moa/id/auth/parser/InfoboxReadResponseParser.java +++ b/id.server/src/at/gv/egovernment/moa/id/auth/parser/InfoboxReadResponseParser.java @@ -19,75 +19,104 @@ import at.gv.egovernment.moa.util.XPathUtils; */ public class InfoboxReadResponseParser { -// // -// // XPath namespace prefix shortcuts -// // -// /** Xpath prefix for reaching SecurityLayer 1.0 Namespaces */ -// private static final String SL10 = Constants.SL10_PREFIX + ":"; -// /** Xpath prefix for reaching SAML Namespaces */ -// private static final String SAML = Constants.SAML_PREFIX + ":"; -// /** Xpath expression to the root element */ -// private static final String ROOT = "/" + SL10 + "InfoboxReadResponse/"; -// /** Xpath expression to the SAML:Assertion element */ -// private static final String SAML_ASSERTION_XPATH = ROOT + SL10 + "BinaryFileData/" + SL10 + "XMLContent/" + SAML + "Assertion"; /** This is the root element of the XML-Document provided by the Security Layer Card*/ - private Element infoBoxElem; + private Element infoBoxElem_; /** - * Constructor for InfoboxReadResponseParser. - * A DOM-representation of the incoming String will be created + * Parses and validates the document given as string and extracts the + * root element. + * * @param xmlResponse <InfoboxReadResponse> as String * @throws ParseException on any parsing error */ public InfoboxReadResponseParser(String xmlResponse) throws ParseException, AuthenticationException { - - 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")); - infoBoxElem = DOMUtils.parseXmlValidating(s); + init(s); } catch (Throwable t) { - throw new ParseException("parser.01", new Object[] { t.toString()}, t); + throw new ParseException("parser.01", new Object[] { t.toString()}, t); } } /** - * Constructor for InfoboxReadResponseParser. - * A DOM-representation of the incoming Inputstream will be created + * Parses and validates the document given as stream and extracts the + * root element. + * * @param xmlResponse <InfoboxReadResponse> as InputStream * @throws ParseException on any parsing error */ public InfoboxReadResponseParser(InputStream is) throws ParseException, AuthenticationException { + init(is); + } - ErrorResponseParser erp = new ErrorResponseParser(is); - if (erp.getErrorCode() != null) { - throw new AuthenticationException("auth.08", new Object[] { erp.getErrorCode(), erp.getErrorInfo()}); - } - + /** + * Initializes the parser. + * Parses and validates the document given as stream and extracts the + * root element. + * + * @param is The InfoBoxReadResponse as stream. + * @throws AuthenticationException if an authentication error occurs. + * @throws ParseException If an error occurs on parsing the the document. + */ + private void init(InputStream is) throws AuthenticationException, ParseException { try { - - infoBoxElem = DOMUtils.parseXmlValidating(is); - } - catch (Throwable t) { + + Element responseElem = DOMUtils.parseXmlValidating(is); + + if ("InfoboxReadResponse".equals(responseElem.getLocalName())) { + infoBoxElem_ = responseElem; + } else { + ErrorResponseParser erp = new ErrorResponseParser(responseElem); + throw new AuthenticationException("auth.08", new Object[] { erp.getErrorCode(), erp.getErrorInfo()}); + } + + } catch (Throwable t) { throw new ParseException("parser.01", new Object[] { t.toString()}, t); } } + + + /** + * Parses the embedded <saml:Assertion> element from <InfoboxReadResponse> + * @return <saml:Assertion> as String + * @throws ParseException on any parsing error + */ +// public String parseSAMLAssertion() throws ParseException { +// try { +// +// String slPrefix = XPathUtils.getSlPrefix(infoBoxElem_); +// StringBuffer sb = new StringBuffer("/"); +// sb.append(slPrefix); +// sb.append(":InfoboxReadResponse/"); +// sb.append(slPrefix); +// sb.append(":BinaryFileData/"); +// sb.append(slPrefix); +// sb.append(":XMLContent/"); +// sb.append(Constants.SAML_PREFIX); +// sb.append(":Assertion"); +// String samlAssertionXPath = sb.toString(); +// Element samlAssertion = (Element) XPathUtils.selectSingleNode(infoBoxElem_, samlAssertionXPath); +// return DOMUtils.serializeNode(samlAssertion); +// +// } +// catch (Throwable t) { +// throw new ParseException("parser.01", new Object[] { t.toString()}, t); +// } +// } + /** * Parses the embedded <saml:Assertion> element from <InfoboxReadResponse> * @return <saml:Assertion> as String * @throws ParseException on any parsing error */ - public String parseSAMLAssertion() throws ParseException { + public Element parseSAMLAssertion() throws ParseException { try { - String slPrefix = XPathUtils.getSlPrefix(infoBoxElem); + String slPrefix = XPathUtils.getSlPrefix(infoBoxElem_); StringBuffer sb = new StringBuffer("/"); sb.append(slPrefix); sb.append(":InfoboxReadResponse/"); @@ -98,8 +127,8 @@ public class InfoboxReadResponseParser { sb.append(Constants.SAML_PREFIX); sb.append(":Assertion"); String samlAssertionXPath = sb.toString(); - Element samlAssertion = (Element) XPathUtils.selectSingleNode(infoBoxElem, samlAssertionXPath); - return DOMUtils.serializeNode(samlAssertion); + Element samlAssertion = (Element) XPathUtils.selectSingleNode(infoBoxElem_, samlAssertionXPath); + return samlAssertion; } catch (Throwable t) { @@ -113,9 +142,20 @@ public class InfoboxReadResponseParser { * @throws ParseException on any parsing error */ - public IdentityLink parseIdentityLink() throws ParseException { - String samlAssertionString = parseSAMLAssertion(); - IdentityLinkAssertionParser ilParser = new IdentityLinkAssertionParser(samlAssertionString); +// public IdentityLink parseIdentityLink() throws ParseException { +// String samlAssertionString = parseSAMLAssertion(); +// IdentityLinkAssertionParser ilParser = new IdentityLinkAssertionParser(samlAssertionString); +// return ilParser.parseIdentityLink(); +// } + + /** + * Parses the identity link from the <saml:Assertion> + * @return Identity link + * @throws ParseException on any parsing error + */ + public IdentityLink parseIdentityLink() throws ParseException { + Element samlAssertion = parseSAMLAssertion(); + IdentityLinkAssertionParser ilParser = new IdentityLinkAssertionParser(samlAssertion); return ilParser.parseIdentityLink(); } -- cgit v1.2.3