Search in sources :

Example 21 with ResponseType

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

the class SAMLResponseWriter method write.

/**
 * Write a {@code ResponseType} to stream
 *
 * @param response
 * @param out
 *
 * @throws org.keycloak.saml.common.exceptions.ProcessingException
 */
public void write(ResponseType response) throws ProcessingException {
    StaxUtil.writeStartElement(writer, PROTOCOL_PREFIX, JBossSAMLConstants.RESPONSE__PROTOCOL.get(), JBossSAMLURIConstants.PROTOCOL_NSURI.get());
    StaxUtil.writeNameSpace(writer, PROTOCOL_PREFIX, JBossSAMLURIConstants.PROTOCOL_NSURI.get());
    StaxUtil.writeNameSpace(writer, ASSERTION_PREFIX, JBossSAMLURIConstants.ASSERTION_NSURI.get());
    writeBaseAttributes(response);
    NameIDType issuer = response.getIssuer();
    if (issuer != null) {
        write(issuer, new QName(JBossSAMLURIConstants.ASSERTION_NSURI.get(), JBossSAMLConstants.ISSUER.get(), ASSERTION_PREFIX));
    }
    ExtensionsType extensions = response.getExtensions();
    if (extensions != null && extensions.getAny() != null && !extensions.getAny().isEmpty()) {
        write(extensions);
    }
    StatusType status = response.getStatus();
    write(status);
    List<ResponseType.RTChoiceType> choiceTypes = response.getAssertions();
    if (choiceTypes != null) {
        for (ResponseType.RTChoiceType choiceType : choiceTypes) {
            AssertionType assertion = choiceType.getAssertion();
            if (assertion != null) {
                assertionWriter.write(assertion);
            }
            EncryptedAssertionType encryptedAssertion = choiceType.getEncryptedAssertion();
            if (encryptedAssertion != null) {
                Element encElement = encryptedAssertion.getEncryptedElement();
                StaxUtil.writeDOMElement(writer, encElement);
            }
        }
    }
    StaxUtil.writeEndElement(writer);
    StaxUtil.flush(writer);
}
Also used : QName(javax.xml.namespace.QName) StatusType(org.keycloak.dom.saml.v2.protocol.StatusType) ExtensionsType(org.keycloak.dom.saml.v2.protocol.ExtensionsType) Element(org.w3c.dom.Element) EncryptedAssertionType(org.keycloak.dom.saml.v2.assertion.EncryptedAssertionType) AssertionType(org.keycloak.dom.saml.v2.assertion.AssertionType) EncryptedAssertionType(org.keycloak.dom.saml.v2.assertion.EncryptedAssertionType) NameIDType(org.keycloak.dom.saml.v2.assertion.NameIDType) ArtifactResponseType(org.keycloak.dom.saml.v2.protocol.ArtifactResponseType) ResponseType(org.keycloak.dom.saml.v2.protocol.ResponseType) StatusResponseType(org.keycloak.dom.saml.v2.protocol.StatusResponseType)

Example 22 with ResponseType

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

the class JBossSAMLAuthnResponseFactory method createResponseType.

/**
 * Create a Response Type
 *
 * @param ID
 * @param issuerInfo
 * @param encryptedAssertion a DOM {@link Element} that represents an encrypted assertion
 *
 * @return
 *
 * @throws ConfigurationException
 */
public static ResponseType createResponseType(String ID, IssuerInfoHolder issuerInfo, Element encryptedAssertion) {
    ResponseType responseType = new ResponseType(ID, XMLTimeUtil.getIssueInstant());
    // Issuer
    NameIDType issuer = issuerInfo.getIssuer();
    responseType.setIssuer(issuer);
    // Status
    String statusCode = issuerInfo.getStatusCode();
    if (statusCode == null)
        throw logger.issuerInfoMissingStatusCodeError();
    responseType.setStatus(createStatusType(statusCode));
    responseType.addAssertion(new RTChoiceType(new EncryptedAssertionType(encryptedAssertion)));
    return responseType;
}
Also used : RTChoiceType(org.keycloak.dom.saml.v2.protocol.ResponseType.RTChoiceType) EncryptedAssertionType(org.keycloak.dom.saml.v2.assertion.EncryptedAssertionType) NameIDType(org.keycloak.dom.saml.v2.assertion.NameIDType) ResponseType(org.keycloak.dom.saml.v2.protocol.ResponseType)

Example 23 with ResponseType

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

the class JBossSAMLAuthnResponseFactory method createResponseType.

/**
 * Create a Response Type
 *
 * @param ID
 * @param issuerInfo
 * @param assertionType
 *
 * @return
 *
 * @throws ConfigurationException
 */
