Search in sources :

Example 1 with KeycloakDeployment

use of org.keycloak.adapters.KeycloakDeployment in project keycloak by keycloak.

the class AbstractKeycloakLoginModule method resolveDeployment.

protected KeycloakDeployment resolveDeployment(String keycloakConfigFile) {
    try {
        InputStream is = null;
        if (keycloakConfigFile.startsWith(PROFILE_RESOURCE)) {
            try {
                is = new URL(keycloakConfigFile).openStream();
            } catch (MalformedURLException mfue) {
                throw new RuntimeException(mfue);
            } catch (IOException ioe) {
                throw new RuntimeException(ioe);
            }
        } else {
            is = FindFile.findFile(keycloakConfigFile);
        }
        KeycloakDeployment kd = KeycloakDeploymentBuilder.build(is);
        return kd;
    } catch (RuntimeException e) {
        getLogger().debug("Unable to find or parse file " + keycloakConfigFile + " due to " + e.getMessage(), e);
        throw e;
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) InputStream(java.io.InputStream) KeycloakDeployment(org.keycloak.adapters.KeycloakDeployment) IOException(java.io.IOException) URL(java.net.URL)

Example 2 with KeycloakDeployment

use of org.keycloak.adapters.KeycloakDeployment in project keycloak by keycloak.

the class AdapterActionsFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest servletReq = (HttpServletRequest) request;
    HttpServletResponse servletResp = (HttpServletResponse) response;
    // Accept timeOffset as argument to enforce timeouts
    String timeOffsetParam = request.getParameter(TIME_OFFSET_PARAM);
    String resetDeploymentParam = request.getParameter(RESET_DEPLOYMENT_PARAM);
    if (timeOffsetParam != null && !timeOffsetParam.isEmpty()) {
        int timeOffset = Integer.parseInt(timeOffsetParam);
        log.infof("Time offset updated to %d for application %s", timeOffset, servletReq.getRequestURI());
        Time.setOffset(timeOffset);
        writeResponse(servletResp, "Offset set successfully");
    } else if (resetDeploymentParam != null && !resetDeploymentParam.isEmpty()) {
        AdapterDeploymentContext deploymentContext = (AdapterDeploymentContext) request.getServletContext().getAttribute(AdapterDeploymentContext.class.getName());
        Field field = Reflections.findDeclaredField(AdapterDeploymentContext.class, "deployment");
        Reflections.setAccessible(field);
        KeycloakDeployment deployment = (KeycloakDeployment) Reflections.getFieldValue(field, deploymentContext);
        Time.setOffset(0);
        deployment.setNotBefore(0);
        if (deployment.getPublicKeyLocator() instanceof JWKPublicKeyLocator) {
            deployment.setPublicKeyLocator(new JWKPublicKeyLocator());
        }
        log.infof("Restarted PublicKeyLocator, notBefore and timeOffset for application %s", servletReq.getRequestURI());
        writeResponse(servletResp, "Restarted PublicKeyLocator, notBefore and timeOffset successfully");
    } else {
        // Continue request
        chain.doFilter(request, response);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Field(java.lang.reflect.Field) AdapterDeploymentContext(org.keycloak.adapters.AdapterDeploymentContext) KeycloakDeployment(org.keycloak.adapters.KeycloakDeployment) HttpServletResponse(javax.servlet.http.HttpServletResponse) JWKPublicKeyLocator(org.keycloak.adapters.rotation.JWKPublicKeyLocator)

Example 3 with KeycloakDeployment

use of org.keycloak.adapters.KeycloakDeployment in project keycloak by keycloak.

the class MultiTenantResolver method resolve.

@Override
public KeycloakDeployment resolve(HttpFacade.Request request) {
    String path = request.getURI();
    int multitenantIndex = path.indexOf("multi-tenant/");
    if (multitenantIndex == -1) {
        throw new IllegalStateException("Not able to resolve realm from the request path!");
    }
    String realm = path.substring(path.indexOf("multi-tenant/")).split("/")[1];
    if (realm.contains("?")) {
        realm = realm.split("\\?")[0];
    }
    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("/" + realm + "-keycloak.json");
    if (is == null) {
        throw new IllegalStateException("Not able to find the file /" + realm + "-keycloak.json");
    }
    KeycloakDeployment deployment = KeycloakDeploymentBuilder.build(is);
    return deployment;
}
Also used : InputStream(java.io.InputStream) KeycloakDeployment(org.keycloak.adapters.KeycloakDeployment)

Example 4 with KeycloakDeployment

use of org.keycloak.adapters.KeycloakDeployment in project keycloak by keycloak.

the class JaxrsBearerTokenFilterImpl method filter.

// REQUEST HANDLING
@Override
public void filter(ContainerRequestContext request) throws IOException {
    SecurityContext securityContext = getRequestSecurityContext(request);
    JaxrsHttpFacade facade = new JaxrsHttpFacade(request, securityContext);
    if (handlePreauth(facade)) {
        return;
    }
    KeycloakDeployment resolvedDeployment = deploymentContext.resolveDeployment(facade);
    nodesRegistrationManagement.tryRegister(resolvedDeployment);
    bearerAuthentication(facade, request, resolvedDeployment);
}
Also used : KeycloakDeployment(org.keycloak.adapters.KeycloakDeployment) SecurityContext(javax.ws.rs.core.SecurityContext) RefreshableKeycloakSecurityContext(org.keycloak.adapters.RefreshableKeycloakSecurityContext)

Example 5 with KeycloakDeployment

use of org.keycloak.adapters.KeycloakDeployment in project keycloak by keycloak.

the class JaxrsBearerTokenFilterImpl method start.

protected void start() {
    if (started) {
        throw new IllegalStateException("Filter already started. Make sure to specify just keycloakConfigResolver or keycloakConfigFile but not both");
    }
    if (keycloakConfigResolverClass != null) {
        Class<? extends KeycloakConfigResolver> resolverClass = loadResolverClass();
        try {
            KeycloakConfigResolver resolver = resolverClass.newInstance();
            log.info("Using " + resolver + " to resolve Keycloak configuration on a per-request basis.");
            this.deploymentContext = new AdapterDeploymentContext(resolver);
        } catch (Exception e) {
            throw new RuntimeException("Unable to instantiate resolver " + resolverClass);
        }
    } else {
        if (keycloakConfigFile == null) {
            throw new IllegalArgumentException("You need to specify either keycloakConfigResolverClass or keycloakConfigFile in configuration");
        }
        InputStream is = loadKeycloakConfigFile();
        KeycloakDeployment kd = KeycloakDeploymentBuilder.build(is);
        deploymentContext = new AdapterDeploymentContext(kd);
        log.info("Keycloak is using a per-deployment configuration loaded from: " + keycloakConfigFile);
    }
    nodesRegistrationManagement = new NodesRegistrationManagement();
    started = true;
}
Also used : NodesRegistrationManagement(org.keycloak.adapters.NodesRegistrationManagement) KeycloakConfigResolver(org.keycloak.adapters.KeycloakConfigResolver) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) AdapterDeploymentContext(org.keycloak.adapters.AdapterDeploymentContext) KeycloakDeployment(org.keycloak.adapters.KeycloakDeployment) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

KeycloakDeployment (org.keycloak.adapters.KeycloakDeployment)69 Test (org.junit.Test)21 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)21 PolicyEnforcer (org.keycloak.adapters.authorization.PolicyEnforcer)20 AuthorizationContext (org.keycloak.AuthorizationContext)16 OIDCHttpFacade (org.keycloak.adapters.OIDCHttpFacade)14 RefreshableKeycloakSecurityContext (org.keycloak.adapters.RefreshableKeycloakSecurityContext)12 AdapterDeploymentContext (org.keycloak.adapters.AdapterDeploymentContext)11 OAuthClient (org.keycloak.testsuite.util.OAuthClient)11 AdapterTokenStore (org.keycloak.adapters.AdapterTokenStore)10 InputStream (java.io.InputStream)9 KeycloakSecurityContext (org.keycloak.KeycloakSecurityContext)9 AuthenticatedActionsHandler (org.keycloak.adapters.AuthenticatedActionsHandler)9 FileInputStream (java.io.FileInputStream)7 FileNotFoundException (java.io.FileNotFoundException)7 HashMap (java.util.HashMap)7 KeycloakConfigResolver (org.keycloak.adapters.KeycloakConfigResolver)6 AuthChallenge (org.keycloak.adapters.spi.AuthChallenge)6 AuthOutcome (org.keycloak.adapters.spi.AuthOutcome)6 List (java.util.List)5