Search in sources :

Example 21 with Attribute

use of javax.xml.stream.events.Attribute 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 22 with Attribute

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

the class SAML11AssertionParser method parseBaseAttributes.

private SAML11AssertionType parseBaseAttributes(StartElement nextElement) throws ParsingException {
    Attribute idAttribute = nextElement.getAttributeByName(new QName(SAML11Constants.ASSERTIONID));
    if (idAttribute == null)
        throw logger.parserRequiredAttribute("AssertionID");
    String id = StaxParserUtil.getAttributeValue(idAttribute);
    Attribute majVersionAttribute = nextElement.getAttributeByName(new QName(SAML11Constants.MAJOR_VERSION));
    String majVersion = StaxParserUtil.getAttributeValue(majVersionAttribute);
    StringUtil.match("1", majVersion);
    Attribute minVersionAttribute = nextElement.getAttributeByName(new QName(SAML11Constants.MINOR_VERSION));
    String minVersion = StaxParserUtil.getAttributeValue(minVersionAttribute);
    StringUtil.match("1", minVersion);
    Attribute issueInstantAttribute = nextElement.getAttributeByName(new QName(JBossSAMLConstants.ISSUE_INSTANT.get()));
    XMLGregorianCalendar issueInstant = XMLTimeUtil.parse(StaxParserUtil.getAttributeValue(issueInstantAttribute));
    return new SAML11AssertionType(id, issueInstant);
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) SAML11AssertionType(org.keycloak.dom.saml.v1.assertion.SAML11AssertionType)

Example 23 with Attribute

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

the class SAML11ResponseParser method parse.

/**
 * @see {@link ParserNamespaceSupport#parse(XMLEventReader)}
 */
public Object parse(XMLEventReader xmlEventReader) throws ParsingException {
    // Get the startelement
    StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
    StaxParserUtil.validate(startElement, RESPONSE);
    Attribute idAttr = startElement.getAttributeByName(new QName(SAML11Constants.RESPONSE_ID));
    if (idAttr == null)
        throw logger.parserRequiredAttribute(SAML11Constants.RESPONSE_ID);
    String id = StaxParserUtil.getAttributeValue(idAttr);
    Attribute issueInstant = startElement.getAttributeByName(new QName(SAML11Constants.ISSUE_INSTANT));
    if (issueInstant == null)
        throw logger.parserRequiredAttribute(SAML11Constants.ISSUE_INSTANT);
    XMLGregorianCalendar issueInstantVal = XMLTimeUtil.parse(StaxParserUtil.getAttributeValue(issueInstant));
    SAML11ResponseType response = new SAML11ResponseType(id, issueInstantVal);
    while (xmlEventReader.hasNext()) {
        // Let us peek at the next start element
        startElement = StaxParserUtil.peekNextStartElement(xmlEventReader);
        if (startElement == null)
            break;
        String elementName = StaxParserUtil.getElementName(startElement);
        if (JBossSAMLConstants.SIGNATURE.get().equals(elementName)) {
            Element sig = StaxParserUtil.getDOMElement(xmlEventReader);
            response.setSignature(sig);
        } else if (JBossSAMLConstants.ASSERTION.get().equals(elementName)) {
            SAML11AssertionParser assertionParser = new SAML11AssertionParser();
            response.add((SAML11AssertionType) assertionParser.parse(xmlEventReader));
        } else if (JBossSAMLConstants.STATUS.get().equals(elementName)) {
            response.setStatus(parseStatus(xmlEventReader));
        } else
            throw logger.parserUnknownStartElement(elementName, startElement.getLocation());
    }
    return response;
}
Also used : StartElement(javax.xml.stream.events.StartElement) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Attribute(javax.xml.stream.events.Attribute) SAML11ResponseType(org.keycloak.dom.saml.v1.protocol.SAML11ResponseType) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) EndElement(javax.xml.stream.events.EndElement) StartElement(javax.xml.stream.events.StartElement) SAML11AssertionType(org.keycloak.dom.saml.v1.assertion.SAML11AssertionType)

Example 24 with Attribute

use of javax.xml.stream.events.Attribute 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 25 with Attribute

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

the class SAMLAttributeParser method collectUnknownAttributesFrom.

/**
 * Returns a {@link Map} with the found non-standard attribute values for the given {@link StartElement}.
 * An attribute is considered as non-standard, if it is not contained in DEFAULT_KNOWN_LOCAL_ATTRIBUTE_NAMES.
 *
 * @return Map
 */
private static Map<QName, String> collectUnknownAttributesFrom(StartElement element) {
    Map<QName, String> otherAttributes = new HashMap<>();
    Iterator<?> attributes = element.getAttributes();
    while (attributes.hasNext()) {
        Attribute currentAttribute = (Attribute) attributes.next();
        QName attributeQName = currentAttribute.getName();
        if (attributeQName == null || DEFAULT_KNOWN_ATTRIBUTE_NAMES.contains(attributeQName)) {
            continue;
        }
        String attributeValue = currentAttribute.getValue();
        otherAttributes.put(attributeQName, attributeValue);
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace(String.format("Adding attribute %s with value %s", attributeQName, attributeValue));
        }
    }
    return otherAttributes;
}
Also used : HashMap(java.util.HashMap) Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName)

Aggregations

Attribute (javax.xml.stream.events.Attribute)140 QName (javax.xml.namespace.QName)71 StartElement (javax.xml.stream.events.StartElement)62 XMLEvent (javax.xml.stream.events.XMLEvent)52 XMLEventReader (javax.xml.stream.XMLEventReader)30 Namespace (javax.xml.stream.events.Namespace)26 EndElement (javax.xml.stream.events.EndElement)25 ArrayList (java.util.ArrayList)23 XMLStreamException (javax.xml.stream.XMLStreamException)20 XMLInputFactory (javax.xml.stream.XMLInputFactory)18 InputStream (java.io.InputStream)14 IOException (java.io.IOException)12 Iterator (java.util.Iterator)11 ByteArrayInputStream (java.io.ByteArrayInputStream)7 FileInputStream (java.io.FileInputStream)7 HashMap (java.util.HashMap)7 Test (org.junit.Test)7 HashSet (java.util.HashSet)6 Characters (javax.xml.stream.events.Characters)5 QNm (org.brackit.xquery.atomic.QNm)5