package at.gv.egovernment.moa.id.proxy.parser; import org.w3c.dom.Element; import at.gv.egovernment.moa.id.ParseException; import at.gv.egovernment.moa.id.data.AuthenticationData; import at.gv.egovernment.moa.id.data.SAMLStatus; import at.gv.egovernment.moa.util.Constants; import at.gv.egovernment.moa.util.XPathUtils; /** * Parser for the <samlp:Response> returned by the * GetAuthenticationData web service. * @author Paul Ivancsics * @version $Id$ */ public class SAMLResponseParser implements Constants { /** Element containing the samlResponse */ private Element samlResponse; /** Xpath prefix for reaching SAMLP Namespaces */ private static String SAMLP = SAMLP_PREFIX + ":"; /** Xpath prefix for reaching SAML Namespaces */ private static String SAML = SAML_PREFIX + ":"; /** Xpath prefix for reaching PersonData Namespaces */ private static String PR = PD_PREFIX + ":"; /** Xpath expression for reaching the SAMLP:Response element */ private static final String ROOT = "/" + SAMLP + "Response/"; /** Xpath expression for reaching the SAMLP:Status element */ private static final String STATUS_XPATH = ROOT + SAMLP + "Status/"; /** Xpath expression for reaching the SAMLP:StatusCode_Value attribute */ private static final String STATUSCODE_XPATH = STATUS_XPATH + SAMLP + "StatusCode/@Value"; /** Xpath expression for reaching the SAMLP:SubStatusCode_Value attribute */ private static final String SUBSTATUSCODE_XPATH = STATUS_XPATH + SAMLP + "StatusCode/" + SAMLP + "StatusCode/@Value"; /** Xpath expression for reaching the SAMLP:StatusMessage element */ private static final String STATUSMESSAGE_XPATH = STATUS_XPATH + SAMLP + "StatusMessage"; /** Xpath expression for reaching the SAML:Assertion element */ private static String ASSERTION_XPATH = ROOT + SAML + "Assertion"; /** * Constructor * @param samlResponse the <samlp:Response> as a DOM element */ public SAMLResponseParser(Element samlResponse) { this.samlResponse = samlResponse; } /** * Parses the <samlp:StatusCode> from the <samlp:Response>. * @return AuthenticationData object * @throws ParseException on any parsing error */ public SAMLStatus parseStatusCode() throws ParseException { SAMLStatus status = new SAMLStatus(); try { status.setStatusCode( XPathUtils.getAttributeValue(samlResponse, STATUSCODE_XPATH, "")); status.setSubStatusCode( XPathUtils.getAttributeValue(samlResponse, SUBSTATUSCODE_XPATH, "")); status.setStatusMessage( XPathUtils.getElementValue(samlResponse, STATUSMESSAGE_XPATH, "")); } catch (Throwable t) { throw new ParseException("parser.01", new Object[] { t.toString() }, t); } return status; } /** * Parses the <saml:Assertion> from the <samlp:Response>. * @return AuthenticationData object * @throws ParseException on any parsing error */ public AuthenticationData parseAuthenticationData() throws ParseException { Element samlAssertion; try { samlAssertion = (Element)XPathUtils.selectSingleNode(samlResponse, ASSERTION_XPATH); } catch (Throwable t) { throw new ParseException("parser.01", new Object[] { t.toString() }, t); } return new AuthenticationDataAssertionParser(samlAssertion).parseAuthenticationData(); } }