Search in sources :

Example 16 with ParsingException

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

the class BasicSamlTest method testPropertyValueInAssertion.

// KEYCLOAK-4160
@Test
public void testPropertyValueInAssertion() throws ParsingException, ConfigurationException, ProcessingException {
    SAMLDocumentHolder document = new SamlClientBuilder().authnRequest(getAuthServerSamlEndpoint(REALM_NAME), SAML_CLIENT_ID_SALES_POST, SAML_ASSERTION_CONSUMER_URL_SALES_POST, Binding.POST).transformDocument(doc -> {
        setDocElementAttributeValue(doc, "samlp:AuthnRequest", "ID", "${java.version}");
        return doc;
    }).build().login().user(bburkeUser).build().getSamlResponse(Binding.POST);
    assertThat(documentToString(document.getSamlDocument()), not(containsString("InResponseTo=\"" + System.getProperty("java.version") + "\"")));
}
Also used : Matchers.statusCodeIsHC(org.keycloak.testsuite.util.Matchers.statusCodeIsHC) EntityUtils(org.apache.http.util.EntityUtils) Document(org.w3c.dom.Document) Matchers.nullValue(org.hamcrest.Matchers.nullValue) SamlClient(org.keycloak.testsuite.util.SamlClient) RedirectStrategyWithSwitchableFollowRedirect(org.keycloak.testsuite.util.SamlClient.RedirectStrategyWithSwitchableFollowRedirect) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) SAMLDocumentHolder(org.keycloak.saml.processing.core.saml.v2.common.SAMLDocumentHolder) NAMEID_FORMAT_TRANSIENT(org.keycloak.saml.common.constants.JBossSAMLURIConstants.NAMEID_FORMAT_TRANSIENT) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Matchers(org.keycloak.testsuite.util.Matchers) Signature(java.security.Signature) SamlProtocol(org.keycloak.protocol.saml.SamlProtocol) Logger(java.util.logging.Logger) DocumentUtil(org.keycloak.saml.common.util.DocumentUtil) Objects(java.util.Objects) ParsingException(org.keycloak.saml.common.exceptions.ParsingException) List(java.util.List) Response(javax.ws.rs.core.Response) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) SamlClientBuilder(org.keycloak.testsuite.util.SamlClientBuilder) PROTOCOL_NSURI(org.keycloak.saml.common.constants.JBossSAMLURIConstants.PROTOCOL_NSURI) RealmsResource(org.keycloak.services.resources.RealmsResource) Encode(org.jboss.resteasy.util.Encode) AuthServerContainerExclude(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude) SAML2Request(org.keycloak.saml.processing.api.saml.v2.request.SAML2Request) GeneralConstants(org.keycloak.saml.common.constants.GeneralConstants) CoreMatchers.not(org.hamcrest.CoreMatchers.not) IOUtil.setDocElementAttributeValue(org.keycloak.testsuite.utils.io.IOUtil.setDocElementAttributeValue) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) Attr(org.w3c.dom.Attr) Level(java.util.logging.Level) ProcessingException(org.keycloak.saml.common.exceptions.ProcessingException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) SignatureAlgorithm(org.keycloak.saml.SignatureAlgorithm) AuthServer(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude.AuthServer) Status(javax.ws.rs.core.Response.Status) ConfigurationException(org.keycloak.saml.common.exceptions.ConfigurationException) RedirectBindingUtil(org.keycloak.saml.processing.web.util.RedirectBindingUtil) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) AuthnRequestType(org.keycloak.dom.saml.v2.protocol.AuthnRequestType) JBossSAMLURIConstants(org.keycloak.saml.common.constants.JBossSAMLURIConstants) Test(org.junit.Test) IOException(java.io.IOException) KeyUtils(org.keycloak.testsuite.util.KeyUtils) AUTH_SERVER_PORT(org.keycloak.testsuite.util.ServerURLs.AUTH_SERVER_PORT) Element(org.w3c.dom.Element) Step(org.keycloak.testsuite.util.SamlClient.Step) Matcher(org.hamcrest.Matcher) Binding(org.keycloak.testsuite.util.SamlClient.Binding) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) IOUtil.documentToString(org.keycloak.testsuite.utils.io.IOUtil.documentToString) SAMLDocumentHolder(org.keycloak.saml.processing.core.saml.v2.common.SAMLDocumentHolder) SamlClientBuilder(org.keycloak.testsuite.util.SamlClientBuilder) Test(org.junit.Test)

