Search in sources :

Example 21 with XMLEvent

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

the class SAML11ParserUtil method parseSubjectConfirmationData.

/**
 * Parse the {@link SubjectConfirmationDataType}
 *
 * @param xmlEventReader
 *
 * @return
 *
 * @throws ParsingException
 */
public static SubjectConfirmationDataType parseSubjectConfirmationData(XMLEventReader xmlEventReader) throws ParsingException {
    StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
    StaxParserUtil.validate(startElement, JBossSAMLConstants.SUBJECT_CONFIRMATION_DATA.get());
    SubjectConfirmationDataType subjectConfirmationData = new SubjectConfirmationDataType();
    Attribute inResponseTo = startElement.getAttributeByName(new QName(JBossSAMLConstants.IN_RESPONSE_TO.get()));
    if (inResponseTo != null) {
        subjectConfirmationData.setInResponseTo(StaxParserUtil.getAttributeValue(inResponseTo));
    }
    Attribute notBefore = startElement.getAttributeByName(new QName(JBossSAMLConstants.NOT_BEFORE.get()));
    if (notBefore != null) {
        subjectConfirmationData.setNotBefore(XMLTimeUtil.parse(StaxParserUtil.getAttributeValue(notBefore)));
    }
    Attribute notOnOrAfter = startElement.getAttributeByName(new QName(JBossSAMLConstants.NOT_ON_OR_AFTER.get()));
    if (notOnOrAfter != null) {
        subjectConfirmationData.setNotOnOrAfter(XMLTimeUtil.parse(StaxParserUtil.getAttributeValue(notOnOrAfter)));
    }
    Attribute recipient = startElement.getAttributeByName(new QName(JBossSAMLConstants.RECIPIENT.get()));
    if (recipient != null) {
        subjectConfirmationData.setRecipient(StaxParserUtil.getAttributeValue(recipient));
    }
    Attribute address = startElement.getAttributeByName(new QName(JBossSAMLConstants.ADDRESS.get()));
    if (address != null) {
        subjectConfirmationData.setAddress(StaxParserUtil.getAttributeValue(address));
    }
    XMLEvent xmlEvent = StaxParserUtil.peek(xmlEventReader);
    if (!(xmlEvent instanceof EndElement)) {
        startElement = StaxParserUtil.peekNextStartElement(xmlEventReader);
        String tag = StaxParserUtil.getElementName(startElement);
        if (tag.equals(WSTrustConstants.XMLDSig.KEYINFO)) {
            KeyInfoType keyInfo = parseKeyInfo(xmlEventReader);
            subjectConfirmationData.setAnyType(keyInfo);
        } else if (tag.equals(WSTrustConstants.XMLEnc.ENCRYPTED_KEY)) {
            subjectConfirmationData.setAnyType(StaxParserUtil.getDOMElement(xmlEventReader));
        } else
            throw logger.parserUnknownTag(tag, startElement.getLocation());
    }
    // Get the end tag
    EndElement endElement = (EndElement) StaxParserUtil.getNextEvent(xmlEventReader);
    StaxParserUtil.matches(endElement, JBossSAMLConstants.SUBJECT_CONFIRMATION_DATA.get());
    return subjectConfirmationData;
}
Also used : StartElement(javax.xml.stream.events.StartElement) SubjectConfirmationDataType(org.keycloak.dom.saml.v2.assertion.SubjectConfirmationDataType) Attribute(javax.xml.stream.events.Attribute) EndElement(javax.xml.stream.events.EndElement) QName(javax.xml.namespace.QName) XMLEvent(javax.xml.stream.events.XMLEvent) KeyInfoType(org.keycloak.dom.xmlsec.w3.xmldsig.KeyInfoType)

Example 22 with XMLEvent

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

the class SAML11ParserUtil method parseSAML11AttributeStatement.

/**
 * Parse an {@code SAML11AttributeStatementType}
 *
 * @param xmlEventReader
 *
 * @return
 *
 * @throws ParsingException
 */