public static ResponseType createResponseType(String ID, IssuerInfoHolder issuerInfo, AssertionType assertionType) {
    XMLGregorianCalendar issueInstant = XMLTimeUtil.getIssueInstant();
    ResponseType responseType = new ResponseType(ID, issueInstant);
    // Issuer
    NameIDType issuer = issuerInfo.getIssuer();
    responseType.setIssuer(issuer);
    // Status
    String statusCode = issuerInfo.getStatusCode();
    if (statusCode == null)
        throw logger.issuerInfoMissingStatusCodeError();
    responseType.setStatus(createStatusType(statusCode));
    responseType.addAssertion(new RTChoiceType(assertionType));
    return responseType;
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) RTChoiceType(org.keycloak.dom.saml.v2.protocol.ResponseType.RTChoiceType) NameIDType(org.keycloak.dom.saml.v2.assertion.NameIDType) ResponseType(org.keycloak.dom.saml.v2.protocol.ResponseType)

Example 24 with ResponseType

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

the class AssertionUtil method getAssertion.

public static AssertionType getAssertion(SAMLDocumentHolder holder, ResponseType responseType, PrivateKey privateKey) throws ParsingException, ProcessingException, ConfigurationException {
    List<ResponseType.RTChoiceType> assertions = responseType.getAssertions();
    if (assertions.isEmpty()) {
        throw new ProcessingException("No assertion from response.");
    }
    ResponseType.RTChoiceType rtChoiceType = assertions.get(0);
    EncryptedAssertionType encryptedAssertion = rtChoiceType.getEncryptedAssertion();
    if (encryptedAssertion != null) {
        if (privateKey == null) {
            throw new ProcessingException("Encryptd assertion and decrypt private key is null");
        }
        decryptAssertion(holder, responseType, privateKey);
    }
    return responseType.getAssertions().get(0).getAssertion();
}
Also used : EncryptedAssertionType(org.keycloak.dom.saml.v2.assertion.EncryptedAssertionType) ProcessingException(org.keycloak.saml.common.exceptions.ProcessingException) ResponseType(org.keycloak.dom.saml.v2.protocol.ResponseType)

Example 25 with ResponseType

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

the class AssertionUtil method decryptId.

public static void decryptId(final ResponseType responseType, final PrivateKey privateKey) throws ConfigurationException, ProcessingException, ParsingException {
    final STSubType subTypeElement = getSubTypeElement(responseType);
    if (subTypeElement == null) {
        return;
    }
    final EncryptedElementType encryptedID = subTypeElement.getEncryptedID();
    if (encryptedID == null) {
        return;
    }
    Element encryptedElement = encryptedID.getEncryptedElement();
    Document newDoc = DocumentUtil.createDocument();
    Node importedNode = newDoc.importNode(encryptedElement, true);
    newDoc.appendChild(importedNode);
    Element decryptedNameIdElement = XMLEncryptionUtil.decryptElementInDocument(newDoc, privateKey);
    final XMLEventReader xmlEventReader = StaxParserUtil.getXMLEventReader(DocumentUtil.getNodeAsStream(decryptedNameIdElement));
    NameIDType nameIDType = SAMLParserUtil.parseNameIDType(xmlEventReader);
    // Add unencrypted id, remove encrypted
    subTypeElement.addBaseID(nameIDType);
    subTypeElement.setEncryptedID(null);
}
Also used : STSubType(org.keycloak.dom.saml.v2.assertion.SubjectType.STSubType) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) XMLEventReader(javax.xml.stream.XMLEventReader) EncryptedElementType(org.keycloak.dom.saml.v2.assertion.EncryptedElementType) Document(org.w3c.dom.Document) NameIDType(org.keycloak.dom.saml.v2.assertion.NameIDType)

Aggregations

ResponseType (org.keycloak.dom.saml.v2.protocol.ResponseType)75 Test (org.junit.Test)50 SamlClientBuilder (org.keycloak.testsuite.util.SamlClientBuilder)38 SAMLDocumentHolder (org.keycloak.saml.processing.core.saml.v2.common.SAMLDocumentHolder)34 StatusResponseType (org.keycloak.dom.saml.v2.protocol.StatusResponseType)33 AssertionType (org.keycloak.dom.saml.v2.assertion.AssertionType)26 NameIDType (org.keycloak.dom.saml.v2.assertion.NameIDType)25 JBossSAMLURIConstants (org.keycloak.saml.common.constants.JBossSAMLURIConstants)16 Document (org.w3c.dom.Document)15 URI (java.net.URI)13 List (java.util.List)12 Matchers.containsString (org.hamcrest.Matchers.containsString)12 Assert.assertThat (org.junit.Assert.assertThat)12 AttributeStatementType (org.keycloak.dom.saml.v2.assertion.AttributeStatementType)12 AttributeType (org.keycloak.dom.saml.v2.assertion.AttributeType)12 StatementAbstractType (org.keycloak.dom.saml.v2.assertion.StatementAbstractType)12 ProcessingException (org.keycloak.saml.common.exceptions.ProcessingException)12 IOException (java.io.IOException)11 Response (javax.ws.rs.core.Response)11 Matchers (org.keycloak.testsuite.util.Matchers)11