Example 17 with ParsingException

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

the class HttpAdapterUtils method downloadKeysFromSamlDescriptor.

public static MultivaluedHashMap<String, KeyInfo> downloadKeysFromSamlDescriptor(HttpClient client, String descriptorUrl) throws HttpClientAdapterException {
    try {
        HttpGet httpRequest = new HttpGet(descriptorUrl);
        HttpResponse response = client.execute(httpRequest);
        int status = response.getStatusLine().getStatusCode();
        if (status != HttpStatus.SC_OK) {
            EntityUtils.consumeQuietly(response.getEntity());
            throw new HttpClientAdapterException("Unexpected status = " + status);
        }
        HttpEntity entity = response.getEntity();
        if (entity == null) {
            throw new HttpClientAdapterException("There was no entity.");
        }
        MultivaluedHashMap<String, KeyInfo> res;
        try (InputStream is = entity.getContent()) {
            res = extractKeysFromSamlDescriptor(is);
        }
        EntityUtils.consumeQuietly(entity);
        return res;
    } catch (IOException | ParsingException e) {
        throw new HttpClientAdapterException("IO error", e);
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) KeyInfo(javax.xml.crypto.dsig.keyinfo.KeyInfo) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) ParsingException(org.keycloak.saml.common.exceptions.ParsingException) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException)

Example 18 with ParsingException

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

the class KeyStoreParser method instantiateElement.

@Override
protected KeyStoreConfig instantiateElement(XMLEventReader xmlEventReader, StartElement element) throws ParsingException {
    final KeyStoreConfig keyStore = new Key.KeyStoreConfig();
    keyStore.setType(StaxParserUtil.getAttributeValueRP(element, KeycloakSamlAdapterV1QNames.ATTR_TYPE));
    keyStore.setAlias(StaxParserUtil.getAttributeValueRP(element, KeycloakSamlAdapterV1QNames.ATTR_ALIAS));
    keyStore.setFile(StaxParserUtil.getAttributeValueRP(element, KeycloakSamlAdapterV1QNames.ATTR_FILE));
    keyStore.setResource(StaxParserUtil.getAttributeValueRP(element, KeycloakSamlAdapterV1QNames.ATTR_RESOURCE));
    keyStore.setPassword(StaxParserUtil.getRequiredAttributeValueRP(element, KeycloakSamlAdapterV1QNames.ATTR_PASSWORD));
    if (keyStore.getFile() == null && keyStore.getResource() == null) {
        throw new ParsingException("KeyStore element must have the url or classpath attribute set");
    }
    return keyStore;
}
Also used : ParsingException(org.keycloak.saml.common.exceptions.ParsingException) KeyStoreConfig(org.keycloak.adapters.saml.config.Key.KeyStoreConfig)

Example 19 with ParsingException

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

the class SamlFilter method init.

