Search in sources :

Example 6 with ArtifactResolveType

use of org.keycloak.dom.saml.v2.protocol.ArtifactResolveType 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)

Example 7 with ArtifactResolveType

use of org.keycloak.dom.saml.v2.protocol.ArtifactResolveType in project keycloak by keycloak.

the class SAMLArtifactResolveParser method instantiateElement.

/**
 * Parse the attributes at the authnrequesttype element
 *
 * @param startElement
 *
 * @return
 *
 * @throws ParsingException
 */
@Override
protected ArtifactResolveType instantiateElement(XMLEventReader xmlEventReader, StartElement startElement) throws ParsingException {
    SAMLParserUtil.validateAttributeValue(startElement, SAMLProtocolQNames.ATTR_VERSION, VERSION_2_0);
    String id = StaxParserUtil.getRequiredAttributeValue(startElement, SAMLProtocolQNames.ATTR_ID);
    XMLGregorianCalendar issueInstant = XMLTimeUtil.parse(StaxParserUtil.getRequiredAttributeValue(startElement, SAMLProtocolQNames.ATTR_ISSUE_INSTANT));
    ArtifactResolveType authnRequest = new ArtifactResolveType(id, issueInstant);
    super.parseBaseAttributes(startElement, authnRequest);
    return authnRequest;
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) ArtifactResolveType(org.keycloak.dom.saml.v2.protocol.ArtifactResolveType)

Example 8 with ArtifactResolveType

use of org.keycloak.dom.saml.v2.protocol.ArtifactResolveType in project keycloak by keycloak.

the class HandleArtifactStepBuilder method perform.

/**
 * Main method. Can read a response with an artifact (redirect or post) and return a POSTed SOAP message containing
 * the ArtifactResolve message. The behaviour changes depending on what builder methods were called.
 *
 * @param client The current http client
 * @param currentURI the current uri
 * @param currentResponse the current response from the IdP
 * @param context the current http context
 * @return a POSTed SOAP message containing the ArtifactResolve message
 * @throws Exception
 */
@Override
public HttpUriRequest perform(CloseableHttpClient client, URI currentURI, CloseableHttpResponse currentResponse, HttpClientContext context) throws Exception {
    if (replayPost && replayPostMessage != null) {
        return replayPostMessage;
    }
    ArtifactResolveType artifactResolve = new ArtifactResolveType(id, XMLTimeUtil.getIssueInstant());
    NameIDType nameIDType = new NameIDType();
    nameIDType.setValue(issuer);
    artifactResolve.setIssuer(nameIDType);
    String artifact = getArtifactFromResponse(currentResponse);
    if (storeArtifact != null)
        storeArtifact.set(artifact);
    artifactResolve.setArtifact(artifact);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    XMLStreamWriter xmlStreamWriter = StaxUtil.getXMLStreamWriter(bos);
    new SAMLRequestWriter(xmlStreamWriter).write(artifactResolve);
    Document doc = DocumentUtil.getDocument(new ByteArrayInputStream(bos.toByteArray()));
    BaseSAML2BindingBuilder binding = new BaseSAML2BindingBuilder();
    if (signingPrivateKeyPem != null && signingPublicKeyPem != null) {
        PrivateKey privateKey = org.keycloak.testsuite.util.KeyUtils.privateKeyFromString(signingPrivateKeyPem);
        PublicKey publicKey = org.keycloak.testsuite.util.KeyUtils.publicKeyFromString(signingPublicKeyPem);
        binding.signatureAlgorithm(SignatureAlgorithm.RSA_SHA256).signWith(KeyUtils.createKeyId(privateKey), privateKey, publicKey).signDocument(doc);
    }
    String documentAsString = DocumentUtil.getDocumentAsString(doc);
    String transformed = getTransformer().transform(documentAsString);
    if (transformed == null)
        return null;
    if (beforeStepChecker != null && beforeStepChecker instanceof SessionStateChecker) {
        SessionStateChecker sessionStateChecker = (SessionStateChecker) beforeStepChecker;
        sessionStateChecker.setUserSessionProvider(session -> session.getProvider(SamlArtifactSessionMappingStoreProvider.class).get(artifact).getUserSessionId());
        sessionStateChecker.setClientSessionProvider(session -> session.getProvider(SamlArtifactSessionMappingStoreProvider.class).get(artifact).getClientSessionId());
    }
    HttpPost post = Soap.createMessage().addToBody(DocumentUtil.getDocument(transformed)).buildHttpPost(authServerSamlUrl);
    replayPostMessage = post;
    return post;
}
Also used : ArtifactResolveType(org.keycloak.dom.saml.v2.protocol.ArtifactResolveType) HttpPost(org.apache.http.client.methods.HttpPost) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) BaseSAML2BindingBuilder(org.keycloak.saml.BaseSAML2BindingBuilder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) SamlArtifactSessionMappingStoreProvider(org.keycloak.models.SamlArtifactSessionMappingStoreProvider) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) SAMLRequestWriter(org.keycloak.saml.processing.core.saml.v2.writers.SAMLRequestWriter) NameIDType(org.keycloak.dom.saml.v2.assertion.NameIDType)

