Search in sources :

Example 11 with SAML11SubjectType

use of org.keycloak.dom.saml.v1.assertion.SAML11SubjectType in project keycloak by keycloak.

the class SAML11ParserUtil method parseSAML11AttributeQuery.

/**
 * Parse the {@link SAML11AttributeQueryType}
 *
 * @param xmlEventReader
 *
 * @return
 *
 * @throws ParsingException
 */
public static SAML11AttributeQueryType parseSAML11AttributeQuery(XMLEventReader xmlEventReader) throws ParsingException {
    SAML11AttributeQueryType query = new SAML11AttributeQueryType();
    StartElement startElement;
    // 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);
            if (StaxParserUtil.matches(endElement, SAML11Constants.ATTRIBUTE_QUERY))
                break;
            else
                throw logger.parserUnknownEndElement(StaxParserUtil.getElementName(endElement), xmlEvent.getLocation());
        }
        if (xmlEvent instanceof StartElement) {
            startElement = (StartElement) xmlEvent;
            String startTag = StaxParserUtil.getElementName(startElement);
            if (startTag.equals(JBossSAMLConstants.SUBJECT.get())) {
                SAML11SubjectParser parser = new SAML11SubjectParser();
                query.setSubject((SAML11SubjectType) parser.parse(xmlEventReader));
            } else
                throw logger.parserUnknownTag(startTag, startElement.getLocation());
        }
    }
    return query;
}
Also used : StartElement(javax.xml.stream.events.StartElement) EndElement(javax.xml.stream.events.EndElement) SAML11SubjectParser(org.keycloak.saml.processing.core.parsers.saml.SAML11SubjectParser) XMLEvent(javax.xml.stream.events.XMLEvent) SAML11AttributeQueryType(org.keycloak.dom.saml.v1.protocol.SAML11AttributeQueryType)

Example 12 with SAML11SubjectType

use of org.keycloak.dom.saml.v1.assertion.SAML11SubjectType in project keycloak by keycloak.

the class SAML11ParserUtil method parseSAML11AuthorizationDecisionQueryType.

/**
 * Parse the {@link SAML11AuthorizationDecisionQueryType}
 *
 * @param xmlEventReader
 *
 * @return
 *
 * @throws ParsingException
 */
public static SAML11AuthorizationDecisionQueryType parseSAML11AuthorizationDecisionQueryType(XMLEventReader xmlEventReader) throws ParsingException {
    SAML11AuthorizationDecisionQueryType query = new SAML11AuthorizationDecisionQueryType();
    StartElement startElement;
    // 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);
            if (StaxParserUtil.matches(endElement, SAML11Constants.AUTHORIZATION_DECISION_QUERY))
                break;
            else
                throw logger.parserUnknownEndElement(StaxParserUtil.getElementName(endElement), xmlEvent.getLocation());
        }
        if (xmlEvent instanceof StartElement) {
            startElement = (StartElement) xmlEvent;
            String startTag = StaxParserUtil.getElementName(startElement);
            if (startTag.equals(JBossSAMLConstants.SUBJECT.get())) {
                SAML11SubjectParser parser = new SAML11SubjectParser();
                query.setSubject((SAML11SubjectType) parser.parse(xmlEventReader));
            } else if (startTag.equals(SAML11Constants.RESOURCE)) {
                startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
                query.setResource(URI.create(StaxParserUtil.getElementText(xmlEventReader)));
            } else if (startTag.equals(SAML11Constants.ACTION)) {
                startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
                SAML11ActionType action = new SAML11ActionType();
                Attribute nsAttr = startElement.getAttributeByName(new QName(SAML11Constants.NAMESPACE));
                if (nsAttr != null) {
                    action.setNamespace(StaxParserUtil.getAttributeValue(nsAttr));
                }
                action.setValue(StaxParserUtil.getElementText(xmlEventReader));
                query.add(action);
            } else
                throw logger.parserUnknownTag(startTag, startElement.getLocation());
        }
    }
    return query;
}
Also used : SAML11AuthorizationDecisionQueryType(org.keycloak.dom.saml.v1.protocol.SAML11AuthorizationDecisionQueryType) StartElement(javax.xml.stream.events.StartElement) EndElement(javax.xml.stream.events.EndElement) Attribute(javax.xml.stream.events.Attribute) SAML11SubjectParser(org.keycloak.saml.processing.core.parsers.saml.SAML11SubjectParser) QName(javax.xml.namespace.QName) XMLEvent(javax.xml.stream.events.XMLEvent) SAML11ActionType(org.keycloak.dom.saml.v1.assertion.SAML11ActionType)

