Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UNDERTOW-2504] Review anonymous classes in Undertow io.undertow.websockets.jsr.test.security #1692

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import jakarta.websocket.ContainerProvider;
import jakarta.websocket.Endpoint;
import jakarta.websocket.EndpointConfig;
import jakarta.websocket.MessageHandler;
import jakarta.websocket.OnOpen;
import jakarta.websocket.Session;
import jakarta.websocket.server.ServerEndpoint;
Expand Down Expand Up @@ -147,12 +146,7 @@ public static void cleanup() throws ServletException {
@Test
public void testAuthenticatedWebsocket() throws Exception {
ProgramaticClientEndpoint endpoint = new ProgramaticClientEndpoint();
ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().configurator(new ClientConfigurator(){
@Override
public void beforeRequest(Map<String, List<String>> headers) {
headers.put(AUTHORIZATION.toString(), Collections.singletonList(BASIC + " " + FlexBase64.encodeString("user1:password1".getBytes(), false)));
}
}).build();
ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().configurator(new CustomClientConfigurator()).build();
ContainerProvider.getWebSocketContainer().connectToServer(endpoint, clientEndpointConfig, new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/servletContext/secured"));
assertEquals("user1", endpoint.getResponses().poll(15, TimeUnit.SECONDS));
endpoint.session.close();
Expand All @@ -179,13 +173,7 @@ public static class ProgramaticClientEndpoint extends Endpoint {
@Override
public void onOpen(Session session, EndpointConfig config) {
this.session = session;
session.addMessageHandler(new MessageHandler.Whole<String>() {

@Override
public void onMessage(String message) {
responses.add(message);
}
});
session.addMessageHandler(String.class, (message) -> responses.add(message));
}

@Override
Expand Down Expand Up @@ -217,12 +205,7 @@ public void init(FilterConfig filterConfig) {

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
filterChain.doFilter(new HttpServletRequestWrapper((HttpServletRequest) servletRequest) {
@Override
public Principal getUserPrincipal() {
return () -> "wrapped";
}
}, servletResponse);
filterChain.doFilter(new ServletRequestWrapper((HttpServletRequest) servletRequest), servletResponse);
}

@Override
Expand All @@ -231,4 +214,23 @@ public void destroy() {
}
}

private static class ServletRequestWrapper extends HttpServletRequestWrapper {

ServletRequestWrapper(HttpServletRequest request) {
super(request);
}

@Override
public Principal getUserPrincipal() {
return () -> "wrapped";
}
}

private static class CustomClientConfigurator extends ClientConfigurator {

@Override
public void beforeRequest(Map<String, List<String>> headers) {
headers.put(AUTHORIZATION.toString(), Collections.singletonList(BASIC + " " + FlexBase64.encodeString("user1:password1".getBytes(), false)));
}
}
}
Loading