public static SAML11AttributeStatementType parseSAML11AttributeStatement(XMLEventReader xmlEventReader) throws ParsingException {
    SAML11AttributeStatementType attributeStatementType = new SAML11AttributeStatementType();
    StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
    String ATTRIBSTATEMT = JBossSAMLConstants.ATTRIBUTE_STATEMENT.get();
    StaxParserUtil.validate(startElement, ATTRIBSTATEMT);
    while (xmlEventReader.hasNext()) {
        XMLEvent xmlEvent = StaxParserUtil.peek(xmlEventReader);
        if (xmlEvent instanceof EndElement) {
            EndElement endElement = StaxParserUtil.getNextEndElement(xmlEventReader);
            StaxParserUtil.validate(endElement, JBossSAMLConstants.ATTRIBUTE_STATEMENT.get());
            break;
        }
        // Get the next start element
        startElement = StaxParserUtil.peekNextStartElement(xmlEventReader);
        String tag = startElement.getName().getLocalPart();
        if (JBossSAMLConstants.ATTRIBUTE.get().equals(tag)) {
            SAML11AttributeType attribute = parseSAML11Attribute(xmlEventReader);
            attributeStatementType.add(attribute);
        } else if (JBossSAMLConstants.SUBJECT.get().equals(tag)) {
            SAML11SubjectParser parser = new SAML11SubjectParser();
            SAML11SubjectType subject = (SAML11SubjectType) parser.parse(xmlEventReader);
            attributeStatementType.setSubject(subject);
        } else
            throw logger.parserUnknownTag(tag, startElement.getLocation());
    }
    return attributeStatementType;
}
Also used : StartElement(javax.xml.stream.events.StartElement) SAML11SubjectType(org.keycloak.dom.saml.v1.assertion.SAML11SubjectType) EndElement(javax.xml.stream.events.EndElement) SAML11SubjectParser(org.keycloak.saml.processing.core.parsers.saml.SAML11SubjectParser) XMLEvent(javax.xml.stream.events.XMLEvent) SAML11AttributeType(org.keycloak.dom.saml.v1.assertion.SAML11AttributeType) SAML11AttributeStatementType(org.keycloak.dom.saml.v1.assertion.SAML11AttributeStatementType)

Example 23 with XMLEvent

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

the class SAML11ParserUtil method parseSAML11AuthorizationDecisionStatement.

public static SAML11AuthorizationDecisionStatementType parseSAML11AuthorizationDecisionStatement(XMLEventReader xmlEventReader) throws ParsingException {
    SAML11AuthorizationDecisionStatementType authzDecision = null;
    StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
    StaxParserUtil.validate(startElement, SAML11Constants.AUTHORIZATION_DECISION_STATEMENT);
    Attribute decision = startElement.getAttributeByName(new QName(SAML11Constants.DECISION));
    if (decision == null)
        throw logger.parserRequiredAttribute("Decision");
    String decisionValue = StaxParserUtil.getAttributeValue(decision);
    Attribute resource = startElement.getAttributeByName(new QName(SAML11Constants.RESOURCE));
    if (resource == null)
        throw logger.parserRequiredAttribute("Namespace");
    String resValue = StaxParserUtil.getAttributeValue(resource);
    authzDecision = new SAML11AuthorizationDecisionStatementType(URI.create(resValue), SAML11DecisionType.valueOf(decisionValue));
    while (xmlEventReader.hasNext()) {
        XMLEvent xmlEvent = StaxParserUtil.peek(xmlEventReader);
        if (xmlEvent instanceof EndElement) {
            EndElement end = StaxParserUtil.getNextEndElement(xmlEventReader);
            if (StaxParserUtil.matches(end, SAML11Constants.AUTHORIZATION_DECISION_STATEMENT))
                break;
        }
        startElement = StaxParserUtil.peekNextStartElement(xmlEventReader);
        if (startElement == null)
            break;
        String tag = StaxParserUtil.getElementName(startElement);
        if (SAML11Constants.ACTION.equals(tag)) {
            startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
            SAML11ActionType samlAction = new SAML11ActionType();
            Attribute namespaceAttr = startElement.getAttributeByName(new QName(SAML11Constants.NAMESPACE));
            if (namespaceAttr != null) {
                samlAction.setNamespace(StaxParserUtil.getAttributeValue(namespaceAttr));
            }
            samlAction.setValue(StaxParserUtil.getElementText(xmlEventReader));
            authzDecision.addAction(samlAction);
        } else if (JBossSAMLConstants.SUBJECT.get().equals(tag)) {
            SAML11SubjectParser parser = new SAML11SubjectParser();
            authzDecision.setSubject((SAML11SubjectType) parser.parse(xmlEventReader));
        } else
            throw logger.parserUnknownTag(tag, startElement.getLocation());
    }
    return authzDecision;
}
Also used : StartElement(javax.xml.stream.events.StartElement) 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) SAML11SubjectParser(org.keycloak.saml.processing.core.parsers.saml.SAML11SubjectParser) SAML11AuthorizationDecisionStatementType(org.keycloak.dom.saml.v1.assertion.SAML11AuthorizationDecisionStatementType) XMLEvent(javax.xml.stream.events.XMLEvent) SAML11ActionType(org.keycloak.dom.saml.v1.assertion.SAML11ActionType)