Example 13 with SAML11SubjectType

use of org.keycloak.dom.saml.v1.assertion.SAML11SubjectType in project keycloak by keycloak.

the class SAML11ParserUtil method parseAuthenticationStatement.

/**
 * Parse the AuthnStatement inside the assertion
 *
 * @param xmlEventReader
 *
 * @return
 *
 * @throws ParsingException
 */
public static SAML11AuthenticationStatementType parseAuthenticationStatement(XMLEventReader xmlEventReader) throws ParsingException {
    StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
    StaxParserUtil.validate(startElement, SAML11Constants.AUTHENTICATION_STATEMENT);
    Attribute authMethod = startElement.getAttributeByName(new QName(SAML11Constants.AUTHENTICATION_METHOD));
    if (authMethod == null)
        throw logger.parserRequiredAttribute(SAML11Constants.AUTHENTICATION_METHOD);
    Attribute authInstant = startElement.getAttributeByName(new QName(SAML11Constants.AUTHENTICATION_INSTANT));
    if (authInstant == null)
        throw logger.parserRequiredAttribute(SAML11Constants.AUTHENTICATION_INSTANT);
    SAML11AuthenticationStatementType authStat = new SAML11AuthenticationStatementType(URI.create(StaxParserUtil.getAttributeValue(authMethod)), XMLTimeUtil.parse(StaxParserUtil.getAttributeValue(authInstant)));
    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(SAML11Constants.AUTHENTICATION_STATEMENT))
                break;
            else
                throw logger.parserUnknownEndElement(endElementTag, xmlEvent.getLocation());
        }
        startElement = null;
        if (xmlEvent instanceof StartElement) {
            startElement = (StartElement) xmlEvent;
        } else {
            startElement = StaxParserUtil.peekNextStartElement(xmlEventReader);
        }
        if (startElement == null)
            break;
        String tag = StaxParserUtil.getElementName(startElement);
        if (JBossSAMLConstants.SUBJECT.get().equalsIgnoreCase(tag)) {
            SAML11SubjectParser subjectParser = new SAML11SubjectParser();
            SAML11SubjectType subject = (SAML11SubjectType) subjectParser.parse(xmlEventReader);
            SAML11SubjectStatementType subStat = new SAML11SubjectStatementType();
            subStat.setSubject(subject);
            authStat.setSubject(subject);
        } else if (JBossSAMLConstants.SUBJECT_LOCALITY.get().equals(tag)) {
            startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
            SAML11SubjectLocalityType subjectLocalityType = new SAML11SubjectLocalityType();
            Attribute address = startElement.getAttributeByName(new QName(SAML11Constants.IP_ADDRESS));
            if (address != null) {
                subjectLocalityType.setIpAddress(StaxParserUtil.getAttributeValue(address));
            }
            Attribute dns = startElement.getAttributeByName(new QName(SAML11Constants.DNS_ADDRESS));
            if (dns != null) {
                subjectLocalityType.setDnsAddress(StaxParserUtil.getAttributeValue(dns));
            }
            authStat.setSubjectLocality(subjectLocalityType);
            StaxParserUtil.validate(StaxParserUtil.getNextEndElement(xmlEventReader), JBossSAMLConstants.SUBJECT_LOCALITY.get());
        } else if (SAML11Constants.AUTHORITY_BINDING.equals(tag)) {
            Attribute authorityKindAttr = startElement.getAttributeByName(new QName(SAML11Constants.AUTHORITY_KIND));
            if (authorityKindAttr == null)
                throw logger.parserRequiredAttribute("AuthorityKind");
            Attribute locationAttr = startElement.getAttributeByName(new QName(SAML11Constants.LOCATION));
            if (locationAttr == null)
                throw logger.parserRequiredAttribute("Location");
            URI location = URI.create(StaxParserUtil.getAttributeValue(locationAttr));
            Attribute bindingAttr = startElement.getAttributeByName(new QName(SAML11Constants.BINDING));
            if (bindingAttr == null)
                throw logger.parserRequiredAttribute("Binding");
            URI binding = URI.create(StaxParserUtil.getAttributeValue(bindingAttr));
            QName authorityKind = QName.valueOf(StaxParserUtil.getAttributeValue(authorityKindAttr));
            SAML11AuthorityBindingType authorityBinding = new SAML11AuthorityBindingType(authorityKind, location, binding);
            authStat.add(authorityBinding);
        } else
            throw logger.parserUnknownTag("", startElement.getLocation());
    }
    return authStat;
}
Also used : SAML11AuthorityBindingType(org.keycloak.dom.saml.v1.assertion.SAML11AuthorityBindingType) 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) URI(java.net.URI) StartElement(javax.xml.stream.events.StartElement) SAML11AuthenticationStatementType(org.keycloak.dom.saml.v1.assertion.SAML11AuthenticationStatementType) XMLEvent(javax.xml.stream.events.XMLEvent) SAML11SubjectLocalityType(org.keycloak.dom.saml.v1.assertion.SAML11SubjectLocalityType) SAML11SubjectStatementType(org.keycloak.dom.saml.v1.assertion.SAML11SubjectStatementType)

