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();
}
});
}
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");
}
}
}
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;
}
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();
}
}
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();
}
}
};
}
Aggregations