Example 24 with XMLEvent

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

the class SAML11ParserUtil method parseSAML11SubjectConfirmation.

/**
 * Parse the {@link org.keycloak.dom.saml.v1.assertion.SAML11SubjectConfirmationType}
 *
 * @param xmlEventReader
 *
 * @return
 *
 * @throws ParsingException
 */
public static SAML11SubjectConfirmationType parseSAML11SubjectConfirmation(XMLEventReader xmlEventReader) throws ParsingException {
    SAML11SubjectConfirmationType subjectConfirmationType = new SAML11SubjectConfirmationType();
    StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
    // There may be additional things under subject confirmation
    while (xmlEventReader.hasNext()) {
        XMLEvent xmlEvent = StaxParserUtil.peek(xmlEventReader);
        if (xmlEvent instanceof EndElement) {
            EndElement endElement = StaxParserUtil.getNextEndElement(xmlEventReader);
            StaxParserUtil.validate(endElement, JBossSAMLConstants.SUBJECT_CONFIRMATION.get());
            break;
        }
        if (xmlEvent instanceof StartElement) {
            startElement = (StartElement) xmlEvent;
            String startTag = StaxParserUtil.getElementName(startElement);
            if (startTag.equals(SAML11Constants.CONFIRMATION_METHOD)) {
                startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
                String method = StaxParserUtil.getElementText(xmlEventReader);
                subjectConfirmationType.addConfirmationMethod(URI.create(method));
            } else if (startTag.equals(JBossSAMLConstants.SUBJECT_CONFIRMATION_DATA.get())) {
                startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
                SubjectConfirmationDataType subjectConfirmationData = parseSubjectConfirmationData(xmlEventReader);
                subjectConfirmationType.setSubjectConfirmationData(subjectConfirmationData);
            } else if (startTag.equals(JBossSAMLConstants.KEY_INFO.get())) {
                Element keyInfo = StaxParserUtil.getDOMElement(xmlEventReader);
                subjectConfirmationType.setKeyInfo(keyInfo);
            } else
                throw logger.parserUnknownTag(startTag, startElement.getLocation());
        }
    }
    return subjectConfirmationType;
}
Also used : StartElement(javax.xml.stream.events.StartElement) SubjectConfirmationDataType(org.keycloak.dom.saml.v2.assertion.SubjectConfirmationDataType) SAML11SubjectConfirmationType(org.keycloak.dom.saml.v1.assertion.SAML11SubjectConfirmationType) EndElement(javax.xml.stream.events.EndElement) EndElement(javax.xml.stream.events.EndElement) StartElement(javax.xml.stream.events.StartElement) Element(org.w3c.dom.Element) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 25 with XMLEvent

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

