Search in sources :

Example 1 with ConditionAbstractType

use of org.keycloak.dom.saml.v2.assertion.ConditionAbstractType in project keycloak by keycloak.

the class SAMLAssertionFactory method createConditions.

/**
 * <p>
 * Creates a {@code Conditions} instance with the specified values.
 * </p>
 *
 * @param notBefore a {@code XMLGregorianCalendar} representing the start of the token lifetime period.
 * @param notOnOrAfter a {@code XMLGregorianCalendar} representing the end of the token lifetime period.
 * @param restrictions an array containing the applicable restrictions.
 *
 * @return the constructed {@code Conditions} instance.
 */
public static ConditionsType createConditions(XMLGregorianCalendar notBefore, XMLGregorianCalendar notOnOrAfter, ConditionAbstractType... restrictions) {
    ConditionsType conditions = new ConditionsType();
    conditions.setNotBefore(notBefore);
    conditions.setNotOnOrAfter(notOnOrAfter);
    if (restrictions != null) {
        for (ConditionAbstractType condition : restrictions) {
            conditions.addCondition(condition);
        }
    }
    return conditions;
}
Also used : ConditionAbstractType(org.keycloak.dom.saml.v2.assertion.ConditionAbstractType) ConditionsType(org.keycloak.dom.saml.v2.assertion.ConditionsType)

Example 2 with ConditionAbstractType

use of org.keycloak.dom.saml.v2.assertion.ConditionAbstractType in project keycloak by keycloak.

the class IncludeOneTimeUseConditionTest method testOneTimeUseConditionIncluded.

private void testOneTimeUseConditionIncluded(Boolean oneTimeUseConditionShouldBeIncluded) throws IOException {
    try (Closeable c = ClientAttributeUpdater.forClient(adminClient, REALM_NAME, SAML_CLIENT_ID_SALES_POST).setAttribute(SamlConfigAttributes.SAML_ONETIMEUSE_CONDITION, oneTimeUseConditionShouldBeIncluded.toString()).update()) {
        SAMLDocumentHolder res = new SamlClientBuilder().authnRequest(getAuthServerSamlEndpoint(REALM_NAME), SAML_CLIENT_ID_SALES_POST, SAML_ASSERTION_CONSUMER_URL_SALES_POST, Binding.POST).build().login().user(bburkeUser).build().getSamlResponse(Binding.POST);
        assertThat(res.getSamlObject(), notNullValue());
        assertThat(res.getSamlObject(), instanceOf(ResponseType.class));
        ResponseType rt = (ResponseType) res.getSamlObject();
        assertThat(rt.getAssertions(), not(empty()));
        final ConditionsType conditionsType = rt.getAssertions().get(0).getAssertion().getConditions();
        assertThat(conditionsType, notNullValue());
        assertThat(conditionsType.getConditions(), not(empty()));
        final List<ConditionAbstractType> conditions = conditionsType.getConditions();
        final Collection<ConditionAbstractType> oneTimeUseConditions = Collections2.filter(conditions, input -> input instanceof OneTimeUseType);
        final boolean oneTimeUseConditionAdded = !oneTimeUseConditions.isEmpty();
        assertThat(oneTimeUseConditionAdded, is(oneTimeUseConditionShouldBeIncluded));
    }
}
Also used : OneTimeUseType(org.keycloak.dom.saml.v2.assertion.OneTimeUseType) ConditionAbstractType(org.keycloak.dom.saml.v2.assertion.ConditionAbstractType) SAMLDocumentHolder(org.keycloak.saml.processing.core.saml.v2.common.SAMLDocumentHolder) SamlClientBuilder(org.keycloak.testsuite.util.SamlClientBuilder) Closeable(java.io.Closeable) ConditionsType(org.keycloak.dom.saml.v2.assertion.ConditionsType) ResponseType(org.keycloak.dom.saml.v2.protocol.ResponseType)

Example 3 with ConditionAbstractType

use of org.keycloak.dom.saml.v2.assertion.ConditionAbstractType in project keycloak by keycloak.

the class ConditionsValidator method validateConditions.

