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

Stomp-Client: add authentication to connection process #18

Open
wants to merge 3 commits 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 @@ -81,6 +81,21 @@ public SSLContext getSSLContext() {
return this.client.getSSLContext();
}

@Override
public boolean isAuthenticated() {
return client.isAuthenticated();
}

@Override
public String getUsername() {
return client.getAuthenticationUser();
}

@Override
public String getPassword() {
return client.getAuthenticationPassword();
}

private StompClient client;

}
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,32 @@ public StompClient(URI uri, SSLContext sslContext) throws URISyntaxException {
}

}

String userInfo = uri.getUserInfo();
if (userInfo != null) {
int firstColonIndex = userInfo.indexOf(":");
if (firstColonIndex > -1 && firstColonIndex < (userInfo.length() - 1)) {
this.useAuthentication = true;
this.authenticationUser = userInfo.substring(0, firstColonIndex);
this.authenticationPassword = userInfo.substring(firstColonIndex + 1);
}
}

this.serverAddress = new InetSocketAddress( host, port );
}

public boolean isAuthenticated() {
return this.useAuthentication;
}

public String getAuthenticationUser() {
return this.authenticationUser;
}

public String getAuthenticationPassword() {
return this.authenticationPassword;
}

public boolean isSecure() {
return this.useSSL;
}
Expand Down Expand Up @@ -488,6 +511,9 @@ public Void call() throws Exception {
private Version version = Version.VERSION_1_0;
private boolean useWebSockets = false;
private boolean useSSL = false;
private boolean useAuthentication = false;
private String authenticationUser;
private String authenticationPassword;
private Class<? extends Handshake> webSocketHandshakeClass = Ietf17Handshake.class;
private SSLContext sslContext;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public interface ClientContext {

boolean isSecure();
SSLContext getSSLContext();


boolean isAuthenticated();
String getUsername();
String getPassword();

void setConnectionState(State state);
void setVersion(Version version);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public void channelConnected(ChannelHandlerContext context, ChannelStateEvent e)
StompControlFrame frame = new StompControlFrame( Command.CONNECT );
frame.setHeader( Header.HOST, this.host );
frame.setHeader( Header.ACCEPT_VERSION, Version.supportedVersions() );
if(getClientContext().isAuthenticated()) {
frame.setHeader( Header.LOGIN, getClientContext().getUsername() );
frame.setHeader( Header.PASSCODE, getClientContext().getPassword() );
}

Channels.write( context.getChannel(), frame );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,22 @@ public void setSSLContext(SSLContext sslContext) {
public SSLContext getSSLContext() {
return this.sslContext;
}


@Override
public boolean isAuthenticated() {
return this.authenticated;
}

@Override
public String getUsername() {
return this.user;
}

@Override
public String getPassword() {
return this.password;
}

@Override
public State getConnectionState() {
return this.state;
Expand Down Expand Up @@ -90,6 +105,9 @@ public void setVersion(Version version) {
private Version version = Version.VERSION_1_0;
private boolean secure = false;
private SSLContext sslContext;
private boolean authenticated = false;
private String user;
private String password;


private List<String> receipts = new ArrayList<String>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public static class Header {
public static final String SERVER = "server";
public static final String MESSAGE = "message";
public static final String HEARTBEAT = "heart-beat";
public static final String LOGIN = "login";
public static final String PASSCODE = "passcode";
}

public static class Command {
Expand Down