Search in sources :

Example 1 with SAML11AttributeType

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

the class SAML11ParserUtil method parseSAML11Attribute.

/**
 * Parse a {@link SAML11AttributeType}
 *
 * @param xmlEventReader
 *
 * @return
 *
 * @throws ParsingException
 */
public static SAML11AttributeType parseSAML11Attribute(XMLEventReader xmlEventReader) throws ParsingException {
    StartElement startElement = StaxParserUtil.getNextStartElement(xmlEventReader);
    StaxParserUtil.validate(startElement, JBossSAMLConstants.ATTRIBUTE.get());
    SAML11AttributeType attributeType = null;
    Attribute name = startElement.getAttributeByName(new QName(SAML11Constants.ATTRIBUTE_NAME));
    if (name == null)
        throw logger.parserRequiredAttribute("Name");
    String attribName = StaxParserUtil.getAttributeValue(name);
    Attribute namesp = startElement.getAttributeByName(new QName(SAML11Constants.ATTRIBUTE_NAMESPACE));
    if (namesp == null)
        throw logger.parserRequiredAttribute("Namespace");
    String attribNamespace = StaxParserUtil.getAttributeValue(namesp);
    attributeType = new SAML11AttributeType(attribName, URI.create(attribNamespace));
    attributeType.add(parseAttributeValue(xmlEventReader));
    parseAttributeType(xmlEventReader, startElement, JBossSAMLConstants.ATTRIBUTE.get(), attributeType);
    return attributeType;
}
Also used : StartElement(javax.xml.stream.events.StartElement) Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) SAML11AttributeType(org.keycloak.dom.saml.v1.assertion.SAML11AttributeType)

Example 2 with SAML11AttributeType

use of org.keycloak.dom.saml.v1.assertion.SAML11AttributeType 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 3 with SAML11AttributeType

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

the class SAML11AssertionWriter method write.

public void write(SAML11AttributeStatementType statement) throws ProcessingException {
    StaxUtil.writeStartElement(writer, ASSERTION_PREFIX, JBossSAMLConstants.ATTRIBUTE_STATEMENT.get(), SAML11Constants.ASSERTION_11_NSURI);
    SAML11SubjectType subject = statement.getSubject();
    if (subject != null)
        write(subject);
    List<SAML11AttributeType> attributes = statement.get();
    if (attributes != null) {
        for (SAML11AttributeType attr : attributes) {
            write(attr);
        }
    }
    StaxUtil.writeEndElement(writer);
    StaxUtil.flush(writer);
}
Also used : SAML11SubjectType(org.keycloak.dom.saml.v1.assertion.SAML11SubjectType) SAML11AttributeType(org.keycloak.dom.saml.v1.assertion.SAML11AttributeType)

Example 4 with SAML11AttributeType

use of org.keycloak.dom.saml.v1.assertion.SAML11AttributeType 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 5 with SAML11AttributeType

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

the class AssertionUtil method getRoles.

/**
 * Given an assertion, return the list of roles it may have
 *
 * @param assertion The {@link SAML11AssertionType}
 * @param roleKeys a list of string values representing the role keys. The list can be null.
 *
 * @return
 */
public static List<String> getRoles(SAML11AssertionType assertion, List<String> roleKeys) {
    List<String> roles = new ArrayList<>();
    List<SAML11StatementAbstractType> statements = assertion.getStatements();
    for (SAML11StatementAbstractType statement : statements) {
        if (statement instanceof SAML11AttributeStatementType) {
            SAML11AttributeStatementType attributeStatement = (SAML11AttributeStatementType) statement;
            List<SAML11AttributeType> attributes = attributeStatement.get();
            for (SAML11AttributeType attr : attributes) {
                if (roleKeys != null && roleKeys.size() > 0) {
                    if (!roleKeys.contains(attr.getAttributeName()))
                        continue;
                }
                List<Object> attributeValues = attr.get();
                if (attributeValues != null) {
                    for (Object attrValue : attributeValues) {
                        if (attrValue instanceof String) {
                            roles.add((String) attrValue);
                        } else if (attrValue instanceof Node) {
                            Node roleNode = (Node) attrValue;
                            roles.add(roleNode.getFirstChild().getNodeValue());
                        } else
                            throw logger.unknownObjectType(attrValue);
                    }
                }
            }
        }
    }
    return roles;
}
Also used : Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) SAML11AttributeType(org.keycloak.dom.saml.v1.assertion.SAML11AttributeType) SAML11StatementAbstractType(org.keycloak.dom.saml.v1.assertion.SAML11StatementAbstractType) SAML11AttributeStatementType(org.keycloak.dom.saml.v1.assertion.SAML11AttributeStatementType)

Aggregations

SAML11AttributeType (org.keycloak.dom.saml.v1.assertion.SAML11AttributeType)5 SAML11SubjectType (org.keycloak.dom.saml.v1.assertion.SAML11SubjectType)3 StartElement (javax.xml.stream.events.StartElement)2 SAML11AttributeStatementType (org.keycloak.dom.saml.v1.assertion.SAML11AttributeStatementType)2 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 QName (javax.xml.namespace.QName)1 Attribute (javax.xml.stream.events.Attribute)1 EndElement (javax.xml.stream.events.EndElement)1 XMLEvent (javax.xml.stream.events.XMLEvent)1 SAML11AttributeDesignatorType (org.keycloak.dom.saml.v1.assertion.SAML11AttributeDesignatorType)1 SAML11StatementAbstractType (org.keycloak.dom.saml.v1.assertion.SAML11StatementAbstractType)1 SAML11SubjectParser (org.keycloak.saml.processing.core.parsers.saml.SAML11SubjectParser)1 Node (org.w3c.dom.Node)1