Search in sources :

Example 26 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project keycloak by keycloak.

the class StaxParserUtil method bypassElementBlock.

/**
 * Bypass an entire XML element block.
 * It is expected that the {@code xmlEventReader} is positioned at (has not yet read)
 * the start element of the block it should bypass.
 *
 * @param xmlEventReader
 * @returns Last XML event which is {@link EndElement} corresponding to the first startElement when no error occurs ({@code null} if not available)
 *
 * @throws org.keycloak.saml.common.exceptions.ParsingException
 */
public static XMLEvent bypassElementBlock(XMLEventReader xmlEventReader) throws ParsingException {
    XMLEvent xmlEvent;
    int levelOfNesting = 0;
    if (!xmlEventReader.hasNext()) {
        return null;
    }
    try {
        do {
            xmlEvent = xmlEventReader.nextEvent();
            if (xmlEvent instanceof StartElement) {
                levelOfNesting++;
            } else if (xmlEvent instanceof EndElement) {
                levelOfNesting--;
            }
        } while (levelOfNesting > 0 && xmlEventReader.hasNext());
    } catch (XMLStreamException e) {
        throw logger.parserException(e);
    }
    return xmlEvent;
}
Also used : StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) EndElement(javax.xml.stream.events.EndElement) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 27 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project keycloak by keycloak.

the class StaxParserUtil method peekNextTag.

/**
 * Peek the next {@link StartElement} or {@link EndElement}.
 *
 * @param xmlEventReader
 * @return
 * @throws ParsingException
 */
