Search in sources :

Example 6 with SAML11AssertionType

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

the class AssertionUtil method createSAML11Assertion.

/**
 * Create an assertion
 *
 * @param id
 * @param issuer
 *
 * @return
 */
public static SAML11AssertionType createSAML11Assertion(String id, XMLGregorianCalendar issueInstant, String issuer) {
    SAML11AssertionType assertion = new SAML11AssertionType(id, issueInstant);
    assertion.setIssuer(issuer);
    return assertion;
}
Also used : SAML11AssertionType(org.keycloak.dom.saml.v1.assertion.SAML11AssertionType)

Example 7 with SAML11AssertionType

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

the class SAML11AssertionWriter method write.

/**
 * Write an {@code SAML11AssertionType} to stream
 *
 * @param assertion
 * @param out
 *
 * @throws ProcessingException
 */
public void write(SAML11AssertionType assertion) throws ProcessingException {
    StaxUtil.writeStartElement(writer, ASSERTION_PREFIX, JBossSAMLConstants.ASSERTION.get(), ns);
    StaxUtil.writeNameSpace(writer, ASSERTION_PREFIX, ns);
    StaxUtil.writeDefaultNameSpace(writer, ns);
    // Attributes
    // StaxUtil.writeAttribute(writer, JBossSAMLConstants.ID.get(), assertion.getID());
    StaxUtil.writeAttribute(writer, SAML11Constants.ASSERTIONID, assertion.getID());
    StaxUtil.writeAttribute(writer, SAML11Constants.MAJOR_VERSION, assertion.getMajorVersion() + "");
    StaxUtil.writeAttribute(writer, SAML11Constants.MINOR_VERSION, assertion.getMinorVersion() + "");
    StaxUtil.writeAttribute(writer, JBossSAMLConstants.ISSUE_INSTANT.get(), assertion.getIssueInstant().toString());
    String issuer = assertion.getIssuer();
    if (issuer != null) {
        StaxUtil.writeAttribute(writer, SAML11Constants.ISSUER, issuer);
    }
    SAML11ConditionsType conditions = assertion.getConditions();
    if (conditions != null) {
        StaxUtil.writeStartElement(writer, ASSERTION_PREFIX, JBossSAMLConstants.CONDITIONS.get(), ns);
        StaxUtil.writeAttribute(writer, JBossSAMLConstants.NOT_BEFORE.get(), conditions.getNotBefore().toString());
        StaxUtil.writeAttribute(writer, JBossSAMLConstants.NOT_ON_OR_AFTER.get(), conditions.getNotOnOrAfter().toString());
        List<SAML11ConditionAbstractType> typeOfConditions = conditions.get();
        if (typeOfConditions != null) {
            for (SAML11ConditionAbstractType typeCondition : typeOfConditions) {
                if (typeCondition instanceof SAML11AudienceRestrictionCondition) {
                    SAML11AudienceRestrictionCondition art = (SAML11AudienceRestrictionCondition) typeCondition;
                    StaxUtil.writeStartElement(writer, ASSERTION_PREFIX, SAML11Constants.AUDIENCE_RESTRICTION_CONDITION, ns);
                    List<URI> audiences = art.get();
                    if (audiences != null) {
                        for (URI audience : audiences) {
                            StaxUtil.writeStartElement(writer, ASSERTION_PREFIX, JBossSAMLConstants.AUDIENCE.get(), ns);
                            StaxUtil.writeCharacters(writer, audience.toString());
                            StaxUtil.writeEndElement(writer);
                        }
                    }
                    StaxUtil.writeEndElement(writer);
                }
            }
        }
        StaxUtil.writeEndElement(writer);
    }
    SAML11AdviceType advice = assertion.getAdvice();
    if (advice != null)
        throw logger.notImplementedYet("Advice");
    List<SAML11StatementAbstractType> statements = assertion.getStatements();
    if (statements != null) {
        for (SAML11StatementAbstractType statement : statements) {
            if (statement instanceof SAML11AuthenticationStatementType) {
                write((SAML11AuthenticationStatementType) statement);
            } else if (statement instanceof SAML11AttributeStatementType) {
                write((SAML11AttributeStatementType) statement);
            } else if (statement instanceof SAML11AuthorizationDecisionStatementType) {
                write((SAML11AuthorizationDecisionStatementType) statement);
            } else if (statement instanceof SAML11SubjectStatementType) {
                write((SAML11SubjectStatementType) statement);
            } else
                throw logger.writerUnknownTypeError(statement.getClass().getName());
        }
    }
    Element sig = assertion.getSignature();
    if (sig != null)
        StaxUtil.writeDOMElement(writer, sig);
    StaxUtil.writeEndElement(writer);
    StaxUtil.flush(writer);
}
Also used : SAML11ConditionsType(org.keycloak.dom.saml.v1.assertion.SAML11ConditionsType) Element(org.w3c.dom.Element) URI(java.net.URI) SAML11AudienceRestrictionCondition(org.keycloak.dom.saml.v1.assertion.SAML11AudienceRestrictionCondition) SAML11AuthenticationStatementType(org.keycloak.dom.saml.v1.assertion.SAML11AuthenticationStatementType) SAML11AuthorizationDecisionStatementType(org.keycloak.dom.saml.v1.assertion.SAML11AuthorizationDecisionStatementType) SAML11ConditionAbstractType(org.keycloak.dom.saml.v1.assertion.SAML11ConditionAbstractType) SAML11AdviceType(org.keycloak.dom.saml.v1.assertion.SAML11AdviceType) SAML11StatementAbstractType(org.keycloak.dom.saml.v1.assertion.SAML11StatementAbstractType) SAML11AttributeStatementType(org.keycloak.dom.saml.v1.assertion.SAML11AttributeStatementType) SAML11SubjectStatementType(org.keycloak.dom.saml.v1.assertion.SAML11SubjectStatementType)