Example 9 with ArtifactResolveType

use of org.keycloak.dom.saml.v2.protocol.ArtifactResolveType in project keycloak by keycloak.

the class ArtifactResolutionService method invoke.

/**
 * This is the method called when a message is received by the endpoint.
 * It gets the message, extracts the ArtifactResolve message from the SOAP, creates a SOAP message containing
 * an ArtifactResponse message with the configured SAML message, and returns it.
 * @param msg The SOAP message received by the endpoint, in Source format
 * @return A StreamSource containing the ArtifactResponse
 */
@Override
public Source invoke(Source msg) {
    byte[] response;
    try (StringWriter w = new StringWriter()) {
        Transformer trans = TransformerFactory.newInstance().newTransformer();
        trans.transform(msg, new StreamResult(w));
        String s = w.toString();
        Document doc = Soap.extractSoapMessage(new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8)));
        SAMLDocumentHolder samlDoc = SAML2Request.getSAML2ObjectFromDocument(doc);
        if (samlDoc.getSamlObject() instanceof ArtifactResolveType) {
            lastArtifactResolve = (ArtifactResolveType) samlDoc.getSamlObject();
        } else {
            lastArtifactResolve = null;
        }
        Document artifactResponse = SamlProtocolUtils.convert(artifactResponseType);
        response = Soap.createMessage().addToBody(artifactResponse).getBytes();
    } catch (ProcessingException | ConfigurationException | TransformerException | ParsingException | IOException e) {
        throw new RuntimeException(e);
    }
    return new StreamSource(new ByteArrayInputStream(response));
}
Also used : ArtifactResolveType(org.keycloak.dom.saml.v2.protocol.ArtifactResolveType) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAMLDocumentHolder(org.keycloak.saml.processing.core.saml.v2.common.SAMLDocumentHolder) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) ConfigurationException(org.keycloak.saml.common.exceptions.ConfigurationException) ParsingException(org.keycloak.saml.common.exceptions.ParsingException) TransformerException(javax.xml.transform.TransformerException) ProcessingException(org.keycloak.saml.common.exceptions.ProcessingException)

Aggregations

ArtifactResolveType (org.keycloak.dom.saml.v2.protocol.ArtifactResolveType)6 ProcessingException (org.keycloak.saml.common.exceptions.ProcessingException)5 Document (org.w3c.dom.Document)5 ConfigurationException (org.keycloak.saml.common.exceptions.ConfigurationException)4 ParsingException (org.keycloak.saml.common.exceptions.ParsingException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)3 NameIDType (org.keycloak.dom.saml.v2.assertion.NameIDType)3 ArtifactResponseType (org.keycloak.dom.saml.v2.protocol.ArtifactResponseType)3 SAMLRequestWriter (org.keycloak.saml.processing.core.saml.v2.writers.SAMLRequestWriter)3 IOException (java.io.IOException)2 VerificationException (org.keycloak.common.VerificationException)2 StatusResponseType (org.keycloak.dom.saml.v2.protocol.StatusResponseType)2 SAMLDocumentHolder (org.keycloak.saml.processing.core.saml.v2.common.SAMLDocumentHolder)2 StringWriter (java.io.StringWriter)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 PrivateKey (java.security.PrivateKey)1