public static XMLEvent peekNextTag(XMLEventReader xmlEventReader) throws ParsingException {
    try {
        XMLEvent xmlEvent = xmlEventReader.peek();
        while (xmlEvent != null && !xmlEvent.isStartElement() && !xmlEvent.isEndElement()) {
            xmlEventReader.nextEvent();
            xmlEvent = xmlEventReader.peek();
        }
        return xmlEvent;
    } catch (XMLStreamException e) {
        throw logger.parserException(e);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 28 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project keycloak by keycloak.

the class SAML11AssertionParser method parse.

/**
 * @see {@link ParserNamespaceSupport#parse(XMLEventReader)}
 */
public Object parse(XMLEventReader xmlEventReader) throws ParsingException {
    StartElement startElement = StaxParserUtil.peekNextStartElement(xmlEventReader);
    startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
    // Special case: Encrypted Assertion
    StaxParserUtil.validate(startElement, ASSERTION);
    SAML11AssertionType assertion = parseBaseAttributes(startElement);
    Attribute issuerAttribute = startElement.getAttributeByName(new QName(SAML11Constants.ISSUER));
    String issuer = StaxParserUtil.getAttributeValue(issuerAttribute);
    assertion.setIssuer(issuer);
    // Peek at the next event
    while (xmlEventReader.hasNext()) {
        XMLEvent xmlEvent = StaxParserUtil.peek(xmlEventReader);
        if (xmlEvent == null)
            break;
        if (xmlEvent instanceof EndElement) {
            xmlEvent = StaxParserUtil.getNextEvent(xmlEventReader);
            EndElement endElement = (EndElement) xmlEvent;
            String endElementTag = StaxParserUtil.getElementName(endElement);
            if (endElementTag.equals(JBossSAMLConstants.ASSERTION.get()))
                break;
            else
                throw logger.parserUnknownEndElement(endElementTag, xmlEvent.getLocation());
        }
        StartElement peekedElement = null;
        if (xmlEvent instanceof StartElement) {
            peekedElement = (StartElement) xmlEvent;
        } else {
            peekedElement = StaxParserUtil.peekNextStartElement(xmlEventReader);
        }
        if (peekedElement == null)
            break;
        String tag = StaxParserUtil.getElementName(peekedElement);
        if (tag.equals(JBossSAMLConstants.SIGNATURE.get())) {
            assertion.setSignature(StaxParserUtil.getDOMElement(xmlEventReader));
        } else if (JBossSAMLConstants.ISSUER.get().equalsIgnoreCase(tag)) {
            startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
            issuer = StaxParserUtil.getElementText(xmlEventReader);
            assertion.setIssuer(issuer);
        } else if (JBossSAMLConstants.SUBJECT.get().equalsIgnoreCase(tag)) {
            SAML11SubjectParser subjectParser = new SAML11SubjectParser();
            SAML11SubjectType subject = (SAML11SubjectType) subjectParser.parse(xmlEventReader);
            SAML11SubjectStatementType subStat = new SAML11SubjectStatementType();
            subStat.setSubject(subject);
        } else if (JBossSAMLConstants.CONDITIONS.get().equalsIgnoreCase(tag)) {
            startElement = (StartElement) xmlEvent;
            SAML11ConditionsType conditions = SAML11ParserUtil.parseSAML11Conditions(xmlEventReader);
            assertion.setConditions(conditions);
        } else if (SAML11Constants.AUTHENTICATION_STATEMENT.equals(tag)) {
            startElement = (StartElement) xmlEvent;
            SAML11AuthenticationStatementType authStat = SAML11ParserUtil.parseAuthenticationStatement(xmlEventReader);
            assertion.add(authStat);
        } else if (SAML11Constants.ATTRIBUTE_STATEMENT.equalsIgnoreCase(tag)) {
            SAML11AttributeStatementType attributeStatementType = SAML11ParserUtil.parseSAML11AttributeStatement(xmlEventReader);
            assertion.add(attributeStatementType);
        } else if (SAML11Constants.AUTHORIZATION_DECISION_STATEMENT.equalsIgnoreCase(tag)) {
            SAML11AuthorizationDecisionStatementType authzStat = SAML11ParserUtil.parseSAML11AuthorizationDecisionStatement(xmlEventReader);
            assertion.add(authzStat);
        } else
            throw logger.parserUnknownTag(tag, peekedElement.getLocation());
    }
    return assertion;
}
Also used : SAML11ConditionsType(org.keycloak.dom.saml.v1.assertion.SAML11ConditionsType) SAML11SubjectType(org.keycloak.dom.saml.v1.assertion.SAML11SubjectType) Attribute(javax.xml.stream.events.Attribute) EndElement(javax.xml.stream.events.EndElement) QName(javax.xml.namespace.QName) StartElement(javax.xml.stream.events.StartElement) SAML11AuthenticationStatementType(org.keycloak.dom.saml.v1.assertion.SAML11AuthenticationStatementType) SAML11AssertionType(org.keycloak.dom.saml.v1.assertion.SAML11AssertionType) XMLEvent(javax.xml.stream.events.XMLEvent) SAML11AuthorizationDecisionStatementType(org.keycloak.dom.saml.v1.assertion.SAML11AuthorizationDecisionStatementType) SAML11SubjectStatementType(org.keycloak.dom.saml.v1.assertion.SAML11SubjectStatementType) SAML11AttributeStatementType(org.keycloak.dom.saml.v1.assertion.SAML11AttributeStatementType)

Example 29 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project keycloak by keycloak.

the class SAML11ResponseParser method parseStatus.

/**
 * Parse the status element
 *
 * @param xmlEventReader
 *
 * @return
 *
 * @throws ParsingException
 */
protected SAML11StatusType parseStatus(XMLEventReader xmlEventReader) throws ParsingException {
    // Get the Start Element
    StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
    String STATUS = JBossSAMLConstants.STATUS.get();
    StaxParserUtil.validate(startElement, STATUS);
    SAML11StatusType status = new SAML11StatusType();
    while (xmlEventReader.hasNext()) {
        startElement = StaxParserUtil.peekNextStartElement(xmlEventReader);
        if (startElement == null)
            break;
        QName startElementName = startElement.getName();
        String elementTag = startElementName.getLocalPart();
        SAML11StatusCodeType statusCode = null;
        if (JBossSAMLConstants.STATUS_CODE.get().equals(elementTag)) {
            startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
            if (startElement == null)
                break;
            Attribute valueAttr = startElement.getAttributeByName(new QName("Value"));
            if (valueAttr != null) {
                statusCode = new SAML11StatusCodeType(new QName(StaxParserUtil.getAttributeValue(valueAttr)));
            }
            status.setStatusCode(statusCode);
            // Peek at the next start element to see if it is status code
            startElement = StaxParserUtil.peekNextStartElement(xmlEventReader);
            elementTag = startElement.getName().getLocalPart();
            if (JBossSAMLConstants.STATUS_CODE.get().equals(elementTag)) {
                SAML11StatusCodeType subStatusCodeType = null;
                startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
                Attribute subValueAttr = startElement.getAttributeByName(new QName("Value"));
                if (subValueAttr != null) {
                    subStatusCodeType = new SAML11StatusCodeType(new QName(StaxParserUtil.getAttributeValue(subValueAttr)));
                }
                statusCode.setStatusCode(subStatusCodeType);
                // Go to Status code end element.
                EndElement endElement = StaxParserUtil.getNextEndElement(xmlEventReader);
                StaxParserUtil.validate(endElement, JBossSAMLConstants.STATUS_CODE.get());
                continue;
            }
        }
        if (JBossSAMLConstants.STATUS_MESSAGE.get().equals(elementTag)) {
            startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
            if (startElement == null)
                break;
            status.setStatusMessage(StaxParserUtil.getElementText(xmlEventReader));
        }
        if (JBossSAMLConstants.STATUS_DETAIL.get().equals(elementTag)) {
            startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
            if (startElement == null)
                break;
            Element domElement = StaxParserUtil.getDOMElement(xmlEventReader);
            StatusDetailType statusDetailType = new StatusDetailType();
            statusDetailType.addStatusDetail(domElement);
            status.setStatusDetail(statusDetailType);
        }
        // Get the next end element
        XMLEvent xmlEvent = StaxParserUtil.peek(xmlEventReader);
        if (xmlEvent instanceof EndElement) {
            EndElement endElement = StaxParserUtil.getNextEndElement(xmlEventReader);
            if (StaxParserUtil.matches(endElement, STATUS))
                break;
            else
                throw logger.parserUnknownEndElement(StaxParserUtil.getElementName(endElement), xmlEvent.getLocation());
        } else
            break;
    }
    return status;
}
Also used : StartElement(javax.xml.stream.events.StartElement) SAML11StatusType(org.keycloak.dom.saml.v1.protocol.SAML11StatusType) StatusDetailType(org.keycloak.dom.saml.v2.protocol.StatusDetailType) Attribute(javax.xml.stream.events.Attribute) EndElement(javax.xml.stream.events.EndElement) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) EndElement(javax.xml.stream.events.EndElement) StartElement(javax.xml.stream.events.StartElement) XMLEvent(javax.xml.stream.events.XMLEvent) SAML11StatusCodeType(org.keycloak.dom.saml.v1.protocol.SAML11StatusCodeType)

Example 30 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project keycloak by keycloak.

the class SAMLParser method parse.

/**
 * @see {@link org.keycloak.saml.common.parsers.ParserNamespaceSupport#parse(XMLEventReader)}
 */
@Override
public Object parse(XMLEventReader xmlEventReader) throws ParsingException {
    while (xmlEventReader.hasNext()) {
        XMLEvent xmlEvent = StaxParserUtil.peek(xmlEventReader);
        if (xmlEvent instanceof StartElement) {
            StartElement startElement = (StartElement) xmlEvent;
            final QName name = startElement.getName();
            ParserFactory pf = PARSERS.get(name);
            if (pf == null) {
                throw logger.parserException(new RuntimeException(ErrorCodes.UNKNOWN_START_ELEMENT + name + "::location=" + startElement.getLocation()));
            }
            return pf.create().parse(xmlEventReader);
        }
        StaxParserUtil.getNextEvent(xmlEventReader);
    }
    throw new RuntimeException(ErrorCodes.FAILED_PARSING + "SAML Parsing has failed");
}
Also used : StartElement(javax.xml.stream.events.StartElement) QName(javax.xml.namespace.QName) XMLEvent(javax.xml.stream.events.XMLEvent)

Aggregations

XMLEvent (javax.xml.stream.events.XMLEvent)269 XMLEventReader (javax.xml.stream.XMLEventReader)114 StartElement (javax.xml.stream.events.StartElement)107 XMLStreamException (javax.xml.stream.XMLStreamException)96 XMLInputFactory (javax.xml.stream.XMLInputFactory)65 QName (javax.xml.namespace.QName)60 Attribute (javax.xml.stream.events.Attribute)52 EndElement (javax.xml.stream.events.EndElement)52 IOException (java.io.IOException)39 ArrayList (java.util.ArrayList)32 InputStream (java.io.InputStream)29 XMLEventWriter (javax.xml.stream.XMLEventWriter)24 Characters (javax.xml.stream.events.Characters)22 ByteArrayInputStream (java.io.ByteArrayInputStream)17 StringWriter (java.io.StringWriter)17 Test (org.junit.Test)17 StringReader (java.io.StringReader)14 HashMap (java.util.HashMap)14 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)14 XMLEventFactory (javax.xml.stream.XMLEventFactory)13