Example 8 with SAML11AssertionType

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

the class SAML11AssertionWriter method write.

public void write(SAML11EvidenceType evidence) throws ProcessingException {
    StaxUtil.writeStartElement(writer, ASSERTION_PREFIX, SAML11Constants.EVIDENCE, ns);
    List<String> assertionIDRefs = evidence.getAssertionIDReference();
    for (String assertionIDRef : assertionIDRefs) {
        StaxUtil.writeStartElement(writer, ASSERTION_PREFIX, SAML11Constants.ASSERTION_ID_REF, ns);
        StaxUtil.writeCharacters(writer, assertionIDRef);
        StaxUtil.writeEndElement(writer);
    }
    List<SAML11AssertionType> assertions = evidence.getAssertions();
    for (SAML11AssertionType assertion : assertions) {
        write(assertion);
    }
    StaxUtil.writeEndElement(writer);
}
Also used : SAML11AssertionType(org.keycloak.dom.saml.v1.assertion.SAML11AssertionType)

Example 9 with SAML11AssertionType

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

the class SAML11ResponseWriter method write.

public void write(SAML11ResponseType response) throws ProcessingException {
    StaxUtil.writeStartElement(writer, PROTOCOL_PREFIX, SAML11Constants.RESPONSE, namespace);
    StaxUtil.writeNameSpace(writer, PROTOCOL_PREFIX, namespace);
    StaxUtil.writeNameSpace(writer, ASSERTION_PREFIX, SAML11Constants.ASSERTION_11_NSURI);
    // Attributes
    StaxUtil.writeAttribute(writer, SAML11Constants.RESPONSE_ID, response.getID());
    StaxUtil.writeAttribute(writer, SAML11Constants.MAJOR_VERSION, response.getMajorVersion() + "");
    StaxUtil.writeAttribute(writer, SAML11Constants.MINOR_VERSION, response.getMinorVersion() + "");
    StaxUtil.writeAttribute(writer, JBossSAMLConstants.ISSUE_INSTANT.get(), response.getIssueInstant().toString());
    String inResp = response.getInResponseTo();
    if (StringUtil.isNotNull(inResp)) {
        StaxUtil.writeAttribute(writer, SAML11Constants.IN_RESPONSE_TO, inResp);
    }
    URI recipient = response.getRecipient();
    if (recipient != null) {
        StaxUtil.writeAttribute(writer, SAML11Constants.RECIPIENT, recipient.toString());
    }
    Element sig = response.getSignature();
    if (sig != null) {
        StaxUtil.writeDOMElement(writer, sig);
    }
    SAML11StatusType status = response.getStatus();
    if (status != null) {
        write(status);
    }
    List<SAML11AssertionType> assertions = response.get();
    for (SAML11AssertionType assertion : assertions) {
        assertionWriter.write(assertion);
    }
    StaxUtil.writeEndElement(writer);
    StaxUtil.flush(writer);
}
Also used : SAML11StatusType(org.keycloak.dom.saml.v1.protocol.SAML11StatusType) Element(org.w3c.dom.Element) SAML11AssertionType(org.keycloak.dom.saml.v1.assertion.SAML11AssertionType) URI(java.net.URI)

Example 10 with SAML11AssertionType

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

SAML11AssertionType (org.keycloak.dom.saml.v1.assertion.SAML11AssertionType)6 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)5 SAML11ConditionsType (org.keycloak.dom.saml.v1.assertion.SAML11ConditionsType)5 QName (javax.xml.namespace.QName)3 Attribute (javax.xml.stream.events.Attribute)3 SAML11AttributeStatementType (org.keycloak.dom.saml.v1.assertion.SAML11AttributeStatementType)3 Element (org.w3c.dom.Element)3 URI (java.net.URI)2 EndElement (javax.xml.stream.events.EndElement)2 StartElement (javax.xml.stream.events.StartElement)2 SAML11AuthenticationStatementType (org.keycloak.dom.saml.v1.assertion.SAML11AuthenticationStatementType)2 SAML11AuthorizationDecisionStatementType (org.keycloak.dom.saml.v1.assertion.SAML11AuthorizationDecisionStatementType)2 SAML11StatementAbstractType (org.keycloak.dom.saml.v1.assertion.SAML11StatementAbstractType)2 SAML11SubjectStatementType (org.keycloak.dom.saml.v1.assertion.SAML11SubjectStatementType)2 ArrayList (java.util.ArrayList)1 XMLEvent (javax.xml.stream.events.XMLEvent)1 SAML11AdviceType (org.keycloak.dom.saml.v1.assertion.SAML11AdviceType)1 SAML11AttributeType (org.keycloak.dom.saml.v1.assertion.SAML11AttributeType)1 SAML11AudienceRestrictionCondition (org.keycloak.dom.saml.v1.assertion.SAML11AudienceRestrictionCondition)1 SAML11ConditionAbstractType (org.keycloak.dom.saml.v1.assertion.SAML11ConditionAbstractType)1