@Override
public void init(final FilterConfig filterConfig) throws ServletException {
    deploymentContext = (SamlDeploymentContext) filterConfig.getServletContext().getAttribute(SamlDeploymentContext.class.getName());
    if (deploymentContext != null) {
        idMapper = (SessionIdMapper) filterConfig.getServletContext().getAttribute(SessionIdMapper.class.getName());
        return;
    }
    String configResolverClass = filterConfig.getInitParameter("keycloak.config.resolver");
    if (configResolverClass != null) {
        try {
            SamlConfigResolver configResolver = (SamlConfigResolver) getClass().getClassLoader().loadClass(configResolverClass).newInstance();
            deploymentContext = new SamlDeploymentContext(configResolver);
            log.log(Level.INFO, "Using {0} to resolve Keycloak configuration on a per-request basis.", configResolverClass);
        } catch (Exception ex) {
            log.log(Level.WARNING, "The specified resolver {0} could NOT be loaded. Keycloak is unconfigured and will deny all requests. Reason: {1}", new Object[] { configResolverClass, ex.getMessage() });
            deploymentContext = new SamlDeploymentContext(new DefaultSamlDeployment());
        }
    } else {
        String fp = filterConfig.getInitParameter("keycloak.config.file");
        InputStream is = null;
        if (fp != null) {
            try {
                is = new FileInputStream(fp);
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }
        } else {
            String path = "/WEB-INF/keycloak-saml.xml";
            String pathParam = filterConfig.getInitParameter("keycloak.config.path");
            if (pathParam != null)
                path = pathParam;
            is = filterConfig.getServletContext().getResourceAsStream(path);
        }
        final SamlDeployment deployment;
        if (is == null) {
            log.info("No adapter configuration. Keycloak is unconfigured and will deny all requests.");
            deployment = new DefaultSamlDeployment();
        } else {
            try {
                ResourceLoader loader = new ResourceLoader() {

                    @Override
                    public InputStream getResourceAsStream(String resource) {
                        return filterConfig.getServletContext().getResourceAsStream(resource);
                    }
                };
                deployment = new DeploymentBuilder().build(is, loader);
            } catch (ParsingException e) {
                throw new RuntimeException(e);
            }
        }
        deploymentContext = new SamlDeploymentContext(deployment);
        log.fine("Keycloak is using a per-deployment configuration.");
    }
    idMapper = new InMemorySessionIdMapper();
    filterConfig.getServletContext().setAttribute(SamlDeploymentContext.class.getName(), deploymentContext);
    filterConfig.getServletContext().setAttribute(SessionIdMapper.class.getName(), idMapper);
}
Also used : SamlDeploymentContext(org.keycloak.adapters.saml.SamlDeploymentContext) ResourceLoader(org.keycloak.adapters.saml.config.parsers.ResourceLoader) InMemorySessionIdMapper(org.keycloak.adapters.spi.InMemorySessionIdMapper) DefaultSamlDeployment(org.keycloak.adapters.saml.DefaultSamlDeployment) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) DefaultSamlDeployment(org.keycloak.adapters.saml.DefaultSamlDeployment) SamlDeployment(org.keycloak.adapters.saml.SamlDeployment) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParsingException(org.keycloak.saml.common.exceptions.ParsingException) FileInputStream(java.io.FileInputStream) ParsingException(org.keycloak.saml.common.exceptions.ParsingException) SamlConfigResolver(org.keycloak.adapters.saml.SamlConfigResolver) InMemorySessionIdMapper(org.keycloak.adapters.spi.InMemorySessionIdMapper) SessionIdMapper(org.keycloak.adapters.spi.SessionIdMapper) DeploymentBuilder(org.keycloak.adapters.saml.config.parsers.DeploymentBuilder)

Example 20 with ParsingException

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

the class SamlDescriptorIDPKeysExtractorTest method testParse.

public void testParse(String fileToParse) {
    InputStream stream = getClass().getResourceAsStream(fileToParse);
    SamlDescriptorIDPKeysExtractor extractor = new SamlDescriptorIDPKeysExtractor();
    try {
        MultivaluedHashMap keyMap = extractor.parse(stream);
        assertFalse(keyMap.isEmpty());
        assertTrue(keyMap.containsKey("signing"));
        assertTrue(keyMap.containsKey("encryption"));
    } catch (ParsingException e) {
        fail(e.getMessage());
    }
}
Also used : MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) InputStream(java.io.InputStream) ParsingException(org.keycloak.saml.common.exceptions.ParsingException)

Aggregations

ParsingException (org.keycloak.saml.common.exceptions.ParsingException)31 ConfigurationException (org.keycloak.saml.common.exceptions.ConfigurationException)14 ProcessingException (org.keycloak.saml.common.exceptions.ProcessingException)14 InputStream (java.io.InputStream)11 Document (org.w3c.dom.Document)10 IOException (java.io.IOException)9 ByteArrayInputStream (java.io.ByteArrayInputStream)7 DeploymentBuilder (org.keycloak.adapters.saml.config.parsers.DeploymentBuilder)7 ResourceLoader (org.keycloak.adapters.saml.config.parsers.ResourceLoader)7 FileNotFoundException (java.io.FileNotFoundException)6 SamlDeployment (org.keycloak.adapters.saml.SamlDeployment)6 FileInputStream (java.io.FileInputStream)5 AuthnRequestType (org.keycloak.dom.saml.v2.protocol.AuthnRequestType)5 Test (org.junit.Test)4 DefaultSamlDeployment (org.keycloak.adapters.saml.DefaultSamlDeployment)4 SamlDeploymentContext (org.keycloak.adapters.saml.SamlDeploymentContext)4 SAML2Request (org.keycloak.saml.processing.api.saml.v2.request.SAML2Request)4 Element (org.w3c.dom.Element)4 HashMap (java.util.HashMap)3 ServletException (javax.servlet.ServletException)3