Search in sources :

Example 1 with SessionIdMapper

use of org.keycloak.adapters.spi.SessionIdMapper in project keycloak by keycloak.

the class KeycloakOIDCFilter method init.

@Override
public void init(final FilterConfig filterConfig) throws ServletException {
    String skipPatternDefinition = filterConfig.getInitParameter(SKIP_PATTERN_PARAM);
    if (skipPatternDefinition != null) {
        skipPattern = Pattern.compile(skipPatternDefinition, Pattern.DOTALL);
    }
    String idMapperClassName = filterConfig.getInitParameter(ID_MAPPER_PARAM);
    if (idMapperClassName != null) {
        try {
            final Class<?> idMapperClass = getClass().getClassLoader().loadClass(idMapperClassName);
            final Constructor<?> idMapperConstructor = idMapperClass.getDeclaredConstructor();
            Object idMapperInstance = null;
            // for KEYCLOAK-13745 test
            if (idMapperConstructor.getModifiers() == Modifier.PRIVATE) {
                idMapperInstance = idMapperClass.getMethod("getInstance").invoke(null);
            } else {
                idMapperInstance = idMapperConstructor.newInstance();
            }
            if (idMapperInstance instanceof SessionIdMapper) {
                this.idMapper = (SessionIdMapper) idMapperInstance;
            } else {
                log.log(Level.WARNING, "SessionIdMapper class {0} is not instance of org.keycloak.adapters.spi.SessionIdMapper", idMapperClassName);
            }
        } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
            log.log(Level.WARNING, "SessionIdMapper class could not be instanced", e);
        }
    }
    if (definedconfigResolver != null) {
        deploymentContext = new AdapterDeploymentContext(definedconfigResolver);
        log.log(Level.INFO, "Using {0} to resolve Keycloak configuration on a per-request basis.", definedconfigResolver.getClass());
    } else {
        String configResolverClass = filterConfig.getInitParameter(CONFIG_RESOLVER_PARAM);
        if (configResolverClass != null) {
            try {
                KeycloakConfigResolver configResolver = (KeycloakConfigResolver) getClass().getClassLoader().loadClass(configResolverClass).newInstance();
                deploymentContext = new AdapterDeploymentContext(configResolver);
                log.log(Level.INFO, "Using {0} to resolve Keycloak configuration on a per-request basis.", configResolverClass);
            } catch (Exception ex) {
                log.log(Level.FINE, "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 AdapterDeploymentContext(new KeycloakDeployment());
            }
        } else {
            String fp = filterConfig.getInitParameter(CONFIG_FILE_PARAM);
            InputStream is = null;
            if (fp != null) {
                try {
                    is = new FileInputStream(fp);
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
                }
            } else {
                String path = "/WEB-INF/keycloak.json";
                String pathParam = filterConfig.getInitParameter(CONFIG_PATH_PARAM);
                if (pathParam != null)
                    path = pathParam;
                is = filterConfig.getServletContext().getResourceAsStream(path);
            }
            KeycloakDeployment kd = createKeycloakDeploymentFrom(is);
            deploymentContext = new AdapterDeploymentContext(kd);
            log.fine("Keycloak is using a per-deployment configuration.");
        }
    }
    filterConfig.getServletContext().setAttribute(AdapterDeploymentContext.class.getName(), deploymentContext);
    nodesRegistrationManagement = new NodesRegistrationManagement();
}
Also used : NodesRegistrationManagement(org.keycloak.adapters.NodesRegistrationManagement) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) AdapterDeploymentContext(org.keycloak.adapters.AdapterDeploymentContext) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) FileInputStream(java.io.FileInputStream) KeycloakConfigResolver(org.keycloak.adapters.KeycloakConfigResolver) KeycloakDeployment(org.keycloak.adapters.KeycloakDeployment) InMemorySessionIdMapper(org.keycloak.adapters.spi.InMemorySessionIdMapper) SessionIdMapper(org.keycloak.adapters.spi.SessionIdMapper)

Example 2 with SessionIdMapper

use of org.keycloak.adapters.spi.SessionIdMapper in project keycloak by keycloak.

the class KeycloakHttpServerAuthenticationMechanism method getSessionIdMapper.

private SessionIdMapper getSessionIdMapper(HttpServerRequest request) {
    HttpScope scope = request.getScope(Scope.APPLICATION);
    SessionIdMapper res = scope == null ? null : (SessionIdMapper) scope.getAttachment(KeycloakConfigurationServletListener.ADAPTER_SESSION_ID_MAPPER_ATTRIBUTE_ELYTRON);
    return res == null ? this.idMapper : res;
}
Also used : HttpScope(org.wildfly.security.http.HttpScope) SessionIdMapper(org.keycloak.adapters.spi.SessionIdMapper)

Example 3 with SessionIdMapper

use of org.keycloak.adapters.spi.SessionIdMapper 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)

Aggregations

SessionIdMapper (org.keycloak.adapters.spi.SessionIdMapper)3 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ServletException (javax.servlet.ServletException)2 InMemorySessionIdMapper (org.keycloak.adapters.spi.InMemorySessionIdMapper)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 AdapterDeploymentContext (org.keycloak.adapters.AdapterDeploymentContext)1 KeycloakConfigResolver (org.keycloak.adapters.KeycloakConfigResolver)1 KeycloakDeployment (org.keycloak.adapters.KeycloakDeployment)1 NodesRegistrationManagement (org.keycloak.adapters.NodesRegistrationManagement)1 DefaultSamlDeployment (org.keycloak.adapters.saml.DefaultSamlDeployment)1 SamlConfigResolver (org.keycloak.adapters.saml.SamlConfigResolver)1 SamlDeployment (org.keycloak.adapters.saml.SamlDeployment)1 SamlDeploymentContext (org.keycloak.adapters.saml.SamlDeploymentContext)1 DeploymentBuilder (org.keycloak.adapters.saml.config.parsers.DeploymentBuilder)1 ResourceLoader (org.keycloak.adapters.saml.config.parsers.ResourceLoader)1 ParsingException (org.keycloak.saml.common.exceptions.ParsingException)1 HttpScope (org.wildfly.security.http.HttpScope)1