Search in sources :

Example 1 with KeycloakSecurityContext

use of org.keycloak.KeycloakSecurityContext in project wildfly-swarm by wildfly-swarm.

the class SecurityContextServletExtension method handleDeployment.

@Override
public void handleDeployment(DeploymentInfo info, ServletContext context) {
    info.addThreadSetupAction(new KeycloakThreadSetupHandler());
    info.addInnerHandlerChainWrapper(next -> exchange -> {
        KeycloakSecurityContext c = exchange.getAttachment(OIDCUndertowHttpFacade.KEYCLOAK_SECURITY_CONTEXT_KEY);
        if (c != null) {
            KeycloakSecurityContextAssociation.associate(c);
        }
        try {
            next.handleRequest(exchange);
        } finally {
            KeycloakSecurityContextAssociation.disassociate();
        }
    });
}
Also used : KeycloakSecurityContext(org.keycloak.KeycloakSecurityContext)

Example 2 with KeycloakSecurityContext

use of org.keycloak.KeycloakSecurityContext in project dolphin-platform by canoo.

the class KeycloakSecurityContextExtractFilter method doFilter.

public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
    final HttpServletRequest req = (HttpServletRequest) request;
    Assert.requireNonNull(chain, "chain");
    final KeycloakSecurityContext securityContext = keyCloakSecurityExtractor.extractContext(request);
    contextHolder.set(securityContext);
    realmHolder.set(req.getHeader(REALM_NAME_HEADER));
    appNameHolder.set(req.getHeader(APPLICATION_NAME_HEADER));
    accessDenied.set(false);
    final Subscription userContextSubscription = Optional.ofNullable(securityContext).map(c -> c.getToken()).map(t -> t.getPreferredUsername()).map(u -> ContextManagerImpl.getInstance().addThreadContext(USER_CONTEXT, u)).orElse(null);
    try {
        chain.doFilter(request, response);
    } catch (Exception e) {
        if (!accessDenied.get()) {
            throw e;
        } else {
            LOG.error("SecurityContext error in request", e);
        }
    } finally {
        Optional.ofNullable(userContextSubscription).ifPresent(s -> s.unsubscribe());
        contextHolder.set(null);
        boolean sendAccessDenied = accessDenied.get();
        accessDenied.set(false);
        if (sendAccessDenied) {
            ((HttpServletResponse) response).sendError(403, "Access Denied");
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FilterChain(javax.servlet.FilterChain) ServletRequest(javax.servlet.ServletRequest) Subscription(com.canoo.platform.core.functional.Subscription) Logger(org.slf4j.Logger) Assert(com.canoo.dp.impl.platform.core.Assert) ContextManagerImpl(com.canoo.dp.impl.platform.core.context.ContextManagerImpl) ServletException(javax.servlet.ServletException) LoggerFactory(org.slf4j.LoggerFactory) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) API(org.apiguardian.api.API) USER_CONTEXT(com.canoo.dp.impl.security.SecurityConstants.USER_CONTEXT) INTERNAL(org.apiguardian.api.API.Status.INTERNAL) HttpServletRequest(javax.servlet.http.HttpServletRequest) REALM_NAME_HEADER(com.canoo.dp.impl.security.SecurityConstants.REALM_NAME_HEADER) KeycloakSecurityContext(org.keycloak.KeycloakSecurityContext) ServletResponse(javax.servlet.ServletResponse) APPLICATION_NAME_HEADER(com.canoo.dp.impl.security.SecurityConstants.APPLICATION_NAME_HEADER) FilterConfig(javax.servlet.FilterConfig) Optional(java.util.Optional) Filter(javax.servlet.Filter) KeycloakSecurityContext(org.keycloak.KeycloakSecurityContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) Subscription(com.canoo.platform.core.functional.Subscription) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 3 with KeycloakSecurityContext

use of org.keycloak.KeycloakSecurityContext in project dolphin-platform by canoo.

the class KeyCloakSecurityExtractor method extractContext.

public KeycloakSecurityContext extractContext(final ServletRequest request) {
    Assert.requireNonNull(request, "request");
    final KeycloakSecurityContext context = (KeycloakSecurityContext) request.getAttribute(KeycloakSecurityContext.class.getName());
    return context;
}
Also used : KeycloakSecurityContext(org.keycloak.KeycloakSecurityContext)

Example 4 with KeycloakSecurityContext

use of org.keycloak.KeycloakSecurityContext in project wildfly-swarm by wildfly-swarm.

the class BearerHeaderAdder method onStartWithServer.

@Override
public void onStartWithServer(ExecutionContext<HttpClientRequest<ByteBuf>> context, ExecutionInfo info) throws AbortExecutionException {
    KeycloakSecurityContext securityContext = KeycloakSecurityContextAssociation.get();
    if (securityContext != null) {
        HttpClientRequest<ByteBuf> request = context.getRequest();
        request.withHeader("Authorization", "Bearer " + securityContext.getTokenString());
        context.put(KeycloakSecurityContextAssociation.class.getName(), securityContext);
    } else {
        KeycloakSecurityContextAssociation.disassociate();
    }
}
Also used : KeycloakSecurityContext(org.keycloak.KeycloakSecurityContext) ByteBuf(io.netty.buffer.ByteBuf) KeycloakSecurityContextAssociation(org.wildfly.swarm.keycloak.deployment.KeycloakSecurityContextAssociation)

Example 5 with KeycloakSecurityContext

use of org.keycloak.KeycloakSecurityContext in project wildfly-swarm by wildfly-swarm.

the class KeycloakThreadSetupHandler method create.

@Override
public <T, C> Action<T, C> create(final Action<T, C> action) {
    return new Action<T, C>() {

        @Override
        public T call(HttpServerExchange exchange, C context) throws Exception {
            if (exchange == null) {
                return action.call(exchange, context);
            }
            KeycloakSecurityContext c = exchange.getAttachment(OIDCUndertowHttpFacade.KEYCLOAK_SECURITY_CONTEXT_KEY);
            KeycloakSecurityContextAssociation.associate(c);
            try {
                return action.call(exchange, context);
            } finally {
                KeycloakSecurityContextAssociation.disassociate();
            }
        }
    };
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) KeycloakSecurityContext(org.keycloak.KeycloakSecurityContext)

Aggregations

KeycloakSecurityContext (org.keycloak.KeycloakSecurityContext)5 Assert (com.canoo.dp.impl.platform.core.Assert)1 ContextManagerImpl (com.canoo.dp.impl.platform.core.context.ContextManagerImpl)1 APPLICATION_NAME_HEADER (com.canoo.dp.impl.security.SecurityConstants.APPLICATION_NAME_HEADER)1 REALM_NAME_HEADER (com.canoo.dp.impl.security.SecurityConstants.REALM_NAME_HEADER)1 USER_CONTEXT (com.canoo.dp.impl.security.SecurityConstants.USER_CONTEXT)1 Subscription (com.canoo.platform.core.functional.Subscription)1 ByteBuf (io.netty.buffer.ByteBuf)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 IOException (java.io.IOException)1 Optional (java.util.Optional)1 Filter (javax.servlet.Filter)1 FilterChain (javax.servlet.FilterChain)1 FilterConfig (javax.servlet.FilterConfig)1 ServletException (javax.servlet.ServletException)1 ServletRequest (javax.servlet.ServletRequest)1 ServletResponse (javax.servlet.ServletResponse)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 API (org.apiguardian.api.API)1