Search in sources :

Example 16 with ProcessingException

use of org.keycloak.saml.common.exceptions.ProcessingException in project keycloak by keycloak.

the class RSAKeyValueType method convertToPrivateKey.

/**
 * Convert to the JDK representation of a RSA Private Key
 *
 * @return
 *
 * @throws ProcessingException
 */
public RSAPrivateKey convertToPrivateKey() throws ProcessingException {
    try {
        BigInteger bigModulus = new BigInteger(1, massage(Base64.decode(new String(modulus))));
        BigInteger bigEx = new BigInteger(1, massage(Base64.decode(new String(exponent))));
        KeyFactory rsaKeyFactory = KeyFactory.getInstance("rsa");
        RSAPrivateKeySpec kspec = new RSAPrivateKeySpec(bigModulus, bigEx);
        return (RSAPrivateKey) rsaKeyFactory.generatePrivate(kspec);
    } catch (Exception e) {
        throw new ProcessingException(e);
    }
}
Also used : RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) BigInteger(java.math.BigInteger) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyFactory(java.security.KeyFactory) ProcessingException(org.keycloak.saml.common.exceptions.ProcessingException) ProcessingException(org.keycloak.saml.common.exceptions.ProcessingException)

Example 17 with ProcessingException

use of org.keycloak.saml.common.exceptions.ProcessingException in project keycloak by keycloak.

the class SAMLMetadataWriter method write.

public void write(IDPSSODescriptorType idpSSODescriptor) throws ProcessingException {
    if (idpSSODescriptor == null)
        throw new ProcessingException(logger.nullArgumentError("IDPSSODescriptorType"));
    StaxUtil.writeStartElement(writer, METADATA_PREFIX, JBossSAMLConstants.IDP_SSO_DESCRIPTOR.get(), JBossSAMLURIConstants.METADATA_NSURI.get());
    Boolean wantsAuthnRequestsSigned = idpSSODescriptor.isWantAuthnRequestsSigned();
    if (wantsAuthnRequestsSigned != null) {
        StaxUtil.writeAttribute(writer, new QName(JBossSAMLConstants.WANT_AUTHN_REQUESTS_SIGNED.get()), wantsAuthnRequestsSigned.toString());
    }
    writeProtocolSupportEnumeration(idpSSODescriptor.getProtocolSupportEnumeration());
    // Get the key descriptors
    List<KeyDescriptorType> keyDescriptors = idpSSODescriptor.getKeyDescriptor();
    for (KeyDescriptorType keyDescriptor : keyDescriptors) {
        writeKeyDescriptor(keyDescriptor);
    }
    List<IndexedEndpointType> artifactResolutionServices = idpSSODescriptor.getArtifactResolutionService();
    for (IndexedEndpointType indexedEndpoint : artifactResolutionServices) {
        writeArtifactResolutionService(indexedEndpoint);
    }
    List<EndpointType> sloServices = idpSSODescriptor.getSingleLogoutService();
    for (EndpointType endpoint : sloServices) {
        writeSingleLogoutService(endpoint);
    }
    List<String> nameIDFormats = idpSSODescriptor.getNameIDFormat();
    for (String nameIDFormat : nameIDFormats) {
        writeNameIDFormat(nameIDFormat);
    }
    List<EndpointType> ssoServices = idpSSODescriptor.getSingleSignOnService();
    for (EndpointType endpoint : ssoServices) {
        writeSingleSignOnService(endpoint);
    }
    List<AttributeType> attributes = idpSSODescriptor.getAttribute();
    for (AttributeType attribType : attributes) {
        write(attribType);
    }
    StaxUtil.writeEndElement(writer);
    StaxUtil.flush(writer);
}
Also used : QName(javax.xml.namespace.QName) IndexedEndpointType(org.keycloak.dom.saml.v2.metadata.IndexedEndpointType) AttributeType(org.keycloak.dom.saml.v2.assertion.AttributeType) RequestedAttributeType(org.keycloak.dom.saml.v2.metadata.RequestedAttributeType) EndpointType(org.keycloak.dom.saml.v2.metadata.EndpointType) IndexedEndpointType(org.keycloak.dom.saml.v2.metadata.IndexedEndpointType) KeyDescriptorType(org.keycloak.dom.saml.v2.metadata.KeyDescriptorType) ProcessingException(org.keycloak.saml.common.exceptions.ProcessingException)

