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

Pass initial connect message to ProxyWebSocketConnectionManager #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -28,14 +28,16 @@
import org.springframework.web.socket.WebSocketSession;

import com.github.mthizo247.cloud.netflix.zuul.web.socket.WebSocketHttpHeadersCallback;
import com.github.mthizo247.cloud.netflix.zuul.web.socket.WebSocketMessageAccessor;

/**
* @author Ronald Mthombeni
*/
public class BasicAuthPrincipalHeadersCallback implements WebSocketHttpHeadersCallback {
protected final Log logger = LogFactory.getLog(getClass());
public WebSocketHttpHeaders getWebSocketHttpHeaders(
WebSocketSession userAgentSession) {
WebSocketSession userAgentSession,
WebSocketMessageAccessor messageAccessor) {
WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
Principal principal = userAgentSession.getPrincipal();
if (principal != null) {
Expand All @@ -55,6 +57,6 @@ public WebSocketHttpHeaders getWebSocketHttpHeaders(
logger.debug("Skipped adding basic authentication header since user session principal is null");
}
}
return headers;
}
return headers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.github.mthizo247.cloud.netflix.zuul.web.authentication;

import com.github.mthizo247.cloud.netflix.zuul.web.socket.WebSocketHttpHeadersCallback;
import com.github.mthizo247.cloud.netflix.zuul.web.socket.WebSocketMessageAccessor;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.HttpHeaders;
Expand All @@ -32,24 +34,24 @@
* @author Marcin Podlodowski
*/
public class OAuth2BearerPrincipalHeadersCallback implements WebSocketHttpHeadersCallback {
private final Log logger = LogFactory.getLog(getClass());
private final Log logger = LogFactory.getLog(getClass());

@Override
public WebSocketHttpHeaders getWebSocketHttpHeaders(final WebSocketSession userAgentSession) {
public WebSocketHttpHeaders getWebSocketHttpHeaders(final WebSocketSession userAgentSession,
final WebSocketMessageAccessor messageAccessor) {
WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
Principal principal = userAgentSession.getPrincipal();
if (principal != null && OAuth2Authentication.class.isAssignableFrom(principal.getClass())) {
OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) principal;
OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) oAuth2Authentication.getDetails();
String accessToken = details.getTokenValue();
headers.put(HttpHeaders.AUTHORIZATION, Collections.singletonList("Bearer " + accessToken));
if(logger.isDebugEnabled()) {
logger.debug("Added Oauth2 bearer token authentication header for user " +
principal.getName() + " to web sockets http headers");
if (logger.isDebugEnabled()) {
logger.debug("Added Oauth2 bearer token authentication header for user " + principal.getName()
+ " to web sockets http headers");
}
}
else {
if(logger.isDebugEnabled()) {
} else {
if (logger.isDebugEnabled()) {
logger.debug("Skipped adding basic authentication header since user session principal is null");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,18 @@ public class ProxyWebSocketConnectionManager extends ConnectionManagerSupport
private Map<String, StompSession.Subscription> subscriptions = new ConcurrentHashMap<>();
private ErrorHandler errorHandler;
private SimpMessagingTemplate messagingTemplate;
private WebSocketMessageAccessor connectMessage;

public ProxyWebSocketConnectionManager(SimpMessagingTemplate messagingTemplate,
WebSocketStompClient stompClient, WebSocketSession userAgentSession,
WebSocketHttpHeadersCallback httpHeadersCallback, String uri) {
WebSocketHttpHeadersCallback httpHeadersCallback, String uri,
WebSocketMessageAccessor connectMessage) {
super(uri);
this.messagingTemplate = messagingTemplate;
this.stompClient = stompClient;
this.userAgentSession = userAgentSession;
this.httpHeadersCallback = httpHeadersCallback;
this.connectMessage = connectMessage;
}

public void errorHandler(ErrorHandler errorHandler) {
Expand All @@ -68,7 +71,7 @@ public void errorHandler(ErrorHandler errorHandler) {

private WebSocketHttpHeaders buildWebSocketHttpHeaders() {
if (httpHeadersCallback != null) {
return httpHeadersCallback.getWebSocketHttpHeaders(userAgentSession);
return httpHeadersCallback.getWebSocketHttpHeaders(userAgentSession, connectMessage);
}
return new WebSocketHttpHeaders();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private void handleMessageFromClient(WebSocketSession session,

if (StompCommand.CONNECT.toString().equalsIgnoreCase(accessor.getCommand())) {
handled = true;
connectToProxiedTarget(session);
connectToProxiedTarget(session, accessor);
}

if (!handled) {
Expand All @@ -158,7 +158,7 @@ private void handleMessageFromClient(WebSocketSession session,
}
}

private void connectToProxiedTarget(WebSocketSession session) {
private void connectToProxiedTarget(WebSocketSession session, WebSocketMessageAccessor accessor) {
URI sessionUri = session.getUri();
ZuulWebSocketProperties.WsBrokerage wsBrokerage = getWebSocketBrokarage(
sessionUri);
Expand All @@ -178,7 +178,7 @@ private void connectToProxiedTarget(WebSocketSession session) {
.toUriString();

ProxyWebSocketConnectionManager connectionManager = new ProxyWebSocketConnectionManager(
messagingTemplate, stompClient, session, headersCallback, uri);
messagingTemplate, stompClient, session, headersCallback, uri, accessor);
connectionManager.errorHandler(this.errorHandler);
managers.put(session, connectionManager);
connectionManager.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
* @author Salman Noor
*/
public interface WebSocketHttpHeadersCallback {
WebSocketHttpHeaders getWebSocketHttpHeaders(WebSocketSession userAgentSession);
WebSocketHttpHeaders getWebSocketHttpHeaders(WebSocketSession userAgentSession, WebSocketMessageAccessor connectMessage);
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ public WebSocketHttpHeadersCallback webSocketHttpHeadersCallback() {
return new WebSocketHttpHeadersCallback() {
@Override
public WebSocketHttpHeaders getWebSocketHttpHeaders(
WebSocketSession userAgentSession) {
WebSocketSession userAgentSession,
WebSocketMessageAccessor connectMessage) {
return new WebSocketHttpHeaders();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,18 @@ public class ProxyWebSocketConnectionManagerTests {
private ListenableFuture<StompSession> listenableFuture = (ListenableFuture<StompSession>) mock(
ListenableFuture.class);
private ErrorHandler errHandler = mock(ErrorHandler.class);
private WebSocketMessageAccessor connectMessage = WebSocketMessageAccessor.create("testmessage");

@Before
public void init() throws Exception {
String uri = "http://example.com";
proxyConnectionManager = new ProxyWebSocketConnectionManager(messagingTemplate,
stompClient, wsSession, headersCallback, uri);
stompClient, wsSession, headersCallback, uri, connectMessage);

proxyConnectionManager.errorHandler(errHandler);

when(listenableFuture.get()).thenReturn(serverSession);
when(stompClient.connect(uri, headersCallback.getWebSocketHttpHeaders(wsSession),
when(stompClient.connect(uri, headersCallback.getWebSocketHttpHeaders(wsSession, connectMessage),
proxyConnectionManager)).thenReturn(listenableFuture);
}

Expand Down