Example 14 with SAML11SubjectType

use of org.keycloak.dom.saml.v1.assertion.SAML11SubjectType in project keycloak by keycloak.

the class SAML11RequestWriter method write.

public void write(SAML11AttributeQueryType attr) throws ProcessingException {
    StaxUtil.writeStartElement(writer, PROTOCOL_PREFIX, SAML11Constants.ATTRIBUTE_QUERY, namespace);
    URI resource = attr.getResource();
    if (resource != null) {
        StaxUtil.writeAttribute(writer, SAML11Constants.RESOURCE, resource.toString());
    }
    SAML11SubjectType subject = attr.getSubject();
    if (subject != null) {
        assertionWriter.write(subject);
    }
    List<SAML11AttributeDesignatorType> attributes = attr.get();
    for (SAML11AttributeDesignatorType attribute : attributes) {
        if (attribute instanceof SAML11AttributeType) {
            SAML11AttributeType sat = (SAML11AttributeType) attribute;
            assertionWriter.write(sat);
        } else
            throw logger.writerUnknownTypeError(attribute.getClass().getName());
    }
    StaxUtil.writeEndElement(writer);
    StaxUtil.flush(writer);
}
Also used : SAML11SubjectType(org.keycloak.dom.saml.v1.assertion.SAML11SubjectType) SAML11AttributeDesignatorType(org.keycloak.dom.saml.v1.assertion.SAML11AttributeDesignatorType) SAML11AttributeType(org.keycloak.dom.saml.v1.assertion.SAML11AttributeType) URI(java.net.URI)

Example 15 with SAML11SubjectType

use of org.keycloak.dom.saml.v1.assertion.SAML11SubjectType in project keycloak by keycloak.

the class SAML11SubjectParser method parse.

/**
 * @see {@link ParserNamespaceSupport#parse(XMLEventReader)}
 */