private Result validateConditions(ConditionsType ct, Result res) {
    Iterator<ConditionAbstractType> it = ct.getConditions() == null ? Collections.<ConditionAbstractType>emptySet().iterator() : ct.getConditions().iterator();
    while (it.hasNext() && res == Result.VALID) {
        ConditionAbstractType cond = it.next();
        Result r;
        if (cond instanceof OneTimeUseType) {
            r = validateOneTimeUse((OneTimeUseType) cond);
        } else if (cond instanceof AudienceRestrictionType) {
            r = validateAudienceRestriction((AudienceRestrictionType) cond);
        } else if (cond instanceof ProxyRestrictionType) {
            r = validateProxyRestriction((ProxyRestrictionType) cond);
        } else {
            r = Result.INDETERMINATE;
            LOG.infof("Unknown condition in assertion %s: %s", assertionId, cond == null ? "<null>" : cond.getClass());
        }
        res = r.joinResult(res);
    }
    return res;
}
Also used : OneTimeUseType(org.keycloak.dom.saml.v2.assertion.OneTimeUseType) ConditionAbstractType(org.keycloak.dom.saml.v2.assertion.ConditionAbstractType) ProxyRestrictionType(org.keycloak.dom.saml.v2.assertion.ProxyRestrictionType) AudienceRestrictionType(org.keycloak.dom.saml.v2.assertion.AudienceRestrictionType)

Example 4 with ConditionAbstractType

use of org.keycloak.dom.saml.v2.assertion.ConditionAbstractType in project keycloak by keycloak.

the class SAMLAssertionWriter method write.

/**
 * Write an {@code AssertionType} to stream
 *
 * @param assertion
 *
 * @throws org.keycloak.saml.common.exceptions.ProcessingException
 */
