Search in sources :

Example 1 with SAMLResponseWriter

use of org.keycloak.saml.processing.core.saml.v2.writers.SAMLResponseWriter in project keycloak by keycloak.

the class SamlProtocolUtilsTest method testBuildArtifactResponse.

@Test
public void testBuildArtifactResponse() throws ConfigurationException, ProcessingException, ParsingException {
    ResponseType response = new SAML2LoginResponseBuilder().requestID(IDGenerator.create("ID_")).destination("http://localhost:8180/auth/realms/demo/broker/saml-broker/endpoint").issuer("http://saml.idp/saml").assertionExpiration(1000000).subjectExpiration(1000000).requestIssuer("http://localhost:8180/auth/realms/demo").nameIdentifier(JBossSAMLURIConstants.NAMEID_FORMAT_EMAIL.get(), "a@b.c").authMethod(JBossSAMLURIConstants.AC_UNSPECIFIED.get()).sessionIndex("idp:" + UUID.randomUUID()).buildModel();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    SAMLResponseWriter writer = new SAMLResponseWriter(StaxUtil.getXMLStreamWriter(bos));
    writer.write(response);
    Document responseDoc = DocumentUtil.getDocument(new ByteArrayInputStream(bos.toByteArray()));
    ArtifactResponseType artifactResponseType = SamlProtocolUtils.buildArtifactResponse(responseDoc);
    Document doc = SamlProtocolUtils.convert(artifactResponseType);
    String artifactResponse = DocumentUtil.asString(doc);
    assertThat(artifactResponse, containsString("samlp:ArtifactResponse"));
    assertThat(artifactResponse, containsString("samlp:Response"));
    assertThat(artifactResponse, containsString("saml:Assertion"));
    assertThat(artifactResponse.indexOf("samlp:ArtifactResponse"), lessThan(artifactResponse.indexOf("samlp:Response")));
    assertThat(artifactResponse.indexOf("samlp:Response"), lessThan(artifactResponse.indexOf("saml:Assertion")));
    assertThat(artifactResponse.split("\\Q<saml:Issuer>http://saml.idp/saml</saml:Issuer>\\E").length, is(4));
    assertThat(artifactResponse.split("\\Q<samlp:StatusCode Value=\"urn:oasis:names:tc:SAML:2.0:status:Success\"/>\\E").length, is(3));
}
Also used : SAMLResponseWriter(org.keycloak.saml.processing.core.saml.v2.writers.SAMLResponseWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) ArtifactResponseType(org.keycloak.dom.saml.v2.protocol.ArtifactResponseType) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) SAML2LoginResponseBuilder(org.keycloak.saml.SAML2LoginResponseBuilder) Document(org.w3c.dom.Document) ArtifactResponseType(org.keycloak.dom.saml.v2.protocol.ArtifactResponseType) ResponseType(org.keycloak.dom.saml.v2.protocol.ResponseType) Test(org.junit.Test)

Example 2 with SAMLResponseWriter

use of org.keycloak.saml.processing.core.saml.v2.writers.SAMLResponseWriter in project keycloak by keycloak.

the class SAML2Response method marshall.

/**
 * Marshall the response type to the output stream
 *
 * @param responseType
 * @param os
 *
 * @throws ProcessingException
 */
public void marshall(ResponseType responseType, OutputStream os) throws ProcessingException {
    SAMLResponseWriter samlWriter = new SAMLResponseWriter(StaxUtil.getXMLStreamWriter(os));
    samlWriter.write(responseType);
}
Also used : SAMLResponseWriter(org.keycloak.saml.processing.core.saml.v2.writers.SAMLResponseWriter)

Example 3 with SAMLResponseWriter

use of org.keycloak.saml.processing.core.saml.v2.writers.SAMLResponseWriter in project keycloak by keycloak.

the class SAML2Response method convert.

/**
 * Convert a SAML2 Response into a Document
 *
 * @param responseType
 *
 * @return
 *
 * @throws ParsingException
 * @throws ConfigurationException
 * @throws ProcessingException
 */
public Document convert(StatusResponseType responseType) throws ProcessingException, ConfigurationException, ParsingException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    SAMLResponseWriter writer = new SAMLResponseWriter(StaxUtil.getXMLStreamWriter(bos));
    if (responseType instanceof ResponseType) {
        ResponseType response = (ResponseType) responseType;
        writer.write(response);
    } else {
        writer.write(responseType, new QName(PROTOCOL_NSURI.get(), JBossSAMLConstants.LOGOUT_RESPONSE.get(), "samlp"));
    }
    return DocumentUtil.getDocument(new ByteArrayInputStream(bos.toByteArray()));
}
Also used : SAMLResponseWriter(org.keycloak.saml.processing.core.saml.v2.writers.SAMLResponseWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) QName(javax.xml.namespace.QName) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ResponseType(org.keycloak.dom.saml.v2.protocol.ResponseType) StatusResponseType(org.keycloak.dom.saml.v2.protocol.StatusResponseType)

Example 4 with SAMLResponseWriter

use of org.keycloak.saml.processing.core.saml.v2.writers.SAMLResponseWriter in project keycloak by keycloak.

the class SamlProtocolUtils method convert.