public Object parse(XMLEventReader xmlEventReader) throws ParsingException {
    StaxParserUtil.getNextEvent(xmlEventReader);
    SAML11SubjectType subject = new SAML11SubjectType();
    // Peek at the next event
    while (xmlEventReader.hasNext()) {
        XMLEvent xmlEvent = StaxParserUtil.peek(xmlEventReader);
        if (xmlEvent instanceof EndElement) {
            EndElement endElement = (EndElement) xmlEvent;
            if (StaxParserUtil.matches(endElement, JBossSAMLConstants.SUBJECT.get())) {
                endElement = StaxParserUtil.getNextEndElement(xmlEventReader);
                break;
            } else
                throw logger.parserUnknownEndElement(StaxParserUtil.getElementName(endElement), xmlEvent.getLocation());
        }
        StartElement peekedElement = StaxParserUtil.peekNextStartElement(xmlEventReader);
        if (peekedElement == null)
            break;
        String tag = StaxParserUtil.getElementName(peekedElement);
        if (SAML11Constants.NAME_IDENTIFIER.equalsIgnoreCase(tag)) {
            peekedElement = StaxParserUtil.getNextStartElement(xmlEventReader);
            String val = StaxParserUtil.getElementText(xmlEventReader);
            SAML11NameIdentifierType nameID = new SAML11NameIdentifierType(val);
            Attribute formatAtt = peekedElement.getAttributeByName(new QName(SAML11Constants.FORMAT));
            if (formatAtt != null) {
                nameID.setFormat(URI.create(StaxParserUtil.getAttributeValue(formatAtt)));
            }
            Attribute nameQAtt = peekedElement.getAttributeByName(new QName(SAML11Constants.NAME_QUALIFIER));
            if (nameQAtt != null) {
                nameID.setNameQualifier(StaxParserUtil.getAttributeValue(nameQAtt));
            }
            SAML11SubjectTypeChoice subChoice = new SAML11SubjectTypeChoice(nameID);
            subject.setChoice(subChoice);
        } else if (JBossSAMLConstants.SUBJECT_CONFIRMATION.get().equalsIgnoreCase(tag)) {
            SAML11SubjectConfirmationType subjectConfirmationType = SAML11ParserUtil.parseSAML11SubjectConfirmation(xmlEventReader);
            subject.setSubjectConfirmation(subjectConfirmationType);
        } else
            throw logger.parserUnknownTag(tag, peekedElement.getLocation());
    }
    return subject;
}
Also used : StartElement(javax.xml.stream.events.StartElement) SAML11SubjectType(org.keycloak.dom.saml.v1.assertion.SAML11SubjectType) SAML11SubjectConfirmationType(org.keycloak.dom.saml.v1.assertion.SAML11SubjectConfirmationType) EndElement(javax.xml.stream.events.EndElement) Attribute(javax.xml.stream.events.Attribute) SAML11NameIdentifierType(org.keycloak.dom.saml.v1.assertion.SAML11NameIdentifierType) QName(javax.xml.namespace.QName) XMLEvent(javax.xml.stream.events.XMLEvent) SAML11SubjectTypeChoice(org.keycloak.dom.saml.v1.assertion.SAML11SubjectType.SAML11SubjectTypeChoice)

Aggregations

SAML11SubjectType (org.keycloak.dom.saml.v1.assertion.SAML11SubjectType)11 EndElement (javax.xml.stream.events.EndElement)8 StartElement (javax.xml.stream.events.StartElement)8 XMLEvent (javax.xml.stream.events.XMLEvent)8 SAML11SubjectParser (org.keycloak.saml.processing.core.parsers.saml.SAML11SubjectParser)6 URI (java.net.URI)5 QName (javax.xml.namespace.QName)5 Attribute (javax.xml.stream.events.Attribute)5 SAML11ActionType (org.keycloak.dom.saml.v1.assertion.SAML11ActionType)4 SAML11AttributeType (org.keycloak.dom.saml.v1.assertion.SAML11AttributeType)3 SAML11AttributeStatementType (org.keycloak.dom.saml.v1.assertion.SAML11AttributeStatementType)2 SAML11AuthenticationStatementType (org.keycloak.dom.saml.v1.assertion.SAML11AuthenticationStatementType)2 SAML11AuthorityBindingType (org.keycloak.dom.saml.v1.assertion.SAML11AuthorityBindingType)2 SAML11AuthorizationDecisionStatementType (org.keycloak.dom.saml.v1.assertion.SAML11AuthorizationDecisionStatementType)2 SAML11NameIdentifierType (org.keycloak.dom.saml.v1.assertion.SAML11NameIdentifierType)2 SAML11SubjectConfirmationType (org.keycloak.dom.saml.v1.assertion.SAML11SubjectConfirmationType)2 SAML11SubjectLocalityType (org.keycloak.dom.saml.v1.assertion.SAML11SubjectLocalityType)2 SAML11SubjectStatementType (org.keycloak.dom.saml.v1.assertion.SAML11SubjectStatementType)2 SAML11SubjectTypeChoice (org.keycloak.dom.saml.v1.assertion.SAML11SubjectType.SAML11SubjectTypeChoice)2 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)1