the class SAML11ParserUtil method parseSAML11Conditions.

/**
 * Parse {@link org.keycloak.dom.saml.v1.assertion.SAML11ConditionsType}
 *
 * @param xmlEventReader
 *
 * @return
 *
 * @throws ParsingException
 */
public static SAML11ConditionsType parseSAML11Conditions(XMLEventReader xmlEventReader) throws ParsingException {
    StartElement startElement;
    SAML11ConditionsType conditions = new SAML11ConditionsType();
    StartElement conditionsElement = StaxParserUtil.getNextStartElement(xmlEventReader);
    StaxParserUtil.validate(conditionsElement, JBossSAMLConstants.CONDITIONS.get());
    String assertionNS = SAML11Constants.ASSERTION_11_NSURI;
    QName notBeforeQName = new QName("", JBossSAMLConstants.NOT_BEFORE.get());
    QName notBeforeQNameWithNS = new QName(assertionNS, JBossSAMLConstants.NOT_BEFORE.get());
    QName notAfterQName = new QName("", JBossSAMLConstants.NOT_ON_OR_AFTER.get());
    QName notAfterQNameWithNS = new QName(assertionNS, JBossSAMLConstants.NOT_ON_OR_AFTER.get());
    Attribute notBeforeAttribute = conditionsElement.getAttributeByName(notBeforeQName);
    if (notBeforeAttribute == null)
        notBeforeAttribute = conditionsElement.getAttributeByName(notBeforeQNameWithNS);
    Attribute notAfterAttribute = conditionsElement.getAttributeByName(notAfterQName);
    if (notAfterAttribute == null)
        notAfterAttribute = conditionsElement.getAttributeByName(notAfterQNameWithNS);
    if (notBeforeAttribute != null) {
        String notBeforeValue = StaxParserUtil.getAttributeValue(notBeforeAttribute);
        conditions.setNotBefore(XMLTimeUtil.parse(notBeforeValue));
    }
    if (notAfterAttribute != null) {
        String notAfterValue = StaxParserUtil.getAttributeValue(notAfterAttribute);
        conditions.setNotOnOrAfter(XMLTimeUtil.parse(notAfterValue));
    }
    while (xmlEventReader.hasNext()) {
        XMLEvent xmlEvent = StaxParserUtil.peek(xmlEventReader);
        if (xmlEvent instanceof EndElement) {
            EndElement end = StaxParserUtil.getNextEndElement(xmlEventReader);
            if (StaxParserUtil.matches(end, JBossSAMLConstants.CONDITIONS.get()))
                break;
        }
        startElement = StaxParserUtil.peekNextStartElement(xmlEventReader);
        if (startElement == null)
            break;
        String tag = StaxParserUtil.getElementName(startElement);
        if (SAML11Constants.AUDIENCE_RESTRICTION_CONDITION.equals(tag)) {
            startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
            SAML11AudienceRestrictionCondition restrictCond = new SAML11AudienceRestrictionCondition();
            startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
            if (StaxParserUtil.getElementName(startElement).equals(JBossSAMLConstants.AUDIENCE.get())) {
                restrictCond.add(URI.create(StaxParserUtil.getElementText(xmlEventReader)));
            }
            EndElement theEndElement = StaxParserUtil.getNextEndElement(xmlEventReader);
            StaxParserUtil.validate(theEndElement, SAML11Constants.AUDIENCE_RESTRICTION_CONDITION);
            conditions.add(restrictCond);
        } else
            throw logger.parserUnknownTag(tag, startElement.getLocation());
    }
    return conditions;
}
Also used : StartElement(javax.xml.stream.events.StartElement) SAML11ConditionsType(org.keycloak.dom.saml.v1.assertion.SAML11ConditionsType) SAML11AudienceRestrictionCondition(org.keycloak.dom.saml.v1.assertion.SAML11AudienceRestrictionCondition) Attribute(javax.xml.stream.events.Attribute) EndElement(javax.xml.stream.events.EndElement) 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