/**
 * Convert a SAML2 ArtifactResponse into a Document
 * @param responseType an artifactResponse
 *
 * @return an artifact response converted to a Document
 *
 * @throws ParsingException
 * @throws ConfigurationException
 * @throws ProcessingException
 */
public static Document convert(ArtifactResponseType responseType) throws ProcessingException, ConfigurationException, ParsingException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    SAMLResponseWriter writer = new SAMLResponseWriter(StaxUtil.getXMLStreamWriter(bos));
    writer.write(responseType);
    return DocumentUtil.getDocument(new ByteArrayInputStream(bos.toByteArray()));
}
Also used : SAMLResponseWriter(org.keycloak.saml.processing.core.saml.v2.writers.SAMLResponseWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 5 with SAMLResponseWriter

use of org.keycloak.saml.processing.core.saml.v2.writers.SAMLResponseWriter in project keycloak by keycloak.

the class SamlDocumentStepBuilder method saml2Object2String.

public static String saml2Object2String(final SAML2Object transformed) {
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        XMLStreamWriter xmlStreamWriter = StaxUtil.getXMLStreamWriter(bos);
        if (transformed instanceof AuthnRequestType) {
            new SAMLRequestWriter(xmlStreamWriter).write((AuthnRequestType) transformed);
        } else if (transformed instanceof LogoutRequestType) {
            new SAMLRequestWriter(xmlStreamWriter).write((LogoutRequestType) transformed);
        } else if (transformed instanceof ArtifactResolveType) {
            new SAMLRequestWriter(xmlStreamWriter).write((ArtifactResolveType) transformed);
        } else if (transformed instanceof AttributeQueryType) {
            new SAMLRequestWriter(xmlStreamWriter).write((AttributeQueryType) transformed);
        } else if (transformed instanceof ResponseType) {
            new SAMLResponseWriter(xmlStreamWriter).write((ResponseType) transformed);
        } else if (transformed instanceof ArtifactResponseType) {
            new SAMLResponseWriter(xmlStreamWriter).write((ArtifactResponseType) transformed);
        } else if (transformed instanceof StatusResponseType) {
            new SAMLResponseWriter(xmlStreamWriter).write((StatusResponseType) transformed, SAMLProtocolQNames.LOGOUT_RESPONSE.getQName("samlp"));
        } else {
            Assert.assertNotNull("Unknown type: <null>", transformed);
            Assert.fail("Unknown type: " + transformed.getClass().getName());
        }
        return new String(bos.toByteArray(), GeneralConstants.SAML_CHARSET);
    } catch (ProcessingException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : ArtifactResolveType(org.keycloak.dom.saml.v2.protocol.ArtifactResolveType) LogoutRequestType(org.keycloak.dom.saml.v2.protocol.LogoutRequestType) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StatusResponseType(org.keycloak.dom.saml.v2.protocol.StatusResponseType) ResponseType(org.keycloak.dom.saml.v2.protocol.ResponseType) ArtifactResponseType(org.keycloak.dom.saml.v2.protocol.ArtifactResponseType) StatusResponseType(org.keycloak.dom.saml.v2.protocol.StatusResponseType) SAMLResponseWriter(org.keycloak.saml.processing.core.saml.v2.writers.SAMLResponseWriter) AuthnRequestType(org.keycloak.dom.saml.v2.protocol.AuthnRequestType) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) SAMLRequestWriter(org.keycloak.saml.processing.core.saml.v2.writers.SAMLRequestWriter) ArtifactResponseType(org.keycloak.dom.saml.v2.protocol.ArtifactResponseType) AttributeQueryType(org.keycloak.dom.saml.v2.protocol.AttributeQueryType) ProcessingException(org.keycloak.saml.common.exceptions.ProcessingException)

Aggregations

SAMLResponseWriter (org.keycloak.saml.processing.core.saml.v2.writers.SAMLResponseWriter)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ResponseType (org.keycloak.dom.saml.v2.protocol.ResponseType)4 ArtifactResponseType (org.keycloak.dom.saml.v2.protocol.ArtifactResponseType)3 StatusResponseType (org.keycloak.dom.saml.v2.protocol.StatusResponseType)2 ProcessingException (org.keycloak.saml.common.exceptions.ProcessingException)2 QName (javax.xml.namespace.QName)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Test (org.junit.Test)1 AssertionType (org.keycloak.dom.saml.v2.assertion.AssertionType)1 AuthnStatementType (org.keycloak.dom.saml.v2.assertion.AuthnStatementType)1 ArtifactResolveType (org.keycloak.dom.saml.v2.protocol.ArtifactResolveType)1 AttributeQueryType (org.keycloak.dom.saml.v2.protocol.AttributeQueryType)1 AuthnRequestType (org.keycloak.dom.saml.v2.protocol.AuthnRequestType)1 LogoutRequestType (org.keycloak.dom.saml.v2.protocol.LogoutRequestType)1 SAML2LoginResponseBuilder (org.keycloak.saml.SAML2LoginResponseBuilder)1 SAMLAssertionWriter (org.keycloak.saml.processing.core.saml.v2.writers.SAMLAssertionWriter)1 SAMLRequestWriter (org.keycloak.saml.processing.core.saml.v2.writers.SAMLRequestWriter)1