public void write(AssertionType assertion) throws ProcessingException {
    StaxUtil.writeStartElement(writer, ASSERTION_PREFIX, JBossSAMLConstants.ASSERTION.get(), ASSERTION_NSURI.get());
    StaxUtil.writeNameSpace(writer, ASSERTION_PREFIX, ASSERTION_NSURI.get());
    StaxUtil.writeDefaultNameSpace(writer, ASSERTION_NSURI.get());
    // Attributes
    StaxUtil.writeAttribute(writer, JBossSAMLConstants.ID.get(), assertion.getID());
    StaxUtil.writeAttribute(writer, JBossSAMLConstants.VERSION.get(), assertion.getVersion());
    StaxUtil.writeAttribute(writer, JBossSAMLConstants.ISSUE_INSTANT.get(), assertion.getIssueInstant().toString());
    NameIDType issuer = assertion.getIssuer();
    if (issuer != null)
        write(issuer, new QName(ASSERTION_NSURI.get(), JBossSAMLConstants.ISSUER.get(), ASSERTION_PREFIX));
    SubjectType subject = assertion.getSubject();
    if (subject != null) {
        write(subject);
    }
    ConditionsType conditions = assertion.getConditions();
    if (conditions != null) {
        StaxUtil.writeStartElement(writer, ASSERTION_PREFIX, JBossSAMLConstants.CONDITIONS.get(), ASSERTION_NSURI.get());
        if (conditions.getNotBefore() != null) {
            StaxUtil.writeAttribute(writer, JBossSAMLConstants.NOT_BEFORE.get(), conditions.getNotBefore().toString());
        }
        if (conditions.getNotOnOrAfter() != null) {
            StaxUtil.writeAttribute(writer, JBossSAMLConstants.NOT_ON_OR_AFTER.get(), conditions.getNotOnOrAfter().toString());
        }
        List<ConditionAbstractType> typeOfConditions = conditions.getConditions();
        if (typeOfConditions != null) {
            for (ConditionAbstractType typeCondition : typeOfConditions) {
                if (typeCondition instanceof AudienceRestrictionType) {
                    AudienceRestrictionType art = (AudienceRestrictionType) typeCondition;
                    StaxUtil.writeStartElement(writer, ASSERTION_PREFIX, JBossSAMLConstants.AUDIENCE_RESTRICTION.get(), ASSERTION_NSURI.get());
                    List<URI> audiences = art.getAudience();
                    if (audiences != null) {
                        for (URI audience : audiences) {
                            StaxUtil.writeStartElement(writer, ASSERTION_PREFIX, JBossSAMLConstants.AUDIENCE.get(), ASSERTION_NSURI.get());
                            StaxUtil.writeCharacters(writer, audience.toString());
                            StaxUtil.writeEndElement(writer);
                        }
                    }
                    StaxUtil.writeEndElement(writer);
                }
                if (typeCondition instanceof OneTimeUseType) {
                    StaxUtil.writeStartElement(writer, ASSERTION_PREFIX, JBossSAMLConstants.ONE_TIME_USE.get(), ASSERTION_NSURI.get());
                    StaxUtil.writeEndElement(writer);
                }
            }
        }
        StaxUtil.writeEndElement(writer);
    }
    AdviceType advice = assertion.getAdvice();
    if (advice != null)
        throw logger.notImplementedYet("Advice");
    Set<StatementAbstractType> statements = assertion.getStatements();
    if (statements != null) {
        for (StatementAbstractType statement : statements) {
            if (statement instanceof AuthnStatementType) {
                write((AuthnStatementType) statement, false);
            } else if (statement instanceof AttributeStatementType) {
                write((AttributeStatementType) statement);
            } else
                throw logger.writerUnknownTypeError(statement.getClass().getName());
        }
    }
    StaxUtil.writeEndElement(writer);
    StaxUtil.flush(writer);
}
Also used : QName(javax.xml.namespace.QName) AudienceRestrictionType(org.keycloak.dom.saml.v2.assertion.AudienceRestrictionType) AttributeStatementType(org.keycloak.dom.saml.v2.assertion.AttributeStatementType) URI(java.net.URI) ASSERTION_NSURI(org.keycloak.saml.common.constants.JBossSAMLURIConstants.ASSERTION_NSURI) OneTimeUseType(org.keycloak.dom.saml.v2.assertion.OneTimeUseType) AuthnStatementType(org.keycloak.dom.saml.v2.assertion.AuthnStatementType) ConditionAbstractType(org.keycloak.dom.saml.v2.assertion.ConditionAbstractType) SubjectType(org.keycloak.dom.saml.v2.assertion.SubjectType) AdviceType(org.keycloak.dom.saml.v2.assertion.AdviceType) ConditionsType(org.keycloak.dom.saml.v2.assertion.ConditionsType) NameIDType(org.keycloak.dom.saml.v2.assertion.NameIDType) StatementAbstractType(org.keycloak.dom.saml.v2.assertion.StatementAbstractType)

Aggregations

ConditionAbstractType (org.keycloak.dom.saml.v2.assertion.ConditionAbstractType)4 ConditionsType (org.keycloak.dom.saml.v2.assertion.ConditionsType)3 OneTimeUseType (org.keycloak.dom.saml.v2.assertion.OneTimeUseType)3 AudienceRestrictionType (org.keycloak.dom.saml.v2.assertion.AudienceRestrictionType)2 Closeable (java.io.Closeable)1 URI (java.net.URI)1 QName (javax.xml.namespace.QName)1 AdviceType (org.keycloak.dom.saml.v2.assertion.AdviceType)1 AttributeStatementType (org.keycloak.dom.saml.v2.assertion.AttributeStatementType)1 AuthnStatementType (org.keycloak.dom.saml.v2.assertion.AuthnStatementType)1 NameIDType (org.keycloak.dom.saml.v2.assertion.NameIDType)1 ProxyRestrictionType (org.keycloak.dom.saml.v2.assertion.ProxyRestrictionType)1 StatementAbstractType (org.keycloak.dom.saml.v2.assertion.StatementAbstractType)1 SubjectType (org.keycloak.dom.saml.v2.assertion.SubjectType)1 ResponseType (org.keycloak.dom.saml.v2.protocol.ResponseType)1 ASSERTION_NSURI (org.keycloak.saml.common.constants.JBossSAMLURIConstants.ASSERTION_NSURI)1 SAMLDocumentHolder (org.keycloak.saml.processing.core.saml.v2.common.SAMLDocumentHolder)1 SamlClientBuilder (org.keycloak.testsuite.util.SamlClientBuilder)1