Example 18 with ProcessingException

use of org.keycloak.saml.common.exceptions.ProcessingException in project keycloak by keycloak.

the class SAMLMetadataWriter method writeOrganization.

public void writeOrganization(OrganizationType org) throws ProcessingException {
    if (org == null)
        throw new ProcessingException(logger.nullArgumentError("Organization"));
    StaxUtil.writeStartElement(writer, METADATA_PREFIX, JBossSAMLConstants.ORGANIZATION.get(), JBossSAMLURIConstants.METADATA_NSURI.get());
    ExtensionsType extensions = org.getExtensions();
    if (extensions != null) {
        write(extensions);
    }
    // Write the name
    List<LocalizedNameType> nameList = org.getOrganizationName();
    for (LocalizedNameType localName : nameList) {
        StaxUtil.writeStartElement(writer, METADATA_PREFIX, JBossSAMLConstants.ORGANIZATION_NAME.get(), JBossSAMLURIConstants.METADATA_NSURI.get());
        writeLocalizedType(localName);
    }
    // Write the display name
    List<LocalizedNameType> displayNameList = org.getOrganizationDisplayName();
    for (LocalizedNameType localName : displayNameList) {
        StaxUtil.writeStartElement(writer, METADATA_PREFIX, JBossSAMLConstants.ORGANIZATION_DISPLAY_NAME.get(), JBossSAMLURIConstants.METADATA_NSURI.get());
        writeLocalizedType(localName);
    }
    // Write the url
    List<LocalizedURIType> uriList = org.getOrganizationURL();
    for (LocalizedURIType uri : uriList) {
        StaxUtil.writeStartElement(writer, METADATA_PREFIX, JBossSAMLConstants.ORGANIZATION_URL.get(), JBossSAMLURIConstants.METADATA_NSURI.get());
        String lang = uri.getLang();
        String val = uri.getValue().toString();
        StaxUtil.writeAttribute(writer, new QName(JBossSAMLURIConstants.XML.get(), JBossSAMLConstants.LANG.get(), "xml"), lang);
        StaxUtil.writeCharacters(writer, val);
        StaxUtil.writeEndElement(writer);
    }
    StaxUtil.writeEndElement(writer);
    StaxUtil.flush(writer);
}
Also used : LocalizedURIType(org.keycloak.dom.saml.v2.metadata.LocalizedURIType) LocalizedNameType(org.keycloak.dom.saml.v2.metadata.LocalizedNameType) QName(javax.xml.namespace.QName) ExtensionsType(org.keycloak.dom.saml.v2.metadata.ExtensionsType) ProcessingException(org.keycloak.saml.common.exceptions.ProcessingException)

Example 19 with ProcessingException

use of org.keycloak.saml.common.exceptions.ProcessingException 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 20 with ProcessingException

use of org.keycloak.saml.common.exceptions.ProcessingException 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

ProcessingException (org.keycloak.saml.common.exceptions.ProcessingException)40 ConfigurationException (org.keycloak.saml.common.exceptions.ConfigurationException)25 Document (org.w3c.dom.Document)16 ParsingException (org.keycloak.saml.common.exceptions.ParsingException)15 Element (org.w3c.dom.Element)12 IOException (java.io.IOException)8 AuthnRequestType (org.keycloak.dom.saml.v2.protocol.AuthnRequestType)8 ResponseType (org.keycloak.dom.saml.v2.protocol.ResponseType)8 QName (javax.xml.namespace.QName)7 SAMLDocumentHolder (org.keycloak.saml.processing.core.saml.v2.common.SAMLDocumentHolder)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 StatusResponseType (org.keycloak.dom.saml.v2.protocol.StatusResponseType)4 SAML2Request (org.keycloak.saml.processing.api.saml.v2.request.SAML2Request)4 BigInteger (java.math.BigInteger)3 KeyFactory (java.security.KeyFactory)3 Response (javax.ws.rs.core.Response)3 EncryptedKey (org.apache.xml.security.encryption.EncryptedKey)3 XMLCipher (org.apache.xml.security.encryption.XMLCipher)3 XMLEncryptionException (org.apache.xml.security.encryption.XMLEncryptionException)3 AttributeType (org.keycloak.dom.saml.v2.assertion.AttributeType)3